| Crates.io | egui_sgr |
| lib.rs | egui_sgr |
| version | 0.1.4 |
| created_at | 2025-11-24 09:21:43.069714+00 |
| updated_at | 2025-11-25 03:01:36.457186+00 |
| description | A Rust library that converts ASCII/ANSI escape sequence color models into colored text in egui |
| homepage | https://github.com/AnlangA/egui-sgr |
| repository | https://github.com/AnlangA/egui-sgr |
| max_upload_size | |
| id | 1947566 |
| size | 524,384 |
egui_sgr is a Rust library that converts ASCII/ANSI escape sequence color models into colored text in egui. It provides a complete parsing system that can handle all three ANSI color models and intelligently manages foreground and background color states.
AnsiParser (src/lib.rs)
current_fg and current_bg to cache the current colors.\x1b\[([0-9;]+)m([^\x1b]*) matches ANSI sequences.parse(&mut self, input: &str) -> Vec<ColoredText>process_ansi_sequence(&mut self, sequence: &str)ColoredText Struct
text (String), foreground_color (Optionbackground_color (OptionColor Model Modules (src/color_models/)
parse_*_color.ansi_color_to_egui(color_code: u8) -> Color32.code = color_code - 16
r = code / 36
g = (code % 36) / 6
b = code % 6
color_component = if c == 0 { 0 } else { 55 + c * 40 }
// Simplified, original has different values for c == 5
ansi_256_to_egui(color_code: u8) -> Color32.38;2;<r>;<g>;<b> (foreground) and 48;2;<r>;<g>;<b> (background).rgb_to_egui(r: u8, g: u8, b: u8) -> Color32.Convenience Function
pub fn ansi_to_rich_text(input: &str) -> Vec<RichText>
Advanced Interface
pub fn convert_to_rich_text(colored_texts: &[ColoredText]) -> Vec<RichText>
Parser Interface
let mut parser = AnsiParser::new();
let colored_segments = parser.parse(input);
Basic Usage
\x1b[31mRed\x1b[0m\x1b[38;5;208mOrange\x1b[0m\x1b[38;2;255;105;180mPink\x1b[0mCombined Usage
\x1b[31;43mRed text on yellow background\x1b[0m\x1b[31mRed\x1b[43mRed text on yellow\x1b[32mGreen text on yellow\x1b[0m\x1b[0m resets all color states.\x1b[38;...m sets the foreground color.\x1b[48;...m sets the background color.\x1b[39m resets the foreground color.\x1b[49m resets the background color.current_state = {fg: None, bg: None}
For each ANSI sequence:
1. Parse the codes in the sequence.
2. Update the current state.
3. Apply the current state to the subsequent text.
Color Model Tests
Parser Tests
Option to avoid unnecessary color allocations.egui: Provides Color32 and RichText types.regex: Provides regular expression matching functionality.examples/demo.rs demonstrates the full functionality of the library, including:
