Add default shortcuts + add flags for super key

This commit is contained in:
Shav Kinderlehrer 2024-03-07 09:34:25 -05:00
parent fa3e812a30
commit 832eb17a68
6 changed files with 56 additions and 44 deletions

View File

@ -22,22 +22,43 @@ impl App {
pub fn new(tick_rate: Duration) -> Result<Self> { pub fn new(tick_rate: Duration) -> Result<Self> {
let tui = tui::init()?; let tui = tui::init()?;
let key_commands = vec![KeyCommand { let key_commands = vec![
key_code: "q".to_string(), KeyCommand {
description: "Quit molehole".to_string(), key_code: "q".to_string(),
action: Some(AppAction::Quit), description: "Quit molehole".to_string(),
}]; action: Some(AppAction::Quit),
},
KeyCommand {
key_code: "g".to_string(),
description: "Scroll to top".to_string(),
action: None,
},
KeyCommand {
key_code: "G".to_string(),
description: "Scroll to bottom".to_string(),
action: None,
},
KeyCommand {
key_code: "k".to_string(),
description: "Scroll up one line".to_string(),
action: None,
},
KeyCommand {
key_code: "j".to_string(),
description: "Scroll down one line".to_string(),
action: None,
},
];
let global_keys = components::global_keys::GlobalKeys { let global_keys = components::global_keys::GlobalKeys {
key_commands: key_commands.clone(), key_commands: key_commands.clone(),
..Default::default() ..Default::default()
}; };
let hello_world = components::hello_world::HelloWorld::default();
Ok(Self { Ok(Self {
tui, tui,
tick_rate, tick_rate,
components: vec![Box::new(hello_world), Box::new(global_keys)], components: vec![Box::new(global_keys)],
key_commands, key_commands,
should_quit: false, should_quit: false,

View File

@ -24,11 +24,11 @@ pub struct GlobalKeys {
impl Component for GlobalKeys { impl Component for GlobalKeys {
fn init(&mut self) -> eyre::Result<()> { fn init(&mut self) -> eyre::Result<()> {
self.key_commands.push(KeyCommand { self.key_commands.append(&mut vec![KeyCommand {
key_code: "?".to_string(), key_code: "?".to_string(),
description: "Toggle help menu".to_string(), description: "Toggle help menu".to_string(),
action: None, action: None,
}); }]);
self.scroll_state = self.scroll_state =
ScrollbarState::new(self.key_commands.len()).position(self.scroll); ScrollbarState::new(self.key_commands.len()).position(self.scroll);
@ -45,26 +45,32 @@ impl Component for GlobalKeys {
let eat_input = match key_event.as_str() { let eat_input = match key_event.as_str() {
"?" => { "?" => {
self.should_show = !self.should_show; self.should_show = !self.should_show;
self.scroll = 0;
true true
} }
"down" => { "g" => {
self.scroll = 0;
true
}
"G" => {
self.scroll = self.key_commands.len() - 1;
true
}
"down" | "j" => {
if self.scroll < self.key_commands.len() - 1 { if self.scroll < self.key_commands.len() - 1 {
self.scroll += 1; self.scroll += 1;
self.scroll_state =
self.scroll_state.position(self.scroll);
} }
true true
} }
"up" => { "up" | "k" => {
if self.scroll > 0 { if self.scroll > 0 {
self.scroll -= 1; self.scroll -= 1;
self.scroll_state =
self.scroll_state.position(self.scroll);
} }
true true
} }
_ => false, _ => false,
}; };
self.scroll_state = self.scroll_state.position(self.scroll);
if eat_input && self.should_show { if eat_input && self.should_show {
return Ok(None); return Ok(None);
} }
@ -119,7 +125,7 @@ impl Component for GlobalKeys {
.block(block) .block(block)
.wrap(Wrap { trim: true }) .wrap(Wrap { trim: true })
.scroll((u16::try_from(self.scroll)?, 0)) .scroll((u16::try_from(self.scroll)?, 0))
.style(Style::default().bg(Color::DarkGray).fg(Color::LightYellow)); .style(Style::default().bg(Color::DarkGray).fg(Color::White));
if self.should_show { if self.should_show {
frame.render_widget(Clear, center); frame.render_widget(Clear, center);

View File

@ -1,25 +0,0 @@
use ratatui::prelude::{Frame, Rect};
use ratatui::widgets::{Paragraph, Wrap};
use crate::component::Component;
#[derive(Default, Clone)]
pub struct HelloWorld {
pub text: String,
}
impl Component for HelloWorld {
fn init(&mut self) -> eyre::Result<()> {
self.text = "Hello, world!".to_string();
Ok(())
}
fn render(&mut self, frame: &mut Frame, rect: Rect) -> eyre::Result<()> {
frame.render_widget(
Paragraph::new(self.text.clone()).wrap(Wrap { trim: true }),
rect,
);
Ok(())
}
}

View File

@ -1,2 +1 @@
pub mod hello_world;
pub mod global_keys; pub mod global_keys;

View File

@ -47,7 +47,7 @@ pub fn serialize_key_event(event: KeyEvent) -> String {
KeyCode::Esc => "esc", KeyCode::Esc => "esc",
_ => "", _ => "",
}; };
let separator = if modifiers.is_empty() { "-" } else { "" }; let separator = if modifiers.is_empty() { "" } else { "-" };
let serialized_event = let serialized_event =
format!("{}{}{}", modifiers.join("-"), separator, key); format!("{}{}{}", modifiers.join("-"), separator, key);

View File

@ -1,8 +1,12 @@
use crossterm::event::{
Event, KeyboardEnhancementFlags, PopKeyboardEnhancementFlags,
PushKeyboardEnhancementFlags,
};
use crossterm::terminal::{ use crossterm::terminal::{
disable_raw_mode, enable_raw_mode, EnterAlternateScreen, disable_raw_mode, enable_raw_mode, EnterAlternateScreen,
LeaveAlternateScreen, LeaveAlternateScreen,
}; };
use crossterm::{event, event::Event, execute}; use crossterm::{event, execute};
use ratatui::prelude::{CrosstermBackend, Terminal}; use ratatui::prelude::{CrosstermBackend, Terminal};
use std::io; use std::io;
use std::io::{stdout, Stdout}; use std::io::{stdout, Stdout};
@ -11,6 +15,12 @@ pub type Tui = Terminal<CrosstermBackend<Stdout>>;
pub fn init() -> io::Result<Tui> { pub fn init() -> io::Result<Tui> {
execute!(stdout(), EnterAlternateScreen)?; execute!(stdout(), EnterAlternateScreen)?;
execute!(
stdout(),
PushKeyboardEnhancementFlags(
KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES,
)
)?;
enable_raw_mode()?; enable_raw_mode()?;
Terminal::new(CrosstermBackend::new(stdout())) Terminal::new(CrosstermBackend::new(stdout()))
@ -18,6 +28,7 @@ pub fn init() -> io::Result<Tui> {
pub fn restore() -> io::Result<()> { pub fn restore() -> io::Result<()> {
execute!(stdout(), LeaveAlternateScreen)?; execute!(stdout(), LeaveAlternateScreen)?;
execute!(stdout(), PopKeyboardEnhancementFlags)?;
disable_raw_mode()?; disable_raw_mode()?;
Ok(()) Ok(())