Merge pull request #241 from cartercanedy/main

Close linutil on Control-C, don't reset cursor position on exit
This commit is contained in:
Chris Titus 2024-09-16 15:41:30 -05:00 committed by GitHub
commit 6d0a357072
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 9 deletions

View File

@ -64,9 +64,7 @@ impl Float {
// Returns true if the floating window is finished. // Returns true if the floating window is finished.
pub fn handle_key_event(&mut self, key: &KeyEvent) -> bool { pub fn handle_key_event(&mut self, key: &KeyEvent) -> bool {
match key.code { match key.code {
KeyCode::Enter | KeyCode::Char('p') | KeyCode::Esc | KeyCode::Char('q') KeyCode::Enter | KeyCode::Char('p') | KeyCode::Esc if self.content.is_finished() => {
if self.content.is_finished() =>
{
true true
} }
_ => self.content.handle_key_event(key), _ => self.content.handle_key_event(key),

View File

@ -14,7 +14,6 @@ use std::{
use crate::theme::Theme; use crate::theme::Theme;
use clap::Parser; use clap::Parser;
use crossterm::{ use crossterm::{
cursor::RestorePosition,
event::{self, DisableMouseCapture, Event, KeyEventKind}, event::{self, DisableMouseCapture, Event, KeyEventKind},
style::ResetColor, style::ResetColor,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
@ -38,7 +37,7 @@ struct Args {
override_validation: bool, override_validation: bool,
} }
fn main() -> std::io::Result<()> { fn main() -> io::Result<()> {
let args = Args::parse(); let args = Args::parse();
let mut state = AppState::new(args.theme, args.override_validation); let mut state = AppState::new(args.theme, args.override_validation);
@ -55,8 +54,8 @@ fn main() -> std::io::Result<()> {
terminal.backend_mut().execute(LeaveAlternateScreen)?; terminal.backend_mut().execute(LeaveAlternateScreen)?;
terminal.backend_mut().execute(DisableMouseCapture)?; terminal.backend_mut().execute(DisableMouseCapture)?;
terminal.backend_mut().execute(ResetColor)?; terminal.backend_mut().execute(ResetColor)?;
terminal.backend_mut().execute(RestorePosition)?;
terminal.show_cursor()?; terminal.show_cursor()?;
Ok(()) Ok(())
} }

View File

@ -6,7 +6,7 @@ use crate::{
running_command::RunningCommand, running_command::RunningCommand,
theme::Theme, theme::Theme,
}; };
use crossterm::event::{KeyCode, KeyEvent, KeyEventKind}; use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use ego_tree::NodeId; use ego_tree::NodeId;
use linutil_core::{Command, ListNode, Tab}; use linutil_core::{Command, ListNode, Tab};
use ratatui::{ use ratatui::{
@ -185,6 +185,7 @@ impl AppState {
draw_shortcuts(self, frame, vertical[1]); draw_shortcuts(self, frame, vertical[1]);
} }
pub fn handle_key(&mut self, key: &KeyEvent) -> bool { pub fn handle_key(&mut self, key: &KeyEvent) -> bool {
match &mut self.focus { match &mut self.focus {
Focus::FloatingWindow(command) => { Focus::FloatingWindow(command) => {
@ -192,31 +193,43 @@ impl AppState {
self.focus = Focus::List; self.focus = Focus::List;
} }
} }
Focus::Search => match self.filter.handle_key(key) { Focus::Search => match self.filter.handle_key(key) {
SearchAction::Exit => self.exit_search(), SearchAction::Exit => self.exit_search(),
SearchAction::Update => self.update_items(), SearchAction::Update => self.update_items(),
_ => {} _ => {}
}, },
_ if key.code == KeyCode::Char('q') => return false,
_ if key.code == KeyCode::Char('q')
|| key.code == KeyCode::Char('c')
&& key.modifiers.contains(KeyModifiers::CONTROL) =>
{
return false;
}
Focus::TabList => match key.code { Focus::TabList => match key.code {
KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right | KeyCode::Tab => { KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right | KeyCode::Tab => {
self.focus = Focus::List self.focus = Focus::List
} }
KeyCode::Char('j') | KeyCode::Down KeyCode::Char('j') | KeyCode::Down
if self.current_tab.selected().unwrap() + 1 < self.tabs.len() => if self.current_tab.selected().unwrap() + 1 < self.tabs.len() =>
{ {
self.current_tab.select_next(); self.current_tab.select_next();
self.refresh_tab(); self.refresh_tab();
} }
KeyCode::Char('k') | KeyCode::Up => { KeyCode::Char('k') | KeyCode::Up => {
self.current_tab.select_previous(); self.current_tab.select_previous();
self.refresh_tab(); self.refresh_tab();
} }
KeyCode::Char('/') => self.enter_search(), KeyCode::Char('/') => self.enter_search(),
KeyCode::Char('t') => self.theme.next(), KeyCode::Char('t') => self.theme.next(),
KeyCode::Char('T') => self.theme.prev(), KeyCode::Char('T') => self.theme.prev(),
_ => {} _ => {}
}, },
Focus::List if key.kind != KeyEventKind::Release => match key.code { Focus::List if key.kind != KeyEventKind::Release => match key.code {
KeyCode::Char('j') | KeyCode::Down => self.selection.select_next(), KeyCode::Char('j') | KeyCode::Down => self.selection.select_next(),
KeyCode::Char('k') | KeyCode::Up => self.selection.select_previous(), KeyCode::Char('k') | KeyCode::Up => self.selection.select_previous(),
@ -235,10 +248,12 @@ impl AppState {
KeyCode::Char('T') => self.theme.prev(), KeyCode::Char('T') => self.theme.prev(),
_ => {} _ => {}
}, },
_ => {}
_ => (),
}; };
true true
} }
fn update_items(&mut self) { fn update_items(&mut self) {
self.filter.update_items( self.filter.update_items(
&self.tabs, &self.tabs,
@ -246,12 +261,14 @@ impl AppState {
*self.visit_stack.last().unwrap(), *self.visit_stack.last().unwrap(),
); );
} }
/// Checks ehther the current tree node is the root node (can we go up the tree or no) /// Checks ehther the current tree node is the root node (can we go up the tree or no)
/// Returns `true` if we can't go up the tree (we are at the tree root) /// Returns `true` if we can't go up the tree (we are at the tree root)
/// else returns `false` /// else returns `false`
pub fn at_root(&self) -> bool { pub fn at_root(&self) -> bool {
self.visit_stack.len() == 1 self.visit_stack.len() == 1
} }
fn enter_parent_directory(&mut self) { fn enter_parent_directory(&mut self) {
self.visit_stack.pop(); self.visit_stack.pop();
self.selection.select(Some(0)); self.selection.select(Some(0));