formatFTSSnippet turns the matched snippet from a full text search into a format suitable for CLI output
(s string)
| 75 | // formatFTSSnippet turns the matched snippet from a full text search |
| 76 | // into a format suitable for CLI output |
| 77 | func formatFTSSnippet(s string) (string, error) { |
| 78 | // first, strip all new lines |
| 79 | body := newLineReg.ReplaceAllString(s, " ") |
| 80 | |
| 81 | var format, buf strings.Builder |
| 82 | var args []interface{} |
| 83 | |
| 84 | toks := tokenize(body) |
| 85 | |
| 86 | for _, tok := range toks { |
| 87 | if tok.Kind == tokenKindHLBegin || tok.Kind == tokenKindEOL { |
| 88 | format.WriteString("%s") |
| 89 | args = append(args, buf.String()) |
| 90 | |
| 91 | buf.Reset() |
| 92 | } else if tok.Kind == tokenKindHLEnd { |
| 93 | format.WriteString("%s") |
| 94 | str := log.ColorYellow.Sprintf("%s", buf.String()) |
| 95 | args = append(args, str) |
| 96 | |
| 97 | buf.Reset() |
| 98 | } else { |
| 99 | if err := buf.WriteByte(tok.Value); err != nil { |
| 100 | return "", errors.Wrap(err, "building string") |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return fmt.Sprintf(format.String(), args...), nil |
| 106 | } |
| 107 | |
| 108 | // escapePhrase escapes the user-supplied FTS keywords by wrapping each term around |
| 109 | // double quotations so that they are treated as 'strings' as defined by SQLite FTS5. |