Skip to content

Commit

Permalink
Merge pull request #104 from blacknon/0.3.11
Browse files Browse the repository at this point in the history
version 0.3.11
  • Loading branch information
blacknon committed Mar 4, 2024
2 parents 1267168 + c75aa34 commit d401e3d
Show file tree
Hide file tree
Showing 14 changed files with 1,069 additions and 468 deletions.
1,185 changes: 865 additions & 320 deletions Cargo.lock

Large diffs are not rendered by default.

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

[dependencies]
# TODO: [ansi-parser](https://crates.io/crates/ansi-parser)のバージョンアップ対応が必要になるので、その対応待ち. PRはmargeされたのでいい加減アップデートされてもいいと思うのだが….
# ansi-parser = {version = "0.9.0", path = "../ansi-parser"}
ansi-parser = "0.8.0"
async-std = {version = "1.11"}
chrono = "0.4.19"
clap = {version = "3.1.18", features = ["cargo"]}
crossbeam-channel = "0.5.4"
crossterm = "0.26.1"
ctrlc = {version = "3.0", features = ["termination"]}
# TODO: ansi-parserが正式にバージョンアップしたらそちらに切り替える
heapless = "0.6.1"
hwatch-ansi-parser = "0.9.0"

async-std = {version = "1.12"}
chrono = "0.4.34"
clap = {version = "3.2.25", features = ["cargo"]}
crossbeam-channel = "0.5.12"
crossterm = "0.27.0"
ctrlc = {version = "3.4.2", features = ["termination"]}
difference = "2.0"
futures = "0.3.21"
futures = "0.3.30"
question = "0.2.2"

# tui = {version = "0.19.0", default-features = false, features = ['crossterm']}
ratatui = {version = "0.20.1", default-features = false, features = ['crossterm']}

regex = "1.3.0"
serde = "1.0.104"
serde_derive = "1.0.104"
serde_json = "1.0.44"
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.15.0"
termwiz = "0.22.0"

# TODO: ansi-parserのバージョンアップグレードの対応が必要になるので、その対応をしたらバージョンアップが必要.
heapless = "0.5.6"
ratatui = {version = "0.26.1", default-features = false, features = ['crossterm', 'unstable-rendered-line-info']}
19 changes: 10 additions & 9 deletions src/ansi.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 Blacknon.
// Copyright (c) 2024 Blacknon.
// This code from https://github.com/blacknon/ansi4tui/blob/master/src/lib.rs

use termwiz::cell::{Blink, Intensity, Underline};
Expand All @@ -9,15 +9,16 @@ use termwiz::escape::{
Action, ControlCode,
};
use tui::style::{Color, Modifier, Style};
use tui::text::{Span, Spans, Text};
use tui::text::{Span,Text};
use tui::prelude::Line;

/// Converts ANSI-escaped strings to tui-rs compatible text
pub fn bytes_to_text<'a, B: AsRef<[u8]>>(bytes: B) -> Text<'a> {
let mut parser = Parser::new();
let parsed = parser.parse_as_vec(bytes.as_ref());

// each span will be a line
let mut spans = Vec::<Spans>::new();
let mut spans = Vec::<Line>::new();

// create span buffer
let mut span_style = Style::default();
Expand All @@ -39,7 +40,7 @@ pub fn bytes_to_text<'a, B: AsRef<[u8]>>(bytes: B) -> Text<'a> {
span_text = String::new();

// finish the current line
spans.push(Spans::from(current_line));
spans.push(Line::from(current_line));
current_line = Vec::new();
}
Action::CSI(CSI::Sgr(sgr)) => {
Expand Down Expand Up @@ -108,19 +109,19 @@ pub fn bytes_to_text<'a, B: AsRef<[u8]>>(bytes: B) -> Text<'a> {
ColorSpec::Default => span_style = span_style.fg(Color::Reset),
ColorSpec::PaletteIndex(i) => span_style = span_style.fg(Color::Indexed(i)),
ColorSpec::TrueColor(rgb) => {
let rgb_tuple = rgb.to_tuple_rgb8();
let rgb_tuple = rgb.to_srgb_u8();
span_style =
span_style.fg(Color::Rgb(rgb_tuple.0, rgb_tuple.1, rgb_tuple.2));
span_style.bg(Color::Rgb(rgb_tuple.0, rgb_tuple.1, rgb_tuple.2));
}
},
Sgr::Background(c) => match c {
ColorSpec::Default => span_style = span_style.bg(Color::Reset),
ColorSpec::PaletteIndex(i) => span_style = span_style.bg(Color::Indexed(i)),
ColorSpec::TrueColor(rgb) => {
let rgb_tuple = rgb.to_tuple_rgb8();
let rgb_tuple = rgb.to_srgb_u8();
span_style =
span_style.bg(Color::Rgb(rgb_tuple.0, rgb_tuple.1, rgb_tuple.2));
}
},
},
_ => {}
}
Expand All @@ -134,7 +135,7 @@ pub fn bytes_to_text<'a, B: AsRef<[u8]>>(bytes: B) -> Text<'a> {
// finish the current span
current_line.push(Span::styled(span_text, span_style));
// finish the current line
spans.push(Spans::from(current_line));
spans.push(Line::from(current_line));
}

