(io *iostreams.IOStreams, gists []shared.Gist, filter *regexp.Regexp)
| 151 | } |
| 152 | |
| 153 | func printTable(io *iostreams.IOStreams, gists []shared.Gist, filter *regexp.Regexp) error { |
| 154 | cs := io.ColorScheme() |
| 155 | tp := tableprinter.New(io, tableprinter.WithHeader("ID", "DESCRIPTION", "FILES", "VISIBILITY", "UPDATED")) |
| 156 | |
| 157 | // Highlight filter matches in the description when printing the table. |
| 158 | highlightDescription := func(s string) string { |
| 159 | if filter != nil { |
| 160 | if str, err := highlightMatch(s, filter, nil, cs.Bold, cs.Highlight); err == nil { |
| 161 | return str |
| 162 | } |
| 163 | } |
| 164 | return cs.Bold(s) |
| 165 | } |
| 166 | |
| 167 | // Highlight the files column when any file name matches the filter. |
| 168 | highlightFilesFunc := func(gist *shared.Gist) func(string) string { |
| 169 | if filter != nil { |
| 170 | for _, file := range gist.Files { |
| 171 | if filter.MatchString(file.Filename) { |
| 172 | return cs.Highlight |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | return normal |
| 177 | } |
| 178 | |
| 179 | for _, gist := range gists { |
| 180 | fileCount := len(gist.Files) |
| 181 | |
| 182 | visibility := "public" |
| 183 | visColor := cs.Green |
| 184 | if !gist.Public { |
| 185 | visibility = "secret" |
| 186 | visColor = cs.Red |
| 187 | } |
| 188 | |
| 189 | description := gist.Description |
| 190 | if description == "" { |
| 191 | for filename := range gist.Files { |
| 192 | if !strings.HasPrefix(filename, "gistfile") { |
| 193 | description = filename |
| 194 | break |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | tp.AddField(gist.ID) |
| 200 | tp.AddField( |
| 201 | text.RemoveExcessiveWhitespace(description), |
| 202 | tableprinter.WithColor(highlightDescription), |
| 203 | ) |
| 204 | tp.AddField( |
| 205 | text.Pluralize(fileCount, "file"), |
| 206 | tableprinter.WithColor(highlightFilesFunc(&gist)), |
| 207 | ) |
| 208 | tp.AddField(visibility, tableprinter.WithColor(visColor)) |
| 209 | tp.AddTimeField(time.Now(), gist.UpdatedAt, cs.Muted) |
| 210 | tp.EndRow() |
no test coverage detected