Skip to content

Commit

Permalink
update. #24 batch mode add. word diff fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
blacknon committed Apr 5, 2024
1 parent be06288 commit 0cc6612
Show file tree
Hide file tree
Showing 4 changed files with 629 additions and 644 deletions.
67 changes: 67 additions & 0 deletions src/ansi.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) 2024 Blacknon.
// This code from https://github.com/blacknon/ansi4tui/blob/master/src/lib.rs

use ansi_parser::{AnsiParser, AnsiSequence, Output};
use heapless::consts::*;
use termwiz::cell::{Blink, Intensity, Underline};
use termwiz::color::ColorSpec;
use termwiz::escape::{
Expand Down Expand Up @@ -140,3 +142,68 @@ pub fn bytes_to_text<'a, B: AsRef<[u8]>>(bytes: B) -> Text<'a> {

spans.into()
}

// Ansi Color Code parse
// ==========

/// Apply ANSI color code character by character.
pub fn gen_ansi_all_set_str<'b>(text: &str) -> Vec<Vec<Span<'b>>> {
// set Result
let mut result = vec![];

// ansi reset code heapless_vec
let mut ansi_reset_vec = heapless::Vec::<u8, U5>::new();
let _ = ansi_reset_vec.push(0);

// get ansi reset code string
let ansi_reset_seq = AnsiSequence::SetGraphicsMode(ansi_reset_vec);
let ansi_reset_seq_str = ansi_reset_seq.to_string();

// init sequence.
let mut sequence: AnsiSequence;
let mut sequence_str = "".to_string();

// text processing
let mut processed_text = vec![];
for block in text.ansi_parse() {
match block {
Output::TextBlock(text) => {
for char in text.chars() {
let append_text = if !sequence_str.is_empty() {
format!("{sequence_str}{char}{ansi_reset_seq_str}")
} else {
format!("{char}")
};

// parse ansi text to tui text.
let data = bytes_to_text(format!("{append_text}\n").as_bytes());
if let Some(d) = data.into_iter().next() {
for x in d.spans {
processed_text.push(x);
}
}
}
}
Output::Escape(seq) => {
sequence = seq;
sequence_str = sequence.to_string();
}
}
}

result.push(processed_text);

result
}

///
pub fn get_ansi_strip_str(text: &str) -> String {
let mut line_str = "".to_string();
for block in text.ansi_parse() {
if let Output::TextBlock(t) = block {
line_str.push_str(t);
}
}

line_str
}
39 changes: 1 addition & 38 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,6 @@ impl<'a> App<'a> {
return;
}

// text_src ... old text.
let text_src: &str;

// set target number at new history.
let mut target_dst: usize = num;

Expand All @@ -400,49 +397,15 @@ impl<'a> App<'a> {
let previous_dst = get_results_previous_index(&results, target_dst);

// set new text(text_dst)
let text_dst = match self.output_mode {
OutputMode::Output => &results[&target_dst].output,
OutputMode::Stdout => &results[&target_dst].stdout,
OutputMode::Stderr => &results[&target_dst].stderr,
};
let dest = results[&target_dst].clone();

// set old text(text_src)
let mut src = CommandResult::default();
if previous_dst > 0 {
src = results[&previous_dst].clone();
match self.output_mode {
OutputMode::Output => text_src = &results[&previous_dst].output,
OutputMode::Stdout => text_src = &results[&previous_dst].stdout,
OutputMode::Stderr => text_src = &results[&previous_dst].stderr,
}
} else {
text_src = "";
}

let output_data = match self.diff_mode {
DiffMode::Disable => self.printer.get_watch_text(dest, src),
DiffMode::Watch => self.printer.get_watch_text(dest, src),
DiffMode::Line => self.printer.get_watch_text(dest, src),

// DiffMode::Line => output::get_line_diff(
// self.ansi_color,
// self.line_number,
// self.is_only_diffline,
// text_src,
// text_dst,
// self.tab_size,
// ),

DiffMode::Word => output::get_word_diff(
self.ansi_color,
self.line_number,
self.is_only_diffline,
text_src,
text_dst,
self.tab_size,
),
};
let output_data = self.printer.get_watch_text(dest, src);

// TODO: output_dataのtabをスペース展開する処理を追加

Expand Down
112 changes: 1 addition & 111 deletions src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
// that can be found in the LICENSE file.

use crossbeam_channel::{Receiver, Sender};
use difference::{Changeset, Difference};
use std::{io, collections::HashMap};
use std::thread;
use ansi_term::Colour;

use crate::common::{DiffMode, OutputMode};
use crate::event::AppEvent;
use crate::exec::{exec_after_command, CommandResult};
use crate::{output, LINE_ENDING};
use crate::output;

/// Struct at watch view window.
pub struct Batch {
Expand Down Expand Up @@ -207,111 +205,3 @@ impl Batch {
self
}
}


// fn generate_watch_diff_printout_data() -> Vec<String> {}


fn generate_line_diff_printout_data(
dst: CommandResult,
src: CommandResult,
line_number: bool,
is_color: bool,
output_mode: OutputMode,
only_diff_line: bool,
) -> Vec<String> {
let mut result: Vec<String> = Vec::new();

// Switch the result depending on the output mode.
let text_dst = match output_mode {
OutputMode::Output => dst.output.clone(),
OutputMode::Stdout => dst.stdout.clone(),
OutputMode::Stderr => dst.stderr.clone(),
};

// Switch the result depending on the output mode.
let text_src = match output_mode {
OutputMode::Output => src.output.clone(),
OutputMode::Stdout => src.stdout.clone(),
OutputMode::Stderr => src.stderr.clone(),
};

// get diff
let Changeset { diffs, .. } = Changeset::new(&text_src, &text_dst, LINE_ENDING);

let mut src_counter = 1;
let mut dst_counter = 1;

for i in 0..diffs.len() {
match diffs[i] {
Difference::Same(ref x) => {
for line in x.split("\n") {
let line_number = if line_number {
if is_color {
let style = Colour::Fixed(240);
let format = style.paint(format!("{:5} | ", src_counter));
format!("{}", format)
} else {
format!("{:4} | ", src_counter)
}
} else {
"".to_string()
};

result.push(format!("{:}{:}", line_number, line));
src_counter += 1;
dst_counter += 1;
}
}

Difference::Add(ref x) => {
for line in x.split("\n") {
let line_number = if line_number {
if is_color {
let style = Colour::Green;
let format = style.paint(format!("{:5} | ", dst_counter));
format!("{}", format)
} else {
format!("{:4} | ", dst_counter)
}
} else {
"".to_string()
};

result.push(format!("{:}{:}", line_number, line));
dst_counter += 1;
}
}

Difference::Rem(ref x) => {
for line in x.split("\n") {
let line_number = if line_number {
if is_color {
let style = Colour::Red;
let format = style.paint(format!("{:5} | ", src_counter));
format!("{}", format)
} else {
format!("{:4} | ", src_counter)
}
} else {
"".to_string()
};

if only_diff_line {
result.push(format!("{:}{:}", line_number, line));
}

src_counter += 1;
}
}
}
}

return result;
}

// fn generate_word_diff_printout_data(
//
// ) -> Vec<String> {
//
// }
Loading

0 comments on commit 0cc6612

Please sign in to comment.