Skip to content

Commit

Permalink
Support tab
Browse files Browse the repository at this point in the history
  • Loading branch information
sachaos committed Aug 18, 2024
1 parent cadbcc5 commit eb31496
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use tracing_subscriber::field::debug;

use crate::{
action::{self, Action, DiffMode},
bytes::normalize_stdout,
cli::Cli,
components::{fps::FpsCounter, home::Home, Component},
config::{Config, RuntimeConfig},
Expand Down Expand Up @@ -337,10 +338,12 @@ impl<S: Store> App<S> {
let mut string = "".to_string();
if let Some(record) = record {
action_tx.send(Action::SetClock(record.start_time))?;
let mut result =
termtext::Converter::new(style).convert(&record.stdout);
let mut result = termtext::Converter::new(style)
.convert(&normalize_stdout(record.stdout.clone()));
log::debug!("result: {:?}", result);
if record.stdout.is_empty() {
result = termtext::Converter::new(style).convert(&record.stderr);
result = termtext::Converter::new(style)
.convert(&normalize_stdout(record.stderr.clone()));
result.mark_text(
0,
result.len(),
Expand All @@ -354,7 +357,7 @@ impl<S: Store> App<S> {
let previous_record = self.store.get_record(previous_id)?;
if let Some(previous_record) = previous_record {
let previous_result = termtext::Converter::new(style)
.convert(&previous_record.stdout);
.convert(&normalize_stdout(previous_record.stdout));
let previous_string = previous_result.plain_text();
if diff_mode == DiffMode::Add {
diff_and_mark(
Expand Down
28 changes: 28 additions & 0 deletions src/bytes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
pub fn normalize_stdout(b: Vec<u8>) -> Vec<u8> {
// To fix '\t' width issue replace '\t' with ' '
let mut b = b;
let mut i = 0;
while i < b.len() {
if b[i] == b'\t' {
b[i] = b' ';
b.insert(i, b' ');
b.insert(i, b' ');
b.insert(i, b' ');
i += 4;
} else {
i += 1;
}
}
b
}

mod test {
use super::*;

#[test]
fn test_normalize_stdout() {
let b = b"hello\tworld".to_vec();
let b = normalize_stdout(b);
assert_eq!(b, b"hello world".to_vec());
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

pub mod action;
pub mod app;
mod bytes;
pub mod cli;
pub mod components;
pub mod config;
Expand Down
1 change: 1 addition & 0 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use tokio::{

use crate::{
action::Action,
bytes::normalize_stdout,
config::{Config, RuntimeConfig},
exec::exec,
store::{Record, Store},
Expand Down
35 changes: 34 additions & 1 deletion src/termtext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl Perform for Converter {
fn execute(&mut self, byte: u8) {
if byte == b'\n' {
self.text.add_char(Char {
c: '\n',
c: byte as char,
style: self.style,
});
}
Expand Down Expand Up @@ -401,4 +401,37 @@ mod test {
}
);
}

#[test]
fn test_convert_tab() {
// echo "1\t2" | xxd
// 00000000: 3109 320a 1.2.
let text: Vec<u8> = vec![0x31, 0x09, 0x32, 0x0a];
let mut converter = Converter::new(Style::new());
let result = converter.convert(&text);

assert_eq!(
result,
Text {
chars: vec![
Char {
c: '1',
style: Style::new(),
},
Char {
c: '\t',
style: Style::new(),
},
Char {
c: '2',
style: Style::new(),
},
Char {
c: '\n',
style: Style::new()
},
]
}
);
}
}

0 comments on commit eb31496

Please sign in to comment.