|
5 | 5 | "html"
|
6 | 6 | "strings"
|
7 | 7 |
|
8 |
| - "github.com/fatih/color" |
| 8 | + "github.com/charmbracelet/lipgloss" |
9 | 9 | "github.com/fschuermeyer/GoWordlytics/internal/format"
|
| 10 | + "github.com/fschuermeyer/GoWordlytics/internal/render" |
10 | 11 | )
|
11 | 12 |
|
12 | 13 | type Report struct {
|
@@ -65,57 +66,86 @@ func (r *Report) HasWordPress() bool {
|
65 | 66 | return r.hasWordPress
|
66 | 67 | }
|
67 | 68 |
|
68 |
| -func (r *Report) Output() { |
69 |
| - color.White("\nGoWordlytics Report") |
70 |
| - color.Blue("Report for %s", r.GetUrl()) |
71 |
| - fmt.Println("------------------------") |
| 69 | +// Render renders the report by calling the RenderOverview, RenderPlugins, and RenderThemes methods. |
| 70 | +// It applies the provided headline style to the rendered sections. |
| 71 | +func (r *Report) Render() { |
| 72 | + var headline = lipgloss.NewStyle().Foreground(lipgloss.Color("#E7D617")).Bold(true) |
72 | 73 |
|
73 |
| - c := color.New(color.FgGreen) |
| 74 | + r.renderOverview(headline) |
74 | 75 |
|
75 |
| - c.Print("Has WordPress: ") |
76 |
| - fmt.Println(r.hasWordPress) |
| 76 | + if len(r.pluginDetails) > 0 { |
| 77 | + r.renderPlugins(headline) |
| 78 | + } |
77 | 79 |
|
78 |
| - c.Print("Has readme.html: ") |
79 |
| - fmt.Println(r.hasReadme) |
| 80 | + if len(r.themes) > 0 { |
| 81 | + r.renderThemes(headline) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// RenderOverview renders the overview of the GoWordlytics report. |
| 86 | +// It takes a headline lipgloss.Style as a parameter and prints the report to the console. |
| 87 | +// The report includes the URL and whether it is a WordPress site. |
| 88 | +// If the site is a WordPress site, it also includes whether it has a readme file. |
| 89 | +// If the report includes version information, it also includes the version number, version status, and current version. |
| 90 | +// The report is displayed in a table format. |
| 91 | +func (r *Report) renderOverview(headline lipgloss.Style) { |
| 92 | + fmt.Printf("%s\n", headline.Render("GoWordlytics Report")) |
| 93 | + |
| 94 | + headers := []string{"URL", "WordPress?"} |
| 95 | + values := []string{r.GetUrl(), fmt.Sprintf("%t", r.HasWordPress())} |
| 96 | + |
| 97 | + if r.HasWordPress() { |
| 98 | + headers = append(headers, "Readme?") |
| 99 | + values = append(values, fmt.Sprintf("%t", r.hasReadme)) |
| 100 | + } |
80 | 101 |
|
81 | 102 | if len(r.version) > 0 {
|
82 |
| - c.Print("Version: ") |
83 |
| - fmt.Printf("%s ", r.version) |
| 103 | + headers = append(headers, "Version") |
| 104 | + values = append(values, r.version) |
84 | 105 |
|
85 | 106 | if len(r.versionStatus) > 0 && r.versionStatus != "error" && r.versionStatus != "latest" {
|
86 |
| - text := color.RedString("(%s to %s)", r.versionStatus, r.versionCurrent) |
87 |
| - |
88 |
| - fmt.Print(text) |
89 |
| - } |
| 107 | + headers = append(headers, "Version Status") |
90 | 108 |
|
91 |
| - fmt.Println() |
92 |
| - } |
| 109 | + if r.versionStatus == "upgrade" { |
| 110 | + r.versionStatus = lipgloss.NewStyle().Foreground(lipgloss.Color("#F16208")).Render(r.versionStatus) |
| 111 | + } |
93 | 112 |
|
94 |
| - if len(r.pluginDetails) > 0 { |
95 |
| - r.OutputPlugins() |
96 |
| - } |
| 113 | + values = append(values, r.versionStatus) |
97 | 114 |
|
98 |
| - if len(r.themes) > 0 { |
99 |
| - r.OutputThemes() |
| 115 | + headers = append(headers, "Current") |
| 116 | + values = append(values, r.versionCurrent) |
| 117 | + } |
100 | 118 | }
|
101 | 119 |
|
102 |
| - fmt.Print("------------------------\n\n") |
| 120 | + render.Table(headers, [][]string{values}) |
103 | 121 | }
|
104 | 122 |
|
105 |
| -func (r *Report) OutputPlugins() { |
106 |
| - fmt.Println("------------------------") |
107 |
| - color.Blue("Plugins") |
| 123 | +// RenderPlugins renders the plugins report with the given headline style. |
| 124 | +// It prints the headline and then displays a table with the plugin details, |
| 125 | +// including the name, slug, version, number of downloads, and homepage link. |
| 126 | +func (r *Report) renderPlugins(headline lipgloss.Style) { |
| 127 | + fmt.Printf("%s\n", headline.Render("Plugins Report")) |
| 128 | + |
| 129 | + rows := [][]string{} |
108 | 130 |
|
109 | 131 | for _, plugin := range r.pluginDetails {
|
110 |
| - fmt.Printf("%s (%s) - Version: %s - Downloaded: %s | %s \n", html.UnescapeString(plugin.Name), plugin.Slug, plugin.Version, format.InsertThousandSeparator(plugin.Downloaded, '.'), plugin.Homepage) |
| 132 | + rows = append(rows, []string{html.UnescapeString(plugin.Name), plugin.Slug, plugin.Version, format.InsertThousandSeparator(plugin.Downloaded, '.'), plugin.Homepage}) |
111 | 133 | }
|
| 134 | + |
| 135 | + render.Table([]string{"Name", "Slug", "Version", "Downloads", "Link"}, rows) |
112 | 136 | }
|
113 | 137 |
|
114 |
| -func (r *Report) OutputThemes() { |
115 |
| - fmt.Println("------------------------") |
116 |
| - color.Blue("Themes") |
| 138 | +// RenderThemes renders a report of the themes. |
| 139 | +// It takes a headline style as input and prints the themes' information in a table format. |
| 140 | +// The themes' information includes the name, description, text domain, author, author URI, and version. |
| 141 | +func (r *Report) renderThemes(headline lipgloss.Style) { |
| 142 | + |
| 143 | + rows := [][]string{} |
117 | 144 |
|
118 | 145 | for _, theme := range r.themes {
|
119 |
| - fmt.Printf("(%s)\n- %s %s - %s %s\n", theme.Description, theme.TextDomain, theme.Author, theme.AuthorURI, theme.Version) |
| 146 | + rows = append(rows, []string{theme.Name, theme.Description, theme.TextDomain, theme.Author, theme.AuthorURI, theme.Version}) |
120 | 147 | }
|
| 148 | + |
| 149 | + fmt.Printf("%s\n", headline.Render("Themes Report")) |
| 150 | + render.Table([]string{"Name", "Description", "TextDomain", "Author", "AuthorURI", "Version"}, rows) |
121 | 151 | }
|
0 commit comments