|
8 | 8 | "text/template" |
9 | 9 | "time" |
10 | 10 |
|
| 11 | + "github.com/mattn/go-tty" |
11 | 12 | "github.com/scalvert/glean-cli/pkg/config" |
12 | 13 | "github.com/scalvert/glean-cli/pkg/http" |
13 | 14 | "github.com/spf13/cobra" |
@@ -177,13 +178,18 @@ func formatDatasource(s string) string { |
177 | 178 | return string(words) |
178 | 179 | } |
179 | 180 |
|
180 | | -var defaultTemplate = `{{range $i, $result := .Results}} |
181 | | -{{add $i 1}} {{formatDatasource $result.Document.Datasource}} | {{gleanBlue $result.Document.Title}} |
| 181 | +var defaultTemplate = `{{- range $i, $result := .Results -}} |
| 182 | +{{if $i}} |
| 183 | +
|
| 184 | +{{end}}{{add $i 1}} {{formatDatasource $result.Document.Datasource}} | {{gleanBlue $result.Document.Title}} |
182 | 185 | {{gleanYellow $result.Document.URL}} |
183 | | -{{range $result.Snippets}}{{.Text}} |
184 | | -{{end}} |
185 | | -{{end}}{{if .SuggestedSpellCorrectedQuery}}Did you mean: {{.SuggestedSpellCorrectedQuery}}?{{end}} |
186 | | -{{if .RewrittenQuery}}Showing results for: {{.RewrittenQuery}}{{end}}` |
| 186 | +{{- range $result.Snippets}} |
| 187 | +{{.Text}}{{end}} |
| 188 | +{{- end}}{{if .SuggestedSpellCorrectedQuery}} |
| 189 | +
|
| 190 | +Did you mean: {{.SuggestedSpellCorrectedQuery}}?{{end}}{{if .RewrittenQuery}} |
| 191 | +
|
| 192 | +Showing results for: {{.RewrittenQuery}}{{end}}` |
187 | 193 |
|
188 | 194 | func NewCmdSearch() *cobra.Command { |
189 | 195 | opts := &SearchOptions{ |
@@ -466,10 +472,21 @@ func runSearch(cmd *cobra.Command, opts *SearchOptions) error { |
466 | 472 |
|
467 | 473 | // Handle pagination if there are more results |
468 | 474 | for response.HasMoreResults { |
469 | | - fmt.Fprint(cmd.OutOrStdout(), "\nPress 'q' to quit, any other key to load more results...") |
470 | | - var input string |
471 | | - fmt.Scanln(&input) |
472 | | - if input == "q" { |
| 475 | + fmt.Fprint(cmd.OutOrStdout(), "\n\nPress 'q' to quit, any other key to load more results...") |
| 476 | + |
| 477 | + tty, err := tty.Open() |
| 478 | + if err != nil { |
| 479 | + return fmt.Errorf("failed to open tty: %w", err) |
| 480 | + } |
| 481 | + defer tty.Close() |
| 482 | + |
| 483 | + r, err := tty.ReadRune() |
| 484 | + if err != nil { |
| 485 | + return fmt.Errorf("failed to read input: %w", err) |
| 486 | + } |
| 487 | + |
| 488 | + if r == 'q' || r == 'Q' { |
| 489 | + fmt.Fprintln(cmd.OutOrStdout()) |
473 | 490 | break |
474 | 491 | } |
475 | 492 |
|
|
0 commit comments