Add command to format and print battery level
This commit is contained in:
26
src/battery.rs
Normal file
26
src/battery.rs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
use std::{fs, path::Path};
|
||||||
|
|
||||||
|
pub struct BatteryInfo {
|
||||||
|
pub level: u32,
|
||||||
|
pub status: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_info() -> Option<BatteryInfo> {
|
||||||
|
let path = Path::new("/sys/class/power_supply/BAT0");
|
||||||
|
let level_path = path.join("capacity");
|
||||||
|
let status_path = path.join("status");
|
||||||
|
|
||||||
|
let level = fs::read_to_string(level_path).ok()?;
|
||||||
|
let level: u32 = level.trim().parse().ok()?;
|
||||||
|
|
||||||
|
let status = fs::read_to_string(status_path).ok()?;
|
||||||
|
let status = status.trim().to_string();
|
||||||
|
|
||||||
|
Some(BatteryInfo { level, status })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn print_info() {
|
||||||
|
if let Some(battery) = get_info() {
|
||||||
|
println!("{}% - {}", battery.level, battery.status);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
use crate::{eww, output, pulse, util::CommandExt, Command};
|
use crate::{eww, output, util::CommandExt, WmCommand};
|
||||||
use eyre::{bail, Context};
|
use eyre::{bail, Context};
|
||||||
use serde::{de::DeserializeOwned, Deserialize};
|
use serde::{de::DeserializeOwned, Deserialize};
|
||||||
use std::str;
|
use std::str;
|
||||||
@ -13,12 +13,12 @@ struct HyprWorkspace {
|
|||||||
// id, windows, monitor, hasfullscreen, lastwindow, lastwindowtitle
|
// id, windows, monitor, hasfullscreen, lastwindow, lastwindowtitle
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle(command: Command) -> eyre::Result<()> {
|
pub fn handle(command: WmCommand) -> eyre::Result<()> {
|
||||||
match command {
|
match command {
|
||||||
Command::Workspaces {} => {
|
WmCommand::Workspaces {} => {
|
||||||
println!("{}", get_workspaces()?);
|
println!("{}", get_workspaces()?);
|
||||||
}
|
}
|
||||||
Command::SwitchWorkspace { to } => {
|
WmCommand::SwitchWorkspace { to } => {
|
||||||
std::process::Command::new("hyprctl")
|
std::process::Command::new("hyprctl")
|
||||||
.args(["dispatch", "workspace"])
|
.args(["dispatch", "workspace"])
|
||||||
.arg(format!("{to}"))
|
.arg(format!("{to}"))
|
||||||
@ -26,12 +26,9 @@ pub fn handle(command: Command) -> eyre::Result<()> {
|
|||||||
|
|
||||||
eww::update_var("workspaces", &get_workspaces()?)?;
|
eww::update_var("workspaces", &get_workspaces()?)?;
|
||||||
}
|
}
|
||||||
Command::KeyboardLayout { .. } => {
|
WmCommand::KeyboardLayout { .. } => {
|
||||||
bail!("not supported on Hyprland");
|
bail!("not supported on Hyprland");
|
||||||
}
|
}
|
||||||
Command::Sinks => {
|
|
||||||
println!("{}", serde_json::to_string(&pulse::get_sinks()?)?);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
29
src/main.rs
29
src/main.rs
@ -3,6 +3,7 @@ use once_cell::sync::Lazy;
|
|||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
|
mod battery;
|
||||||
mod eww;
|
mod eww;
|
||||||
mod hyprland;
|
mod hyprland;
|
||||||
mod niri;
|
mod niri;
|
||||||
@ -17,7 +18,7 @@ struct Opt {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Subcommand)]
|
#[derive(Clone, Subcommand)]
|
||||||
enum Command {
|
enum WmCommand {
|
||||||
/// Get the list of workspaces
|
/// Get the list of workspaces
|
||||||
Workspaces {},
|
Workspaces {},
|
||||||
|
|
||||||
@ -30,9 +31,19 @@ enum Command {
|
|||||||
SwitchWorkspace {
|
SwitchWorkspace {
|
||||||
to: u8,
|
to: u8,
|
||||||
},
|
},
|
||||||
|
}
|
||||||
|
|
||||||
/// Get the list out audio sinks
|
#[derive(Clone, Subcommand)]
|
||||||
|
enum Command {
|
||||||
|
/// Commands that need to be handled differently on a per-wm basis
|
||||||
|
#[clap(flatten)]
|
||||||
|
Wm(WmCommand),
|
||||||
|
|
||||||
|
/// Get the list of audio sinks
|
||||||
Sinks,
|
Sinks,
|
||||||
|
|
||||||
|
/// Print battery information
|
||||||
|
Battery,
|
||||||
}
|
}
|
||||||
|
|
||||||
enum WindowManager {
|
enum WindowManager {
|
||||||
@ -64,10 +75,16 @@ fn main() -> eyre::Result<()> {
|
|||||||
let opt = Opt::parse();
|
let opt = Opt::parse();
|
||||||
color_eyre::install()?;
|
color_eyre::install()?;
|
||||||
|
|
||||||
match &*WM {
|
match opt.command {
|
||||||
WindowManager::Niri => niri::handle(opt.command)?,
|
Command::Wm(wm_command) => match &*WM {
|
||||||
WindowManager::Hyprland => hyprland::handle(opt.command)?,
|
WindowManager::Niri => niri::handle(wm_command)?,
|
||||||
_ => eyre::bail!("Window manager not supported"),
|
WindowManager::Hyprland => hyprland::handle(wm_command)?,
|
||||||
|
_ => eyre::bail!("Window manager not supported"),
|
||||||
|
},
|
||||||
|
Command::Sinks => {
|
||||||
|
println!("{}", serde_json::to_string(&pulse::get_sinks()?)?);
|
||||||
|
}
|
||||||
|
Command::Battery => battery::print_info(),
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
13
src/niri.rs
13
src/niri.rs
@ -1,16 +1,16 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::{eww, pulse, Command, Workspace};
|
use crate::{eww, WmCommand, Workspace};
|
||||||
use eyre::{bail, eyre, Context};
|
use eyre::{bail, eyre, Context};
|
||||||
use niri_ipc::{Socket, WorkspaceReferenceArg};
|
use niri_ipc::{Socket, WorkspaceReferenceArg};
|
||||||
|
|
||||||
pub fn handle(command: Command) -> eyre::Result<()> {
|
pub fn handle(command: WmCommand) -> eyre::Result<()> {
|
||||||
match command {
|
match command {
|
||||||
Command::Workspaces {} => {
|
WmCommand::Workspaces {} => {
|
||||||
println!("{}", get_workspaces()?);
|
println!("{}", get_workspaces()?);
|
||||||
}
|
}
|
||||||
|
|
||||||
Command::SwitchWorkspace { to } => {
|
WmCommand::SwitchWorkspace { to } => {
|
||||||
let _reply = open_socket()?
|
let _reply = open_socket()?
|
||||||
.send(niri_ipc::Request::Action(
|
.send(niri_ipc::Request::Action(
|
||||||
niri_ipc::Action::FocusWorkspace {
|
niri_ipc::Action::FocusWorkspace {
|
||||||
@ -21,10 +21,7 @@ pub fn handle(command: Command) -> eyre::Result<()> {
|
|||||||
|
|
||||||
eww::update_var("workspaces", &get_workspaces()?)?;
|
eww::update_var("workspaces", &get_workspaces()?)?;
|
||||||
}
|
}
|
||||||
Command::KeyboardLayout { next: _ } => bail!("Not implemented"),
|
WmCommand::KeyboardLayout { next: _ } => bail!("Not implemented"),
|
||||||
Command::Sinks => {
|
|
||||||
println!("{}", serde_json::to_string(&pulse::get_sinks()?)?);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Reference in New Issue
Block a user