Skip to content

Commit

Permalink
feat: reload on config change added
Browse files Browse the repository at this point in the history
feat: new sorting of launches based on times run
Signed-off-by: Enrico Stemmer <enrico@h3rmt.zip>
  • Loading branch information
H3rmt committed Feb 12, 2025
1 parent 0e69ecb commit e05f2c5
Show file tree
Hide file tree
Showing 16 changed files with 245 additions and 158 deletions.
14 changes: 14 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,27 @@ pub enum Command {
simple_conf: simple::SimpleConf,
},

/// Generate or check the config file
Config {
#[clap(subcommand)]
command: ConfigCommand,
},

/// Debug command to debug finding icons for the GUI, doesn't interact with the Daemon
Debug {
#[clap(subcommand)]
command: DebugCommand,
},
}

#[derive(Subcommand, Debug, Clone)]
pub enum ConfigCommand {
/// Generate a default config file
Generate {},
/// Check the config file for errors
Check {},
}

#[derive(Subcommand, Debug, Clone)]
pub enum DebugCommand {
/// Search for an icon with a window class
Expand Down
66 changes: 33 additions & 33 deletions src/config/config_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use serde::{Deserialize, Serialize};
use smart_default::SmartDefault;
use std::fmt::Display;

#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Config {
pub binds: Vec<Bind>,
pub general: General,
}

