Skip to content

Commit

Permalink
Merge pull request #105 from blacknon/0.3.12
Browse files Browse the repository at this point in the history
0.3.12
  • Loading branch information
blacknon authored Apr 7, 2024
2 parents d401e3d + a854948 commit 5108e96
Show file tree
Hide file tree
Showing 15 changed files with 2,668 additions and 1,184 deletions.
172 changes: 89 additions & 83 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ keywords = ["watch", "command", "monitoring"]
license-file = "LICENSE"
name = "hwatch"
repository = "https://github.com/blacknon/hwatch"
version = "0.3.11"
version = "0.3.12"

[dependencies]
# TODO: ansi-parserが正式にバージョンアップしたらそちらに切り替える
heapless = "0.6.1"
hwatch-ansi-parser = "0.9.0"

ansi_term = "0.12.1"
async-std = {version = "1.12"}
chrono = "0.4.34"
clap = {version = "3.2.25", features = ["cargo"]}
clap = {version = "4.5.3", features = ["cargo"]}
crossbeam-channel = "0.5.12"
crossterm = "0.27.0"
ctrlc = {version = "3.4.2", features = ["termination"]}
difference = "2.0"
futures = "0.3.30"
question = "0.2.2"
ratatui = {version = "0.26.1", default-features = false, features = ['crossterm', 'unstable-rendered-line-info']}
regex = "1.10.3"
serde = "1.0.197"
serde_derive = "1.0.197"
serde_json = "1.0.114"
shell-words = "1.1.0"
termwiz = "0.22.0"

ratatui = {version = "0.26.1", default-features = false, features = ['crossterm', 'unstable-rendered-line-info']}
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
}
Loading

0 comments on commit 5108e96

Please sign in to comment.