mirror of
https://github.com/house-of-vanity/tmux-helper.git
synced 2026-02-04 17:57:58 +00:00
Compare commits
32 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b1e418a633 | ||
|
|
8e569078de | ||
|
|
5d23b7da2d | ||
|
|
5cf2ab0e40 | ||
|
|
7c7d9070a4 | ||
|
|
456d423f47 | ||
|
|
cd854d450a | ||
|
|
7a934a063f | ||
|
|
08cf01ee81 | ||
|
|
530a753527 | ||
|
|
614185e7f8 | ||
|
|
ddd09761f5 | ||
|
|
b264f3b823 | ||
|
|
86a95efa49 | ||
|
|
3e131088e6 | ||
|
|
f96467272b | ||
|
|
8cdaf4628b | ||
|
|
23cc78cc0a | ||
|
|
dec3226f41 | ||
|
|
e085d55dde | ||
|
|
17228e3ca8 | ||
|
|
3b3f77a0e0 | ||
|
|
f655e542fc | ||
|
|
5785b15a78 | ||
|
|
c69a95009d | ||
|
|
b95183fd94 | ||
|
|
a9216ff630 | ||
|
|
b070d50bc7 | ||
|
|
139e7c7577 | ||
|
|
23730088d5 | ||
|
|
3a04d115bf | ||
|
|
971de9c6f0 |
BIN
.github/prev.png
vendored
Normal file
BIN
.github/prev.png
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 19 KiB |
20
.github/workflows/push-build.yml
vendored
Normal file
20
.github/workflows/push-build.yml
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
name: Rust
|
||||
|
||||
on: push
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: Pre-build
|
||||
run: sudo apt install -y libdbus-1-dev pkg-config libdbus-1-3
|
||||
- name: Build
|
||||
run: cargo build --verbose --release
|
||||
env:
|
||||
RUST_BACKTRACE: 1
|
||||
- name: Strip
|
||||
run: strip target/release/tmux-helper
|
||||
|
||||
6
.github/workflows/release-upload.yml
vendored
6
.github/workflows/release-upload.yml
vendored
@@ -9,8 +9,14 @@ jobs:
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: Pre-build
|
||||
run: sudo apt install -y libdbus-1-dev pkg-config libdbus-1-3
|
||||
- name: Build
|
||||
run: cargo build --verbose --release
|
||||
env:
|
||||
RUST_BACKTRACE: 1
|
||||
- name: Strip
|
||||
run: strip target/release/tmux-helper
|
||||
- name: Upload to release
|
||||
uses: JasonEtco/upload-to-release@master
|
||||
with:
|
||||
|
||||
15
.github/workflows/rust.yml
vendored
15
.github/workflows/rust.yml
vendored
@@ -1,15 +0,0 @@
|
||||
name: Rust
|
||||
|
||||
on: [push]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: Build
|
||||
run: cargo build --verbose
|
||||
- name: Run tests
|
||||
run: cargo test --verbose
|
||||
@@ -1,8 +1,10 @@
|
||||
[package]
|
||||
name = "tmux-helper"
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
authors = ["Ultra Desu <ultradesu@hexor.ru>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
sys-info = "*"
|
||||
dbus = "*"
|
||||
chrono = "*"
|
||||
|
||||
13
LICENSE-WTFPL
Normal file
13
LICENSE-WTFPL
Normal file
@@ -0,0 +1,13 @@
|
||||
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
||||
Version 2, December 2004
|
||||
|
||||
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
|
||||
|
||||
Everyone is permitted to copy and distribute verbatim or modified
|
||||
copies of this license document, and changing it is allowed as long
|
||||
as the name is changed.
|
||||
|
||||
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||
|
||||
0. You just DO WHAT THE FUCK YOU WANT TO.
|
||||
16
README.md
Normal file
16
README.md
Normal file
@@ -0,0 +1,16 @@
|
||||
# Tmux helper
|
||||
Small app that perform system check and print TMUX friendly output.
|
||||
|
||||

|
||||
|
||||
### Features:
|
||||
1. `tmux-helper -cb` print cpu load with bar.
|
||||
2. `tmux-helper -mb` print memory usage with bar.
|
||||
3. `tmux-helper -p` show current player status using d-bus.
|
||||
|
||||
### Building
|
||||
`cargo build --release`
|
||||
or get binary on release page
|
||||
|
||||
### Customization
|
||||
Colours are hardcoded but it's easy to change it. It defined as CONST in very beginning of main.rs file.
|
||||
240
src/main.rs
240
src/main.rs
@@ -1,18 +1,29 @@
|
||||
use std::fs;
|
||||
use std::env;
|
||||
extern crate chrono;
|
||||
extern crate dbus;
|
||||
|
||||
use crate::dbus::blocking::stdintf::org_freedesktop_dbus::Properties;
|
||||
use chrono::{DateTime, Local, Utc};
|
||||
use dbus::{arg, blocking::Connection};
|
||||
use std::{env, time::Duration};
|
||||
use sys_info;
|
||||
|
||||
const LOW: &str = "#[fg=colour186]";
|
||||
const MID: &str = "#[fg=colour208]";
|
||||
const HIGH: &str = "#[fg=colour160]";
|
||||
const END: &str = "#[fg=colour7]";
|
||||
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]";
|
||||
|
||||
fn read_file(file_path: &str) -> String {
|
||||
fs::read_to_string(file_path)
|
||||
.expect("Cant read file.")
|
||||
#[derive(Debug, Clone)]
|
||||
struct TrackInfo {
|
||||
title: String,
|
||||
artist: String,
|
||||
position: String,
|
||||
duration: String,
|
||||
status: String,
|
||||
}
|
||||
|
||||
|
||||
fn to_bar(value: i32, max: i32, low: f32, mid: f32) {
|
||||
let mut bar = "".to_string();
|
||||
let bar_sym = "▮".to_string();
|
||||
@@ -26,7 +37,9 @@ fn to_bar(value: i32, max: i32, low: f32, mid: f32) {
|
||||
for i in 0..max {
|
||||
if i < value as i32 {
|
||||
bar.push_str(&bar_sym);
|
||||
} else {bar.push_str(" ")}
|
||||
} else {
|
||||
bar.push_str(" ")
|
||||
}
|
||||
}
|
||||
bar.push_str(END);
|
||||
bar.push_str("|");
|
||||
@@ -39,38 +52,205 @@ fn mem_load_bar(bar_len: i32) {
|
||||
Err(w) => panic!("{:?}", w),
|
||||
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);
|
||||
print!("{:.0} MiB", memory.avail/1024);
|
||||
print!("{:.0} MiB#[default]", memory.avail / 1024);
|
||||
}
|
||||
|
||||
fn cpu_load_bar(bar_len: i32) {
|
||||
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();
|
||||
let len: f32 = one as f32 / cpu_count as f32 * bar_len as f32;
|
||||
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;
|
||||
to_bar(len as i32, bar_len, 0.3, 0.7);
|
||||
print!("{:.2} LA1", one);
|
||||
print!("{:.2} LA1#[default]", la_one);
|
||||
}
|
||||
|
||||
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_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);
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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());
|
||||
}
|
||||
}
|
||||
Ok(players_vec[players_vec.len() - 1].clone())
|
||||
}
|
||||
|
||||
fn format_time(sec: i64) -> String {
|
||||
let minutes = sec / 60;
|
||||
let secondes = sec % 60;
|
||||
let result = format!("{:02}:{:02}", minutes, secondes);
|
||||
result.to_string()
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
let help_text: &str = "Available commands -mb, -cb";
|
||||
match args.len() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
let help_text: &str = "Available commands -mb, -cb, -tl <TIME FORMAT>, -tu <TIME FORMAT>, -p";
|
||||
match args.len() {
|
||||
1 => {
|
||||
panic!(help_text);
|
||||
}
|
||||
2 => match args[1].as_ref() {
|
||||
"-cb" => cpu_load_bar(15),
|
||||
"-mb" => mem_load_bar(15),
|
||||
"-tl" => get_time(false, ""),
|
||||
"-tu" => get_time(true, ""),
|
||||
"-p" => match player_info(get_player().unwrap()) {
|
||||
Ok(mut track_info) => {
|
||||
let mut title_len = 30;
|
||||
let mut artist_len = 30;
|
||||
let mut separator: String = " — ".to_string();
|
||||
let max_shift = 6;
|
||||
if track_info.artist.chars().count() == 0 {
|
||||
separator = "".to_string();
|
||||
title_len += artist_len;
|
||||
}
|
||||
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 {
|
||||
artist.push_str("..");
|
||||
break;
|
||||
}
|
||||
artist.push(ch);
|
||||
counter += 1;
|
||||
}
|
||||
track_info.artist = artist;
|
||||
}
|
||||
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;
|
||||
}
|
||||
println!(
|
||||
"#[none]#[bold]{}{}{}#[none]{}{}{}{} {}[{}/{}] {} {}#[default]",
|
||||
TRACK_NAME,
|
||||
track_info.title,
|
||||
END,
|
||||
separator,
|
||||
TRACK_ARTIST,
|
||||
track_info.artist,
|
||||
END,
|
||||
TRACK_TIME,
|
||||
track_info.position,
|
||||
track_info.duration,
|
||||
track_info.status,
|
||||
END,
|
||||
);
|
||||
}
|
||||
Err(_e) => println!("No music playing"),
|
||||
},
|
||||
_ => panic!(help_text),
|
||||
},
|
||||
2 => {
|
||||
match args[1].as_ref() {
|
||||
"-cb" => cpu_load_bar(15),
|
||||
"-mb" => mem_load_bar(15),
|
||||
_ => panic!(help_text),
|
||||
}
|
||||
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),
|
||||
},
|
||||
_ => {
|
||||
panic!(help_text);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user