Skip to content

Commit

Permalink
update. help message update
Browse files Browse the repository at this point in the history
  • Loading branch information
blacknon committed Apr 7, 2024
1 parent 994787a commit a854948
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 32 deletions.
122 changes: 90 additions & 32 deletions src/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,28 @@
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.

// TODO(blacknon): keyのhelpをテキストからテーブルにする?
// TODO(blacknon): keyの内容をカスタムし、それをhelpで出せるようにする
// TODO(blacknon): keyの内容をvecで渡してやるようにする
// TODO(blacknon): keyの内容を折り返して表示させるようにする

use ratatui::text::Span;
use tui::{
layout::{Constraint, Direction, Layout, Rect},
style::{Color, Style},
prelude::Line,
widgets::{Block, Borders, Clear, Paragraph},
widgets::{Block, Borders, Clear, Paragraph, Wrap},
Frame,
};

use crate::keys::{self, KeyData};

pub struct HelpWindow<'a> {
///
data: Vec<Line<'a>>,
text: Vec<Line<'a>>,

///
area: Rect,

///
position: i16,
Expand All @@ -21,9 +32,13 @@ pub struct HelpWindow<'a> {
/// History Area Object Trait
impl<'a> HelpWindow<'a> {
pub fn new() -> Self {
let data = gen_help_text();
let text = gen_help_text();

Self { data, position: 0 }
Self {
text,
area: Rect::new(0, 0, 0, 0),
position: 0
}
}

///
Expand All @@ -34,14 +49,15 @@ impl<'a> HelpWindow<'a> {
let area = centered_rect(80, 70, size);

// create block.
let block = Paragraph::new(self.data.clone())
let block = Paragraph::new(self.text.clone())
.style(Style::default().fg(Color::LightGreen))
.block(
Block::default()
.title(title)
.borders(Borders::ALL)
.border_style(Style::default().fg(Color::Gray).bg(Color::Reset)),
)
.wrap(Wrap { trim: false })
.scroll((self.position as u16, 0));

f.render_widget(Clear, area);
Expand All @@ -50,56 +66,98 @@ impl<'a> HelpWindow<'a> {

///
pub fn scroll_up(&mut self, num: i16) {
if 0 <= self.position - num {
self.position -= num
}
self.position = std::cmp::max(0, self.position - num);
}

///
pub fn scroll_down(&mut self, num: i16) {
// get area data size
let data_size = self.data.len() as i16;
let data_size = self.text.len() as i16;

if data_size > self.position + num {
self.position += num
}

if self.text.len() as i16 > self.area.height as i16 {
self.position = std::cmp::min(self.position + num, self.text.len() as i16 - self.area.height as i16);
}
}
}

fn gen_help_text_from_key_data<'a>(data: Vec<keys::KeyData>) -> Vec<Line<'a>> {
let mut text = vec![];

for key_data in data {
let line1 = Line::from(vec![
Span::styled(
" - [",
Style::default().fg(Color::LightGreen),
),
Span::styled(
key_data.key,
Style::default().fg(Color::Yellow),
),
Span::styled(
"] key :",
Style::default().fg(Color::LightGreen),
)
]
);

let line2 = Line::from(vec![
Span::styled(
" ",
Style::default(),
),
Span::styled(
key_data.description,
Style::default().fg(Color::White),
),
]
);

text.push(line1);
text.push(line2);
}

text
}

///
fn gen_help_text<'a>() -> Vec<Line<'a>> {
// set help messages.
let text = vec![
Line::from(" - [h] key ... show this help message."),
let keydata_list = vec![
KeyData { key: "h".to_string(), description: "show this help message.".to_string() },
// toggle
Line::from(" - [c] key ... toggle color mode."),
Line::from(" - [n] key ... toggle line number."),
Line::from(" - [d] key ... switch diff mode at None, Watch, Line, and Word mode. "),
Line::from(" - [t] key ... toggle ui (history pane & header both on/off). "),
Line::from(" - [Bkspace] ... toggle history pane. "),
Line::from(" - [m] key ... toggle mouse wheel support. With this option, copying text with your terminal may be harder. Try holding the Shift key."),
KeyData { key: "c".to_string(), description: "toggle color mode.".to_string() },
KeyData { key: "n".to_string(), description: "toggle line number.".to_string() },
KeyData { key: "d".to_string(), description: "switch diff mode at None, Watch, Line, and Word mode.".to_string() },
KeyData { key: "t".to_string(), description: "toggle ui (history pane & header both on/off).".to_string() },
KeyData { key: "Bkspace".to_string(), description: "toggle history pane.".to_string() },
KeyData { key: "m".to_string(), description: "toggle mouse wheel support. With this option, copying text with your terminal may be harder. Try holding the Shift key.".to_string() },
// exit hwatch
Line::from(" - [q] key ... exit hwatch."),
KeyData { key: "q".to_string(), description: "exit hwatch.".to_string() },
// change diff
Line::from(" - [0] key ... disable diff."),
Line::from(" - [1] key ... switch Watch type diff."),
Line::from(" - [2] key ... switch Line type diff."),
Line::from(" - [3] key ... switch Word type diff."),
KeyData { key: "0".to_string(), description: "disable diff.".to_string() },
KeyData { key: "1".to_string(), description: "switch Watch type diff.".to_string() },
KeyData { key: "2".to_string(), description: "switch Line type diff.".to_string() },
KeyData { key: "3".to_string(), description: "switch Word type diff.".to_string() },
// change output
Line::from(" - [F1] key ... change output mode as stdout."),
Line::from(" - [F2] key ... change output mode as stderr."),
Line::from(" - [F3] key ... change output mode as output(stdout/stderr set.)"),
KeyData { key: "F1".to_string(), description: "change output mode as stdout.".to_string() },
KeyData { key: "F2".to_string(), description: "change output mode as stderr.".to_string() },
KeyData { key: "F3".to_string(), description: "change output mode as output(stdout/stderr set.).".to_string() },
// change interval
Line::from(" - [+] key ... Increase interval by .5 seconds."),
Line::from(" - [-] key ... Decrease interval by .5 seconds."),
KeyData { key: "+".to_string(), description: "Increase interval by .5 seconds.".to_string() },
KeyData { key: "-".to_string(), description: "Decrease interval by .5 seconds.".to_string() },
// change use area
Line::from(" - [Tab] key ... toggle current area at history or watch."),
KeyData { key: "Tab".to_string(), description: "toggle current area at history or watch.".to_string() },
// filter text input
Line::from(" - [/] key ... filter history by string."),
Line::from(" - [*] key ... filter history by regex."),
Line::from(" - [ESC] key ... unfiltering."),
KeyData { key: "/".to_string(), description: "filter history by string.".to_string() },
KeyData { key: "*".to_string(), description: "filter history by regex.".to_string() },
KeyData { key: "ESC".to_string(), description: "unfiltering.".to_string() },
];

let text = gen_help_text_from_key_data(keydata_list);

text
}

Expand Down
8 changes: 8 additions & 0 deletions src/keys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// 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.

pub struct KeyData {
pub key: String,
pub description: String,
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ mod exec;
mod header;
mod help;
mod history;
mod keys;
mod output;
mod view;
mod watch;
Expand Down

0 comments on commit a854948

Please sign in to comment.