From cbd969a3e683de4512b9a7fce8018d55b36eb43f Mon Sep 17 00:00:00 2001 From: blacknon Date: Sat, 12 Feb 2022 13:58:15 +0900 Subject: [PATCH] bugfix. #32. --- Cargo.toml | 9 ++++---- src/output.rs | 58 +++++++++++++++++++++++++++++++++++---------------- 2 files changed, 45 insertions(+), 22 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 256cf07..e0eedb5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,13 +5,13 @@ keywords = ["watch", "command", "monitoring"] license-file = "LICENSE" name = "hwatch" repository = "https://github.com/blacknon/hwatch" -version = "0.3.1" +version = "0.3.2" [dependencies] ansi-parser = "0.8.0" # TODO: マージされたらバージョン置き換える -# ansi4tui = {git = "https://github.com/blacknon/ansi4tui.git", branch = "master", version = "0.2.1"} -ansi4tui = "0.2" +ansi4tui = {git = "https://github.com/blacknon/ansi4tui.git", branch = "master", version = "0.2.1"} +# ansi4tui = "0.2" chrono = "0.4" clap = "2.20.3" crossterm = "0.22" @@ -23,4 +23,5 @@ serde_derive = "1.0.104" serde_json = "1.0.44" termwiz = "0.15.0" # TODO: マージされたらバージョン上げる(0.16.0) -tui = {version = "0.14.0", default-features = false, features = ['crossterm']} +# tui = {version = "0.14.0", default-features = false, features = ['crossterm']} +tui = {version = "0.16.0", default-features = false, features = ['crossterm']} diff --git a/src/output.rs b/src/output.rs index 5a54ba0..e99f2fa 100644 --- a/src/output.rs +++ b/src/output.rs @@ -212,10 +212,7 @@ pub fn get_watch_diff<'a>(color: bool, line_number: bool, old: &str, new: &str) match color { false => line_data = get_watch_diff_line(old_vec[i], new_vec[i]), - true => { - // line_data = Spans::from(vec![]); - line_data = get_watch_diff_line_with_ansi(old_vec[i], new_vec[i]) - } + true => line_data = get_watch_diff_line_with_ansi(old_vec[i], new_vec[i]), } if line_number { @@ -247,25 +244,35 @@ fn get_watch_diff_line<'a>(old_line: &str, new_line: &str) -> Spans<'a> { let mut old_line_chars: Vec = old_line.chars().collect(); let mut new_line_chars: Vec = new_line.chars().collect(); - let space: char = ' '; + let space: char = '\u{007f}'; let max_char = cmp::max(old_line_chars.len(), new_line_chars.len()); let mut _result = vec![]; for x in 0..max_char { if old_line_chars.len() <= x { - old_line_chars.push(space.clone()); + old_line_chars.push(space); } if new_line_chars.len() <= x { - new_line_chars.push(space.clone()); + new_line_chars.push(space); } - if old_line_chars[x] != new_line_chars[x] { + let old_char = old_line_chars[x]; + let new_char = new_line_chars[x]; + + if old_char != new_char { + let mut data: Span; + if new_char == space { + data = Span::from(' '.to_string()); + data.style = Style::default().add_modifier(Modifier::REVERSED); + } else { + data = Span::styled( + new_line_chars[x].to_string(), + Style::default().add_modifier(Modifier::REVERSED), + ); + } // add span - _result.push(Span::styled( - new_line_chars[x].to_string(), - Style::default().add_modifier(Modifier::REVERSED), - )); + _result.push(data); } else { // add span _result.push(Span::styled( @@ -275,6 +282,10 @@ fn get_watch_diff_line<'a>(old_line: &str, new_line: &str) -> Spans<'a> { } } + // last char + // + _result.push(Span::styled(space.to_string(), Style::default())); + return Spans::from(_result); } @@ -301,33 +312,44 @@ fn get_watch_diff_line_with_ansi<'a>(old_line: &str, new_line: &str) -> Spans<'a // break; } + let space = '\u{007f}'.to_string(); let max_span = cmp::max(old_spans.len(), new_spans.len()); // let mut _result = vec![]; for x in 0..max_span { // if old_spans.len() <= x { - old_spans.push(Span::from(" ".to_string())); + old_spans.push(Span::from(space.to_string())); } // if new_spans.len() <= x { - new_spans.push(Span::from(" ".to_string())); + new_spans.push(Span::from(space.to_string())); } // if old_spans[x].content != new_spans[x].content || old_spans[x].style != new_spans[x].style { - // add span - new_spans[x].style = new_spans[x] - .style - .patch(Style::default().add_modifier(Modifier::REVERSED)); + if new_spans[x].content == space { + let mut data = Span::from(' '.to_string()); + data.style = Style::default().add_modifier(Modifier::REVERSED); + new_spans[x] = data; + } else { + // add span + new_spans[x].style = new_spans[x] + .style + .patch(Style::default().add_modifier(Modifier::REVERSED)); + } } // _result.push(new_spans[x].clone()); } + // last char + // + _result.push(Span::styled(space.to_string(), Style::default())); + // return Spans::from(_result); }