add profile list with json and dmenu display modes

This commit is contained in:
Penelope Gwen 2026-01-27 10:57:31 -08:00
parent 4e2716cb46
commit 0b7b681e86
3 changed files with 34 additions and 8 deletions

View file

@ -56,12 +56,12 @@ pub enum ShortcutCommand {
#[clap(alias = "g")] #[clap(alias = "g")]
Get { Get {
#[arg(short, long)] #[arg(short, long)]
mode: Option<ShortcutMode>, mode: Option<DisplayMode>,
}, },
} }
#[derive(Clone, Copy, ValueEnum, Subcommand)] #[derive(Clone, Copy, ValueEnum, Subcommand)]
pub enum ShortcutMode { pub enum DisplayMode {
/// Print formatted for dmenu/wofi /// Print formatted for dmenu/wofi
#[clap(alias = "d")] #[clap(alias = "d")]
Dmenu, Dmenu,
@ -147,6 +147,12 @@ pub enum ProfileCommand {
#[command(subcommand)] #[command(subcommand)]
shortcut_command: ShortcutCommand, shortcut_command: ShortcutCommand,
}, },
/// List Profiles
#[clap(alias = "l")]
List {
#[arg(short, long)]
mode: Option<DisplayMode>,
},
/// Initialize Sway-DE-Utils /// Initialize Sway-DE-Utils
#[clap(alias = "i")] #[clap(alias = "i")]
Init, Init,

View file

@ -4,7 +4,7 @@ use {
get_xdg_dirs, get_xdg_dirs,
lib::{ lib::{
SDUError, SDUError,
cli::{ProfileCommand, ProfileGetCommand, ProfileSwitchCommand}, cli::{DisplayMode, ProfileCommand, ProfileGetCommand, ProfileSwitchCommand},
get, get_sway_connection, run_sway_command, shortcuts_fn, get, get_sway_connection, run_sway_command, shortcuts_fn,
sway::focused_workspace_profile, sway::focused_workspace_profile,
}, },
@ -130,9 +130,28 @@ pub fn profile_fn(
.expect("could not find scripts for profile"), .expect("could not find scripts for profile"),
) )
} }
ProfileCommand::List { mode } => {
list_profiles(mode.unwrap_or(DisplayMode::Json), profiles_config)
}
} }
} }
//TODO: Consolidate this function with print_shortcuts in shortcuts.rs
fn list_profiles(mode: DisplayMode, profiles: Vec<Profile>) -> Result<(), SDUError> {
let output = match mode {
DisplayMode::Dmenu => {
let mut dmenu_entries = Vec::<String>::new();
for profile in profiles {
dmenu_entries.push(format!("{} {}", profile.icon, profile.name));
}
dmenu_entries.join("\n")
}
DisplayMode::Json => json!(profiles).to_string(),
};
println!("{}", output);
Ok(())
}
fn initialize( fn initialize(
mut sway_connection: Connection, mut sway_connection: Connection,
profile: Profile, profile: Profile,

View file

@ -5,21 +5,22 @@ use crate::{
config::ScriptConf, config::ScriptConf,
lib::{ lib::{
SDUError, SDUError,
cli::{ShortcutCommand, ShortcutMode}, cli::{DisplayMode, ShortcutCommand},
sway_ipc::{get_sway_connection, run_sway_command}, sway_ipc::{get_sway_connection, run_sway_command},
}, },
}; };
fn print_shortcuts(mode: &ShortcutMode, shortcuts: Vec<ScriptConf>) -> String { fn print_shortcuts(mode: &DisplayMode, shortcuts: Vec<ScriptConf>) -> String {
match mode { match mode {
ShortcutMode::Dmenu => { DisplayMode::Dmenu => {
let mut dmenu_entries = Vec::<String>::new(); let mut dmenu_entries = Vec::<String>::new();
for shortcut in shortcuts { for shortcut in shortcuts {
dmenu_entries.push(format!("{} {}", shortcut.icon, shortcut.name)); dmenu_entries.push(format!("{} {}", shortcut.icon, shortcut.name));
} }
//TODO: collect active profile information before returning it here?
dmenu_entries.join("\n") dmenu_entries.join("\n")
} }
ShortcutMode::Json => json!(shortcuts).to_string(), DisplayMode::Json => json!(shortcuts).to_string(),
} }
} }
@ -44,7 +45,7 @@ pub fn shortcuts_fn(
exec_shortcut(shortcut_name.to_string(), shortcuts) exec_shortcut(shortcut_name.to_string(), shortcuts)
} }
ShortcutCommand::Get { mode } => { ShortcutCommand::Get { mode } => {
let shortcuts_string = print_shortcuts(&mode.unwrap_or(ShortcutMode::Json), shortcuts); let shortcuts_string = print_shortcuts(&mode.unwrap_or(DisplayMode::Json), shortcuts);
println!("{}", shortcuts_string); println!("{}", shortcuts_string);
Ok(()) Ok(())
} }