Skip to content

Commit 9d3f97b

Browse files
Seperate root check logic
1 parent 1504f30 commit 9d3f97b

File tree

2 files changed

+74
-69
lines changed

2 files changed

+74
-69
lines changed

tui/src/main.rs

Lines changed: 5 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mod filter;
33
mod float;
44
mod floating_text;
55
mod hint;
6+
mod root;
67
mod running_command;
78
pub mod state;
89
mod theme;
@@ -15,18 +16,12 @@ use std::{
1516
use crate::theme::Theme;
1617
use clap::Parser;
1718
use crossterm::{
18-
event::{self, DisableMouseCapture, Event, KeyCode, KeyEvent, KeyEventKind},
19+
event::{self, DisableMouseCapture, Event, KeyEventKind},
1920
style::ResetColor,
2021
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
2122
ExecutableCommand,
2223
};
23-
use ratatui::{
24-
backend::CrosstermBackend,
25-
layout::{Alignment, Constraint, Layout},
26-
style::Stylize,
27-
widgets::{Paragraph, Wrap},
28-
Terminal,
29-
};
24+
use ratatui::{backend::CrosstermBackend, Terminal};
3025
use state::AppState;
3126

3227
// Linux utility toolbox
@@ -67,67 +62,8 @@ fn run(
6762
terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
6863
state: &mut AppState,
6964
) -> io::Result<()> {
70-
if sudo::check() == sudo::RunningAs::Root {
71-
terminal.draw(|frame| {
72-
let root_warn = Paragraph::new(
73-
r#"
74-
!!!!!!!!!!!!!! YOU ARE ABOUT TO RUN LINUTIL AS ROOT !!!!!!!!!!!!!!
75-
76-
This utility prioritizes compatibility with non-root environments.
77-
Some scripts may work without any issues, some may not.
78-
You have been warned!
79-
80-
!!!!!!!!!!!!!!!!!!!!!! PROCEED WITH CAUTION !!!!!!!!!!!!!!!!!!!!!!
81-
82-
Press [y] to continue, [n] to abort
83-
"#,
84-
)
85-
.on_black()
86-
.white()
87-
.alignment(Alignment::Center)
88-
.wrap(Wrap { trim: true });
89-
90-
let rects = Layout::vertical([
91-
Constraint::Fill(1),
92-
Constraint::Length(10),
93-
Constraint::Fill(1),
94-
])
95-
.split(frame.area());
96-
97-
let centered = rects[1];
98-
99-
frame.render_widget(root_warn, centered);
100-
})?;
101-
102-
loop {
103-
match event::read()? {
104-
Event::Key(
105-
KeyEvent {
106-
code: KeyCode::Char('y'),
107-
..
108-
}
109-
| KeyEvent {
110-
code: KeyCode::Char('Y'),
111-
..
112-
},
113-
) => {
114-
break;
115-
}
116-
Event::Key(
117-
KeyEvent {
118-
code: KeyCode::Char('n'),
119-
..
120-
}
121-
| KeyEvent {
122-
code: KeyCode::Char('N'),
123-
..
124-
},
125-
) => {
126-
return Ok(());
127-
}
128-
_ => {}
129-
}
130-
}
65+
if !root::check_root(terminal)? {
66+
return Ok(());
13167
}
13268

13369
loop {

tui/src/root.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use ratatui::{
2+
backend::CrosstermBackend,
3+
crossterm::event::{self, Event, KeyCode, KeyEvent},
4+
layout::{Alignment, Constraint, Layout},
5+
style::{Style, Stylize},
6+
widgets::{Paragraph, Wrap},
7+
Terminal,
8+
};
9+
use std::io;
10+
11+
pub fn check_root(terminal: &mut Terminal<CrosstermBackend<io::Stdout>>) -> io::Result<bool> {
12+
if sudo::check() == sudo::RunningAs::Root {
13+
terminal.draw(|frame| {
14+
let root_warn = Paragraph::new(
15+
r#"
16+
!!!!!!!!!!!!!! YOU ARE ABOUT TO RUN LINUTIL AS ROOT !!!!!!!!!!!!!!
17+
This utility prioritizes compatibility with non-root environments.
18+
Some scripts may work without any issues, some may not.
19+
You have been warned!
20+
!!!!!!!!!!!!!!!!!!!!!! PROCEED WITH CAUTION !!!!!!!!!!!!!!!!!!!!!!
21+
Press [y] to continue, [n] to abort
22+
"#,
23+
)
24+
.white()
25+
.on_black()
26+
.alignment(Alignment::Center)
27+
.style(Style::default().bold())
28+
.wrap(Wrap { trim: true });
29+
30+
let rects = Layout::vertical([
31+
Constraint::Fill(1),
32+
Constraint::Length(10),
33+
Constraint::Fill(1),
34+
])
35+
.split(frame.area());
36+
37+
let centered = rects[1];
38+
39+
frame.render_widget(root_warn, centered);
40+
})?;
41+
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),
64+
_ => {}
65+
}
66+
}
67+
}
68+
Ok(true)
69+
}

0 commit comments

Comments
 (0)