Merge pull request #2 from lj3954/root

refactor: Reduce nesting in root module
This commit is contained in:
JEEVITHA KANNAN K S 2024-10-26 07:27:46 +05:30 committed by GitHub
commit 8ba2fdd23e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -8,62 +8,50 @@ use ratatui::{
}; };
use std::io; use std::io;
pub fn check_root(terminal: &mut Terminal<CrosstermBackend<io::Stdout>>) -> io::Result<bool> { const ROOT_WARNING: &str = r#"
if nix::unistd::geteuid().is_root() {
terminal.draw(|frame| {
let root_warn = Paragraph::new(
r#"
!!!!!!!!!!!!!! YOU ARE ABOUT TO RUN LINUTIL AS ROOT !!!!!!!!!!!!!! !!!!!!!!!!!!!! YOU ARE ABOUT TO RUN LINUTIL AS ROOT !!!!!!!!!!!!!!
This utility prioritizes compatibility with non-root environments. This utility prioritizes compatibility with non-root environments.
Some scripts may work without any issues, some may not. Some scripts may work without any issues, some may not.
You have been warned! You have been warned!
!!!!!!!!!!!!!!!!!!!!!! PROCEED WITH CAUTION !!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!! PROCEED WITH CAUTION !!!!!!!!!!!!!!!!!!!!!!
Press [y] to continue, [n] to abort Press [y] to continue, [n] to abort
"#, "#;
)
pub fn check_root(terminal: &mut Terminal<CrosstermBackend<io::Stdout>>) -> io::Result<bool> {
if !nix::unistd::geteuid().is_root() {
return Ok(true);
}
terminal.draw(|frame| {
let root_warn = Paragraph::new(ROOT_WARNING)
.white() .white()
.on_black() .on_black()
.alignment(Alignment::Center) .alignment(Alignment::Center)
.style(Style::default().bold()) .style(Style::default().bold())
.wrap(Wrap { trim: true }); .wrap(Wrap { trim: true });
let rects = Layout::vertical([ let rects = Layout::vertical([
Constraint::Fill(1), Constraint::Fill(1),
Constraint::Length(10), Constraint::Length(10),
Constraint::Fill(1), Constraint::Fill(1),
]) ])
.split(frame.area()); .split(frame.area());
let centered = rects[1]; let centered = rects[1];
frame.render_widget(root_warn, centered); frame.render_widget(root_warn, centered);
})?; })?;
loop { loop {
match event::read()? { if let Event::Key(KeyEvent {
Event::Key( code: KeyCode::Char(ch),
KeyEvent { ..
code: KeyCode::Char('y'), }) = event::read()?
.. {
} match ch.to_ascii_lowercase() {
| KeyEvent { 'y' => return Ok(true),
code: KeyCode::Char('Y'), 'n' => return Ok(false),
..
},
) => break,
Event::Key(
KeyEvent {
code: KeyCode::Char('n'),
..
}
| KeyEvent {
code: KeyCode::Char('N'),
..
},
) => return Ok(false),
_ => {} _ => {}
} }
} }
} }
Ok(true)
} }