From 8eb3ba88b45a9adbd3bd07d9560ed9fe8dc70506 Mon Sep 17 00:00:00 2001 From: Liam <33645555+lj3954@users.noreply.github.com> Date: Mon, 21 Oct 2024 23:24:51 -0700 Subject: [PATCH] refactor: Reduce nesting in root module --- tui/src/root.rs | 66 ++++++++++++++++++++----------------------------- 1 file changed, 27 insertions(+), 39 deletions(-) diff --git a/tui/src/root.rs b/tui/src/root.rs index c8ba0b48..638f0c51 100644 --- a/tui/src/root.rs +++ b/tui/src/root.rs @@ -8,62 +8,50 @@ use ratatui::{ }; use std::io; -pub fn check_root(terminal: &mut Terminal>) -> io::Result { - if nix::unistd::geteuid().is_root() { - terminal.draw(|frame| { - let root_warn = Paragraph::new( - r#" +const ROOT_WARNING: &str = r#" !!!!!!!!!!!!!! YOU ARE ABOUT TO RUN LINUTIL AS ROOT !!!!!!!!!!!!!! This utility prioritizes compatibility with non-root environments. Some scripts may work without any issues, some may not. You have been warned! !!!!!!!!!!!!!!!!!!!!!! PROCEED WITH CAUTION !!!!!!!!!!!!!!!!!!!!!! Press [y] to continue, [n] to abort -"#, - ) +"#; + +pub fn check_root(terminal: &mut Terminal>) -> io::Result { + if !nix::unistd::geteuid().is_root() { + return Ok(true); + } + terminal.draw(|frame| { + let root_warn = Paragraph::new(ROOT_WARNING) .white() .on_black() .alignment(Alignment::Center) .style(Style::default().bold()) .wrap(Wrap { trim: true }); - let rects = Layout::vertical([ - Constraint::Fill(1), - Constraint::Length(10), - Constraint::Fill(1), - ]) - .split(frame.area()); + let rects = Layout::vertical([ + Constraint::Fill(1), + Constraint::Length(10), + Constraint::Fill(1), + ]) + .split(frame.area()); - let centered = rects[1]; + let centered = rects[1]; - frame.render_widget(root_warn, centered); - })?; + frame.render_widget(root_warn, centered); + })?; - loop { - match event::read()? { - Event::Key( - KeyEvent { - code: KeyCode::Char('y'), - .. - } - | KeyEvent { - code: KeyCode::Char('Y'), - .. - }, - ) => break, - Event::Key( - KeyEvent { - code: KeyCode::Char('n'), - .. - } - | KeyEvent { - code: KeyCode::Char('N'), - .. - }, - ) => return Ok(false), + loop { + if let Event::Key(KeyEvent { + code: KeyCode::Char(ch), + .. + }) = event::read()? + { + match ch.to_ascii_lowercase() { + 'y' => return Ok(true), + 'n' => return Ok(false), _ => {} } } } - Ok(true) }