rizin icon indicating copy to clipboard operation
rizin copied to clipboard

Merge redundant ANSI Escape Sequences

Open Semnodime opened this issue 1 month ago • 0 comments

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: https://github.com/rizinorg/rizin/blob/57f1070726ef6a8d5c0381d65a7da25818bff923/librz/core/cmd/cmd_print.c#L1495

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

Semnodime avatar May 06 '24 11:05 Semnodime