forked from ChrisTitusTech/linutil
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
116 lines (103 loc) · 3.14 KB
/
main.rs
File metadata and controls
116 lines (103 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
mod confirmation;
mod filter;
mod float;
mod floating_text;
mod hint;
mod root;
mod running_command;
mod state;
mod theme;
#[cfg(feature = "tips")]
mod tips;
use crate::theme::Theme;
use clap::Parser;
use ratatui::{
backend::CrosstermBackend,
crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyEventKind},
style::ResetColor,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
},
Terminal,
};
use running_command::TERMINAL_UPDATED;
use state::AppState;
use std::{
io::{stdout, Result, Stdout},
path::PathBuf,
sync::atomic::Ordering,
time::Duration,
};
// Linux utility toolbox
#[derive(Debug, Parser)]
pub struct Args {
#[arg(short, long, help = "Path to the configuration file")]
config: Option<PathBuf>,
#[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,
#[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,
}
fn main() -> Result<()> {
let args = Args::parse();
let mut state = AppState::new(args);
stdout().execute(EnterAlternateScreen)?;
stdout().execute(EnableMouseCapture)?;
enable_raw_mode()?;
let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?;
terminal.clear()?;
run(&mut terminal, &mut state)?;
// restore terminal
disable_raw_mode()?;
terminal.backend_mut().execute(LeaveAlternateScreen)?;
terminal.backend_mut().execute(DisableMouseCapture)?;
terminal.backend_mut().execute(ResetColor)?;
terminal.show_cursor()?;
Ok(())
}
fn run(terminal: &mut Terminal<CrosstermBackend<Stdout>>, state: &mut AppState) -> Result<()> {
loop {
// Wait for an event
if !event::poll(Duration::from_millis(10))? {
if TERMINAL_UPDATED
.compare_exchange(true, false, Ordering::AcqRel, Ordering::Acquire)
.is_ok()
{
terminal.draw(|frame| state.draw(frame)).unwrap();
}
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(());
}
}
_ => {}
}
terminal.draw(|frame| state.draw(frame)).unwrap();
}
}