| 203 | } |
| 204 | |
| 205 | function snippetAround(text: string, regex: RegExp, maxChars: number): string { |
| 206 | const m = regex.exec(text) |
| 207 | const index = m ? m.index : 0 |
| 208 | const half = Math.floor(maxChars / 2) |
| 209 | const start = Math.max(0, index - half) |
| 210 | const end = Math.min(text.length, start + maxChars) |
| 211 | const prefix = start > 0 ? '…' : '' |
| 212 | const suffix = end < text.length ? '…' : '' |
| 213 | return `${prefix}${text.slice(start, end)}${suffix}` |
| 214 | } |
| 215 | |
| 216 | function done(state: GrepState): boolean { |
| 217 | return state.truncated || state.matches.length >= state.maxMatches |