mirror of
https://github.com/starship/starship.git
synced 2026-06-23 02:05:51 +07:00
feat: Add battery module(#63)
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
extern crate clap;
|
||||
|
||||
extern crate ansi_term;
|
||||
extern crate battery;
|
||||
extern crate dirs;
|
||||
extern crate git2;
|
||||
extern crate pretty_env_logger;
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
use ansi_term::Color;
|
||||
|
||||
use super::{Context, Module};
|
||||
|
||||
/// Creates a segment for the battery percentage and charging state
|
||||
pub fn segment(_context: &Context) -> Option<Module> {
|
||||
const BATTERY_FULL: &str = "•";
|
||||
const BATTERY_CHARGING: &str = "⇡";
|
||||
const BATTERY_DISCHARGING: &str = "⇣";
|
||||
const BATTERY_THRESHOLD: f32 = 10.0;
|
||||
|
||||
let battery_status = get_battery_status()?;
|
||||
let BatteryStatus { state, percentage } = battery_status;
|
||||
|
||||
if percentage > BATTERY_THRESHOLD {
|
||||
log::debug!(
|
||||
"Battery percentage is higher than threshold ({} > {})",
|
||||
percentage,
|
||||
BATTERY_THRESHOLD
|
||||
);
|
||||
return None;
|
||||
}
|
||||
|
||||
// TODO: Set style based on percentage when threshold is modifiable
|
||||
let mut module = Module::new("battery");
|
||||
module.set_style(Color::Red.bold());
|
||||
module.get_prefix().set_value("");
|
||||
|
||||
match state {
|
||||
battery::State::Full => {
|
||||
module.new_segment("full", BATTERY_FULL);
|
||||
}
|
||||
battery::State::Charging => {
|
||||
module.new_segment("charging", BATTERY_CHARGING);
|
||||
}
|
||||
battery::State::Discharging => {
|
||||
module.new_segment("discharging", BATTERY_DISCHARGING);
|
||||
}
|
||||
_ => return None,
|
||||
}
|
||||
|
||||
let mut percent_string = Vec::<String>::with_capacity(2);
|
||||
// Round the percentage to a whole number
|
||||
percent_string.push(percentage.round().to_string());
|
||||
percent_string.push("%".to_string());
|
||||
module.new_segment("percentage", percent_string.join(""));
|
||||
|
||||
Some(module)
|
||||
}
|
||||
|
||||
fn get_battery_status() -> Option<BatteryStatus> {
|
||||
let battery_manager = battery::Manager::new().ok()?;
|
||||
match battery_manager.batteries().ok()?.next() {
|
||||
Some(Ok(battery)) => {
|
||||
log::debug!("Battery found: {:?}", battery);
|
||||
let battery_status = BatteryStatus {
|
||||
percentage: battery.state_of_charge().value * 100.0,
|
||||
state: battery.state(),
|
||||
};
|
||||
|
||||
Some(battery_status)
|
||||
}
|
||||
Some(Err(e)) => {
|
||||
log::debug!("Unable to access battery information:\n{}", e);
|
||||
None
|
||||
}
|
||||
None => {
|
||||
log::debug!("No batteries found");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct BatteryStatus {
|
||||
percentage: f32,
|
||||
state: battery::State,
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
mod battery;
|
||||
mod character;
|
||||
mod directory;
|
||||
mod git_branch;
|
||||
@@ -26,6 +27,7 @@ pub fn handle(module: &str, context: &Context) -> Option<Module> {
|
||||
"git_branch" => git_branch::segment(context),
|
||||
"git_status" => git_status::segment(context),
|
||||
"username" => username::segment(context),
|
||||
"battery" => battery::segment(context),
|
||||
|
||||
_ => panic!("Unknown module: {}", module),
|
||||
}
|
||||
|
||||
+1
-4
@@ -8,6 +8,7 @@ use crate::modules;
|
||||
|
||||
pub fn prompt(args: ArgMatches) {
|
||||
let prompt_order = vec![
|
||||
"battery",
|
||||
"username",
|
||||
"directory",
|
||||
"git_branch",
|
||||
@@ -22,10 +23,6 @@ pub fn prompt(args: ArgMatches) {
|
||||
];
|
||||
let context = Context::new(args);
|
||||
|
||||
// TODO:
|
||||
// - List files in directory
|
||||
// - Index binaries in PATH
|
||||
|
||||
let stdout = io::stdout();
|
||||
let mut handle = stdout.lock();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user