|
| 1 | +package display |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/fatih/color" |
| 9 | + "github.com/gambitier/tag-manager/pkg/discovery" |
| 10 | + "github.com/olekukonko/tablewriter" |
| 11 | +) |
| 12 | + |
| 13 | +// DisplayMode represents the display mode for package lists |
| 14 | +type DisplayMode int |
| 15 | + |
| 16 | +const ( |
| 17 | + // Compact shows only package name and latest tag |
| 18 | + Compact DisplayMode = iota |
| 19 | + // Verbose shows all details |
| 20 | + Verbose |
| 21 | +) |
| 22 | + |
| 23 | +// ShowPackageList displays a list of packages in a table format |
| 24 | +func ShowPackageList(packages []discovery.Package, mode DisplayMode) { |
| 25 | + if len(packages) == 0 { |
| 26 | + color.Red("No Go packages found.") |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + // Create table |
| 31 | + table := tablewriter.NewWriter(os.Stdout) |
| 32 | + |
| 33 | + if mode == Verbose { |
| 34 | + table.Header("#", "Module", "Package", "Go Version", "GitHub", "Latest Tag") |
| 35 | + } else { |
| 36 | + table.Header("#", "Package", "Latest Tag") |
| 37 | + } |
| 38 | + |
| 39 | + // Add rows |
| 40 | + for i, pkg := range packages { |
| 41 | + // Handle empty values |
| 42 | + goVersion := pkg.GoVersion |
| 43 | + if goVersion == "" { |
| 44 | + goVersion = "-" |
| 45 | + } |
| 46 | + |
| 47 | + github := pkg.GitHubRepo |
| 48 | + if github == "" { |
| 49 | + github = "-" |
| 50 | + } |
| 51 | + |
| 52 | + latestTag := pkg.LatestTag |
| 53 | + if latestTag == "" { |
| 54 | + latestTag = "(no tags)" |
| 55 | + } |
| 56 | + |
| 57 | + if mode == Verbose { |
| 58 | + table.Append(fmt.Sprintf("%d", i+1), pkg.ModulePath, pkg.PackageName, goVersion, github, latestTag) |
| 59 | + } else { |
| 60 | + table.Append(fmt.Sprintf("%d", i+1), pkg.PackageName, latestTag) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + table.Render() |
| 65 | +} |
| 66 | + |
| 67 | +// ShowPackageListWithHeader displays a list of packages with a header |
| 68 | +func ShowPackageListWithHeader(packages []discovery.Package, mode DisplayMode, searchPaths []string) { |
| 69 | + color.Cyan("Discovered %d Go packages:", len(packages)) |
| 70 | + color.White("Search paths: %s", strings.Join(searchPaths, ", ")) |
| 71 | + color.White("") |
| 72 | + |
| 73 | + ShowPackageList(packages, mode) |
| 74 | +} |
0 commit comments