NewSnippet creates a new [Snippet] from a byte slice and a line and column number. The line and column numbers should be 1-indexed. For example, the first character in the file would be 1:1 (line 1, column 1). The padding determines the number of lines to include before and after the chosen line.
(b []byte, opts ...SnippetOption)
| 56 | // first character in the file would be 1:1 (line 1, column 1). The padding |
| 57 | // determines the number of lines to include before and after the chosen line. |
| 58 | func NewSnippet(b []byte, opts ...SnippetOption) *Snippet { |
| 59 | snippet := &Snippet{} |
| 60 | snippet.Options(opts...) |
| 61 | |
| 62 | // Syntax highlight the input and split it into lines |
| 63 | buf := &bytes.Buffer{} |
| 64 | if err := quick.Highlight(buf, string(b), "yaml", "terminal", "task"); err != nil { |
| 65 | buf.Write(b) |
| 66 | } |
| 67 | linesRaw := strings.Split(string(b), "\n") |
| 68 | linesHighlighted := strings.Split(buf.String(), "\n") |
| 69 | |
| 70 | // Work out the start and end lines of the snippet |
| 71 | snippet.start = max(snippet.line-snippet.padding, 1) |
| 72 | snippet.end = min(snippet.line+snippet.padding, len(linesRaw)-1) |
| 73 | snippet.linesRaw = linesRaw[snippet.start-1 : snippet.end] |
| 74 | snippet.linesHighlighted = linesHighlighted[snippet.start-1 : snippet.end] |
| 75 | |
| 76 | return snippet |
| 77 | } |
| 78 | |
| 79 | // Options loops through the given [SnippetOption] functions and applies them |
| 80 | // to the [Snippet]. |
searching dependent graphs…