diff --git a/src/battery.rs b/src/battery.rs new file mode 100644 index 0000000..ac32bca --- /dev/null +++ b/src/battery.rs @@ -0,0 +1,26 @@ +use std::{fs, path::Path}; + +pub struct BatteryInfo { + pub level: u32, + pub status: String, +} + +pub fn get_info() -> Option { + 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); + } +} diff --git a/src/hyprland.rs b/src/hyprland.rs index 5f31788..59ea16d 100644 --- a/src/hyprland.rs +++ b/src/hyprland.rs @@ -1,4 +1,4 @@ -use crate::{eww, output, pulse, util::CommandExt, Command}; +use crate::{eww, output, util::CommandExt, WmCommand}; use eyre::{bail, Context}; use serde::{de::DeserializeOwned, Deserialize}; use std::str; @@ -13,12 +13,12 @@ struct HyprWorkspace { // id, windows, monitor, hasfullscreen, lastwindow, lastwindowtitle } -pub fn handle(command: Command) -> eyre::Result<()> { +pub fn handle(command: WmCommand) -> eyre::Result<()> { match command { - Command::Workspaces {} => { + WmCommand::Workspaces {} => { println!("{}", get_workspaces()?); } - Command::SwitchWorkspace { to } => { + WmCommand::SwitchWorkspace { to } => { std::process::Command::new("hyprctl") .args(["dispatch", "workspace"]) .arg(format!("{to}")) @@ -26,12 +26,9 @@ pub fn handle(command: Command) -> eyre::Result<()> { eww::update_var("workspaces", &get_workspaces()?)?; } - Command::KeyboardLayout { .. } => { + WmCommand::KeyboardLayout { .. } => { bail!("not supported on Hyprland"); } - Command::Sinks => { - println!("{}", serde_json::to_string(&pulse::get_sinks()?)?); - } } Ok(()) diff --git a/src/main.rs b/src/main.rs index f82743f..194c476 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ use once_cell::sync::Lazy; use serde::Serialize; use std::env; +mod battery; mod eww; mod hyprland; mod niri; @@ -17,7 +18,7 @@ struct Opt { } #[derive(Clone, Subcommand)] -enum Command { +enum WmCommand { /// Get the list of workspaces Workspaces {}, @@ -30,9 +31,19 @@ enum Command { SwitchWorkspace { 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, + + /// Print battery information + Battery, } enum WindowManager { @@ -64,10 +75,16 @@ fn main() -> eyre::Result<()> { let opt = Opt::parse(); color_eyre::install()?; - match &*WM { - WindowManager::Niri => niri::handle(opt.command)?, - WindowManager::Hyprland => hyprland::handle(opt.command)?, - _ => eyre::bail!("Window manager not supported"), + match opt.command { + Command::Wm(wm_command) => match &*WM { + WindowManager::Niri => niri::handle(wm_command)?, + 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(()) diff --git a/src/niri.rs b/src/niri.rs index 0f8d858..bceb8cb 100644 --- a/src/niri.rs +++ b/src/niri.rs @@ -1,16 +1,16 @@ use std::collections::HashMap; -use crate::{eww, pulse, Command, Workspace}; +use crate::{eww, WmCommand, Workspace}; use eyre::{bail, eyre, Context}; use niri_ipc::{Socket, WorkspaceReferenceArg}; -pub fn handle(command: Command) -> eyre::Result<()> { +pub fn handle(command: WmCommand) -> eyre::Result<()> { match command { - Command::Workspaces {} => { + WmCommand::Workspaces {} => { println!("{}", get_workspaces()?); } - Command::SwitchWorkspace { to } => { + WmCommand::SwitchWorkspace { to } => { let _reply = open_socket()? .send(niri_ipc::Request::Action( niri_ipc::Action::FocusWorkspace { @@ -21,10 +21,7 @@ pub fn handle(command: Command) -> eyre::Result<()> { eww::update_var("workspaces", &get_workspaces()?)?; } - Command::KeyboardLayout { next: _ } => bail!("Not implemented"), - Command::Sinks => { - println!("{}", serde_json::to_string(&pulse::get_sinks()?)?); - } + WmCommand::KeyboardLayout { next: _ } => bail!("Not implemented"), } Ok(())