From c082c328f549de55abe040fa01e1e8856103e0ce Mon Sep 17 00:00:00 2001 From: Ultradesu Date: Mon, 7 Apr 2025 19:07:57 +0100 Subject: [PATCH] Improved styling --- Cargo.toml | 12 +++--- src/config.rs | 82 ++++++++++++++++++++++++++++++++++++---- src/main.rs | 1 - src/utils.rs | 103 ++++++++++++++++++++++++++++++++++++-------------- 4 files changed, 156 insertions(+), 42 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 31dfd91..9915507 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,9 +6,9 @@ authors = ["Ultra Desu "] edition = "2021" [dependencies] -sys-info = "*" -dbus = "*" -chrono = "*" -mpd = "*" -clap = "*" -size_format = "1.0" +sys-info = "0.9" +dbus = "0.9" +chrono = "0.4" +mpd = "0.1" +clap = "4" +size_format = "1" diff --git a/src/config.rs b/src/config.rs index 20f5cc9..24c5e00 100644 --- a/src/config.rs +++ b/src/config.rs @@ -22,6 +22,10 @@ pub struct Config { pub mpd_server: String, pub lt_format: Option, pub ut_format: Option, + pub bar_symbol: Option, + pub bar_empty_symbol: Option, + pub low_threshold: f32, + pub mid_threshold: f32, pub color_low: String, pub color_mid: String, pub color_high: String, @@ -55,6 +59,20 @@ pub fn read() -> Config { .action(clap::ArgAction::SetTrue) .help("Print mem usage bar."), ) + .arg( + Arg::new("low") + .long("low") + .help("Low threshold (0.0 - 1.0)") + .value_parser(clap::value_parser!(f32)) + .default_value("0.7"), + ) + .arg( + Arg::new("mid") + .long("mid") + .help("Mid threshold (0.0 - 1.0)") + .value_parser(clap::value_parser!(f32)) + .default_value("0.9"), + ) .arg( Arg::new("mpris") .short('p') @@ -85,6 +103,22 @@ pub fn read() -> Config { .num_args(0..=1) .default_missing_value("%H:%M"), ) + .arg( + Arg::new("bar_symbol") + .short('s') + .long("symbol") + .help("Symbol to build bar") + .num_args(0..=1) + .default_value("▮"), + ) + .arg( + Arg::new("bar_empty_symbol") + .short('e') + .long("empty-symbol") + .help("Symbol to represent the empty part of the bar") + .num_args(0..=1) + .default_value("▯"), + ) .arg( Arg::new("mpd_address") .short('a') @@ -136,20 +170,55 @@ pub fn read() -> Config { ) .get_matches(); - let lt_format = cli_args.get_one::("localtime").map(|s| s.to_string()); + let lt_format = cli_args + .get_one::("localtime") + .map(|s| s.to_string()); let ut_format = cli_args.get_one::("utctime").map(|s| s.to_string()); + let bar_symbol = cli_args + .get_one::("bar_symbol") + .map(|s| s.to_string()); + let bar_empty_symbol = cli_args + .get_one::("bar_empty_symbol") + .map(|s| s.to_string()); let mut cfg = Config { action: Action::Cpu, - mpd_server: cli_args.get_one::("mpd_address").unwrap().to_string(), + mpd_server: cli_args + .get_one::("mpd_address") + .unwrap() + .to_string(), lt_format, ut_format, + bar_symbol, + bar_empty_symbol, + low_threshold: *cli_args.get_one::("low").unwrap(), + mid_threshold: *cli_args.get_one::("mid").unwrap(), color_low: colorize(cli_args.get_one::("COLOR_LOW").unwrap().to_string()), color_mid: colorize(cli_args.get_one::("COLOR_MID").unwrap().to_string()), - color_high: colorize(cli_args.get_one::("COLOR_HIGH").unwrap().to_string()), - color_track_name: colorize(cli_args.get_one::("COLOR_TRACK_NAME").unwrap().to_string()), - color_track_artist: colorize(cli_args.get_one::("COLOR_TRACK_ARTIST").unwrap().to_string()), - color_track_time: colorize(cli_args.get_one::("COLOR_TRACK_TIME").unwrap().to_string()), + color_high: colorize( + cli_args + .get_one::("COLOR_HIGH") + .unwrap() + .to_string(), + ), + color_track_name: colorize( + cli_args + .get_one::("COLOR_TRACK_NAME") + .unwrap() + .to_string(), + ), + color_track_artist: colorize( + cli_args + .get_one::("COLOR_TRACK_ARTIST") + .unwrap() + .to_string(), + ), + color_track_time: colorize( + cli_args + .get_one::("COLOR_TRACK_TIME") + .unwrap() + .to_string(), + ), color_end: colorize(cli_args.get_one::("COLOR_END").unwrap().to_string()), }; @@ -174,4 +243,3 @@ pub fn read() -> Config { cfg } - diff --git a/src/main.rs b/src/main.rs index ddc4eb6..9240678 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,4 +22,3 @@ fn main() { config::Action::Mpd => utils::mpd(&conf), } } - diff --git a/src/utils.rs b/src/utils.rs index 084b7f5..6be181e 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,13 +1,13 @@ -use dbus::arg::RefArg; use crate::config; use chrono::{Local, Utc}; +use dbus::arg::RefArg; +use dbus::blocking::stdintf::org_freedesktop_dbus::Properties; use dbus::{arg, blocking::Connection}; use mpd::Client; use size_format::SizeFormatterBinary; use std::process; use std::time::Duration; use sys_info; -use dbus::blocking::stdintf::org_freedesktop_dbus::Properties; #[derive(Debug, Clone)] pub struct TrackInfo { @@ -20,7 +20,6 @@ pub struct TrackInfo { pub fn to_bar(value: i32, max: i32, low: f32, mid: f32, config: &config::Config) { let mut bar = "".to_string(); - let bar_sym = "▮"; let ratio = (value as f32) / (max as f32); bar.push_str(if ratio < low { &config.color_low @@ -29,8 +28,10 @@ pub fn to_bar(value: i32, max: i32, low: f32, mid: f32, config: &config::Config) } else { &config.color_high }); + let symbol = config.bar_symbol.as_deref().unwrap_or("█"); + let empty = config.bar_empty_symbol.as_deref().unwrap_or(" "); for i in 0..max { - bar.push_str(if i < value { bar_sym } else { " " }); + bar.push_str(if i < value { symbol } else { empty }); } bar.push_str(&config.color_end); bar.push('|'); @@ -41,7 +42,13 @@ pub fn mem_load_bar(bar_len: i32, config: &config::Config) { let memory = sys_info::mem_info().expect("Failed to get mem_info"); let used_ratio = (memory.total - memory.avail) as f32 / memory.total as f32; let len = (used_ratio * bar_len as f32) as i32; - to_bar(len, bar_len, 0.7, 0.9, config); + to_bar( + len, + bar_len, + config.low_threshold, + config.mid_threshold, + config, + ); print!( "{}B #[default]", SizeFormatterBinary::new((memory.avail * 1024) as u64) @@ -52,7 +59,14 @@ pub fn cpu_load_bar(bar_len: i32, config: &config::Config) { let cpu_count = sys_info::cpu_num().expect("Failed to get cpu_num"); let la_one = sys_info::loadavg().expect("Failed to get loadavg").one; let len = (la_one / cpu_count as f64 * bar_len as f64).round() as i32; - to_bar(len, bar_len, 0.3, 0.7, config); + to_bar( + len, + bar_len, + config.low_threshold, + config.mid_threshold, + config, + ); + print!("{:.2} LA1#[default]", la_one); } @@ -60,7 +74,10 @@ pub fn get_player() -> Result, Box> { let conn = Connection::new_session()?; let proxy = conn.with_proxy("org.freedesktop.DBus", "/", Duration::from_secs(5)); let (names,): (Vec,) = proxy.method_call("org.freedesktop.DBus", "ListNames", ())?; - Ok(names.into_iter().filter(|n| n.contains("org.mpris.MediaPlayer2")).collect()) + Ok(names + .into_iter() + .filter(|n| n.contains("org.mpris.MediaPlayer2")) + .collect()) } pub fn player_info(players: Vec) -> Result> { @@ -68,13 +85,22 @@ pub fn player_info(players: Vec) -> Result) -> Result "▶", "Paused" => "⏸", _ => "⏹", - }.to_string(); + } + .to_string(); let track_info = TrackInfo { title, @@ -105,7 +132,11 @@ pub fn format_time(sec: i64) -> String { pub fn get_time(utc: bool, format: Option) { let fmt = format.unwrap_or_else(|| "%H:%M".to_string()); - let now = if utc { Utc::now().format(&fmt) } else { Local::now().format(&fmt) }; + let now = if utc { + Utc::now().format(&fmt) + } else { + Local::now().format(&fmt) + }; println!("{}", now); } @@ -118,7 +149,11 @@ fn shorten(line: &str, max_len: usize) -> String { } fn format_player(track_info: TrackInfo, config: &config::Config) { - let separator = if track_info.artist.is_empty() { "" } else { " — " }; + let separator = if track_info.artist.is_empty() { + "" + } else { + " — " + }; let max_len = if track_info.artist.is_empty() { 60 } else { 30 }; let artist_line = shorten(&track_info.artist, max_len); @@ -127,19 +162,31 @@ fn format_player(track_info: TrackInfo, config: &config::Config) { if track_info.position == "00:00" || track_info.duration.is_empty() { println!( "#[bold]{}{}{}{}{}{} {}{} {}#[default]", - config.color_track_name, title_line, config.color_end, + config.color_track_name, + title_line, + config.color_end, separator, - config.color_track_artist, artist_line, config.color_end, - config.color_track_time, track_info.status + config.color_track_artist, + artist_line, + config.color_end, + config.color_track_time, + track_info.status ); } else { println!( "#[bold]{}{}{}{}{}{} {}[{}/{}] {}{}{}#[default]", - config.color_track_name, title_line, config.color_end, + config.color_track_name, + title_line, + config.color_end, separator, - config.color_track_artist, artist_line, config.color_end, - config.color_track_time, track_info.position, track_info.duration, - track_info.status, config.color_end + config.color_track_artist, + artist_line, + config.color_end, + config.color_track_time, + track_info.position, + track_info.duration, + track_info.status, + config.color_end ); } } @@ -160,17 +207,17 @@ pub fn mpd(config: &config::Config) { let song = conn.currentsong().unwrap_or(None); let status = conn.status().unwrap(); - let artist = song.as_ref() + let artist = song + .as_ref() .and_then(|s| s.tags.iter().find(|(k, _)| k == "Artist").map(|(_, v)| v)) .cloned() .unwrap_or_default(); - - let title = song.as_ref() + let title = song + .as_ref() .and_then(|s| s.title.clone().or_else(|| s.name.clone())) .unwrap_or_default(); - let (position, duration) = status.time.unwrap_or_default(); let track_info = TrackInfo { @@ -182,9 +229,9 @@ pub fn mpd(config: &config::Config) { mpd::State::Play => "▶", mpd::State::Pause => "⏸", mpd::State::Stop => "⏹", - }.to_string(), + } + .to_string(), }; format_player(track_info, config); } -