#[derive(SmartDefault, Debug, Deserialize, Serialize)]
#[derive(SmartDefault, Debug, Clone, Deserialize, Serialize)]
#[serde(default)]
pub struct General {
#[default = false]
Expand All @@ -21,7 +21,7 @@ pub struct General {
pub windows: Windows,
}

#[derive(SmartDefault, Debug, Deserialize, Serialize)]
#[derive(SmartDefault, Debug, Clone, Deserialize, Serialize)]
#[serde(default)]
pub struct Launcher {
#[default = 6]
Expand All @@ -34,7 +34,7 @@ pub struct Launcher {
pub animate_launch_time_ms: u64,
}

#[derive(SmartDefault, Debug, Deserialize, Serialize)]
#[derive(SmartDefault, Debug, Clone, Deserialize, Serialize)]
#[serde(default)]
pub struct Windows {
#[default = true]
Expand All @@ -45,34 +45,34 @@ pub struct Windows {
pub strip_html_from_workspace_title: bool,
}

#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum Bind {
Switch(SwitchBindConfig),
Overview(OverviewBindConfig),
}

#[derive(SmartDefault, Debug, Deserialize, Serialize)]
#[derive(SmartDefault, Debug, Clone, Deserialize, Serialize)]
#[serde(default)]
pub struct SwitchBindConfig {
pub open: OpenSwitch,
pub navigate: Navigate,
pub close: CloseSwitch,
pub other: Other,
}
#[derive(SmartDefault, Debug, Deserialize, Serialize)]
#[derive(SmartDefault, Debug, Clone, Deserialize, Serialize)]
#[serde(default)]
pub struct OpenSwitch {
#[default(Mod::Super)]
pub modifier: Mod,
}
#[derive(SmartDefault, Debug, Deserialize, Serialize)]
#[derive(SmartDefault, Debug, Clone, Deserialize, Serialize)]
#[serde(default)]
pub struct CloseSwitch {
#[default = true]
pub escape: bool,
}

#[derive(SmartDefault, Debug, Deserialize, Serialize)]
#[derive(SmartDefault, Debug, Clone, Deserialize, Serialize)]
#[serde(default)]
pub struct OverviewBindConfig {
#[default = true]
Expand All @@ -82,7 +82,7 @@ pub struct OverviewBindConfig {
pub close: CloseOverview,
pub other: Other,
}
#[derive(SmartDefault, Debug, Deserialize, Serialize)]
#[derive(SmartDefault, Debug, Clone, Deserialize, Serialize)]
#[serde(default)]
pub struct OpenOverview {
#[default(Mod::Super)]
Expand All @@ -91,7 +91,7 @@ pub struct OpenOverview {
pub key: KeyMaybeMod,
}

#[derive(SmartDefault, Debug, Deserialize, Serialize)]
#[derive(SmartDefault, Debug, Clone, Deserialize, Serialize)]
#[serde(default)]
pub struct CloseOverview {
#[default = true]
Expand All @@ -100,7 +100,7 @@ pub struct CloseOverview {
pub close_on_reopen: bool,
}

#[derive(SmartDefault, Debug, Deserialize, Serialize)]
#[derive(SmartDefault, Debug, Clone, Deserialize, Serialize)]
#[serde(default)]
pub struct Navigate {
#[default = "tab"]
Expand All @@ -109,7 +109,7 @@ pub struct Navigate {
pub reverse: Reverse,
}

#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum Reverse {
Key(String),
Mod(Mod),
Expand All @@ -124,24 +124,24 @@ pub enum Reverse {
// }
// }

#[derive(Debug, Deserialize, Serialize)]
pub struct SimpleBindConfig {
#[serde(default)]
pub reverse: bool,
#[serde(default)]
pub offset: u8,
pub open: OpenSimple,
#[serde(default)]
pub other: Other,
}
// #[derive(Debug, Deserialize, Serialize)]
// pub struct SimpleBindConfig {
// #[serde(default)]
// pub reverse: bool,
// #[serde(default)]
// pub offset: u8,
// pub open: OpenSimple,
// #[serde(default)]
// pub other: Other,
// }

#[derive(Debug, Deserialize, Serialize)]
pub struct OpenSimple {
pub modifier: Mod,
pub key: KeyMaybeMod,
}
// #[derive(Debug, Deserialize, Serialize)]
// pub struct OpenSimple {
// pub modifier: Mod,
// pub key: KeyMaybeMod,
// }

#[derive(SmartDefault, Debug, Deserialize, Serialize)]
#[derive(SmartDefault, Debug, Clone, Deserialize, Serialize)]
#[serde(default)]
pub struct Other {
#[default = 6]
Expand All @@ -163,15 +163,15 @@ pub struct Other {
pub filter_by: Option<Vec<FilterBy>>,
}

#[derive(Debug, Deserialize, Serialize, Eq, PartialEq)]
#[derive(Debug, Clone, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum FilterBy {
SameClass,
CurrentWorkspace,
CurrentMonitor,
}

#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum SwitchType {
Client,
Expand All @@ -189,7 +189,7 @@ pub enum SwitchType {
// }
// }

#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Mod {
Alt,
Expand Down Expand Up @@ -221,7 +221,7 @@ impl Display for Mod {
}
}

#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct KeyMaybeMod(String);
impl From<&str> for KeyMaybeMod {
fn from(s: &str) -> Self {
Expand Down
19 changes: 11 additions & 8 deletions src/config/generate.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::config::config_structs::{
Bind, Config, FilterBy, OverviewBindConfig, Reverse, SwitchBindConfig, ToKey,
Bind, FilterBy, OverviewBindConfig, Reverse, SwitchBindConfig, ToKey,
};
use crate::get_daemon_socket_path_buff;
use crate::transfer::{DispatchConfig, OpenConfig, TransferType};
Expand All @@ -8,14 +8,17 @@ use rand::Rng;
use std::path::PathBuf;
use tracing::{span, trace, Level};

pub fn create_binds_and_submaps<'a>(
config: Config,
) -> anyhow::Result<Vec<(&'a str, String)>> {
pub fn create_binds_and_submaps<'a>(binds: &Vec<Bind>) -> anyhow::Result<Vec<(&'a str, String)>> {
let _span = span!(Level::DEBUG, "create_binds_and_submaps").entered();
let mut keyword_list = Vec::<(&str, String)>::new();
let rand_id = rand::rng().random_range(10..=99);

for (i, bind) in config.binds.into_iter().enumerate() {
// TODO add config for these
keyword_list.push(("layerrule", "dimaround, hyprswitch".to_string()));
keyword_list.push(("layerrule", "noanim, hyprswitch_launcher".to_string()));
keyword_list.push(("layerrule", "noanim, hyprswitch".to_string()));

for (i, bind) in binds.into_iter().enumerate() {
let submap_name = format!("hyprswitch-{rand_id}-{i}");
trace!("submap_name: {}", submap_name);
match bind {
Expand Down Expand Up @@ -121,7 +124,7 @@ fn generate_dispatch(reverse: bool, offset: u8) -> anyhow::Result<String> {

fn generate_overview(
keyword_list: &mut Vec<(&str, String)>,
press: OverviewBindConfig,
press: &OverviewBindConfig,
submap_name: String,
) -> anyhow::Result<()> {
keyword_list.push((
Expand Down Expand Up @@ -195,7 +198,7 @@ fn generate_overview(
generate_dispatch(false, 1)?
),
));
match press.navigate.reverse {
match &press.navigate.reverse {
Reverse::Key(key) => keyword_list.push((
"bind",
format!(", {}, exec, {}", key, generate_dispatch(true, 1)?),
Expand All @@ -217,7 +220,7 @@ fn generate_overview(

fn generate_switch(
keyword_list: &mut Vec<(&str, String)>,
hold: SwitchBindConfig,
hold: &SwitchBindConfig,
submap_name: String,
) -> anyhow::Result<()> {
keyword_list.push((
Expand Down
8 changes: 6 additions & 2 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::config::config_structs::Config;
use anyhow::Context;
use anyhow::{bail, Context};
use ron::extensions::Extensions;
use ron::Options;
use std::env;
Expand All @@ -14,6 +13,8 @@ mod config_structs;
mod generate;
mod validate;

pub use config_structs::*;

pub fn load(config_file: Option<PathBuf>) -> anyhow::Result<Config> {
let _span = span!(Level::TRACE, "load_config").entered();
let config = config_file
Expand All @@ -24,6 +25,9 @@ pub fn load(config_file: Option<PathBuf>) -> anyhow::Result<Config> {
.with_default_extension(Extensions::IMPLICIT_SOME)
.with_default_extension(Extensions::UNWRAP_NEWTYPES)
.with_default_extension(Extensions::UNWRAP_VARIANT_NEWTYPES);
if !config.exists() {
bail!("Config file does not exist, create it using `hyprswitch config generate`");
}
let file = std::fs::File::open(&config)
.with_context(|| format!("Failed to open config at ({config:?})"))?;
let config: Config = options
Expand Down
23 changes: 11 additions & 12 deletions src/daemon/cache.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use anyhow::Context;
use std::collections::HashMap;
use std::env;
use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf;

pub fn get_cached_runs() -> anyhow::Result<Vec<(String, i64)>> {
pub fn get_cached_runs() -> anyhow::Result<HashMap<Box<str>, i64>> {
let cache_path = get_path().context("Failed to get cache path")?;
let cache_data = if cache_path.exists() {
let file = OpenOptions::new()
Expand All @@ -16,17 +17,15 @@ pub fn get_cached_runs() -> anyhow::Result<Vec<(String, i64)>> {
serde_json::json!({})
};

let mut runs = cache_data
.as_object()
.unwrap_or(&serde_json::Map::new())
.iter()
.map(|(run, count)| (run.clone(), count.as_i64().unwrap_or(0)))
.collect::<Vec<_>>();
runs.sort_by(|a, b| b.1.cmp(&a.1));
Ok(runs)
let runs = cache_data.as_object().map(|o| {
o.into_iter()
.map(|(k, v)| (k.as_str().into(), v.as_i64().unwrap_or(0)))
.collect()
});
Ok(runs.unwrap_or_default())
}

pub fn cache_run(run: &str) -> anyhow::Result<()> {
pub fn cache_run(desktop_file: &str) -> anyhow::Result<()> {
let cache_path = get_path().context("Failed to get cache path")?;
let mut cache_data = if cache_path.exists() {
let file = OpenOptions::new()
Expand All @@ -41,8 +40,8 @@ pub fn cache_run(run: &str) -> anyhow::Result<()> {
serde_json::json!({})
};

cache_data[run] = serde_json::json!(cache_data
.get(run)
cache_data[desktop_file] = serde_json::json!(cache_data
.get(desktop_file)
.map(|v| v.as_i64().unwrap_or(0) + 1)
.unwrap_or(1));

Expand Down
2 changes: 1 addition & 1 deletion src/daemon/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct Exec {
pub exec: Box<str>,
pub path: Option<Box<str>>,
pub terminal: bool,
// TODO maybe add path to .desktop file for caching
pub desktop_file: Box<str>,
}

#[derive(Debug, Default, Copy, Clone)]
Expand Down
4 changes: 2 additions & 2 deletions src/daemon/gui/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ pub fn debug_list() -> anyhow::Result<()> {

pub fn debug_desktop_files() -> anyhow::Result<()> {
let map = get_desktop_files_debug()?;
for (name, icon, _, exec, _, _, file) in map {
for entry in map {
#[allow(clippy::print_stdout)]
{
println!(
"Desktop file: {} [{:?}] -> {:?} [{:?}]",
name, file, exec, icon
entry.name, entry.desktop_file, entry.exec, entry.icon
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/daemon/gui/gui_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ pub(crate) fn gui_exec(share: &Share, selected: usize) {
lock.launcher_data.selected = Some(selected);
if let Some(exec) = lock.launcher_data.execs.get(selected) {
run_program(&exec.exec, &exec.path, exec.terminal);
cache_run(&exec.exec).warn("Failed to cache run");
cache_run(&exec.desktop_file).warn("Failed to cache run");
} else {
warn!("Selected program (nr. {}) not found, closing", selected);
}
Expand Down
2 changes: 1 addition & 1 deletion src/daemon/gui/launcher/launcher.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.launcher {
min-width: 600px;
min-width: 650px;
font-size: 20px;
}

Expand Down
Loading

0 comments on commit e05f2c5

Please sign in to comment.