From f771dcb5b7883dec15e898578d1643a96e1c751f Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Mon, 6 Nov 2023 18:57:46 +0100 Subject: [PATCH] MISC: On Linux we detect when we where executed as an AppImage and do things accordingly --- amadeus/src/main.rs | 28 ++++++++++++++++++++-------- lektor_utils/src/appimage.rs | 32 ++++++++++++++++++++++++++++++++ lektor_utils/src/lib.rs | 3 +++ lektord/src/main.rs | 3 +++ lkt/src/main.rs | 3 +++ 5 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 lektor_utils/src/appimage.rs diff --git a/amadeus/src/main.rs b/amadeus/src/main.rs index ffb31bf7..d5ff6303 100644 --- a/amadeus/src/main.rs +++ b/amadeus/src/main.rs @@ -42,29 +42,41 @@ use style_sheet::sizes::SIZE_FONT_NORMAL; fn main() -> Result<()> { logger::init(Some(log::Level::Debug)).expect("failed to install logger"); + #[cfg(target_os = "linux")] + appimage::detect_appimage()?; + let config = config::get_or_write_default_config::<AmadeusConfig>("amadeus") .with_context(|| "failed to read or write the config file")?; logger::level(config.log); - Ok(<Amadeus as iced::Application>::run(iced::Settings { + + let settings = iced::Settings { id: Some("Amadeus".to_string()), + flags: config, + default_font: iced::font::Font::DEFAULT, + default_text_size: SIZE_FONT_NORMAL.into(), + antialiasing: false, + exit_on_close_request: false, + window: iced::window::Settings { icon: Some(iced::window::icon::from_file_data( include_bytes!("../amadeus.png"), Some(image::ImageFormat::Png), )?), + resizable: true, - decorations: true, // Wait for https://github.com/iced-rs/iced/pull/1542 to be merged transparent: false, visible: true, min_size: Some((900, 600)), level: iced::window::Level::AlwaysOnTop, position: iced::window::Position::Centered, + + // Wait for https://github.com/iced-rs/iced/pull/1542 to be merged + decorations: true, + ..Default::default() }, - flags: config, - default_font: iced::font::Font::DEFAULT, - default_text_size: SIZE_FONT_NORMAL.into(), - antialiasing: false, - exit_on_close_request: false, - })?) + }; + + <Amadeus as iced::Application>::run(settings)?; + Ok(()) } diff --git a/lektor_utils/src/appimage.rs b/lektor_utils/src/appimage.rs new file mode 100644 index 00000000..9acea0fe --- /dev/null +++ b/lektor_utils/src/appimage.rs @@ -0,0 +1,32 @@ +//! AppImage detection, log and change directories accordingly. Available only on Linux. + +use anyhow::{bail, Context, Result}; + +/// Detect AppImage runtime throu env variables and change directory to the one where the AppImage +/// was called from. We log everything for debuging purpose. Available only in Linux. +/// ``` +/// #[cfg(target_os = "linux")] +/// appimage::detect_appimage()?; +/// ``` +pub fn detect_appimage() -> Result<()> { + match ( + std::env::var_os("APPIMAGE"), + std::env::var_os("APPDIR"), + std::env::var_os("OWD"), + std::env::var_os("ARGV0"), + ) { + (Some(appimage), Some(mountpoint), Some(cwd), Some(name)) => { + let (appimage, mountpoint, owd, name) = ( + appimage.to_str().unwrap_or("..."), + mountpoint.to_str().unwrap_or("..."), + cwd.to_str().unwrap_or("..."), + name.to_str().unwrap_or("..."), + ); + log::info!("runing as an AppImage\n- appimage: {appimage}\n- launched as: {name}\n- mountpoint: {mountpoint}\n- working dir: {owd}"); + std::env::set_current_dir(&cwd).with_context(|| "failed to go to the folder where the AppImage was launched from")?; + } + (None, None, None, None) => log::info!("runing with no AppImage runtime"), + invalid => bail!("invalid AppImage runtime V2 env variables, should get none or all, got partial: {invalid:?}"), + } + Ok(()) +} diff --git a/lektor_utils/src/lib.rs b/lektor_utils/src/lib.rs index abfd8fbb..0f98c698 100644 --- a/lektor_utils/src/lib.rs +++ b/lektor_utils/src/lib.rs @@ -19,6 +19,9 @@ mod macros; ))] pub mod is; +#[cfg(target_os = "linux")] +pub mod appimage; + /// Pathdiff to handle some errors with WSL... pub mod pathdiff; diff --git a/lektord/src/main.rs b/lektord/src/main.rs index c3edf9d7..bad93de7 100644 --- a/lektord/src/main.rs +++ b/lektord/src/main.rs @@ -15,6 +15,9 @@ use tokio::{signal, sync::oneshot::Receiver}; fn main() -> Result<()> { logger::init(Some(log::Level::Debug)).expect("failed to install logger"); + #[cfg(target_os = "linux")] + appimage::detect_appimage()?; + let config = lektor_utils::config::get_or_write_default_config::<LektorConfig>("lektord")?; let args = <cmd::Args as clap::Parser>::parse(); lektor_utils::logger::level(config.log); diff --git a/lkt/src/main.rs b/lkt/src/main.rs index 1742cf74..0f244fe5 100644 --- a/lkt/src/main.rs +++ b/lkt/src/main.rs @@ -15,6 +15,9 @@ use std::{borrow::Cow, ops::RangeBounds}; fn main() -> Result<()> { logger::init(Some(log::Level::Trace)).expect("failed to install logger"); + #[cfg(target_os = "linux")] + appimage::detect_appimage()?; + let args = <args::Args as clap::Parser>::parse(); let manpage = matches!(args.action, SubCommand::Admin { manpage: true, .. }); let shell = match args.action { -- GitLab