2019-12-24 13:16:23 +03:00
|
|
|
extern crate chrono;
|
2019-11-28 17:59:24 +03:00
|
|
|
extern crate dbus;
|
2019-12-24 13:16:23 +03:00
|
|
|
|
2019-11-29 18:17:57 +03:00
|
|
|
use crate::dbus::blocking::stdintf::org_freedesktop_dbus::Properties;
|
2020-04-10 17:20:02 +03:00
|
|
|
use chrono::{DateTime, Local, Utc};
|
2019-11-29 18:17:57 +03:00
|
|
|
use dbus::{arg, blocking::Connection};
|
2020-04-11 16:35:55 +03:00
|
|
|
use std::{env, time::Duration};
|
2019-12-04 14:30:56 +03:00
|
|
|
use sys_info;
|
2019-11-21 01:14:53 +03:00
|
|
|
|
2020-04-10 17:20:02 +03:00
|
|
|
const LOW: &str = "#[fg=colour119]";
|
|
|
|
|
const MID: &str = "#[fg=colour220]";
|
|
|
|
|
const HIGH: &str = "#[fg=colour197]";
|
|
|
|
|
const END: &str = "#[fg=colour153]";
|
|
|
|
|
const TRACK_NAME: &str = "#[fg=colour46]";
|
|
|
|
|
const TRACK_ARTIST: &str = "#[fg=colour46]";
|
|
|
|
|
const TRACK_TIME: &str = "#[fg=colour153]";
|
2019-11-21 01:14:53 +03:00
|
|
|
|
2019-12-17 15:05:24 +03:00
|
|
|
#[derive(Debug, Clone)]
|
2019-11-30 14:10:05 +03:00
|
|
|
struct TrackInfo {
|
|
|
|
|
title: String,
|
|
|
|
|
artist: String,
|
|
|
|
|
position: String,
|
|
|
|
|
duration: String,
|
2019-12-03 09:33:26 +03:00
|
|
|
status: String,
|
2019-11-30 03:14:00 +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);
|
2020-04-10 17:20:02 +03:00
|
|
|
print!("{:.0} MiB#[default]", 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) {
|
2020-04-11 16:35:55 +03:00
|
|
|
let cpu_count = match sys_info::cpu_num() {
|
|
|
|
|
Ok(c) => c,
|
|
|
|
|
Err(e) => panic!("{:?}", e),
|
|
|
|
|
};
|
|
|
|
|
let la_one: f32 = match sys_info::loadavg() {
|
|
|
|
|
Ok(l) => l.one as f32,
|
|
|
|
|
Err(e) => panic!("{:?}", e),
|
|
|
|
|
};
|
|
|
|
|
let len: f32 = la_one as f32 / cpu_count as f32 * bar_len as f32;
|
2019-11-23 22:24:42 +03:00
|
|
|
to_bar(len as i32, bar_len, 0.3, 0.7);
|
2020-04-11 16:35:55 +03:00
|
|
|
print!("{:.2} LA1#[default]", la_one);
|
2019-11-21 01:14:53 +03:00
|
|
|
}
|
|
|
|
|
|
2019-12-17 15:05:24 +03:00
|
|
|
fn get_player() -> Result<Vec<String>, Box<dyn std::error::Error>> {
|
2019-11-28 17:59:24 +03:00
|
|
|
let conn = Connection::new_session()?;
|
2019-12-17 15:05:24 +03:00
|
|
|
let proxy = conn.with_proxy("org.freedesktop.DBus", "/", Duration::from_millis(5000));
|
|
|
|
|
let (names,): (Vec<String>,) = proxy.method_call("org.freedesktop.DBus", "ListNames", ())?;
|
|
|
|
|
let mut players: Vec<String> = Vec::new();
|
|
|
|
|
for name in names {
|
|
|
|
|
if name.contains("org.mpris.MediaPlayer2") {
|
|
|
|
|
players.push(name);
|
2019-11-30 14:10:05 +03:00
|
|
|
}
|
2019-12-17 15:05:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(players)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn player_info(players: Vec<String>) -> Result<TrackInfo, Box<dyn std::error::Error>> {
|
|
|
|
|
let mut players_vec: Vec<TrackInfo> = Vec::new();
|
|
|
|
|
for player in players {
|
|
|
|
|
let mut track_info = TrackInfo {
|
|
|
|
|
artist: "".to_string(),
|
|
|
|
|
title: "".to_string(),
|
|
|
|
|
position: "".to_string(),
|
|
|
|
|
duration: "".to_string(),
|
|
|
|
|
status: "".to_string(),
|
|
|
|
|
};
|
|
|
|
|
let conn = Connection::new_session()?;
|
|
|
|
|
let proxy = conn.with_proxy(
|
|
|
|
|
player,
|
|
|
|
|
"/org/mpris/MediaPlayer2",
|
|
|
|
|
Duration::from_millis(5000),
|
|
|
|
|
);
|
|
|
|
|
let metadata: Box<dyn arg::RefArg> =
|
|
|
|
|
proxy.get("org.mpris.MediaPlayer2.Player", "Metadata")?;
|
|
|
|
|
let mut iter = metadata.as_iter().unwrap();
|
|
|
|
|
while let Some(key) = iter.next() {
|
|
|
|
|
if key.as_str() == Some("xesam:title") {
|
|
|
|
|
if let Some(title) = iter.next().unwrap().as_str() {
|
|
|
|
|
track_info.title = title.to_string();
|
|
|
|
|
}
|
2019-11-30 03:14:00 +03:00
|
|
|
}
|
2019-12-17 15:05:24 +03:00
|
|
|
if key.as_str() == Some("mpris:length") {
|
|
|
|
|
if let Some(length) = iter.next().unwrap().as_i64() {
|
|
|
|
|
track_info.duration = format_time(length / 1000000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
|
track_info.artist = line.next().unwrap().as_str().unwrap().to_string();
|
|
|
|
|
}
|
2019-11-30 03:14:00 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-17 15:05:24 +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);
|
|
|
|
|
// ugly
|
|
|
|
|
let _status_text_box: Box<dyn arg::RefArg> =
|
|
|
|
|
proxy.get("org.mpris.MediaPlayer2.Player", "PlaybackStatus")?;
|
|
|
|
|
let _status_text = _status_text_box.as_str().unwrap();
|
|
|
|
|
match _status_text.as_ref() {
|
|
|
|
|
"Playing" => track_info.status = "▶".to_string(),
|
|
|
|
|
"Paused" => track_info.status = "⏸".to_string(),
|
|
|
|
|
_ => track_info.status = "⏹".to_string(),
|
|
|
|
|
};
|
|
|
|
|
players_vec.push(track_info);
|
|
|
|
|
}
|
|
|
|
|
for player in &players_vec {
|
|
|
|
|
if player.status == "▶".to_string() {
|
|
|
|
|
return Ok(player.clone());
|
|
|
|
|
}
|
2019-11-28 18:00:00 +03:00
|
|
|
}
|
2019-12-17 15:05:24 +03:00
|
|
|
Ok(players_vec[players_vec.len() - 1].clone())
|
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-12-24 13:16:23 +03:00
|
|
|
fn get_time(utc: bool, mut format: &str) {
|
|
|
|
|
// Format reference: https://docs.rs/chrono/0.4.10/chrono/format/strftime/index.html
|
|
|
|
|
if format.len() == 0 {
|
|
|
|
|
format = "%H:%M";
|
|
|
|
|
}
|
|
|
|
|
if utc {
|
|
|
|
|
let local_time = Local::now();
|
|
|
|
|
let utc_time = DateTime::<Utc>::from_utc(local_time.naive_utc(), Utc);
|
|
|
|
|
println!("{}", utc_time.format(format));
|
|
|
|
|
} else {
|
|
|
|
|
let local_time = Local::now();
|
|
|
|
|
println!("{}", local_time.format(format));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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();
|
2019-12-24 13:16:23 +03:00
|
|
|
let help_text: &str = "Available commands -mb, -cb, -tl <TIME FORMAT>, -tu <TIME FORMAT>, -p";
|
2019-11-28 18:00:00 +03:00
|
|
|
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-12-24 13:16:23 +03:00
|
|
|
"-tl" => get_time(false, ""),
|
|
|
|
|
"-tu" => get_time(true, ""),
|
2019-12-17 15:05:24 +03:00
|
|
|
"-p" => match player_info(get_player().unwrap()) {
|
2019-11-30 16:21:33 +03:00
|
|
|
Ok(mut track_info) => {
|
2019-12-19 00:02:33 +03:00
|
|
|
let mut title_len = 30;
|
|
|
|
|
let mut artist_len = 30;
|
2019-12-19 01:39:22 +03:00
|
|
|
let mut separator: String = " — ".to_string();
|
2019-12-19 00:02:33 +03:00
|
|
|
let max_shift = 6;
|
2019-12-19 01:36:57 +03:00
|
|
|
if track_info.artist.chars().count() == 0 {
|
|
|
|
|
separator = "".to_string();
|
|
|
|
|
title_len += artist_len;
|
2019-12-19 00:02:33 +03:00
|
|
|
}
|
|
|
|
|
if artist_len + max_shift >= track_info.artist.chars().count() {
|
|
|
|
|
artist_len = track_info.artist.chars().count()
|
|
|
|
|
}
|
|
|
|
|
if track_info.artist.len() > artist_len {
|
|
|
|
|
let mut artist: String = String::new();
|
|
|
|
|
let mut counter = 0;
|
|
|
|
|
for ch in track_info.artist.chars() {
|
|
|
|
|
if counter == artist_len {
|
2019-12-19 01:36:57 +03:00
|
|
|
artist.push_str("..");
|
2019-12-19 00:02:33 +03:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
artist.push(ch);
|
|
|
|
|
counter += 1;
|
|
|
|
|
}
|
|
|
|
|
track_info.artist = artist;
|
2019-11-30 16:21:33 +03:00
|
|
|
}
|
2019-12-19 01:36:57 +03:00
|
|
|
if title_len + max_shift >= track_info.title.chars().count() {
|
|
|
|
|
title_len = track_info.title.chars().count()
|
|
|
|
|
}
|
|
|
|
|
if track_info.title.len() > title_len {
|
|
|
|
|
let mut title: String = String::new();
|
|
|
|
|
let mut counter = 0;
|
|
|
|
|
for ch in track_info.title.chars() {
|
|
|
|
|
if counter == title_len {
|
|
|
|
|
title.push_str("..");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
title.push(ch);
|
|
|
|
|
counter += 1;
|
|
|
|
|
}
|
|
|
|
|
track_info.title = title;
|
|
|
|
|
}
|
2019-11-30 16:21:33 +03:00
|
|
|
println!(
|
2020-04-10 17:20:02 +03:00
|
|
|
"#[none]#[bold]{}{}{}#[none]{}{}{}{} {}[{}/{}] {} {}#[default]",
|
2019-11-30 16:21:33 +03:00
|
|
|
TRACK_NAME,
|
|
|
|
|
track_info.title,
|
|
|
|
|
END,
|
2019-12-19 00:02:33 +03:00
|
|
|
separator,
|
2019-11-30 16:21:33 +03:00
|
|
|
TRACK_ARTIST,
|
|
|
|
|
track_info.artist,
|
|
|
|
|
END,
|
|
|
|
|
TRACK_TIME,
|
|
|
|
|
track_info.position,
|
|
|
|
|
track_info.duration,
|
2019-12-03 23:53:21 +03:00
|
|
|
track_info.status,
|
2019-11-30 16:21:33 +03:00
|
|
|
END,
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-12-03 09:33:26 +03:00
|
|
|
Err(_e) => println!("No music playing"),
|
2019-11-30 14:10:05 +03:00
|
|
|
},
|
2019-11-28 18:00:00 +03:00
|
|
|
_ => panic!(help_text),
|
2019-11-21 01:14:53 +03:00
|
|
|
},
|
2019-12-24 13:16:23 +03:00
|
|
|
3 => match args[1].as_ref() {
|
|
|
|
|
"-tl" => get_time(false, args[2].as_ref()),
|
|
|
|
|
"-tu" => get_time(true, args[2].as_ref()),
|
|
|
|
|
_ => 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
|
|
|
}
|