linutil/tui/src/main.rs

109 lines
2.8 KiB
Rust
Raw Normal View History

mod confirmation;
mod filter;
2024-06-06 23:56:45 +01:00
mod float;
mod floating_text;
2024-09-06 22:36:12 +01:00
mod hint;
mod root;
2024-06-06 23:56:45 +01:00
mod running_command;
pub mod state;
2024-06-06 23:56:45 +01:00
mod theme;
refact: rust fixes and optimizations (#933) * fix: getting locked out when running script * Use success and fail colors and reorder imports Use theme color instead of using ratatui::Color for running_command success and fail + search preview text color + min tui warning color, add colors for confirmation prompt, fix inverted success and fail colors * Remove redundant code in themes Removed redundant match statement with a function * Fix scroll beyond list, color bleeding and refact in confirmation.rs Remove unnecessary usage of pub in ConfirmPropmt struct fields, simplify numbering, prevent scrolling beyond list, fix color bleeding * Implement case insensitive, fix word disappearing bug Use regex for case insesitive finding, implement String instead of char<Vec>, fix word disappearing by recalculating the render x for preview text * Revert "Remove redundant code in themes" This reverts commit 3b7e859af80bfa1f453928a74c47efd897e26934. * Reference instead of passing the vector * Revert regex and String implementation Use Vec<char> for search_input to prevent panics when using multi-byte characters, use lowercase conversion instead of regex, Added comments for clarity * Replace ansi and text wrapping code with ratatui Replaced ansi related code for tree sitter highlight with direct ratatui::text. Cache the processed text in appstate to remove processing of text for every frame render.Create paragraph instead of list so that scroll and wrapping can be done without external crates. Add caps keys for handle_key_event. * Fix conflicts * Reference instead of borrowing commands, refact mut variables Reference instead of borrowing commands from state, Refactor draw function variables to immutable, calculate innersize from block instead of manual definition * Update tui/src/filter.rs Co-authored-by: Liam <33645555+lj3954@users.noreply.github.com> * Rendering optimizations and function refactors Handle `find_command` inside state itself -> `get_command_by_name`. Move tips to a seperate file for modularity. Pass the whole args to state instead of seperate args. Use const for float and confirmation prompt float sizes. Add the `longest_tab_length` to appstate struct so that it will not be calculated for each frame render use static str instead String for tips. Use function for spawning confirmprompt. Merge command list and task items list rendering a single widget instead of two. Remove redundant keys in handle_key. Optimize scrolling logic. Rename `toggle_task_list_guide` -> `enable_task_list_guide` * Cleanup Use prelude for ratatui imports. Use const for theme functions, add missing hints * Update deps, remove unused temp-dir * Add accidentally deleted preview.tape Add labels + Wait 2sec after program ends * Add fields to config files Skip Confirmation, Bypass Size * Remove accidentally commited config file --------- Co-authored-by: Liam <33645555+lj3954@users.noreply.github.com>
2024-11-17 18:24:54 +00:00
#[cfg(feature = "tips")]
mod tips;
2024-06-06 23:56:45 +01:00
2024-08-15 23:13:47 +01:00
use crate::theme::Theme;
2024-06-06 23:56:45 +01:00
use clap::Parser;
2024-11-06 22:29:52 +00:00
use ratatui::{
backend::CrosstermBackend,
crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyEventKind},
2024-11-06 22:29:52 +00:00
style::ResetColor,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
},
Terminal,
2024-06-06 23:56:45 +01:00
};
use state::AppState;
refact: rust fixes and optimizations (#933) * fix: getting locked out when running script * Use success and fail colors and reorder imports Use theme color instead of using ratatui::Color for running_command success and fail + search preview text color + min tui warning color, add colors for confirmation prompt, fix inverted success and fail colors * Remove redundant code in themes Removed redundant match statement with a function * Fix scroll beyond list, color bleeding and refact in confirmation.rs Remove unnecessary usage of pub in ConfirmPropmt struct fields, simplify numbering, prevent scrolling beyond list, fix color bleeding * Implement case insensitive, fix word disappearing bug Use regex for case insesitive finding, implement String instead of char<Vec>, fix word disappearing by recalculating the render x for preview text * Revert "Remove redundant code in themes" This reverts commit 3b7e859af80bfa1f453928a74c47efd897e26934. * Reference instead of passing the vector * Revert regex and String implementation Use Vec<char> for search_input to prevent panics when using multi-byte characters, use lowercase conversion instead of regex, Added comments for clarity * Replace ansi and text wrapping code with ratatui Replaced ansi related code for tree sitter highlight with direct ratatui::text. Cache the processed text in appstate to remove processing of text for every frame render.Create paragraph instead of list so that scroll and wrapping can be done without external crates. Add caps keys for handle_key_event. * Fix conflicts * Reference instead of borrowing commands, refact mut variables Reference instead of borrowing commands from state, Refactor draw function variables to immutable, calculate innersize from block instead of manual definition * Update tui/src/filter.rs Co-authored-by: Liam <33645555+lj3954@users.noreply.github.com> * Rendering optimizations and function refactors Handle `find_command` inside state itself -> `get_command_by_name`. Move tips to a seperate file for modularity. Pass the whole args to state instead of seperate args. Use const for float and confirmation prompt float sizes. Add the `longest_tab_length` to appstate struct so that it will not be calculated for each frame render use static str instead String for tips. Use function for spawning confirmprompt. Merge command list and task items list rendering a single widget instead of two. Remove redundant keys in handle_key. Optimize scrolling logic. Rename `toggle_task_list_guide` -> `enable_task_list_guide` * Cleanup Use prelude for ratatui imports. Use const for theme functions, add missing hints * Update deps, remove unused temp-dir * Add accidentally deleted preview.tape Add labels + Wait 2sec after program ends * Add fields to config files Skip Confirmation, Bypass Size * Remove accidentally commited config file --------- Co-authored-by: Liam <33645555+lj3954@users.noreply.github.com>
2024-11-17 18:24:54 +00:00
use std::{
io::{stdout, Result, Stdout},
path::PathBuf,
time::Duration,
};
2024-06-06 23:56:45 +01:00
2024-08-15 23:13:47 +01:00
// Linux utility toolbox
2024-06-06 23:56:45 +01:00
#[derive(Debug, Parser)]
refact: rust fixes and optimizations (#933) * fix: getting locked out when running script * Use success and fail colors and reorder imports Use theme color instead of using ratatui::Color for running_command success and fail + search preview text color + min tui warning color, add colors for confirmation prompt, fix inverted success and fail colors * Remove redundant code in themes Removed redundant match statement with a function * Fix scroll beyond list, color bleeding and refact in confirmation.rs Remove unnecessary usage of pub in ConfirmPropmt struct fields, simplify numbering, prevent scrolling beyond list, fix color bleeding * Implement case insensitive, fix word disappearing bug Use regex for case insesitive finding, implement String instead of char<Vec>, fix word disappearing by recalculating the render x for preview text * Revert "Remove redundant code in themes" This reverts commit 3b7e859af80bfa1f453928a74c47efd897e26934. * Reference instead of passing the vector * Revert regex and String implementation Use Vec<char> for search_input to prevent panics when using multi-byte characters, use lowercase conversion instead of regex, Added comments for clarity * Replace ansi and text wrapping code with ratatui Replaced ansi related code for tree sitter highlight with direct ratatui::text. Cache the processed text in appstate to remove processing of text for every frame render.Create paragraph instead of list so that scroll and wrapping can be done without external crates. Add caps keys for handle_key_event. * Fix conflicts * Reference instead of borrowing commands, refact mut variables Reference instead of borrowing commands from state, Refactor draw function variables to immutable, calculate innersize from block instead of manual definition * Update tui/src/filter.rs Co-authored-by: Liam <33645555+lj3954@users.noreply.github.com> * Rendering optimizations and function refactors Handle `find_command` inside state itself -> `get_command_by_name`. Move tips to a seperate file for modularity. Pass the whole args to state instead of seperate args. Use const for float and confirmation prompt float sizes. Add the `longest_tab_length` to appstate struct so that it will not be calculated for each frame render use static str instead String for tips. Use function for spawning confirmprompt. Merge command list and task items list rendering a single widget instead of two. Remove redundant keys in handle_key. Optimize scrolling logic. Rename `toggle_task_list_guide` -> `enable_task_list_guide` * Cleanup Use prelude for ratatui imports. Use const for theme functions, add missing hints * Update deps, remove unused temp-dir * Add accidentally deleted preview.tape Add labels + Wait 2sec after program ends * Add fields to config files Skip Confirmation, Bypass Size * Remove accidentally commited config file --------- Co-authored-by: Liam <33645555+lj3954@users.noreply.github.com>
2024-11-17 18:24:54 +00:00
pub struct Args {
#[arg(short, long, help = "Path to the configuration file")]
config: Option<PathBuf>,
2024-08-15 23:13:47 +01:00
#[arg(short, long, value_enum)]
#[arg(default_value_t = Theme::Default)]
#[arg(help = "Set the theme to use in the application")]
theme: Theme,
#[arg(
short = 'y',
long,
help = "Skip confirmation prompt before executing commands"
)]
skip_confirmation: bool,
2024-08-17 19:27:46 +01:00
#[arg(long, default_value_t = false)]
#[clap(help = "Show all available options, disregarding compatibility checks (UNSAFE)")]
override_validation: bool,
#[arg(long, default_value_t = false)]
#[clap(help = "Bypass the terminal size limit")]
size_bypass: bool,
2024-06-06 23:56:45 +01:00
}
refact: rust fixes and optimizations (#933) * fix: getting locked out when running script * Use success and fail colors and reorder imports Use theme color instead of using ratatui::Color for running_command success and fail + search preview text color + min tui warning color, add colors for confirmation prompt, fix inverted success and fail colors * Remove redundant code in themes Removed redundant match statement with a function * Fix scroll beyond list, color bleeding and refact in confirmation.rs Remove unnecessary usage of pub in ConfirmPropmt struct fields, simplify numbering, prevent scrolling beyond list, fix color bleeding * Implement case insensitive, fix word disappearing bug Use regex for case insesitive finding, implement String instead of char<Vec>, fix word disappearing by recalculating the render x for preview text * Revert "Remove redundant code in themes" This reverts commit 3b7e859af80bfa1f453928a74c47efd897e26934. * Reference instead of passing the vector * Revert regex and String implementation Use Vec<char> for search_input to prevent panics when using multi-byte characters, use lowercase conversion instead of regex, Added comments for clarity * Replace ansi and text wrapping code with ratatui Replaced ansi related code for tree sitter highlight with direct ratatui::text. Cache the processed text in appstate to remove processing of text for every frame render.Create paragraph instead of list so that scroll and wrapping can be done without external crates. Add caps keys for handle_key_event. * Fix conflicts * Reference instead of borrowing commands, refact mut variables Reference instead of borrowing commands from state, Refactor draw function variables to immutable, calculate innersize from block instead of manual definition * Update tui/src/filter.rs Co-authored-by: Liam <33645555+lj3954@users.noreply.github.com> * Rendering optimizations and function refactors Handle `find_command` inside state itself -> `get_command_by_name`. Move tips to a seperate file for modularity. Pass the whole args to state instead of seperate args. Use const for float and confirmation prompt float sizes. Add the `longest_tab_length` to appstate struct so that it will not be calculated for each frame render use static str instead String for tips. Use function for spawning confirmprompt. Merge command list and task items list rendering a single widget instead of two. Remove redundant keys in handle_key. Optimize scrolling logic. Rename `toggle_task_list_guide` -> `enable_task_list_guide` * Cleanup Use prelude for ratatui imports. Use const for theme functions, add missing hints * Update deps, remove unused temp-dir * Add accidentally deleted preview.tape Add labels + Wait 2sec after program ends * Add fields to config files Skip Confirmation, Bypass Size * Remove accidentally commited config file --------- Co-authored-by: Liam <33645555+lj3954@users.noreply.github.com>
2024-11-17 18:24:54 +00:00
fn main() -> Result<()> {
2024-06-06 23:56:45 +01:00
let args = Args::parse();
refact: rust fixes and optimizations (#933) * fix: getting locked out when running script * Use success and fail colors and reorder imports Use theme color instead of using ratatui::Color for running_command success and fail + search preview text color + min tui warning color, add colors for confirmation prompt, fix inverted success and fail colors * Remove redundant code in themes Removed redundant match statement with a function * Fix scroll beyond list, color bleeding and refact in confirmation.rs Remove unnecessary usage of pub in ConfirmPropmt struct fields, simplify numbering, prevent scrolling beyond list, fix color bleeding * Implement case insensitive, fix word disappearing bug Use regex for case insesitive finding, implement String instead of char<Vec>, fix word disappearing by recalculating the render x for preview text * Revert "Remove redundant code in themes" This reverts commit 3b7e859af80bfa1f453928a74c47efd897e26934. * Reference instead of passing the vector * Revert regex and String implementation Use Vec<char> for search_input to prevent panics when using multi-byte characters, use lowercase conversion instead of regex, Added comments for clarity * Replace ansi and text wrapping code with ratatui Replaced ansi related code for tree sitter highlight with direct ratatui::text. Cache the processed text in appstate to remove processing of text for every frame render.Create paragraph instead of list so that scroll and wrapping can be done without external crates. Add caps keys for handle_key_event. * Fix conflicts * Reference instead of borrowing commands, refact mut variables Reference instead of borrowing commands from state, Refactor draw function variables to immutable, calculate innersize from block instead of manual definition * Update tui/src/filter.rs Co-authored-by: Liam <33645555+lj3954@users.noreply.github.com> * Rendering optimizations and function refactors Handle `find_command` inside state itself -> `get_command_by_name`. Move tips to a seperate file for modularity. Pass the whole args to state instead of seperate args. Use const for float and confirmation prompt float sizes. Add the `longest_tab_length` to appstate struct so that it will not be calculated for each frame render use static str instead String for tips. Use function for spawning confirmprompt. Merge command list and task items list rendering a single widget instead of two. Remove redundant keys in handle_key. Optimize scrolling logic. Rename `toggle_task_list_guide` -> `enable_task_list_guide` * Cleanup Use prelude for ratatui imports. Use const for theme functions, add missing hints * Update deps, remove unused temp-dir * Add accidentally deleted preview.tape Add labels + Wait 2sec after program ends * Add fields to config files Skip Confirmation, Bypass Size * Remove accidentally commited config file --------- Co-authored-by: Liam <33645555+lj3954@users.noreply.github.com>
2024-11-17 18:24:54 +00:00
let mut state = AppState::new(args);
2024-06-06 23:56:45 +01:00
stdout().execute(EnterAlternateScreen)?;
stdout().execute(EnableMouseCapture)?;
2024-06-06 23:56:45 +01:00
enable_raw_mode()?;
let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?;
terminal.clear()?;
2024-08-09 10:22:19 +01:00
run(&mut terminal, &mut state)?;
2024-06-06 23:56:45 +01:00
// restore terminal
disable_raw_mode()?;
terminal.backend_mut().execute(LeaveAlternateScreen)?;
terminal.backend_mut().execute(DisableMouseCapture)?;
terminal.backend_mut().execute(ResetColor)?;
terminal.show_cursor()?;
2024-06-06 23:56:45 +01:00
Ok(())
}
refact: rust fixes and optimizations (#933) * fix: getting locked out when running script * Use success and fail colors and reorder imports Use theme color instead of using ratatui::Color for running_command success and fail + search preview text color + min tui warning color, add colors for confirmation prompt, fix inverted success and fail colors * Remove redundant code in themes Removed redundant match statement with a function * Fix scroll beyond list, color bleeding and refact in confirmation.rs Remove unnecessary usage of pub in ConfirmPropmt struct fields, simplify numbering, prevent scrolling beyond list, fix color bleeding * Implement case insensitive, fix word disappearing bug Use regex for case insesitive finding, implement String instead of char<Vec>, fix word disappearing by recalculating the render x for preview text * Revert "Remove redundant code in themes" This reverts commit 3b7e859af80bfa1f453928a74c47efd897e26934. * Reference instead of passing the vector * Revert regex and String implementation Use Vec<char> for search_input to prevent panics when using multi-byte characters, use lowercase conversion instead of regex, Added comments for clarity * Replace ansi and text wrapping code with ratatui Replaced ansi related code for tree sitter highlight with direct ratatui::text. Cache the processed text in appstate to remove processing of text for every frame render.Create paragraph instead of list so that scroll and wrapping can be done without external crates. Add caps keys for handle_key_event. * Fix conflicts * Reference instead of borrowing commands, refact mut variables Reference instead of borrowing commands from state, Refactor draw function variables to immutable, calculate innersize from block instead of manual definition * Update tui/src/filter.rs Co-authored-by: Liam <33645555+lj3954@users.noreply.github.com> * Rendering optimizations and function refactors Handle `find_command` inside state itself -> `get_command_by_name`. Move tips to a seperate file for modularity. Pass the whole args to state instead of seperate args. Use const for float and confirmation prompt float sizes. Add the `longest_tab_length` to appstate struct so that it will not be calculated for each frame render use static str instead String for tips. Use function for spawning confirmprompt. Merge command list and task items list rendering a single widget instead of two. Remove redundant keys in handle_key. Optimize scrolling logic. Rename `toggle_task_list_guide` -> `enable_task_list_guide` * Cleanup Use prelude for ratatui imports. Use const for theme functions, add missing hints * Update deps, remove unused temp-dir * Add accidentally deleted preview.tape Add labels + Wait 2sec after program ends * Add fields to config files Skip Confirmation, Bypass Size * Remove accidentally commited config file --------- Co-authored-by: Liam <33645555+lj3954@users.noreply.github.com>
2024-11-17 18:24:54 +00:00
fn run(terminal: &mut Terminal<CrosstermBackend<Stdout>>, state: &mut AppState) -> Result<()> {
2024-06-06 23:56:45 +01:00
loop {
2024-08-09 10:22:19 +01:00
terminal.draw(|frame| state.draw(frame)).unwrap();
// Wait for an event
if !event::poll(Duration::from_millis(10))? {
continue;
}
// It's guaranteed that the `read()` won't block when the `poll()`
// function returns `true`
match event::read()? {
Event::Key(key) => {
if key.kind != KeyEventKind::Press && key.kind != KeyEventKind::Repeat {
continue;
}
if !state.handle_key(&key) {
return Ok(());
}
}
Event::Mouse(mouse_event) => {
if !state.handle_mouse(&mouse_event) {
return Ok(());
}
}
_ => {}
}
2024-06-06 23:56:45 +01:00
}
}