This commit is contained in:
Alexandr Bogomyakov
2019-11-28 18:00:00 +03:00
parent 23730088d5
commit 139e7c7577

View File

@@ -1,9 +1,9 @@
extern crate dbus; extern crate dbus;
use std::fs;
use std::env;
use std::time::Duration;
use dbus::blocking::Connection; use dbus::blocking::Connection;
use std::env;
use std::fs;
use std::time::Duration;
use sys_info; use sys_info;
const LOW: &str = "#[fg=colour186]"; const LOW: &str = "#[fg=colour186]";
@@ -12,11 +12,9 @@ const HIGH: &str = "#[fg=colour160]";
const END: &str = "#[fg=colour7]"; const END: &str = "#[fg=colour7]";
fn read_file(file_path: &str) -> String { fn read_file(file_path: &str) -> String {
fs::read_to_string(file_path) fs::read_to_string(file_path).expect("Cant read file.")
.expect("Cant read file.")
} }
fn to_bar(value: i32, max: i32, low: f32, mid: f32) { fn to_bar(value: i32, max: i32, low: f32, mid: f32) {
let mut bar = "".to_string(); let mut bar = "".to_string();
let bar_sym = "".to_string(); let bar_sym = "".to_string();
@@ -30,7 +28,9 @@ fn to_bar(value: i32, max: i32, low: f32, mid: f32) {
for i in 0..max { for i in 0..max {
if i < value as i32 { if i < value as i32 {
bar.push_str(&bar_sym); bar.push_str(&bar_sym);
} else {bar.push_str(" ")} } else {
bar.push_str(" ")
}
} }
bar.push_str(END); bar.push_str(END);
bar.push_str("|"); bar.push_str("|");
@@ -43,9 +43,10 @@ fn mem_load_bar(bar_len: i32) {
Err(w) => panic!("{:?}", w), Err(w) => panic!("{:?}", w),
Ok(mem_data) => memory = mem_data, Ok(mem_data) => memory = mem_data,
} }
let len = ((memory.total - memory.avail) as f32 / (memory.total as f32) * bar_len as f32) as i32; let len =
((memory.total - memory.avail) as f32 / (memory.total as f32) * bar_len as f32) as i32;
to_bar(len, bar_len, 0.7, 0.9); to_bar(len, bar_len, 0.7, 0.9);
print!("{:.0} MiB", memory.avail/1024); print!("{:.0} MiB", memory.avail / 1024);
} }
fn cpu_load_bar(bar_len: i32) { fn cpu_load_bar(bar_len: i32) {
@@ -65,36 +66,43 @@ fn player_info() -> Result<(), Box<dyn std::error::Error>> {
// Second, create a wrapper struct around the connection that makes it easy // Second, create a wrapper struct around the connection that makes it easy
// to send method calls to a specific destination and path. // to send method calls to a specific destination and path.
let proxy = conn.with_proxy("org.mpris.MediaPlayer2.cmus", "/org/mpris/MediaPlayer2", Duration::from_millis(5000)); let proxy = conn.with_proxy(
"org.mpris.MediaPlayer2.cmus",
"/org/mpris/MediaPlayer2",
Duration::from_millis(5000),
);
// Now make the method call. The ListNames method call takes zero input parameters and // Now make the method call. The ListNames method call takes zero input parameters and
// one output parameter which is an array of strings. // one output parameter which is an array of strings.
// Therefore the input is a zero tuple "()", and the output is a single tuple "(names,)". // Therefore the input is a zero tuple "()", and the output is a single tuple "(names,)".
let (names,): (Vec<String>,) = proxy.method_call("org.mpris.MediaPlayer2.cmus", "Metadata", ())?; let (names,): (Vec<String>,) =
proxy.method_call("org.mpris.MediaPlayer2.cmus", "Metadata", ())?;
// Let's print all the names to stdout. // Let's print all the names to stdout.
for name in names { println!("{}", name); } for name in names {
println!("{}", name);
}
Ok(()) Ok(())
} }
fn main() { fn main() {
let args: Vec<String> = env::args().collect(); let args: Vec<String> = env::args().collect();
let help_text: &str = "Available commands -mb, -cb"; let help_text: &str = "Available commands -mb, -cb";
match args.len() { match args.len() {
1 => { 1 => {
panic!(help_text); panic!(help_text);
}, }
2 => { 2 => match args[1].as_ref() {
match args[1].as_ref() { "-cb" => cpu_load_bar(15),
"-cb" => cpu_load_bar(15), "-mb" => mem_load_bar(15),
"-mb" => mem_load_bar(15), "-p" => {
"-p" => { let x = player_info();}, let x = player_info();
_ => panic!(help_text),
} }
_ => panic!(help_text),
}, },
_ => { _ => {
panic!(help_text); panic!(help_text);
} }
} }
} }