Support configuring which program :open runs (#95)

This commit is contained in:
Benjamin Große 2023-05-01 12:07:54 +01:00 committed by Ulyssa
parent 0ddefcd7b3
commit 430c601ff2
No known key found for this signature in database
GPG key ID: 1B3965A3D18B9B64
3 changed files with 44 additions and 8 deletions

View file

@ -237,6 +237,7 @@ pub struct TunableValues {
pub typing_notice_display: bool,
pub users: UserOverrides,
pub default_room: Option<String>,
pub open_command: Option<Vec<String>>,
}
#[derive(Clone, Default, Deserialize)]
@ -251,6 +252,7 @@ pub struct Tunables {
pub typing_notice_display: Option<bool>,
pub users: Option<UserOverrides>,
pub default_room: Option<String>,
pub open_command: Option<Vec<String>>,
}
impl Tunables {
@ -268,6 +270,7 @@ impl Tunables {
typing_notice_display: self.typing_notice_display.or(other.typing_notice_display),
users: merge_users(self.users, other.users),
default_room: self.default_room.or(other.default_room),
open_command: self.open_command.or(other.open_command),
}
}
@ -283,6 +286,7 @@ impl Tunables {
typing_notice_display: self.typing_notice_display.unwrap_or(true),
users: self.users.unwrap_or_default(),
default_room: self.default_room,
open_command: self.open_command,
}
}
}

View file

@ -188,6 +188,7 @@ pub fn mock_tunables() -> TunableValues {
})]
.into_iter()
.collect::<HashMap<_, _>>(),
open_command: None,
}
}

View file

@ -1,10 +1,11 @@
use std::borrow::Cow;
use std::ffi::OsStr;
use std::ffi::{OsStr, OsString};
use std::fs;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use modalkit::editing::store::RegisterError;
use std::process::Command;
use tokio;
use matrix_sdk::{
@ -246,14 +247,21 @@ impl ChatState {
}
let info = if flags.contains(DownloadFlags::OPEN) {
// open::that may not return until the spawned program closes.
let target = filename.clone().into_os_string();
tokio::task::spawn_blocking(move || open::that(target));
match open_command(
store.application.settings.tunables.open_command.as_ref(),
target,
) {
Ok(_) => {
InfoMessage::from(format!(
"Attachment downloaded to {} and opened",
filename.display()
))
},
Err(err) => {
return Err(err);
},
}
} else {
InfoMessage::from(format!(
"Attachment downloaded to {}",
@ -844,3 +852,26 @@ impl<'a> StatefulWidget for Chat<'a> {
scrollback.render(scrollarea, buf, &mut state.scrollback);
}
}
fn open_command(open_command: Option<&Vec<String>>, target: OsString) -> IambResult<()> {
if let Some(mut cmd) = open_command.and_then(cmd) {
cmd.arg(target);
cmd.spawn()?;
return Ok(());
} else {
// open::that may not return until the spawned program closes.
tokio::task::spawn_blocking(move || {
return open::that(target);
});
return Ok(());
}
}
fn cmd(open_command: &Vec<String>) -> Option<Command> {
if let [program, args @ ..] = open_command.as_slice() {
let mut cmd = Command::new(program);
cmd.args(args);
return Some(cmd);
}
None
}