Skip to content

Commit

Permalink
bugfix. #32.
Browse files Browse the repository at this point in the history
  • Loading branch information
blacknon committed Feb 12, 2022
1 parent 36f11de commit cbd969a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 22 deletions.
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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']}
58 changes: 40 additions & 18 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -247,25 +244,35 @@ fn get_watch_diff_line<'a>(old_line: &str, new_line: &str) -> Spans<'a> {
let mut old_line_chars: Vec<char> = old_line.chars().collect();
let mut new_line_chars: Vec<char> = 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(
Expand All @@ -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);
}

Expand All @@ -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);
}
Expand Down

0 comments on commit cbd969a

Please sign in to comment.