Skip to content

Conversation

LeoRiether
Copy link
Contributor

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 on frame_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.
Screenshot_20230427_002742

Screenshot_20230427_005333

pub chars: Chars<'a>,
}

/// Constructs an iterator over the chars of the input string, stripping away ANSI escape codes.
Copy link
Owner

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

Copy link
Contributor Author

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.


/// 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<'_> {
Copy link
Owner

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

Copy link
Contributor Author

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`
Copy link
Owner

@mikaelmello mikaelmello left a 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)

@mikaelmello
Copy link
Owner

I've also confirmed this fixes #135 :)

@LeoRiether
Copy link
Contributor Author

LeoRiether commented May 1, 2023

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)
more inconsistencies

I did overlook one thing in my implementation though: only CSI (the sequences that begin with \x1b[ or \9b in wezterm) are parsed currently. I should implement OSCs and DSCs too, in case someone wants to set their terminal's title in an inquire prompt I guess :)

@LeoRiether
Copy link
Contributor Author

Should be mergeable now 👀

Copy link
Owner

@mikaelmello mikaelmello left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic

@mikaelmello mikaelmello merged commit 040147a into mikaelmello:main May 4, 2023
mikaelmello added a commit that referenced this pull request May 7, 2023
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cursor Wonkiness When Using Console In Help Message
2 participants