From 9aec63d7ee0a954a9262a350801759d82f9686a6 Mon Sep 17 00:00:00 2001 From: r0r-5chach Date: Sun, 21 Jul 2024 23:26:08 +0100 Subject: [PATCH] cli --- src/config.rs | 0 src/lib.rs | 25 ++++++++++++++++ src/main.rs | 3 +- src/utils.rs | 15 ++++++++++ src/utils/color.rs | 1 - src/utils/volume.rs | 66 ++++++++++++++++++++++++++++++++++++------ src/utils/workspace.rs | 38 ++++++++++++++++++++---- 7 files changed, 131 insertions(+), 17 deletions(-) delete mode 100644 src/config.rs diff --git a/src/config.rs b/src/config.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/lib.rs b/src/lib.rs index b5614dd..09d6825 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,26 @@ +use clap::Parser; +use utils::{color::color_picker, Util, volume, workspace}; + pub mod utils; + +pub fn run() { + let args = Args::parse(); + + match args.util { + Util::Color => color_picker(), + Util::Volume(command) => volume::parse_command(command), + Util::Workspaces(command) => workspace::parse_command(command), + }; +} + +#[derive(Parser)] +#[command( + name = "yucky-utils", + author = "r0r-5chach", + version = "1.0", + about = "", //TODO: Command about + long_about = None)] +struct Args { + #[command(subcommand)] + util: Util, +} diff --git a/src/main.rs b/src/main.rs index f79c691..e7ad5d1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,2 +1,3 @@ fn main() { -} + yucky_utils::run(); +} //TODO: Handle unwraps diff --git a/src/utils.rs b/src/utils.rs index e764a91..a1e7ad7 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,3 +1,5 @@ +use clap::{Subcommand, ValueEnum}; + pub mod color; pub mod volume; pub mod workspace; @@ -11,8 +13,21 @@ pub fn update_eww_var(key: &str, value: &str) { .spawn(); } +#[derive(Clone, ValueEnum)] pub enum Direction { Up, Down, Absolute, } + +#[derive(Subcommand)] +pub enum Util { + /// Color Picking Utility + Color, + /// Volume Management Utility + #[command(subcommand)] + Volume(volume::Command), + /// Workspace Management Utility + #[command(subcommand)] + Workspaces(workspace::Command), +} diff --git a/src/utils/color.rs b/src/utils/color.rs index c33b335..9694649 100644 --- a/src/utils/color.rs +++ b/src/utils/color.rs @@ -1,6 +1,5 @@ use std::process::Command; - pub fn color_picker() { use super::update_eww_var; diff --git a/src/utils/volume.rs b/src/utils/volume.rs index acbf77c..ecf5928 100644 --- a/src/utils/volume.rs +++ b/src/utils/volume.rs @@ -1,11 +1,44 @@ -use std::process::Command; +use core::fmt; +use std::fmt::{Display, Result}; + +use clap::Subcommand; use super::Direction; -pub fn step(directon: Direction) { //onscroll +#[derive(Subcommand)] +#[command(about = "", long_about = None)] //Volume command usage +pub enum Command { + /// Adjust volume Up/Down by 5% + Step { + /// The direction to adjust the volume + #[arg(value_enum)] + directon: Direction, + }, + /// Adjust the volume to 0% or last volume + ToggleMute { + /// The value to set the volume to + #[arg()] + volume: u8, + }, + /// Get the name of the current icon + CurrentIcon, + /// Get the current volume percent + CurrentValue, +} + +pub fn parse_command(command: Command) { + match command { + Command::CurrentValue => println!("{}", get_current_value()), + Command::CurrentIcon => println!("{}", get_current_icon()), + Command::Step { directon } => step(directon), + Command::ToggleMute { volume } => toggle_mute(volume), + } +} + +fn step(directon: Direction) { //onscroll set(5, directon); } -pub fn toggle_mute(saved_volume: u8) { //onclick +fn toggle_mute(saved_volume: u8) { //onclick use super::update_eww_var; let volume = get_current_value(); @@ -15,7 +48,7 @@ pub fn toggle_mute(saved_volume: u8) { //onclick std::process::exit(0); } -pub fn get_current_icon() -> Level { //for path var in image (add to defpoll) +fn get_current_icon() -> Level { //for path var in image (add to defpoll) match get_current_value() { value if value <= 0 => Level::Mute, value if value <= 25 => Level::Low, @@ -24,19 +57,25 @@ pub fn get_current_icon() -> Level { //for path var in image (add to defpoll) } } -pub fn get_current_value() -> u8 { //for value in circular-progrss (add to defpoll) +fn get_current_value() -> u8 { //for value in circular-progrss (add to defpoll) + use std::process::Command; + let value = Command::new("amixer") .arg("sget").arg("Master") .output().unwrap().stdout; let value = String::from_utf8(value).unwrap(); let value: Vec<&str> = value.split("[").collect(); - - value[3].trim().get(0..value.len()-3).unwrap().parse().unwrap() + let value = value[3].trim(); + let value = value.get(0..value.len() -2).unwrap().replace("%", ""); + + value.parse().unwrap() } fn set(value: u8, directon: Direction) { + use std::process::Command; + let mut value = value.to_string(); value.push('%'); @@ -46,15 +85,24 @@ fn set(value: u8, directon: Direction) { Direction::Absolute => (), } - Command::new("amixer") + let _ = Command::new("amixer") .arg("sset").arg("Master").arg(value).arg("> /dev/null") .spawn(); } - enum Level { High, Mid, Low, Mute, } +impl Display for Level { + fn fmt(&self, formatter: &mut fmt::Formatter) -> Result { + match self { + Level::Mute => write!(formatter, "mute"), + Level::Low => write!(formatter, "low"), + Level::Mid => write!(formatter, "mid"), + Level::High => write!(formatter, "high"), + } + } +} diff --git a/src/utils/workspace.rs b/src/utils/workspace.rs index 4ad93f9..ad2b5e6 100644 --- a/src/utils/workspace.rs +++ b/src/utils/workspace.rs @@ -1,16 +1,40 @@ -use std::process::Command; - +use clap::Subcommand; use super::Direction; -pub fn step(direction: Direction) { +#[derive(Subcommand)] +#[command(about = "", long_about = "")] +pub enum Command { + /// Move Up/Down 1 workspace + Step { + /// The direction to move + #[arg(value_enum)] + direction: Direction, + }, + /// Move to empty workspace + Empty, + /// Get id of the active workspace + GetActive, +} + +pub fn parse_command(command: Command) { + match command { + Command::Empty => empty(), + Command::GetActive => println!("{}", get_active()), + Command::Step { direction } => step(direction), + } +} + +fn step(direction: Direction) { set(1, direction); } -pub fn empty() { +fn empty() { set(0, Direction::Absolute); } -pub fn get_active() -> u8 { +fn get_active() -> u8 { + use std::process::Command; + let value = Command::new("hyprctl") .arg("activeworkspace") .output().unwrap().stdout; @@ -23,6 +47,8 @@ pub fn get_active() -> u8 { } fn set(id: u8, direction: Direction) { + use std::process::Command; + let workspace_arg = match id { 0 => "emptym", _ => match direction { @@ -32,7 +58,7 @@ fn set(id: u8, direction: Direction) { } }; - Command::new("hyprctl") + let _ = Command::new("hyprctl") .arg("dispatch").arg("workspace").arg(workspace_arg) .spawn();