spans.into()
Expand Down
64 changes: 52 additions & 12 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
// Copyright (c) 2022 Blacknon. All rights reserved.
// Copyright (c) 2024 Blacknon. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.

// TODO: batch modeを処理するのはmain.rsではなく、こっち側でやらせるのが良さそう?

use crossbeam_channel::{Receiver, Sender};
// module
use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers, MouseEvent, MouseEventKind, KeyEventState};
use crossterm::{
event::{
DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEvent, KeyEventKind,
KeyEventState, KeyModifiers, MouseEvent, MouseEventKind,
},
execute,
};
use regex::Regex;
use std::{collections::HashMap, io};
use std::{
collections::HashMap,
io::{self, Write},
};
use tui::{
backend::Backend,
layout::{Constraint, Direction, Layout},
Expand All @@ -19,14 +28,14 @@ use tui::{
use std::thread;

// local module
use crate::common::logging_result;
use crate::event::AppEvent;
use crate::exec::{exec_after_command, CommandResult};
use crate::header::HeaderArea;
use crate::help::HelpWindow;
use crate::history::{History, HistoryArea};
use crate::output;
use crate::watch::WatchArea;
use crate::common::logging_result;
use crate::Interval;
use crate::DEFAULT_TAB_SIZE;

Expand Down Expand Up @@ -140,6 +149,9 @@ pub struct App<'a> {
///
help_window: HelpWindow<'a>,

/// Enable mouse wheel support.
mouse_events: bool,

/// It is a flag value to confirm the done of the app.
/// If `true`, exit app.
pub done: bool,
Expand All @@ -154,7 +166,12 @@ pub struct App<'a> {
/// Trail at watch view window.
impl<'a> App<'a> {
///
pub fn new(tx: Sender<AppEvent>, rx: Receiver<AppEvent>, interval: Interval) -> Self {
pub fn new(
tx: Sender<AppEvent>,
rx: Receiver<AppEvent>,
interval: Interval,
mouse_events: bool,
) -> Self {
// method at create new view trail.
Self {
area: ActiveArea::History,
Expand Down Expand Up @@ -186,6 +203,8 @@ impl<'a> App<'a> {

help_window: HelpWindow::new(),

mouse_events,

done: false,
logfile: "".to_string(),
tx,
Expand All @@ -194,7 +213,7 @@ impl<'a> App<'a> {
}

///
pub fn run<B: Backend>(&mut self, terminal: &mut Terminal<B>) -> io::Result<()> {
pub fn run<B: Backend + Write>(&mut self, terminal: &mut Terminal<B>) -> io::Result<()> {
self.history_area.next();
let mut update_draw = true;
loop {
Expand Down Expand Up @@ -230,6 +249,16 @@ impl<'a> App<'a> {
update_draw = true;
}

Ok(AppEvent::ToggleMouseEvents) => {
if self.mouse_events {
execute!(terminal.backend_mut(), DisableMouseCapture)?;
} else {
execute!(terminal.backend_mut(), EnableMouseCapture)?;
}

self.mouse_events = !self.mouse_events;
}

// get exit event
Ok(AppEvent::Exit) => self.done = true,

Expand All @@ -239,7 +268,7 @@ impl<'a> App<'a> {
}

///
pub fn draw<B: Backend>(&mut self, f: &mut Frame<B>) {
pub fn draw(&mut self, f: &mut Frame) {
self.define_subareas(f.size());

if self.show_header {
Expand Down Expand Up @@ -378,15 +407,15 @@ impl<'a> App<'a> {
self.is_filtered,
self.is_regex_filter,
&self.filtered_text,
self.tab_size
self.tab_size,
),

DiffMode::Watch => output::get_watch_diff(
self.ansi_color,
self.line_number,
text_src,
text_dst,
self.tab_size
self.tab_size,
),

DiffMode::Line => output::get_line_diff(
Expand All @@ -395,7 +424,7 @@ impl<'a> App<'a> {
self.is_only_diffline,
text_src,
text_dst,
self.tab_size
self.tab_size,
),

DiffMode::Word => output::get_word_diff(
Expand All @@ -404,7 +433,7 @@ impl<'a> App<'a> {
self.is_only_diffline,
text_src,
text_dst,
self.tab_size
self.tab_size,
),
};

Expand Down Expand Up @@ -599,7 +628,7 @@ impl<'a> App<'a> {
return false;
}

if self.after_command != "".to_string() {
if !self.after_command.is_empty() {
let after_command = self.after_command.clone();

let results = self.results.clone();
Expand Down Expand Up @@ -919,6 +948,13 @@ impl<'a> App<'a> {
state: KeyEventState::NONE,
}) => self.toggle_window(),

Event::Key(KeyEvent {
code: KeyCode::Char('m'),
modifiers: KeyModifiers::NONE,
kind: KeyEventKind::Press,
state: KeyEventState::NONE,
}) => self.toggle_mouse_events(),

// q ... exit hwatch.
Event::Key(KeyEvent {
code: KeyCode::Char('q'),
Expand Down Expand Up @@ -1108,6 +1144,10 @@ impl<'a> App<'a> {
let _ = self.tx.send(AppEvent::Redraw);
}

pub fn toggle_mouse_events(&mut self) {
let _ = self.tx.send(AppEvent::ToggleMouseEvents);
}

///
pub fn show_ui(&mut self, visible: bool) {
self.show_header = visible;
Expand Down
2 changes: 1 addition & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 Blacknon. All rights reserved.
// Copyright (c) 2024 Blacknon. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.

Expand Down
3 changes: 2 additions & 1 deletion src/event.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 Blacknon. All rights reserved.
// Copyright (c) 2024 Blacknon. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.

Expand All @@ -8,5 +8,6 @@ pub enum AppEvent {
OutputUpdate(CommandResult),
TerminalEvent(crossterm::event::Event),
Redraw,
ToggleMouseEvents,
Exit,
}
2 changes: 1 addition & 1 deletion src/exec.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 Blacknon. All rights reserved.
// Copyright (c) 2024 Blacknon. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.

Expand Down
17 changes: 9 additions & 8 deletions src/header.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 Blacknon. All rights reserved.
// Copyright (c) 2024 Blacknon. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.

Expand All @@ -7,11 +7,12 @@
// TODO: 幅調整系の数字をconstにする(生数字で雑計算だとわけわからん)

use tui::{
backend::Backend,
style::{Color, Modifier, Style},
text::{Span, Spans},
text::Span,

widgets::Paragraph,
Frame,
prelude::Line,
};

// local module
Expand Down Expand Up @@ -40,7 +41,7 @@ pub struct HeaderArea<'a> {
exec_status: bool,

///
data: Vec<Spans<'a>>,
data: Vec<Line<'a>>,

///
line_number: bool,
Expand Down Expand Up @@ -85,7 +86,7 @@ impl<'a> HeaderArea<'a> {
timestamp: "".to_string(),
exec_status: true,

data: vec![Spans::from("")],
data: vec![Line::from("")],
ansi_color: false,
line_number: false,
banner: "".to_string(),
Expand Down Expand Up @@ -249,7 +250,7 @@ impl<'a> HeaderArea<'a> {
};

// Create 1st line.
self.data.push(Spans::from(vec![
self.data.push(Line::from(vec![
Span::raw("Every "),
Span::styled(
format!("{:>wid$}", interval, wid = 9),
Expand All @@ -271,7 +272,7 @@ impl<'a> HeaderArea<'a> {
]));

// Create 2nd line
self.data.push(Spans::from(vec![
self.data.push(Line::from(vec![
// filter keyword
Span::styled(self.input_prompt.clone(), Style::default().fg(Color::Gray)),
Span::styled(filter_keyword, filter_keyword_style),
Expand Down Expand Up @@ -320,7 +321,7 @@ impl<'a> HeaderArea<'a> {
]));
}

pub fn draw<B: Backend>(&mut self, frame: &mut Frame<B>) {
pub fn draw(&mut self, frame: &mut Frame) {
let block = Paragraph::new(self.data.clone());
frame.render_widget(block, self.area);
}
Expand Down
Loading

0 comments on commit d401e3d

Please sign in to comment.