From 313e4ba56fd54b3bbbb8789f77eb9d0d4077ac83 Mon Sep 17 00:00:00 2001 From: Jeevitha Kannan K S Date: Mon, 14 Oct 2024 11:05:15 +0530 Subject: [PATCH] Seperate root check logic --- tui/src/main.rs | 74 +++---------------------------------------------- tui/src/root.rs | 72 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 70 deletions(-) create mode 100644 tui/src/root.rs diff --git a/tui/src/main.rs b/tui/src/main.rs index 8320cc6d3..3d07d76d5 100644 --- a/tui/src/main.rs +++ b/tui/src/main.rs @@ -3,6 +3,7 @@ mod filter; mod float; mod floating_text; mod hint; +mod root; mod running_command; pub mod state; mod theme; @@ -15,18 +16,12 @@ use std::{ use crate::theme::Theme; use clap::Parser; use crossterm::{ - event::{self, DisableMouseCapture, Event, KeyCode, KeyEvent, KeyEventKind}, + event::{self, DisableMouseCapture, Event, KeyEventKind}, style::ResetColor, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, ExecutableCommand, }; -use ratatui::{ - backend::CrosstermBackend, - layout::{Alignment, Constraint, Layout}, - style::Stylize, - widgets::{Paragraph, Wrap}, - Terminal, -}; +use ratatui::{backend::CrosstermBackend, Terminal}; use state::AppState; // Linux utility toolbox @@ -67,68 +62,7 @@ fn run( terminal: &mut Terminal>, state: &mut AppState, ) -> io::Result<()> { - if sudo::check() == sudo::RunningAs::Root { - terminal.draw(|frame| { - let root_warn = Paragraph::new( - 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 -"#, - ) - .on_black() - .white() - .alignment(Alignment::Center) - .wrap(Wrap { trim: true }); - - let rects = Layout::vertical([ - Constraint::Fill(1), - Constraint::Length(10), - Constraint::Fill(1), - ]) - .split(frame.area()); - - let centered = rects[1]; - - 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(()); - } - _ => {} - } - } - } + root::check_root(terminal)?; loop { terminal.draw(|frame| state.draw(frame)).unwrap(); diff --git a/tui/src/root.rs b/tui/src/root.rs new file mode 100644 index 000000000..914118bda --- /dev/null +++ b/tui/src/root.rs @@ -0,0 +1,72 @@ +use ratatui::{ + backend::CrosstermBackend, + crossterm::event::{self, Event, KeyCode, KeyEvent}, + layout::{Alignment, Constraint, Layout}, + style::Stylize, + widgets::{Paragraph, Wrap}, + Terminal, +}; +use std::io; + +pub fn check_root(terminal: &mut Terminal>) -> io::Result<()> { + if sudo::check() == sudo::RunningAs::Root { + terminal.draw(|frame| { + let root_warn = Paragraph::new( + 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 +"#, + ) + .on_black() + .white() + .alignment(Alignment::Center) + .wrap(Wrap { trim: true }); + + let rects = Layout::vertical([ + Constraint::Fill(1), + Constraint::Length(10), + Constraint::Fill(1), + ]) + .split(frame.area()); + + let centered = rects[1]; + + 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(()); + } + _ => {} + } + } + } + Ok(()) +}