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 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(())
|
||||
|
||||
27
src/main.rs
27
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)?,
|
||||
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(())
|
||||
|
||||
13
src/niri.rs
13
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(())
|
||||
|
||||
Reference in New Issue
Block a user