2024-09-30 22:48:22 +01:00
|
|
|
mod confirmation;
|
2024-08-18 20:51:47 +01:00
|
|
|
mod filter;
|
2024-06-06 23:56:45 +01:00
|
|
|
mod float;
|
2024-08-08 23:37:19 +01:00
|
|
|
mod floating_text;
|
2024-09-06 22:36:12 +01:00
|
|
|
mod hint;
|
2024-06-06 23:56:45 +01:00
|
|
|
mod running_command;
|
2024-07-28 16:17:06 +01:00
|
|
|
pub mod state;
|
2024-06-06 23:56:45 +01:00
|
|
|
mod theme;
|
|
|
|
|
|
|
|
use std::{
|
|
|
|
io::{self, stdout},
|
|
|
|
time::Duration,
|
|
|
|
};
|
|
|
|
|
2024-08-15 23:13:47 +01:00
|
|
|
use crate::theme::Theme;
|
2024-06-06 23:56:45 +01:00
|
|
|
use clap::Parser;
|
|
|
|
use crossterm::{
|
2024-08-09 10:22:19 +01:00
|
|
|
event::{self, DisableMouseCapture, Event, KeyEventKind},
|
2024-06-06 23:56:45 +01:00
|
|
|
style::ResetColor,
|
|
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
|
|
|
ExecutableCommand,
|
|
|
|
};
|
2024-09-22 17:50:43 +01:00
|
|
|
use ratatui::{backend::CrosstermBackend, Terminal};
|
2024-07-28 16:17:06 +01:00
|
|
|
use state::AppState;
|
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)]
|
|
|
|
struct Args {
|
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,
|
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,
|
2024-06-06 23:56:45 +01:00
|
|
|
}
|
|
|
|
|
2024-09-05 18:52:30 +01:00
|
|
|
fn main() -> io::Result<()> {
|
2024-06-06 23:56:45 +01:00
|
|
|
let args = Args::parse();
|
2024-07-28 16:17:06 +01:00
|
|
|
|
2024-09-06 00:39:10 +01:00
|
|
|
let mut state = AppState::new(args.theme, args.override_validation);
|
2024-06-06 23:56:45 +01:00
|
|
|
|
|
|
|
stdout().execute(EnterAlternateScreen)?;
|
|
|
|
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-09-05 18:52:30 +01:00
|
|
|
|
2024-06-06 23:56:45 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-09-22 17:50:43 +01:00
|
|
|
fn run(
|
|
|
|
terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
|
|
|
|
state: &mut AppState,
|
|
|
|
) -> io::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();
|
2024-07-11 23:58:27 +01:00
|
|
|
// 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`
|
|
|
|
if let Event::Key(key) = event::read()? {
|
|
|
|
// We are only interested in Press and Repeat events
|
|
|
|
if key.kind != KeyEventKind::Press && key.kind != KeyEventKind::Repeat {
|
|
|
|
continue;
|
|
|
|
}
|
2024-08-08 23:37:19 +01:00
|
|
|
|
2024-08-09 10:22:19 +01:00
|
|
|
if !state.handle_key(&key) {
|
|
|
|
return Ok(());
|
2024-07-11 23:58:27 +01:00
|
|
|
}
|
|
|
|
}
|
2024-06-06 23:56:45 +01:00
|
|
|
}
|
|
|
|
}
|