Commit 430c601ff2

430c601ff2f6307608a53e7737b196225740dc34

parent: 0ddefcd7b3

Unregistered key

Benjamin Große <ste3ls@gmail.com> · 2023-05-01 11:07 UTC
committer: <git@ulyssa.dev>

Support configuring which program `:open` runs (#95)

Layout: unified · split

src/config.rs +4
@@ -237,6 +237,7 @@ pub struct TunableValues {
237237 pub typing_notice_display: bool,
238238 pub users: UserOverrides,
239239 pub default_room: Option<String>,
240 pub open_command: Option<Vec<String>>,
240241}
241242
242243#[derive(Clone, Default, Deserialize)]
@@ -251,6 +252,7 @@ pub struct Tunables {
251252 pub typing_notice_display: Option<bool>,
252253 pub users: Option<UserOverrides>,
253254 pub default_room: Option<String>,
255 pub open_command: Option<Vec<String>>,
254256}
255257
256258impl Tunables {
@@ -268,6 +270,7 @@ impl Tunables {
268270 typing_notice_display: self.typing_notice_display.or(other.typing_notice_display),
269271 users: merge_users(self.users, other.users),
270272 default_room: self.default_room.or(other.default_room),
273 open_command: self.open_command.or(other.open_command),
271274 }
272275 }
273276
@@ -283,6 +286,7 @@ impl Tunables {
283286 typing_notice_display: self.typing_notice_display.unwrap_or(true),
284287 users: self.users.unwrap_or_default(),
285288 default_room: self.default_room,
289 open_command: self.open_command,
286290 }
287291 }
288292}
src/tests.rs +1
@@ -188,6 +188,7 @@ pub fn mock_tunables() -> TunableValues {
188188 })]
189189 .into_iter()
190190 .collect::<HashMap<_, _>>(),
191 open_command: None,
191192 }
192193}
193194
src/windows/room/chat.rs +39 −8
@@ -1,10 +1,11 @@
11use std::borrow::Cow;
2use std::ffi::OsStr;
2use std::ffi::{OsStr, OsString};
33use std::fs;
44use std::ops::Deref;
55use std::path::{Path, PathBuf};
66
77use modalkit::editing::store::RegisterError;
8use std::process::Command;
89use tokio;
910
1011use matrix_sdk::{
@@ -246,14 +247,21 @@ impl ChatState {
246247 }
247248
248249 let info = if flags.contains(DownloadFlags::OPEN) {
249 // open::that may not return until the spawned program closes.
250250 let target = filename.clone().into_os_string();
251 tokio::task::spawn_blocking(move || open::that(target));
252
253 InfoMessage::from(format!(
254 "Attachment downloaded to {} and opened",
255 filename.display()
256 ))
251 match open_command(
252 store.application.settings.tunables.open_command.as_ref(),
253 target,
254 ) {
255 Ok(_) => {
256 InfoMessage::from(format!(
257 "Attachment downloaded to {} and opened",
258 filename.display()
259 ))
260 },
261 Err(err) => {
262 return Err(err);
263 },
264 }
257265 } else {
258266 InfoMessage::from(format!(
259267 "Attachment downloaded to {}",
@@ -844,3 +852,26 @@ impl<'a> StatefulWidget for Chat<'a> {
844852 scrollback.render(scrollarea, buf, &mut state.scrollback);
845853 }
846854}
855
856fn open_command(open_command: Option<&Vec<String>>, target: OsString) -> IambResult<()> {
857 if let Some(mut cmd) = open_command.and_then(cmd) {
858 cmd.arg(target);
859 cmd.spawn()?;
860 return Ok(());
861 } else {
862 // open::that may not return until the spawned program closes.
863 tokio::task::spawn_blocking(move || {
864 return open::that(target);
865 });
866 return Ok(());
867 }
868}
869
870fn cmd(open_command: &Vec<String>) -> Option<Command> {
871 if let [program, args @ ..] = open_command.as_slice() {
872 let mut cmd = Command::new(program);
873 cmd.args(args);
874 return Some(cmd);
875 }
876 None
877}