2 Commits

Author SHA1 Message Date
Ultradesu
eb3577979c Improved styling 2025-04-07 19:08:24 +01:00
Ultradesu
c082c328f5 Improved styling 2025-04-07 19:07:57 +01:00
4 changed files with 157 additions and 43 deletions

View File

@@ -1,14 +1,14 @@
[package]
name = "tmux-helper"
version = "0.3.5"
version = "0.4.0"
description = "Utility for printing system info for tmux status line."
authors = ["Ultra Desu <ultradesu@hexor.ru>"]
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"

View File

@@ -22,6 +22,10 @@ pub struct Config {
pub mpd_server: String,
pub lt_format: Option<String>,
pub ut_format: Option<String>,
pub bar_symbol: Option<String>,
pub bar_empty_symbol: Option<String>,
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::<String>("localtime").map(|s| s.to_string());
let lt_format = cli_args
.get_one::<String>("localtime")
.map(|s| s.to_string());
let ut_format = cli_args.get_one::<String>("utctime").map(|s| s.to_string());
let bar_symbol = cli_args
.get_one::<String>("bar_symbol")
.map(|s| s.to_string());
let bar_empty_symbol = cli_args
.get_one::<String>("bar_empty_symbol")
.map(|s| s.to_string());
let mut cfg = Config {
action: Action::Cpu,
mpd_server: cli_args.get_one::<String>("mpd_address").unwrap().to_string(),
mpd_server: cli_args
.get_one::<String>("mpd_address")
.unwrap()
.to_string(),
lt_format,
ut_format,
bar_symbol,
bar_empty_symbol,
low_threshold: *cli_args.get_one::<f32>("low").unwrap(),
mid_threshold: *cli_args.get_one::<f32>("mid").unwrap(),
color_low: colorize(cli_args.get_one::<String>("COLOR_LOW").unwrap().to_string()),
color_mid: colorize(cli_args.get_one::<String>("COLOR_MID").unwrap().to_string()),
color_high: colorize(cli_args.get_one::<String>("COLOR_HIGH").unwrap().to_string()),
color_track_name: colorize(cli_args.get_one::<String>("COLOR_TRACK_NAME").unwrap().to_string()),
color_track_artist: colorize(cli_args.get_one::<String>("COLOR_TRACK_ARTIST").unwrap().to_string()),
color_track_time: colorize(cli_args.get_one::<String>("COLOR_TRACK_TIME").unwrap().to_string()),
color_high: colorize(
cli_args
.get_one::<String>("COLOR_HIGH")
.unwrap()
.to_string(),
),
color_track_name: colorize(
cli_args
.get_one::<String>("COLOR_TRACK_NAME")
.unwrap()
.to_string(),
),
color_track_artist: colorize(
cli_args
.get_one::<String>("COLOR_TRACK_ARTIST")
.unwrap()
.to_string(),
),
color_track_time: colorize(
cli_args
.get_one::<String>("COLOR_TRACK_TIME")
.unwrap()
.to_string(),
),
color_end: colorize(cli_args.get_one::<String>("COLOR_END").unwrap().to_string()),
};
@@ -174,4 +243,3 @@ pub fn read() -> Config {
cfg
}

View File

@@ -22,4 +22,3 @@ fn main() {
config::Action::Mpd => utils::mpd(&conf),
}
}

View File

@@ -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<Vec<String>, Box<dyn std::error::Error>> {
let conn = Connection::new_session()?;
let proxy = conn.with_proxy("org.freedesktop.DBus", "/", Duration::from_secs(5));
let (names,): (Vec<String>,) = 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<String>) -> Result<TrackInfo, Box<dyn std::error::Error>> {
@@ -69,12 +86,21 @@ pub fn player_info(players: Vec<String>) -> Result<TrackInfo, Box<dyn std::error
let proxy = conn.with_proxy(player, "/org/mpris/MediaPlayer2", Duration::from_secs(5));
let metadata: arg::PropMap = proxy.get("org.mpris.MediaPlayer2.Player", "Metadata")?;
let title = metadata.get("xesam:title").and_then(|v| v.as_str()).unwrap_or("").to_string();
let artist = metadata.get("xesam:artist")
let title = metadata
.get("xesam:title")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string();
let artist = metadata
.get("xesam:artist")
.and_then(|v| v.as_iter())
.and_then(|mut artists| artists.next().and_then(|a| a.as_str()))
.unwrap_or("").to_string();
let duration_us = metadata.get("mpris:length").and_then(|v| v.as_i64()).unwrap_or(0);
.unwrap_or("")
.to_string();
let duration_us = metadata
.get("mpris:length")
.and_then(|v| v.as_i64())
.unwrap_or(0);
let position_us: i64 = proxy.get("org.mpris.MediaPlayer2.Player", "Position")?;
let status_text: String = proxy.get("org.mpris.MediaPlayer2.Player", "PlaybackStatus")?;
@@ -82,7 +108,8 @@ pub fn player_info(players: Vec<String>) -> Result<TrackInfo, Box<dyn std::error
"Playing" => "",
"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<String>) {
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);
}