Skip to content

Commit 53d9fbc

Browse files
committed
Print battery level
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
1 parent d3442f9 commit 53d9fbc

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

tools/rusty-prompt/Cargo.lock

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/rusty-prompt/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ git2 = "0.7"
1010
libc = "0.2"
1111
humantime = "1.1"
1212
dirs = "1"
13+
systemstat = "0.1"

tools/rusty-prompt/src/main.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ extern crate hostname;
44
extern crate humantime;
55
extern crate libc;
66
extern crate dirs;
7+
extern crate systemstat;
78

89
use colored::*;
910
use git2::{DescribeOptions, Repository, RepositoryState};
1011
use hostname::get_hostname;
1112
use std::env;
1213
use std::ffi::CStr;
1314
use std::time::Duration;
15+
use systemstat::{Platform, System};
1416

1517
fn cmd_duration() -> Option<String> {
1618
let start = env::var("KN_CMD_START_TIME_NS")
@@ -140,6 +142,24 @@ fn main() {
140142
if let Some(venv_path) = virtual_env() {
141143
prompt.push(format!("({})", tilde_home(venv_path).blue()));
142144
}
145+
146+
let sys = System::new();
147+
if let Ok(battery) = sys.battery_life() {
148+
let cap = (battery.remaining_capacity * 100.0) as u64;
149+
let colored_cap = if cap < 20 {
150+
cap.to_string().red()
151+
} else if cap > 20 && cap < 50 {
152+
cap.to_string().yellow()
153+
} else {
154+
cap.to_string().green()
155+
};
156+
prompt.push(format!("bat: {}", colored_cap));
157+
}
158+
if let Ok(power) = sys.on_ac_power() {
159+
if power {
160+
prompt.push(format!("bat: {}", "AC".blue()));
161+
}
162+
}
143163
if let Some(dur) = cmd_duration() {
144164
prompt.push(format!("{}", dur))
145165
}

0 commit comments

Comments
 (0)