Files
tmux-helper/src/main.rs

142 lines
4.4 KiB
Rust
Raw Normal View History

2019-11-28 17:59:24 +03:00
extern crate dbus;
2019-11-29 18:17:57 +03:00
use crate::dbus::blocking::stdintf::org_freedesktop_dbus::Properties;
use dbus::{arg, blocking::Connection};
2019-11-30 14:10:05 +03:00
use std::{env, fs, time::Duration};
2019-11-21 01:14:53 +03:00
use sys_info;
const LOW: &str = "#[fg=colour186]";
const MID: &str = "#[fg=colour208]";
const HIGH: &str = "#[fg=colour160]";
const END: &str = "#[fg=colour7]";
2019-11-30 14:10:05 +03:00
struct TrackInfo {
title: String,
artist: String,
position: String,
duration: String,
2019-11-30 03:14:00 +03:00
}
2019-11-21 01:14:53 +03:00
fn read_file(file_path: &str) -> String {
2019-11-28 18:00:00 +03:00
fs::read_to_string(file_path).expect("Cant read file.")
2019-11-21 01:14:53 +03:00
}
2019-11-23 22:24:42 +03:00
fn to_bar(value: i32, max: i32, low: f32, mid: f32) {
let mut bar = "".to_string();
let bar_sym = "".to_string();
if (value as f32) / (max as f32) < low {
bar.push_str(LOW);
} else if (value as f32) / (max as f32) < mid {
bar.push_str(MID);
} else {
bar.push_str(HIGH);
}
for i in 0..max {
if i < value as i32 {
bar.push_str(&bar_sym);
2019-11-28 18:00:00 +03:00
} else {
bar.push_str(" ")
}
2019-11-23 22:24:42 +03:00
}
bar.push_str(END);
bar.push_str("|");
print!("{}", bar)
}
fn mem_load_bar(bar_len: i32) {
2019-11-21 01:14:53 +03:00
let memory;
match sys_info::mem_info() {
Err(w) => panic!("{:?}", w),
Ok(mem_data) => memory = mem_data,
}
2019-11-28 18:00:00 +03:00
let len =
((memory.total - memory.avail) as f32 / (memory.total as f32) * bar_len as f32) as i32;
2019-11-23 22:24:42 +03:00
to_bar(len, bar_len, 0.7, 0.9);
2019-11-28 18:00:00 +03:00
print!("{:.0} MiB", memory.avail / 1024);
2019-11-21 01:14:53 +03:00
}
2019-11-23 22:24:42 +03:00
fn cpu_load_bar(bar_len: i32) {
2019-11-21 01:14:53 +03:00
let load = read_file("/proc/loadavg");
let load_data = load.split_whitespace().collect::<Vec<&str>>();
let _cpu_count = read_file("/proc/cpuinfo");
let cpu_count = _cpu_count.matches("model name").count();
let one: f32 = load_data[0].parse().unwrap();
2019-11-23 22:24:42 +03:00
let len: f32 = one as f32 / cpu_count as f32 * bar_len as f32;
to_bar(len as i32, bar_len, 0.3, 0.7);
print!("{:.2} LA1", one);
2019-11-21 01:14:53 +03:00
}
2019-11-30 14:10:05 +03:00
fn player_info(player: &str) -> Result<TrackInfo, Box<dyn std::error::Error>> {
2019-11-28 17:59:24 +03:00
let conn = Connection::new_session()?;
2019-11-30 03:14:00 +03:00
let mut service: String = "org.mpris.MediaPlayer2.".to_owned();
service.push_str(player);
2019-11-28 18:00:00 +03:00
let proxy = conn.with_proxy(
2019-11-30 03:14:00 +03:00
service,
2019-11-28 18:00:00 +03:00
"/org/mpris/MediaPlayer2",
Duration::from_millis(5000),
);
2019-11-30 03:14:00 +03:00
let metadata: Box<dyn arg::RefArg> = proxy.get("org.mpris.MediaPlayer2.Player", "Metadata")?;
2019-11-29 18:17:57 +03:00
let mut iter = metadata.as_iter().unwrap();
2019-11-30 14:10:05 +03:00
let mut track_info = TrackInfo {
artist: "".to_string(),
title: "".to_string(),
position: "".to_string(),
duration: "".to_string(),
2019-11-30 03:14:00 +03:00
};
2019-11-29 18:17:57 +03:00
while let Some(key) = iter.next() {
2019-11-30 03:14:00 +03:00
if key.as_str() == Some("xesam:title") {
if let Some(title) = iter.next().unwrap().as_str() {
2019-11-30 14:10:05 +03:00
track_info.title = title.to_string();
}
}
if key.as_str() == Some("mpris:length") {
if let Some(length) = iter.next().unwrap().as_i64() {
track_info.duration = format_time(length / 1000000);
2019-11-30 03:14:00 +03:00
}
}
if key.as_str() == Some("xesam:artist") {
if let Some(mut artists) = iter.next().unwrap().as_iter() {
while let Some(artist) = artists.next() {
if let Some(mut line) = artist.as_iter() {
2019-11-30 14:10:05 +03:00
track_info.artist = line.next().unwrap().as_str().unwrap().to_string();
2019-11-30 03:14:00 +03:00
}
}
}
}
2019-11-28 18:00:00 +03:00
}
2019-11-30 14:10:05 +03:00
let position: Box<dyn arg::RefArg> = proxy.get("org.mpris.MediaPlayer2.Player", "Position")?;
track_info.position = format_time(position.as_i64().unwrap() / 1000000);
2019-11-30 03:14:00 +03:00
Ok(track_info)
2019-11-28 17:59:24 +03:00
}
2019-11-30 14:10:05 +03:00
fn format_time(sec: i64) -> String {
let minutes = sec / 60;
let secondes = sec % 60;
let result = format!("{:02}:{:02}", minutes, secondes);
result.to_string()
}
2019-11-21 01:14:53 +03:00
fn main() {
2019-11-28 18:00:00 +03:00
let args: Vec<String> = env::args().collect();
let help_text: &str = "Available commands -mb, -cb";
match args.len() {
2019-11-21 01:14:53 +03:00
1 => {
2019-11-23 22:24:42 +03:00
panic!(help_text);
2019-11-28 18:00:00 +03:00
}
2 => match args[1].as_ref() {
"-cb" => cpu_load_bar(15),
"-mb" => mem_load_bar(15),
2019-11-30 14:10:05 +03:00
"-p" => match player_info("cmus") {
Ok(track_info) => println!(
"{} - {} [{}/{}]",
track_info.title, track_info.artist, track_info.position, track_info.duration
),
Err(_e) => panic!("Can't get mediaplayer info."),
},
2019-11-28 18:00:00 +03:00
_ => panic!(help_text),
2019-11-21 01:14:53 +03:00
},
_ => {
2019-11-23 22:24:42 +03:00
panic!(help_text);
2019-11-21 01:14:53 +03:00
}
2019-11-28 18:00:00 +03:00
}
2019-11-21 01:14:53 +03:00
}