diff --git a/.gitignore b/.gitignore index 1a4595e..4767901 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /target *.log +docs/ +.superpowers/ diff --git a/assets/theme.toml b/assets/theme.toml index e2fb98c..8b38b13 100644 --- a/assets/theme.toml +++ b/assets/theme.toml @@ -149,9 +149,9 @@ accent_fg = "accent" [popup_import] bg = "bg" -border_fg = "#71a5de" -field_active_border_fg = "#71a5de" +border_fg = "fg" +field_active_border_fg = "fg" field_inactive_border_fg = "muted" text_fg = "fg" error_fg = "error" -accent_fg = "#71a5de" +accent_fg = "accent" diff --git a/src/cli/instance.rs b/src/cli/instance.rs index 0c80128..66a5323 100644 --- a/src/cli/instance.rs +++ b/src/cli/instance.rs @@ -17,6 +17,7 @@ pub async fn handle_instance(matches: &ArgMatches) -> CliResult { Some(("rename", sub_matches)) => rename_instance(sub_matches), Some(("launch", sub_matches)) => launch_instance(sub_matches).await, Some(("config", sub_matches)) => config_instance(sub_matches), + Some(("desktop", sub_matches)) => desktop_instance(sub_matches), _ => Ok(()), } } @@ -153,6 +154,18 @@ async fn launch_instance(matches: &ArgMatches) -> CliResult { Ok(()) } +fn desktop_instance(matches: &ArgMatches) -> CliResult { + let name = required_arg(matches, "name")?; + let config = manager().load_one(name)?; + let enabled = crate::instance::desktop::toggle(&config)?; + if enabled { + println!("Desktop shortcut created for '{}'.", name); + } else { + println!("Desktop shortcut removed for '{}'.", name); + } + Ok(()) +} + fn config_instance(matches: &ArgMatches) -> CliResult { let name = required_arg(matches, "name")?; let manager = manager(); diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 35c62b2..2b0da44 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -98,6 +98,12 @@ fn build_command() -> Command { .arg_required_else_help(true) .arg(Arg::new("name").required(true).action(ArgAction::Set)) .arg(Arg::new("set").long("set").action(ArgAction::Set)), + ) + .subcommand( + Command::new("desktop") + .about("Toggle desktop shortcut for an instance") + .arg_required_else_help(true) + .arg(Arg::new("name").required(true).action(ArgAction::Set)), ), ) .subcommand(build_content_command("mod", "mods")) diff --git a/src/instance/desktop.rs b/src/instance/desktop.rs new file mode 100644 index 0000000..3e3aafe --- /dev/null +++ b/src/instance/desktop.rs @@ -0,0 +1,123 @@ +use std::path::{Path, PathBuf}; + +use crate::instance::models::InstanceConfig; + +pub fn desktop_path(name: &str) -> Option { + dirs_next::data_dir().map(|d| { + d.join("applications") + .join(format!("mcl-{}.desktop", sanitize(name))) + }) +} + +pub fn icon_path() -> Option { + dirs_next::data_dir().map(|d| d.join("mcl").join("icon.png")) +} + +pub fn exists(name: &str) -> bool { + desktop_path(name).map(|p| p.exists()).unwrap_or(false) +} + +pub fn create(config: &InstanceConfig) -> std::io::Result { + let path = desktop_path(&config.name) + .ok_or_else(|| std::io::Error::other("cannot resolve data directory"))?; + + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent)?; + } + + let icon = icon_path().filter(|p| p.exists()); + let content = build_content(&config.name, icon.as_deref()); + std::fs::write(&path, content)?; + Ok(path) +} + +pub fn remove(name: &str) -> std::io::Result<()> { + let Some(path) = desktop_path(name) else { + return Ok(()); + }; + if path.exists() { + std::fs::remove_file(path)?; + } + Ok(()) +} + +pub fn toggle(config: &InstanceConfig) -> std::io::Result { + if exists(&config.name) { + remove(&config.name)?; + Ok(false) + } else { + create(config)?; + Ok(true) + } +} + +pub fn rename(old_name: &str, new_config: &InstanceConfig) -> std::io::Result<()> { + if !exists(old_name) { + return Ok(()); + } + remove(old_name)?; + create(new_config)?; + Ok(()) +} + +fn build_content(name: &str, icon: Option<&Path>) -> String { + let mut out = String::new(); + out.push_str("[Desktop Entry]\n"); + out.push_str("Version=1.0\n"); + out.push_str("Type=Application\n"); + out.push_str(&format!("Name=Minecraft - {name}\n")); + out.push_str(&format!("Comment=Launch {name} Minecraft instance\n")); + out.push_str(&format!("Exec=mcl instance launch \"{name}\"\n")); + if let Some(icon) = icon { + out.push_str(&format!("Icon={}\n", icon.display())); + } + out.push_str("Terminal=true\n"); + out.push_str("Categories=Game;\n"); + out +} + +fn sanitize(name: &str) -> String { + name.chars() + .map(|c| { + if c.is_alphanumeric() || c == '-' || c == '_' { + c + } else { + '_' + } + }) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn sanitize_keeps_alphanumeric() { + assert_eq!(sanitize("my-instance_123"), "my-instance_123"); + } + + #[test] + fn sanitize_replaces_special_chars() { + assert_eq!(sanitize("my instance!"), "my_instance_"); + assert_eq!(sanitize("path/traversal"), "path_traversal"); + } + + #[test] + fn build_content_with_icon() { + let icon = PathBuf::from("/home/user/icon.png"); + let content = build_content("TestPack", Some(&icon)); + assert!(content.contains("Name=Minecraft - TestPack")); + assert!(content.contains("Exec=mcl instance launch \"TestPack\"")); + assert!(content.contains("Icon=/home/user/icon.png")); + assert!(content.contains("Terminal=true")); + assert!(content.contains("Categories=Game;")); + } + + #[test] + fn build_content_without_icon() { + let content = build_content("TestPack", None); + assert!(content.contains("Name=Minecraft - TestPack")); + assert!(!content.contains("Icon=")); + } +} diff --git a/src/instance/manager.rs b/src/instance/manager.rs index ddc662a..664f1ac 100644 --- a/src/instance/manager.rs +++ b/src/instance/manager.rs @@ -272,7 +272,12 @@ impl InstanceManager { return Err(InstanceError::NotFound(name.to_string())); } match std::fs::remove_dir_all(&instance_dir) { - Ok(_) => Ok(()), + Ok(_) => { + if let Err(e) = crate::instance::desktop::remove(name) { + tracing::warn!("Failed to remove desktop shortcut for '{}': {}", name, e); + } + Ok(()) + } Err(e) => { tracing::error!("Failed to delete instance '{}': {}", name, e); Err(InstanceError::Io(e)) @@ -307,6 +312,9 @@ impl InstanceManager { if let Ok(json) = serde_json::to_string_pretty(&config) { let _ = std::fs::write(&config_path, json); } + if let Err(e) = crate::instance::desktop::rename(old_name, &config) { + tracing::warn!("Failed to rename desktop shortcut: {}", e); + } } } diff --git a/src/instance/mod.rs b/src/instance/mod.rs index 5656507..9087552 100644 --- a/src/instance/mod.rs +++ b/src/instance/mod.rs @@ -1,5 +1,6 @@ pub mod launch; pub mod loader; +pub mod desktop; pub mod log_files; pub mod manager; pub mod models; diff --git a/src/tui/layout.rs b/src/tui/layout.rs index e3465b9..6c6308c 100644 --- a/src/tui/layout.rs +++ b/src/tui/layout.rs @@ -419,6 +419,14 @@ impl App { self.pending_editor = Some(path); return Ok(()); } + widgets::details::SettingsAction::ToggleDesktop => { + if let Some(inst) = self.profiles_state.selected_instance() { + if let Err(e) = crate::instance::desktop::toggle(inst) { + tracing::error!("Failed to toggle desktop shortcut: {}", e); + } + } + return Ok(()); + } widgets::details::SettingsAction::None => {} } } diff --git a/src/tui/widgets/details.rs b/src/tui/widgets/details.rs index aa46ebc..f8c8b0e 100644 --- a/src/tui/widgets/details.rs +++ b/src/tui/widgets/details.rs @@ -33,7 +33,11 @@ pub fn render( if focused == FocusedArea::Settings { let lines = super::popups::keybind_lines_wrapped( - &[("e", " edit instance"), ("g", " edit global")], + &[ + ("e", " edit instance"), + ("g", " edit global"), + ("d", " desktop shortcut"), + ], area.width.saturating_sub(2), ); for line in lines { @@ -98,6 +102,7 @@ pub enum SettingsAction { None, EditInstance(std::path::PathBuf), EditGlobal(std::path::PathBuf), + ToggleDesktop, } pub fn handle_key( @@ -118,6 +123,7 @@ pub fn handle_key( let path = crate::config::get_config_path().join("config.toml"); SettingsAction::EditGlobal(path) } + crossterm::event::KeyCode::Char('d') => SettingsAction::ToggleDesktop, _ => SettingsAction::None, } }