Skip to content

Commit 8eb3ba8

Browse files
committed
refactor: Reduce nesting in root module
1 parent ef59216 commit 8eb3ba8

File tree

1 file changed

+27
-39
lines changed

1 file changed

+27
-39
lines changed

tui/src/root.rs

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,62 +8,50 @@ use ratatui::{
88
};
99
use std::io;
1010

11-
pub fn check_root(terminal: &mut Terminal<CrosstermBackend<io::Stdout>>) -> io::Result<bool> {
12-
if nix::unistd::geteuid().is_root() {
13-
terminal.draw(|frame| {
14-
let root_warn = Paragraph::new(
15-
r#"
11+
const ROOT_WARNING: &str = r#"
1612
!!!!!!!!!!!!!! YOU ARE ABOUT TO RUN LINUTIL AS ROOT !!!!!!!!!!!!!!
1713
This utility prioritizes compatibility with non-root environments.
1814
Some scripts may work without any issues, some may not.
1915
You have been warned!
2016
!!!!!!!!!!!!!!!!!!!!!! PROCEED WITH CAUTION !!!!!!!!!!!!!!!!!!!!!!
2117
Press [y] to continue, [n] to abort
22-
"#,
23-
)
18+
"#;
19+
20+
pub fn check_root(terminal: &mut Terminal<CrosstermBackend<io::Stdout>>) -> io::Result<bool> {
21+
if !nix::unistd::geteuid().is_root() {
22+
return Ok(true);
23+
}
24+
terminal.draw(|frame| {
25+
let root_warn = Paragraph::new(ROOT_WARNING)
2426
.white()
2527
.on_black()
2628
.alignment(Alignment::Center)
2729
.style(Style::default().bold())
2830
.wrap(Wrap { trim: true });
2931

30-
let rects = Layout::vertical([
31-
Constraint::Fill(1),
32-
Constraint::Length(10),
33-
Constraint::Fill(1),
34-
])
35-
.split(frame.area());
32+
let rects = Layout::vertical([
33+
Constraint::Fill(1),
34+
Constraint::Length(10),
35+
Constraint::Fill(1),
36+
])
37+
.split(frame.area());
3638

37-
let centered = rects[1];
39+
let centered = rects[1];
3840

39-
frame.render_widget(root_warn, centered);
40-
})?;
41+
frame.render_widget(root_warn, centered);
42+
})?;
4143

42-
loop {
43-
match event::read()? {
44-
Event::Key(
45-
KeyEvent {
46-
code: KeyCode::Char('y'),
47-
..
48-
}
49-
| KeyEvent {
50-
code: KeyCode::Char('Y'),
51-
..
52-
},
53-
) => break,
54-
Event::Key(
55-
KeyEvent {
56-
code: KeyCode::Char('n'),
57-
..
58-
}
59-
| KeyEvent {
60-
code: KeyCode::Char('N'),
61-
..
62-
},
63-
) => return Ok(false),
44+
loop {
45+
if let Event::Key(KeyEvent {
46+
code: KeyCode::Char(ch),
47+
..
48+
}) = event::read()?
49+
{
50+
match ch.to_ascii_lowercase() {
51+
'y' => return Ok(true),
52+
'n' => return Ok(false),
6453
_ => {}
6554
}
6655
}
6756
}
68-
Ok(true)
6957
}

0 commit comments

Comments
 (0)