printContent prints a gist with optional description and content similar to `gh search code` including highlighted matches in the form: {{gist ID}} {{file name}} {{description, if any}} {{content lines with matches, if any}} If printing to a non-TTY stream the format will be the sam
(io *iostreams.IOStreams, gists []shared.Gist, filter *regexp.Regexp)
| 222 | // |
| 223 | // If printing to a non-TTY stream the format will be the same but without highlights. |
| 224 | func printContent(io *iostreams.IOStreams, gists []shared.Gist, filter *regexp.Regexp) error { |
| 225 | const tab string = " " |
| 226 | cs := io.ColorScheme() |
| 227 | |
| 228 | out := &strings.Builder{} |
| 229 | var filename, description string |
| 230 | var err error |
| 231 | split := func(r rune) bool { |
| 232 | return r == '\n' || r == '\r' |
| 233 | } |
| 234 | |
| 235 | for _, gist := range gists { |
| 236 | for _, file := range gist.Files { |
| 237 | matched := false |
| 238 | out.Reset() |
| 239 | |
| 240 | if filename, err = highlightMatch(file.Filename, filter, &matched, cs.Green, cs.Highlight); err != nil { |
| 241 | return err |
| 242 | } |
| 243 | fmt.Fprintln(out, cs.Blue(gist.ID), filename) |
| 244 | |
| 245 | if gist.Description != "" { |
| 246 | if description, err = highlightMatch(gist.Description, filter, &matched, cs.Bold, cs.Highlight); err != nil { |
| 247 | return err |
| 248 | } |
| 249 | fmt.Fprintf(out, "%s%s\n", tab, description) |
| 250 | } |
| 251 | |
| 252 | if file.Content != "" { |
| 253 | for line := range strings.FieldsFuncSeq(file.Content, split) { |
| 254 | if filter.MatchString(line) { |
| 255 | if line, err = highlightMatch(line, filter, &matched, normal, cs.Highlight); err != nil { |
| 256 | return err |
| 257 | } |
| 258 | fmt.Fprintf(out, "%[1]s%[1]s%[2]s\n", tab, line) |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | if matched { |
| 264 | fmt.Fprintln(io.Out, out.String()) |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | return nil |
| 270 | } |
| 271 | |
| 272 | func highlightMatch(s string, filter *regexp.Regexp, matched *bool, color, highlight func(string) string) (string, error) { |
| 273 | matches := filter.FindAllStringIndex(s, -1) |
no test coverage detected