Skip to content
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

Merge redundant ANSI Escape Sequences #4475

Open
Semnodime opened this issue May 6, 2024 · 0 comments
Open

Merge redundant ANSI Escape Sequences #4475

Semnodime opened this issue May 6, 2024 · 0 comments

Comments

@Semnodime
Copy link

Issue

Colored output often contains redundant ANSI Escape Sequences leading to poor signal to noise ratio.

Example

To render the following single colored line of a hex dump view
0x00000400 0000 0000 0000 0000 0000 0000 0000 0000 ................

the following data has to be processed by the terminal emulator

\x1b[0m\x1b[38;2;19;161;14m0x00000400\x1b[0m \x1b[38;2;19;161;14m00\x1b[0m\x1b[38;2;19;161;14m00\x1b[0m \x1b[38;2;19;161;14m00\x1b[0m\x1b[38;2;19;161;14m00\x1b[0m \x1b[38;2;19;161;14m00\x1b[0m\x1b[38;2;19;161;14m00\x1b[0m \x1b[38;2;19;161;14m00\x1b[0m\x1b[38;2;19;161;14m00\x1b[0m \x1b[38;2;19;161;14m00\x1b[0m\x1b[38;2;19;161;14m00\x1b[0m \x1b[38;2;19;161;14m00\x1b[0m\x1b[38;2;19;161;14m00\x1b[0m \x1b[38;2;19;161;14m00\x1b[0m\x1b[38;2;19;161;14m00\x1b[0m \x1b[38;2;19;161;14m00\x1b[0m\x1b[38;2;19;161;14m00\x1b[0m \x1b[38;2;19;161;14m.\x1b[0m\x1b[38;2;19;161;14m.\x1b[0m\x1b[38;2;19;161;14m.\x1b[0m\x1b[38;2;19;161;14m.\x1b[0m\x1b[38;2;19;161;14m.\x1b[0m\x1b[38;2;19;161;14m.\x1b[0m\x1b[38;2;19;161;14m.\x1b[0m\x1b[38;2;19;161;14m.\x1b[0m\x1b[38;2;19;161;14m.\x1b[0m\x1b[38;2;19;161;14m.\x1b[0m\x1b[38;2;19;161;14m.\x1b[0m\x1b[38;2;19;161;14m.\x1b[0m\x1b[38;2;19;161;14m.\x1b[0m\x1b[38;2;19;161;14m.\x1b[0m\x1b[38;2;19;161;14m.\x1b[0m\x1b[38;2;19;161;14m.\x1b[0m \x1b[38;2;19;161;14m\n

When mergin redundant ANSI Escape Sequences, the data simplifies to
\x1b[0m\x1b[38;2;19;161;14m00x00000400 0000 0000 0000 0000 0000 0000 0000 0000 ................\x1b[0m

Implementation Suggestion

This could be solved by introducing a state machine which tracks the current font properties such as foreground color, background color, italic, underline, bold, etc.
Then, whenever a print to a terminal with certain font properties is requested, the state machine would check if a font mode change is required and yield a corresponding ANSI Escape Sequence prefixing the textual data.
In case the modes partially or fully match, only the shortest prefix necessary will be output, reducing redundant data.

Affected Code

  • code printing colored text
  • code related to state management regarding terminal output
    For example:
    rz_cons_printf("%s%s%s\x1b[0m", bgcolor, fgcolor, text);

Reasoning

Category Argument
Pros - Reduced data footprint
- Reduced data rate required for remote sessions
- Reduced latency from start of output to appearance of text
Cons - When output data is lost at times, (e.g. when reconnecting to a running session?) a reset might be necessary, as the state machine intrinsic to the terminal emulator might be out of sync with the one in rizin
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

No branches or pull requests

1 participant