help scrolling

pull/32/head
Takayuki Maeda 3 years ago
parent 58a0485742
commit 961fde8c48

13
Cargo.lock generated

@ -534,6 +534,7 @@ dependencies = [
"futures",
"itertools",
"regex",
"rust_decimal",
"serde",
"serde_json",
"sqlx",
@ -1181,6 +1182,17 @@ dependencies = [
"zeroize",
]
[[package]]
name = "rust_decimal"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c5446d1cf2dfe2d6367c8b27f2082bdf011e60e76fa1fcd140047f535156d6e7"
dependencies = [
"arrayvec",
"num-traits",
"serde",
]
[[package]]
name = "rustls"
version = "0.18.1"
@ -1399,6 +1411,7 @@ dependencies = [
"percent-encoding",
"rand",
"rsa",
"rust_decimal",
"rustls",
"sha-1",
"sha2",

@ -19,7 +19,7 @@ tui = { version = "0.14.0", features = ["crossterm"], default-features = false }
crossterm = "0.19"
anyhow = "1.0.38"
unicode-width = "0.1"
sqlx = { version = "0.4.1", features = ["mysql", "chrono", "runtime-tokio-rustls"] }
sqlx = { version = "0.4.1", features = ["mysql", "chrono", "runtime-tokio-rustls", "decimal"] }
chrono = "0.4"
tokio = { version = "0.2.22", features = ["full"] }
futures = "0.3.5"
@ -34,7 +34,7 @@ easy-cast = "0.4"
copypasta = { version = "0.7.0", default-features = false }
async-trait = "0.1.50"
itertools = "0.10.0"
rust_decimal = "1.15"
[target.'cfg(any(target_os = "macos", windows))'.dependencies]
copypasta = { version = "0.7.0", default-features = false }

@ -125,6 +125,13 @@ impl App {
CommandInfo::new(crate::components::command::move_up("k"), true, true),
CommandInfo::new(crate::components::command::move_right("l"), true, true),
CommandInfo::new(crate::components::command::filter("/"), true, true),
CommandInfo::new(
crate::components::command::move_focus_to_right_widget(
Key::Right.to_string().as_str(),
),
true,
true,
),
];
res
@ -151,6 +158,10 @@ impl App {
}
pub async fn components_event(&mut self, key: Key) -> anyhow::Result<EventState> {
if self.help.event(key)?.is_consumed() {
return Ok(EventState::Consumed);
}
match self.focus {
Focus::ConnectionList => {
if self.connections.event(key)?.is_consumed() {
@ -303,9 +314,6 @@ impl App {
}
pub fn move_focus(&mut self, key: Key) -> anyhow::Result<EventState> {
if self.help.event(key)?.is_consumed() {
return Ok(EventState::Consumed);
}
if let Key::Char('c') = key {
self.focus = Focus::ConnectionList;
return Ok(EventState::Consumed);

@ -80,3 +80,11 @@ pub fn filter(key: &str) -> CommandText {
CMD_GROUP_GENERAL,
)
}
pub fn move_focus_to_right_widget(key: &str) -> CommandText {
CommandText::new(
format!("Move focus to right [{}]", key),
"move focus to right",
CMD_GROUP_GENERAL,
)
}

@ -7,7 +7,7 @@ use std::convert::From;
use tui::{
backend::Backend,
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::{Modifier, Style},
style::{Color, Modifier, Style},
text::{Span, Spans},
widgets::{Block, BorderType, Borders, Clear, Paragraph},
Frame,
@ -23,6 +23,8 @@ impl DrawableComponent for HelpComponent {
fn draw<B: Backend>(&mut self, f: &mut Frame<B>, _area: Rect, _focused: bool) -> Result<()> {
if self.visible {
const SIZE: (u16, u16) = (65, 24);
let scroll_threshold = SIZE.1 / 3;
let scroll = self.selection.saturating_sub(scroll_threshold);
let area = Rect::new(
(f.size().width.saturating_sub(SIZE.0)) / 2,
@ -31,8 +33,6 @@ impl DrawableComponent for HelpComponent {
SIZE.1.min(f.size().height),
);
let scroll = 0;
f.render_widget(Clear, area);
f.render_widget(
Block::default()
@ -50,7 +50,7 @@ impl DrawableComponent for HelpComponent {
.split(area);
f.render_widget(
Paragraph::new(self.get_text()).scroll((scroll, 0)),
Paragraph::new(self.get_text(chunks[0])).scroll((scroll, 0)),
chunks[0],
);
@ -73,9 +73,20 @@ impl Component for HelpComponent {
fn event(&mut self, key: Key) -> Result<EventState> {
if self.visible {
if let Key::Esc = key {
self.hide();
return Ok(EventState::Consumed);
match key {
Key::Esc => {
self.hide();
return Ok(EventState::Consumed);
}
Key::Char('j') => {
self.move_selection(true);
return Ok(EventState::Consumed);
}
Key::Char('k') => {
self.move_selection(false);
return Ok(EventState::Consumed);
}
_ => (),
}
return Ok(EventState::NotConsumed);
} else if let Key::Char('?') = key {
@ -112,9 +123,24 @@ impl HelpComponent {
.collect::<Vec<_>>();
}
fn get_text(&self) -> Vec<Spans> {
fn move_selection(&mut self, inc: bool) {
let mut new_selection = self.selection;
new_selection = if inc {
new_selection.saturating_add(1)
} else {
new_selection.saturating_sub(1)
};
new_selection = new_selection.max(0);
self.selection = new_selection.min(self.cmds.len().saturating_sub(1) as u16);
}
fn get_text(&self, area: Rect) -> Vec<Spans> {
let mut txt: Vec<Spans> = Vec::new();
let mut processed = 0;
for (key, group) in &self.cmds.iter().group_by(|e| e.text.group) {
txt.push(Spans::from(Span::styled(
key.to_string(),
@ -122,9 +148,16 @@ impl HelpComponent {
)));
for command_info in group {
let is_selected = self.selection == processed;
processed += 1;
txt.push(Spans::from(Span::styled(
command_info.text.name.to_string(),
Style::default(),
format!("{}{:w$},", command_info.text.name, w = area.width as usize),
if is_selected {
Style::default().bg(Color::Blue)
} else {
Style::default()
},
)));
}
}

Loading…
Cancel
Save