Skip to content

Commit

Permalink
Removed a lot of unnsecessary code.
Browse files Browse the repository at this point in the history
Removed the style module and main module.
Added README
  • Loading branch information
uttarayan21 committed Apr 25, 2021
1 parent 8581b1a commit 6e2e4dd
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 186 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
[package]
name = "ansi-to-tui"
version = "0.1.0"
version = "0.1.1"
edition = "2018"

[dependencies]
tui = { version = "0.14.0", default-features = false, features = ["crossterm"] }
unicode-width = "0.1.8"
tui = { version = "0.14.0", default-features = false }
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# ansi-to-tui

Parse text with ansi color codes and turn them into [`tui::text::Text`](https://docs.rs/tui/0.14.0/tui/text/struct.Text.html).

Supports TrueColor ( RGB ) ( `\x1b[38;2;<r>;<g>;<b>m`)
Supports 8 - Bit Color ( 0..256 ) ( `\x1b[38;5;<n>m` )
Supports 4 - Bit Color Pallete ( `\x1b[30..37;40..47m` )

A naive implementation, relatively fast.
Only dependency is the tui crate.
Lots of room for improvement.
13 changes: 0 additions & 13 deletions ansi.py

This file was deleted.

16 changes: 1 addition & 15 deletions src/ansi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Stack<u8> {
let r = self.pop().unwrap();
color = Color::Rgb(r, g, b);
} else {
return Err(Error::TempError);
return Err(Error::UnknownColor);
}
self.clear();
Ok(color)
Expand Down Expand Up @@ -75,7 +75,6 @@ impl AnsiGraphicsStack {
let mut color_parse_mode: Option<AnsiColorMode> = None;
let mut last_sequence: usize = 0;
let mut color_layer: Option<AnsiColorLayer> = None;
// println!("{:?}", self);
for sequence in self.iter().cloned() {
if color_parse {
if sequence < 255 {
Expand Down Expand Up @@ -121,7 +120,6 @@ impl AnsiGraphicsStack {
color_parse = false;
}
} else {
// println!("stop color index");
color_parse = false;
}
last_sequence = sequence;
Expand All @@ -142,12 +140,10 @@ impl AnsiGraphicsStack {
AnsiCode::DefaultForegroundColor => style = style.fg(Color::Reset),
AnsiCode::DefaultBackgroundColor => style = style.bg(Color::Reset),
AnsiCode::ForegroundColorIndex => {
// println!("foreground color index");
color_parse = true;
color_layer = Some(AnsiColorLayer::Foreground)
}
AnsiCode::BackgroundColorIndex => {
// println!("background color index");
color_parse = true;
color_layer = Some(AnsiColorLayer::Background)
}
Expand All @@ -158,8 +154,6 @@ impl AnsiGraphicsStack {
last_sequence = sequence;
}
self.stack.clear();
// println!("style {:?}", style);
// println!("-----------------------------");
style
}
}
Expand Down Expand Up @@ -207,18 +201,10 @@ pub fn ansi_to_text<'t, B: AsRef<[u8]>>(bytes: B) -> Result<Text<'t>, Error> {
}
last_byte = byte;
}
// println!("{:?}", buffer);
if !spans_buffer.is_empty() {
buffer.push(Spans::from(spans_buffer.clone()));
spans_buffer.clear();
}

// for span in buffer.iter() {
// println!("HELLOA");
// println!("{:?}", span);
// }

// println!("bufferlen {}", buffer.len());
// println!("{:?}", &buffer);
Ok(buffer.into())
}
19 changes: 0 additions & 19 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,3 @@ impl From<AnsiColor> for Color {
}
}
}
// pub fn ansi_to_color(ansi: u8) -> AnsiColor {
// match ansi {
// 30 | 40 => AnsiColor::Black,
// 31 | 41 => AnsiColor::Red,
// 32 | 42 => AnsiColor::Green,
// 33 | 43 => AnsiColor::Yellow,
// 34 | 44 => AnsiColor::Blue,
// 35 | 45 => AnsiColor::Gray,
// 90 | 100 => AnsiColor::DarkGray,
// 91 | 101 => AnsiColor::LightRed,
// 92 | 102 => AnsiColor::LightGreen,
// 93 | 103 => AnsiColor::LightYellow,
// 94 | 104 => AnsiColor::LightBlue,
// 95 | 105 => AnsiColor::LightMagenta,
// 96 | 106 => AnsiColor::LightCyan,
// 97 | 107 => AnsiColor::White,
// _ => AnsiColor::Black,
// }
// }
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ pub enum Error {
StackLocked,
StackUnlocked,
InvalidAnsi,
TempError,
UnknownColor,
}
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ mod code;
mod color;
mod error;
mod stack;
mod style;
mod tests;

pub use ansi::ansi_to_text;
92 changes: 0 additions & 92 deletions src/main.rs

This file was deleted.

40 changes: 0 additions & 40 deletions src/style.rs

This file was deleted.

10 changes: 8 additions & 2 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#[cfg(test)]
#[test]
fn test_color() {
use crate::ansi_to_color;
use crate::stack::Stack;
use tui::style::Color;
assert_eq!(ansi_to_color(31_u8), Color::Red);
let mut stack: Stack<u8> = Stack::new();
stack.push(30);
assert_eq!(stack.parse_color().unwrap(), Color::Indexed(30));
stack.push(30);
stack.push(3);
stack.push(55);
assert_eq!(stack.parse_color().unwrap(), Color::Rgb(30, 3, 55));
}

0 comments on commit 6e2e4dd

Please sign in to comment.