-
Notifications
You must be signed in to change notification settings - Fork 92
Strip ANSI escape codes to compute cursor position #136
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
Conversation
inquire/src/ansi.rs
Outdated
pub chars: Chars<'a>, | ||
} | ||
|
||
/// Constructs an iterator over the chars of the input string, stripping away ANSI escape codes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make a Trait with this method into_ansi_strip_iter()
and implement it for &str
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea! I've implemented this in the last commit, but renamed it to ansi_stripped_chars()
to more closely match input.chars()
though, as this iterator is just a wrapper around Chars
.
inquire/src/ansi.rs
Outdated
|
||
/// Match/consume the next ANSI escape code if it exists. | ||
/// The format is based on [this description](https://handwiki.org/wiki/ANSI_escape_code#CSI_(Control_Sequence_Introducer)_sequences) | ||
fn match_escape_code(chars: Chars<'_>) -> MatchResult<'_> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this cross-platform? Should we use different rules depending on the platform? From a quick search looks like Windows ANSI and Standard ANSI differ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is, but I'll need to research more before being sure.
Also renamed `strip_iter` to `ansi_stripped_chars`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only the changelog missing (and possibly the cross-platform double check)
I've also confirmed this fixes #135 :) |
Cross-platform compatibility doesn't seem like a concern, from what I've seen. Terminal emulators like Alacritty and Wezterm apparently use the same code to parse ANSI escape sequences, no matter the OS. Cross-terminal compatibility, however, is impossible to achieve, so I'm not sure which standard to implement... Both Wezterm and Alacritty point to this state machine for parsing escape and control sequences, but they're somehow inconsistent?? (this might be a bug in the "vte" crate) I did overlook one thing in my implementation though: only CSI (the sequences that begin with |
Should be mergeable now 👀 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fantastic
* Strip ANSI escape codes to compute cursor position (#136) * Allow releases from non-main * chore: Release inquire version 0.6.2 --------- Co-authored-by: Leonardo Riether <leonardoalves8217@gmail.com>
An attempt to fix #135
I added a module
crate::ansi
that parses ANSI escape codes according to this description, which is then used to skip them when calculating the cursor position onframe_finish()
;Note that there are some rendering inconsistencies for ANSI codes between terminals, so it's impossible to make this work for every escape sequence in every terminal, but for non-pathological escape codes I think this implementation should be fine.
Pictured: inconsistencies between Kitty, Wezterm and Alacritty.
