charmbracelet / charmbracelet/x
ansi: add a End of Sequence item for the ParserDispatcher to complete on
- Dominant language
- Go
- Stars
- 314
- Forks
- 94
- Avg merge
- 3d 22h
- Merged PRs (30d)
- 2
Description
I was noticing that the `ParserDispatcher` can not complete cleanly, it gets no signal that the buffer has ended and you need to check it at the end. You can see it here in the example.
examples/parserlog/main.go
```go
parser.Parse(dispatcher, bts)
if str != "" {
fmt.Printf("[Print] %s\n", str)
}
```
having some irritation with checking and cleaning up state at the end, like shown here with the `str != ""`
It would be nice to add a nil terminating `Sequence` item to the sequence to allow the `ParserDispatcher` a chance to clean any state.
We would need to small change in the `parser.Parse` function to something like this.
```go
// Parse parses the given dispatcher and byte buffer.
func (p *Parser) Parse(dispatcher ParserDispatcher, b []byte) {
for i := 0; i < len(b); i++ {
p.Advance(dispatcher, b[i], i < len(b)-1)
}
dispatcher(SequenceEnd(0)) /// <-- added line
}
```
Thanks
Tom
Contributor guide
Research direction
Start with examples/parserlog/main.go and the parser.Parse function described in the issue, then trace how ParserDispatcher receives Sequence items and how SequenceEnd is represented. Add the end-of-sequence signal so the dispatcher can clean up buffered state without the caller checking str, and verify the example completes cleanly.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- cli
- Issue type
- Feature
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100