From 805c0b812b96d5130d39eb9b794e344a0f273d6d Mon Sep 17 00:00:00 2001 From: grtcdr Date: Fri, 19 Feb 2021 23:36:49 +0100 Subject: [PATCH] fix: extra whitespace in battery bar, and move relevant functions inside Elements struct --- src/bars.rs | 30 ++++++------ src/display.rs | 128 +++++++++++++++++++++---------------------------- src/format.rs | 38 ++++++++------- 3 files changed, 91 insertions(+), 105 deletions(-) diff --git a/src/bars.rs b/src/bars.rs index 8319845..03e3d52 100644 --- a/src/bars.rs +++ b/src/bars.rs @@ -1,13 +1,15 @@ use crate::extra; use crate::memory; use crate::read; -use extra::percent_of_total; /// Returns a usize (0 .. 10) based on the battery percentage, -/// `display::show_bar` takes this usize as a parameter to handle +/// `display::show_bar` takes this function as a parameter to handle /// displaying the bar pub fn battery() -> usize { - match read::battery_percentage().parse::().unwrap() { + match read::battery_percentage() + .parse::() + .expect("error: battery percentage could not be parsed") + { 0..=10 => 1, 11..=20 => 2, 21..=30 => 3, @@ -23,30 +25,30 @@ pub fn battery() -> usize { } /// Returns a usize (0 .. 10) based on the memory usage, -/// `display::show_bar` takes this usize as a parameter to handle +/// `display::show_bar` takes this function as a parameter to handle /// displaying the bar pub fn memory() -> usize { let u = memory::used(); - if u <= percent_of_total(10) { + if u <= extra::percent_of_total(10) { return 1; - } else if u <= percent_of_total(20) { + } else if u <= extra::percent_of_total(20) { return 2; - } else if u <= percent_of_total(30) { + } else if u <= extra::percent_of_total(30) { return 3; - } else if u <= percent_of_total(40) { + } else if u <= extra::percent_of_total(40) { return 4; - } else if u <= percent_of_total(50) { + } else if u <= extra::percent_of_total(50) { return 5; - } else if u <= percent_of_total(60) { + } else if u <= extra::percent_of_total(60) { return 6; - } else if u <= percent_of_total(70) { + } else if u <= extra::percent_of_total(70) { return 7; - } else if u <= percent_of_total(80) { + } else if u <= extra::percent_of_total(80) { return 8; - } else if u <= percent_of_total(90) { + } else if u <= extra::percent_of_total(90) { return 9; - } else if u <= percent_of_total(100) { + } else if u <= extra::percent_of_total(100) { return 10; } diff --git a/src/display.rs b/src/display.rs index f320f36..294ac6f 100644 --- a/src/display.rs +++ b/src/display.rs @@ -1,6 +1,4 @@ -use crate::{ - bars, format, memory, product, read, DEFAULT_COLOR, DEFAULT_PADDING, DEFAULT_SEPARATOR_COLOR, -}; +use crate::{bars, format, read, DEFAULT_COLOR, DEFAULT_PADDING, DEFAULT_SEPARATOR_COLOR}; use colored::{Color, ColoredString, Colorize}; use rand::Rng; use std::fmt; @@ -103,38 +101,18 @@ pub struct Elements { impl Elements { pub fn new() -> Elements { Elements { - host: Pair::new( - String::from("Host"), - format::host(read::hostname(), read::username()), - ), + host: Pair::new(String::from("Host"), format::host()), distro: Pair::new(String::from("Dist"), read::operating_system()), desktop_env: Pair::new(String::from("Desk"), read::desktop_session()), kernel: Pair::new(String::from("Kern"), read::kernel_version()), packages: Pair::new(String::from("Pkgs"), read::package_count()), shell: Pair::new(String::from("Shll"), String::new()), - machine: Pair::new( - String::from("Mach"), - format::machine( - product::product_version(), - product::sys_vendor(), - product::product_family(), - product::product_name(), - ), - ), + machine: Pair::new(String::from("Mach"), format::machine()), terminal: Pair::new(String::from("Term"), read::terminal()), - cpu: Pair::new( - String::from("Proc"), - format::cpu(read::cpu_model_name(), num_cpus::get()), - ), - memory: Pair::new( - String::from("Memo"), - format::memory(memory::used(), memory::memtotal()), - ), + cpu: Pair::new(String::from("Proc"), format::cpu()), + memory: Pair::new(String::from("Memo"), format::memory()), uptime: Pair::new(String::from("Upti"), read::uptime()), - battery: Pair::new( - String::from("Batt"), - format::battery(read::battery_percentage(), read::battery_status()), - ), + battery: Pair::new(String::from("Batt"), format::battery()), format: Format::new(), } } @@ -183,16 +161,19 @@ impl Elements { pub fn set_left_padding_to(&mut self, amount: usize) { self.format.padding = " ".repeat(amount) } - pub fn enable_bar(&mut self) { - self.format.bar = true; - } pub fn set_longest_key(&mut self) { self.format.longest_key = self.longest_key(); } pub fn set_spacing(&mut self, v: usize) { self.format.spacing = v; } + pub fn enable_bar(&mut self) { + self.format.bar = true; + } pub fn longest_key(&self) -> String { + // Instead of manually declaring which key is the longest + // in order to satisfy auto-spacing's algorithm, let longest_key() + // determine the longest key let keys: Vec = vec![ self.host.key.clone(), self.machine.key.clone(), @@ -234,6 +215,8 @@ trait Printing { fn print_uptime(&self); fn print_memory(&self); fn print_battery(&self); + fn print_bar(&self, bar: usize); + fn print_palette(&self); } impl Printing for Elements { @@ -411,7 +394,7 @@ impl Printing for Elements { .bold(), " ".repeat(self.format.spacing), ); - show_bar(self, bars::memory()); + Printing::print_bar(self, bars::memory()); } else { println!( "{}{}{}{}{}{}", @@ -442,7 +425,7 @@ impl Printing for Elements { .bold(), " ".repeat(self.format.spacing), ); - show_bar(self, bars::battery()); + Printing::print_bar(self, bars::battery()); } else { println!( "{}{}{}{}{}{}", @@ -459,6 +442,42 @@ impl Printing for Elements { } } } + /// Print a bar next to memory and battery keys: + /// it takes a function from the _bars crate_ as the first parameter + /// and the color of the keys as a second + fn print_bar(&self, bar: usize) { + match &self.format.color { + Color::White => println!( + "{} {} {} {}", + self.format.bracket_open, + colored_blocks(self, bar), + hidden_blocks(self, bar), + self.format.bracket_close + ), + _ => println!( + "{} {} {} {}", + self.format.bracket_open, + colored_blocks(self, bar), + colorless_blocks(self, bar), + self.format.bracket_close + ), + } + } + /// Print a palette using the terminal's colorscheme + fn print_palette(&self) { + println!( + "{}{}{}{}{}{}{}{}{}", + self.format.padding, + " ".on_bright_black(), + " ".on_bright_red(), + " ".on_bright_green(), + " ".on_bright_yellow(), + " ".on_bright_blue(), + " ".on_bright_purple(), + " ".on_bright_cyan(), + " ".on_bright_white() + ); + } } /// Handles displaying each element (key and value pair) found in @@ -485,27 +504,11 @@ pub fn print_info(mut elems: Elements, opts: Options) { if opts.palette_status { println!(); - print_palette(&elems); + elems.print_palette(); println!(); } } -/// Print a palette using the terminal's colorscheme -pub fn print_palette(elems: &Elements) { - println!( - "{}{}{}{}{}{}{}{}{}", - elems.format.padding, - " ".on_bright_black(), - " ".on_bright_red(), - " ".on_bright_green(), - " ".on_bright_yellow(), - " ".on_bright_blue(), - " ".on_bright_purple(), - " ".on_bright_cyan(), - " ".on_bright_white() - ); -} - /// Hide an element or more e.g. package count, uptime etc. _(--hide )_ pub fn hide(mut elems: Elements, options: Options, hide_parameters: Vec<&str>) { if hide_parameters.contains(&"host") { @@ -634,28 +637,6 @@ pub fn help() { println!("{}", help_string); } -/// Print a bar next to memory and battery keys: -/// it takes a function from the _bars crate_ as the first parameter -/// and the color of the keys as a second -pub fn show_bar(elems: &Elements, bar: usize) { - match elems.format.color { - Color::White => println!( - "{} {} {} {}", - elems.format.bracket_open, - colored_blocks(elems, bar), - hidden_blocks(elems, bar), - elems.format.bracket_close - ), - _ => println!( - "{} {} {} {}", - elems.format.bracket_open, - colored_blocks(elems, bar), - colorless_blocks(elems, bar), - elems.format.bracket_close - ), - } -} - /// Return the correct amount of colored blocks: colored blocks are used blocks pub fn colored_blocks(elems: &Elements, block_count: usize) -> ColoredString { let colored_blocks = elems.format.bar_glyph.repeat(block_count); @@ -671,7 +652,7 @@ pub fn colored_blocks(elems: &Elements, block_count: usize) -> ColoredString { } /// Return the correct amount of colorless blocks: colorless blocks are unused blocks -pub fn colorless_blocks(elems: &Elements, block_count: usize) -> ColoredString { +pub fn colorless_blocks(elems: &Elements, block_count: usize) -> String { let colorless_blocks = elems.format.bar_glyph.repeat(10 - block_count); colorless_blocks .trim() @@ -681,7 +662,6 @@ pub fn colorless_blocks(elems: &Elements, block_count: usize) -> ColoredString { .map(|c| c.iter().collect::()) .collect::>() .join(" ") - .color(Color::White) } // Used to correctly format the bars when using `--no-color`: diff --git a/src/format.rs b/src/format.rs index 5dcfcbf..f944df3 100644 --- a/src/format.rs +++ b/src/format.rs @@ -1,3 +1,4 @@ +use crate::{memory, product, read}; use bytesize::ByteSize; /// Construct a new _String_ from the value @@ -36,13 +37,15 @@ pub fn uptime(up: String) -> String { /// Construct a new _String_ from the values /// returned by `read::hostname` and `read::username` -pub fn host(hostname: String, username: String) -> String { - username + "@" + &hostname +pub fn host() -> String { + read::username() + "@" + &read::hostname() } /// Construct a new _String_ from the values /// returned by `read::battery_percentage` and `read::battery_status` -pub fn battery(percentage: String, status: String) -> String { +pub fn battery() -> String { + let percentage = read::battery_percentage(); + let status = read::battery_status(); if !percentage.is_empty() && !status.is_empty() { if percentage != "100" { return String::from(percentage + "% - " + &status); @@ -54,30 +57,31 @@ pub fn battery(percentage: String, status: String) -> String { /// Construct a new _String_ from the values /// returned by `memory::used` and `memory::memtotal` -pub fn memory(used: u64, total: u64) -> String { - let total = ByteSize::kb(total); - let used = ByteSize::kb(used); +pub fn memory() -> String { + let total = ByteSize::kb(memory::memtotal()); + let used = ByteSize::kb(memory::used()); String::from(used.to_string() + "/" + &total.to_string()) } /// Construct a new _String_ from the values /// returned by `read::cpu_model_name` and `num_cpus::get` -pub fn cpu(cpu_model_name: String, logical_cores: usize) -> String { - String::from(cpu_model_name + " (" + &logical_cores.to_string() + ")") +pub fn cpu() -> String { + String::from(read::cpu_model_name() + " (" + &num_cpus::get().to_string() + ")") } /// Construct a new _String_ from the values /// returned by `machine::sys_vendor` and `machine::product_family` or `machine::product_version` -pub fn machine( - product_version: String, - sys_vendor: String, - product_family: String, - product_name: String, -) -> String { - if product_version.is_empty() || product_version.len() <= 15 { - return String::from(sys_vendor + " " + &product_family + " " + &product_name); +pub fn machine() -> String { + if product::product_version().is_empty() || product::product_version().len() <= 15 { + return String::from( + product::sys_vendor() + + " " + + &product::product_family() + + " " + + &product::product_name(), + ); } - product_version + product::product_version() } pub fn desktop_session(mut session_name: String) -> String {