Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad-Owais-Warsi committed Oct 9, 2024
1 parent a15aff6 commit f0d0354
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 157 deletions.
45 changes: 15 additions & 30 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ use std::sync::mpsc::Sender;
use termion::event::Key;
use unicode_width::UnicodeWidthStr;

/// Table header of the module table
/// Table header of the module table
pub const TABLE_HEADER: &[&str] = &[" Module", "Size", "Used by"];

/// Available options in the module management menu
/// Available options in the module management menu
const OPTIONS: &[(&str, &str)] = &[
("unload", "Unload the module"),
("reload", "Reload the module"),
Expand All @@ -36,7 +36,7 @@ const OPTIONS: &[(&str, &str)] = &[
("clear", "Clear the ring buffer"),
];

/// Supported directions of scrolling
/// Supported directions of scrolling
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ScrollDirection {
Up,
Expand All @@ -48,7 +48,6 @@ pub enum ScrollDirection {
}

impl ScrollDirection {

/// Return iterator of the available scroll directions.
#[allow(dead_code)]
pub fn iter() -> Iter<'static, ScrollDirection> {
Expand All @@ -64,7 +63,7 @@ impl ScrollDirection {
}
}

/// Main blocks of the terminal
/// Main blocks of the terminal
#[derive(Clone, Copy, Debug, PartialEq, Eq, Sequence)]
pub enum Block {
UserInput,
Expand All @@ -73,14 +72,14 @@ pub enum Block {
Activities,
}

/// Sizes of the terminal blocks
/// Sizes of the terminal blocks
pub struct BlockSize {
pub input: u16,
pub info: u16,
pub activities: u16,
}

/// Default initialization values for BlockSize
/// Default initialization values for BlockSize
impl Default for BlockSize {
fn default() -> Self {
Self {
Expand All @@ -91,7 +90,7 @@ impl Default for BlockSize {
}
}

/// User input mode
/// User input mode
#[derive(Clone, Copy, Debug, PartialEq, Eq, Sequence)]
pub enum InputMode {
None,
Expand All @@ -100,14 +99,13 @@ pub enum InputMode {
}

impl InputMode {

/// Check if input mode is set.
pub fn is_none(self) -> bool {
self == Self::None
}
}

/// Implementation of Display for using InputMode members as string
/// Implementation of Display for using InputMode members as string
impl Display for InputMode {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let mut input_mode = *self;
Expand Down Expand Up @@ -136,7 +134,6 @@ pub struct App {
}

impl App {

/// Create a new app instance.
pub fn new(block: Block, style: Style) -> Self {
Self {
Expand Down Expand Up @@ -166,7 +163,7 @@ impl App {
}
}

/// Reset app properties to default.
/// Reset app properties to default.
pub fn refresh(&mut self) {
self.selected_block = self.default_block;
self.block_size = BlockSize::default();
Expand All @@ -177,7 +174,6 @@ impl App {
self.show_options = false;
}


/// Get style depending on the selected state of the block.
pub fn block_style(&self, block: Block) -> TuiStyle {
if self.show_options {
Expand All @@ -189,7 +185,6 @@ impl App {
}
}


/// Get the size of the selected block.
pub fn block_size(&mut self) -> &mut u16 {
match self.selected_block {
Expand All @@ -199,7 +194,6 @@ impl App {
}
}


/// Get clipboard contents as String.
pub fn get_clipboard_contents(&mut self) -> String {
if let Some(clipboard) = self.clipboard.as_mut() {
Expand All @@ -210,7 +204,6 @@ impl App {
String::new()
}


/// Set clipboard contents.
pub fn set_clipboard_contents(&mut self, contents: &str) {
if let Some(clipboard) = self.clipboard.as_mut() {
Expand All @@ -220,7 +213,6 @@ impl App {
}
}


/// Show help message on the information block.
pub fn show_help_message(&mut self, kernel_modules: &mut KernelModules<'_>) {
let key_bindings: Vec<(&str, &str)> = util::KEY_BINDINGS.to_vec();
Expand All @@ -247,7 +239,6 @@ impl App {
.set(Text::from(help_text), help_text_raw.join("\n"));
}


/// Show dependent modules on the information block.
#[allow(clippy::nonminimal_bool)]
pub fn show_dependent_modules(
Expand Down Expand Up @@ -286,7 +277,6 @@ impl App {
}
}


/// Draw a block according to the index.
pub fn draw_dynamic_block(
&mut self,
Expand All @@ -306,7 +296,6 @@ impl App {
}
}


/// Draw a paragraph widget for using as user input.
pub fn draw_user_input(
&self,
Expand Down Expand Up @@ -346,7 +335,6 @@ impl App {
);
}


/// Draw a paragraph widget for showing the kernel information.
pub fn draw_kernel_info(&self, frame: &mut Frame, area: Rect, info: &[String]) {
frame.render_widget(
Expand All @@ -371,15 +359,14 @@ impl App {
);
}


/// Configure and draw kernel modules table.
pub fn draw_kernel_modules(
&mut self,
frame: &mut Frame,
area: Rect,
kernel_modules: &mut KernelModules<'_>,
) {
// Filter the module list depending on the input query.
// Filter the module list depending on the input query.
let mut kernel_module_list = kernel_modules.default_list.clone();
if (self.input_mode == InputMode::None
|| self.input_mode == InputMode::Search)
Expand All @@ -391,7 +378,7 @@ impl App {
.contains(&self.input_query.to_lowercase())
});
}
// Append '...' if dependent modules exceed the block width.
// Append '...' if dependent modules exceed the block width.
let dependent_width = (area.width / 2).saturating_sub(7) as usize;
for module in &mut kernel_module_list {
if module[2].len() > dependent_width {
Expand All @@ -400,13 +387,13 @@ impl App {
}
}
kernel_modules.list = kernel_module_list;
// Set the scroll offset for modules.
// Set the scroll offset for modules.
let modules_scroll_offset = area
.height
.checked_sub(5)
.and_then(|height| kernel_modules.index.checked_sub(height as usize))
.unwrap_or(0);
// Set selected state of the modules and render the table widget.
// Set selected state of the modules and render the table widget.
frame.render_widget(
Table::new(
kernel_modules
Expand Down Expand Up @@ -453,7 +440,8 @@ impl App {
self.style.unicode.get(Symbol::LeftBracket),
if !kernel_modules.list.is_empty() {
((kernel_modules.index + 1) as f64
/ kernel_modules.list.len() as f64 * 100.0) as u64
/ kernel_modules.list.len() as f64
* 100.0) as u64
} else {
0
},
Expand All @@ -469,7 +457,6 @@ impl App {
}
}


/// Draws the options menu as a popup.
pub fn draw_options_menu(
&mut self,
Expand Down Expand Up @@ -548,7 +535,6 @@ impl App {
);
}


/// Draw a paragraph widget for showing module information.
pub fn draw_module_info(
&self,
Expand Down Expand Up @@ -591,7 +577,6 @@ impl App {
);
}


/// Draw a paragraph widget for showing kernel activities.
pub fn draw_kernel_activities(
&self,
Expand Down
3 changes: 1 addition & 2 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::{Arg, ArgAction, Command as App};

/// ASCII format of the project logo
/// ASCII format of the project logo
const ASCII_LOGO: &str = "
`` ```````````` ```` ``````````` ```````````
:NNs `hNNNNNNNNNNNNh` sNNNy yNNNNNNNNNN+ dNNNNNNNNNN:
Expand All @@ -10,7 +10,6 @@ const ASCII_LOGO: &str = "
:mmm/ dmmh +mmm- `mmmmmmmmmmmmmmmmmmmmmmmmmd /mmm:
``` ``` ``` `````````````````````````` ```";


/// Parse command line arguments using clap.
pub fn get_args() -> App {
App::new(env!("CARGO_PKG_NAME"))
Expand Down
17 changes: 8 additions & 9 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use std::time::Duration;
use termion::event::Key;
use termion::input::TermRead;

/// Terminal event methods
/// Terminal event methods
pub enum Event<I> {
Input(I),
Kernel(String),
Tick,
}

/// Terminal events
/// Terminal events
#[allow(dead_code)]
pub struct Events {
pub tx: mpsc::Sender<Event<Key>>,
Expand All @@ -24,14 +24,13 @@ pub struct Events {
}

impl Events {

/// Create a new events instance.
pub fn new(refresh_rate: u64, kernel_logs: &KernelLogs) -> Self {
// Convert refresh rate to Duration from milliseconds.
// Convert refresh rate to Duration from milliseconds.
let refresh_rate = Duration::from_millis(refresh_rate);
// Create a new asynchronous channel.
// Create a new asynchronous channel.
let (tx, rx) = mpsc::channel();
// Handle inputs using stdin stream and sender of the channel.
// Handle inputs using stdin stream and sender of the channel.
let input_handler = {
let tx = tx.clone();
thread::spawn(move || {
Expand All @@ -41,7 +40,7 @@ impl Events {
}
})
};
// Handle kernel logs using 'dmesg' output.
// Handle kernel logs using 'dmesg' output.
let kernel_handler = {
let tx = tx.clone();
let mut kernel_logs = kernel_logs.clone();
Expand All @@ -53,15 +52,15 @@ impl Events {
thread::sleep(refresh_rate * 10);
})
};
// Create a loop for handling events.
// Create a loop for handling events.
let tick_handler = {
let tx = tx.clone();
thread::spawn(move || loop {
tx.send(Event::Tick).unwrap_or_default();
thread::sleep(refresh_rate);
})
};
// Return events.
// Return events.
Self {
tx,
rx,
Expand Down
10 changes: 3 additions & 7 deletions src/kernel/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::style::Symbol;

/// Kernel module related command
/// Kernel module related command
#[derive(Debug)]
pub struct Command {
pub cmd: String,
Expand All @@ -10,15 +10,14 @@ pub struct Command {
}

impl Command {

/// Create a new command instance.
fn new(
cmd: String,
desc: &'static str,
mut title: String,
symbol: Symbol,
) -> Self {
// Parse the command title if '!' is given.
// Parse the command title if '!' is given.
if title.contains('!') {
title = (*title
.split('!')
Expand All @@ -36,7 +35,7 @@ impl Command {
}
}

/// Kernel module management commands
/// Kernel module management commands
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ModuleCommand {
None,
Expand All @@ -62,7 +61,6 @@ impl TryFrom<String> for ModuleCommand {
}

impl ModuleCommand {

/// Get Command struct from a enum element.
pub fn get(self, module_name: &str) -> Command {
match self {
Expand Down Expand Up @@ -120,13 +118,11 @@ impl ModuleCommand {
}
}


/// Check if module command is set.
pub fn is_none(self) -> bool {
self == Self::None
}


/// Check if module name is a filename with suffix 'ko'
pub fn is_module_filename(module_name: &str) -> bool {
match module_name.split('.').collect::<Vec<&str>>().last() {
Expand Down
Loading

0 comments on commit f0d0354

Please sign in to comment.