Skip to content

Commit

Permalink
update. #24 batch mode buffix.
Browse files Browse the repository at this point in the history
  • Loading branch information
blacknon committed Apr 6, 2024
1 parent 0cc6612 commit 994787a
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ impl<'a> HelpWindow<'a> {

///
pub fn draw(&mut self, f: &mut Frame) {
let title = "help";
let title = " [help] ";

let size = f.size();
let area = centered_rect(60, 50, size);
let area = centered_rect(80, 70, size);

// create block.
let block = Paragraph::new(self.data.clone())
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// v0.3.12
// TODO(blakcnon): batch modeの実装.(clapのバージョンをあげてから、diff等のオプションを指定できるようにしたほうがいいのかも??)
// TODO(blacknon): word diffでremove行のワードがハイライト表示されないので、原因を調べる.

// v0.3.13
// TODO(blacknon): 任意時点間のdiffが行えるようにする.
Expand Down
127 changes: 104 additions & 23 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ impl Printer {
// append elements_line
elements_line.push(vec![Span::styled(
data.to_string(),
Style::default().fg(Color::Green),
Style::default().fg(COLOR_WATCH_LINE_ADD),
)]);

// append elements_str
Expand Down Expand Up @@ -1016,7 +1016,7 @@ impl Printer {
if self.is_line_number {
line.insert_str(
0,
&gen_counter_str(self.is_color, dest_counter as usize, header_width, DifferenceType::Add)
&gen_counter_str(self.is_color, dest_counter as usize, header_width, DifferenceType::Add)
);
}

Expand Down Expand Up @@ -1078,7 +1078,7 @@ impl Printer {
// append elements_line
elements_line.push(vec![Span::styled(
data.to_string(),
Style::default().fg(Color::Green),
Style::default().fg(COLOR_WATCH_LINE_REM),
)]);

// append elements_str
Expand Down Expand Up @@ -1106,7 +1106,7 @@ impl Printer {
if self.is_line_number {
line.insert_str(
0,
&gen_counter_str(self.is_color, dest_counter as usize, header_width, DifferenceType::Add)
&gen_counter_str(self.is_color, src_counter as usize, header_width, DifferenceType::Rem)
);
}

Expand All @@ -1119,7 +1119,7 @@ impl Printer {
LineElementData::Spans(data_line) => {
// is watch
for l in data_line {
let mut line = vec![Span::styled("+ ", Style::default().fg(COLOR_WATCH_LINE_REM))];
let mut line = vec![Span::styled("- ", Style::default().fg(COLOR_WATCH_LINE_REM))];

for d in l {
line.push(d);
Expand All @@ -1130,7 +1130,7 @@ impl Printer {
0,
Span::styled(
format!("{src_counter:>header_width$} | "),
Style::default().fg(Color::DarkGray),
Style::default().fg(COLOR_WATCH_LINE_NUMBER_REM),
),
);
}
Expand Down Expand Up @@ -1172,7 +1172,7 @@ impl Printer {

// line
let mut line_data_spans = vec![];
let mut line_data_strs = vec![];
let mut line_data_strs = "".to_string();

match before_diffs {
// Change Line.
Expand All @@ -1193,9 +1193,22 @@ impl Printer {
// batch data
let str_line_style = ansi_term::Style::new()
.fg(COLOR_BATCH_LINE_ADD);
line_data_strs.push(
str_line_style.paint(char).to_string()

let same_element = get_word_diff_line_to_strs(
str_line_style,
char,
);

for (counter, lines) in same_element.into_iter().enumerate() {
if counter > 0 {
result_data_strs.push(line_data_strs);
line_data_strs = "".to_string();
}

for l in lines {
line_data_strs.push_str(&l);
}
}
} else {
// watch data
let same_element = get_word_diff_line_to_spans(
Expand All @@ -1222,11 +1235,24 @@ impl Printer {
if self.is_batch {
// batch data
let str_line_style = ansi_term::Style::new()
.reverse()
.fg(COLOR_BATCH_LINE_ADD);
line_data_strs.push(
str_line_style.paint(char).to_string()
.fg(COLOR_BATCH_LINE_ADD)
.reverse();

let add_element = get_word_diff_line_to_strs(
str_line_style,
char,
);

for (counter, lines) in add_element.into_iter().enumerate() {
if counter > 0 {
result_data_strs.push(line_data_strs);
line_data_strs = "".to_string();
}

for l in lines {
line_data_strs.push_str(&l);
}
}
} else {
// watch data
let add_element = get_word_diff_line_to_spans(
Expand Down Expand Up @@ -1283,8 +1309,11 @@ impl Printer {
result_data_spans.push(line_data_spans);
}

if line_data_strs.len() > 0{
result_data_strs.push(line_data_strs);
}

if self.is_batch {
result_data_strs.push(line_data_strs.join(" "));
return LineElementData::String(result_data_strs);
} else {
return LineElementData::Spans(result_data_spans);
Expand Down Expand Up @@ -1312,7 +1341,7 @@ impl Printer {

// line
let mut line_data_spans = vec![];
let mut line_data_strs = vec![];
let mut line_data_strs = "".to_string();

match before_diffs {
// Change Line.
Expand All @@ -1333,9 +1362,23 @@ impl Printer {
// batch data
let str_line_style = ansi_term::Style::new()
.fg(COLOR_BATCH_LINE_REM);
line_data_strs.push(
str_line_style.paint(char).to_string()

let same_element = get_word_diff_line_to_strs(
str_line_style,
char,
);


for (counter, lines) in same_element.into_iter().enumerate() {
if counter > 0 {
result_data_strs.push(line_data_strs);
line_data_strs = "".to_string();
}

for l in lines {
line_data_strs.push_str(&l);
}
}
} else {
// watch data
let same_element = get_word_diff_line_to_spans(
Expand All @@ -1362,11 +1405,25 @@ impl Printer {
if self.is_batch {
// batch data
let str_line_style = ansi_term::Style::new()
.reverse()
.fg(COLOR_BATCH_LINE_REM);
line_data_strs.push(
str_line_style.paint(char).to_string()
.fg(COLOR_BATCH_LINE_REM)
.reverse();

let same_element = get_word_diff_line_to_strs(
str_line_style,
char,
);


for (counter, lines) in same_element.into_iter().enumerate() {
if counter > 0 {
result_data_strs.push(line_data_strs);
line_data_strs = "".to_string();
}

for l in lines {
line_data_strs.push_str(&l);
}
}
} else {
// watch data
let add_element = get_word_diff_line_to_spans(
Expand Down Expand Up @@ -1406,7 +1463,7 @@ impl Printer {
// batch data
let str_line_style = ansi_term::Style::new()
.fg(COLOR_BATCH_LINE_REM);
line_data_strs.push(str_line_style.paint(data).to_string());
result_data_strs.push(str_line_style.paint(data).to_string());
} else {
// watch data
let line_data = vec![Span::styled(
Expand All @@ -1423,8 +1480,11 @@ impl Printer {
result_data_spans.push(line_data_spans);
}

if line_data_strs.len() > 0{
result_data_strs.push(line_data_strs);
}

if self.is_batch {
result_data_strs.push(line_data_strs.join(" "));
return LineElementData::String(result_data_strs);
} else {
return LineElementData::Spans(result_data_spans);
Expand Down Expand Up @@ -1576,3 +1636,24 @@ fn get_word_diff_line_to_spans<'a>(

result
}

///
fn get_word_diff_line_to_strs(
style: ansi_term::Style,
diff_str: &str,
) -> Vec<Vec<String>> {
// result
let mut result = vec![];

for l in diff_str.split('\n') {
let text = get_ansi_strip_str(l);
result.push(
vec![
style.paint(text).to_string(),
ansi_term::Style::new().paint(" ").to_string(),
]
);
}

result
}

0 comments on commit 994787a

Please sign in to comment.