Skip to content

Commit

Permalink
Print battery level
Browse files Browse the repository at this point in the history
This will print AC if on external power supply.
The coloring can be simplified as soon as range matches are stabilized: rust-lang/rust#37854
  • Loading branch information
klingtnet committed Dec 27, 2018
1 parent d3442f9 commit 53d9fbc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
15 changes: 15 additions & 0 deletions tools/rusty-prompt/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tools/rusty-prompt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ git2 = "0.7"
libc = "0.2"
humantime = "1.1"
dirs = "1"
systemstat = "0.1"
20 changes: 20 additions & 0 deletions tools/rusty-prompt/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ extern crate hostname;
extern crate humantime;
extern crate libc;
extern crate dirs;
extern crate systemstat;

use colored::*;
use git2::{DescribeOptions, Repository, RepositoryState};
use hostname::get_hostname;
use std::env;
use std::ffi::CStr;
use std::time::Duration;
use systemstat::{Platform, System};

fn cmd_duration() -> Option<String> {
let start = env::var("KN_CMD_START_TIME_NS")
Expand Down Expand Up @@ -140,6 +142,24 @@ fn main() {
if let Some(venv_path) = virtual_env() {
prompt.push(format!("({})", tilde_home(venv_path).blue()));
}

let sys = System::new();
if let Ok(battery) = sys.battery_life() {
let cap = (battery.remaining_capacity * 100.0) as u64;
let colored_cap = if cap < 20 {
cap.to_string().red()
} else if cap > 20 && cap < 50 {
cap.to_string().yellow()
} else {
cap.to_string().green()
};
prompt.push(format!("bat: {}", colored_cap));
}
if let Ok(power) = sys.on_ac_power() {
if power {
prompt.push(format!("bat: {}", "AC".blue()));
}
}
if let Some(dur) = cmd_duration() {
prompt.push(format!("{}", dur))
}
Expand Down

0 comments on commit 53d9fbc

Please sign in to comment.