-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(ansi): add Scanner for splitting ANSI encoded strings. #215
base: main
Are you sure you want to change the base?
Conversation
6676890
to
b7f1dd5
Compare
Hi @pachecot, this looks cool ngl. I've thought about a similar idea before but performance was more important and thus that led us to implement both In terms of the PR, I think this adds a lot of code surface to the
The Another thing to keep in mind, is that the purpose behind the Let me know what you think about the |
Hi @aymanbagabas thanks. Still learning this library, will need to play with it some more. The |
That's a fair point. We can use the func NewScanner(r io.Reader) *Scanner
// bufio.Scanner scanner interface to conform to
type Scanner interface {
Bytes() []byte
Text() string
Err() error
Scan() bool
}
// extra functions we need for ansi.Scanner
func (s *Scanner) Width() int // returns the cell width of the last scanned token
func (s *Scanner) Len() int // returns the bytes length of the last scanned token
// bufio.SplitFunc implementation to be used with bufio.Scanner
func SplitFunc(data []byte, atEOF bool) (advance int, token []byte, err error) I'm a little hesitant about introducing a new type just to distinguish the sequence type. I want to keep using primitive types everywhere possible. Let me know if you have a good solution for this. |
Removed the Token Type and simplified it some to be more inline with type Scanner interface {
Bytes() []byte // returns last token
EOF() bool
Error() error
IsEscape() bool // returns the if the last scanned token is a control/escape
Len() int // returns the bytes length of the last scanned token
Scan() bool
Split(f SplitFunc) // set the split function
Text() string // returns last token as a string
Token() ([]byte, int, bool) // returns token, width, escape (same as s.Bytes(), s.Width(), s.IsEscape())
Width() int // returns the cell width of the last scanned token
}
type SplitFunc func(data []byte, width int, atEOF bool) (advance int, token []byte, err error) I was thinking the width in the split function was needed. But, maybe its not needed anymore without the TokenState. Still just using the Is there something you would want to do with the escape sequences here? I was looking at this as |
remove width arg from SplitFunc rename (*scanner).Error to (*scanner).Err update comments
4a6a3ce
to
c0d4f36
Compare
Hey @pachecot, I wonder how's the performance of |
Hi,
Thought this might be helpful for manipulating styled strings without getting into the details of the Parser and all the encodings of styled strings. It could be very useful in other modules like lipgloss or when using it.
Here are a few samples of functions rewritten using this..
I was trying to do something like transpose a line to a column.
\x1b[31mHi\x1b[0m
to
Here is a sample function to do that..
Let me know if you are interested and if you think it still needs some changes..
Thanks.
Tom