linutil/src/main.rs

109 lines
2.9 KiB
Rust
Raw Normal View History

2024-06-06 23:56:45 +01:00
mod float;
mod list;
mod running_command;
pub mod state;
2024-06-06 23:56:45 +01:00
mod theme;
use std::{
io::{self, stdout},
time::Duration,
};
use clap::Parser;
use crossterm::{
cursor::RestorePosition,
event::{self, DisableMouseCapture, Event, KeyCode, KeyEventKind},
2024-06-06 23:56:45 +01:00
style::ResetColor,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
};
use list::CustomList;
use ratatui::{
backend::{Backend, CrosstermBackend},
Terminal,
};
use running_command::RunningCommand;
use state::AppState;
use theme::THEMES;
2024-06-06 23:56:45 +01:00
/// This is a binary :), Chris, change this to update the documentation on -h
#[derive(Debug, Parser)]
struct Args {
/// Enable compatibility mode (disable icons and RGB colors)
#[arg(short, long, default_value_t = false)]
compat: bool,
}
fn main() -> std::io::Result<()> {
let args = Args::parse();
let theme = if args.compat {
THEMES[0].clone()
} else {
THEMES[1].clone()
};
let state = AppState {
theme,
};
2024-06-06 23:56:45 +01:00
stdout().execute(EnterAlternateScreen)?;
enable_raw_mode()?;
let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?;
terminal.clear()?;
run(&mut terminal, &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.backend_mut().execute(RestorePosition)?;
terminal.show_cursor()?;
Ok(())
}
fn run<B: Backend>(terminal: &mut Terminal<B>, state: &AppState) -> io::Result<()> {
2024-06-06 23:56:45 +01:00
let mut command_opt: Option<RunningCommand> = None;
let mut custom_list = CustomList::new();
loop {
// Always redraw
2024-06-06 23:56:45 +01:00
terminal
.draw(|frame| {
custom_list.draw(frame, frame.size(), state);
2024-06-06 23:56:45 +01:00
if let Some(ref mut command) = &mut command_opt {
command.draw(frame, state);
2024-06-06 23:56:45 +01:00
}
})
.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`
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;
}
if let Some(ref mut command) = command_opt {
if command.handle_key_event(&key) {
command_opt = None;
}
} else {
if key.code == KeyCode::Char('q') {
return Ok(());
}
if let Some(cmd) = custom_list.handle_key(key) {
command_opt = Some(RunningCommand::new(cmd));
}
}
}
2024-06-06 23:56:45 +01:00
}
}