Skip to content

Commit

Permalink
Newlines with empty buffers are now shown correctly
Browse files Browse the repository at this point in the history
style is now patched instead of overwritten.
so multiple ansi sequences will work.
\e[31m\e[4m here \e4m will not overwrite \e[31m
  • Loading branch information
uttarayan21 committed Apr 29, 2021
1 parent 371bd6f commit dce8cf1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/ansi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ pub fn ansi_to_text<'t, B: IntoIterator<Item = u8>>(bytes: B) -> Result<Text<'t>
if !span_buffer.is_empty() {
buffer.push(Spans::from(span_buffer.clone()));
span_buffer.clear();
} else {
buffer.push(Spans::default())
}
}

Expand All @@ -74,7 +76,8 @@ pub fn ansi_to_text<'t, B: IntoIterator<Item = u8>>(bytes: B) -> Result<Text<'t>

b'm' => {
ansi_stack.push(stack.parse_usize()?);
style = ansi_stack.parse_ansi()?;

style = style.patch(ansi_stack.parse_ansi()?);

// lock after parse since lock will clear
ansi_stack.lock();
Expand Down
5 changes: 5 additions & 0 deletions tests/arch.ascii
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
    ..    
    cl.   
  .llll.  
 ,oc  :l' 
..      ..
24 changes: 19 additions & 5 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,37 @@ fn text_unicode() {

#[test]
fn ascii_rgb() {
use crate::ansi_to_text;
let bytes: Vec<u8> = b"\x1b[38;2;100;100;100mAAABBB".to_vec();
println!("{:#?}", ansi_to_text(bytes));
}
#[test]
fn ascii_multi() {
let bytes = "\x1b[31m\x1b[4m\x1b[1mHELLO".as_bytes().to_vec();
println!("{:#?}",ansi_to_text(bytes));
}

#[test]
#[ignore = "Gives a lot of output"]
fn arch_ascii() {
use crate::ansi_to_text;
use std::{fs::File, io::Read};
let mut ascii = File::open("tests/arch.ascii").unwrap();
let mut buffer: Vec<u8> = Vec::new();
ascii.read_to_end(&mut buffer).unwrap();
let text = ansi_to_text(buffer).unwrap();
println!("{:#?}",text);
}

#[test]
#[ignore = "Give a lot of output"]
#[ignore = "Gives a lot of output"]
fn archlinux_ascii() {
use crate::ansi_to_text;
use std::{fs::File, io::Read};
let mut ascii = File::open("tests/archlinux.ascii").unwrap();
let mut buffer: Vec<u8> = Vec::new();
ascii.read_to_end(&mut buffer).unwrap();
let text = ansi_to_text(buffer).unwrap();
for line in text.lines {
print!("{:?} ", line.width());
}
println!("{:#?}",text);
}

#[test]
Expand Down

0 comments on commit dce8cf1

Please sign in to comment.