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 { |
| 237 | 237 | pub typing_notice_display: bool, |
| 238 | 238 | pub users: UserOverrides, |
| 239 | 239 | pub default_room: Option<String>, |
| 240 | pub open_command: Option<Vec<String>>, |
| 240 | 241 | } |
| 241 | 242 | |
| 242 | 243 | #[derive(Clone, Default, Deserialize)] |
| @@ -251,6 +252,7 @@ pub struct Tunables { |
| 251 | 252 | pub typing_notice_display: Option<bool>, |
| 252 | 253 | pub users: Option<UserOverrides>, |
| 253 | 254 | pub default_room: Option<String>, |
| 255 | pub open_command: Option<Vec<String>>, |
| 254 | 256 | } |
| 255 | 257 | |
| 256 | 258 | impl Tunables { |
| @@ -268,6 +270,7 @@ impl Tunables { |
| 268 | 270 | typing_notice_display: self.typing_notice_display.or(other.typing_notice_display), |
| 269 | 271 | users: merge_users(self.users, other.users), |
| 270 | 272 | default_room: self.default_room.or(other.default_room), |
| 273 | open_command: self.open_command.or(other.open_command), |
| 271 | 274 | } |
| 272 | 275 | } |
| 273 | 276 | |
| @@ -283,6 +286,7 @@ impl Tunables { |
| 283 | 286 | typing_notice_display: self.typing_notice_display.unwrap_or(true), |
| 284 | 287 | users: self.users.unwrap_or_default(), |
| 285 | 288 | default_room: self.default_room, |
| 289 | open_command: self.open_command, |
| 286 | 290 | } |
| 287 | 291 | } |
| 288 | 292 | } |
src/tests.rs
+1
| @@ -188,6 +188,7 @@ pub fn mock_tunables() -> TunableValues { |
| 188 | 188 | })] |
| 189 | 189 | .into_iter() |
| 190 | 190 | .collect::<HashMap<_, _>>(), |
| 191 | open_command: None, |
| 191 | 192 | } |
| 192 | 193 | } |
| 193 | 194 | |
src/windows/room/chat.rs
+39 −8
| @@ -1,10 +1,11 @@ |
| 1 | 1 | use std::borrow::Cow; |
| 2 | | use std::ffi::OsStr; |
| 2 | use std::ffi::{OsStr, OsString}; |
| 3 | 3 | use std::fs; |
| 4 | 4 | use std::ops::Deref; |
| 5 | 5 | use std::path::{Path, PathBuf}; |
| 6 | 6 | |
| 7 | 7 | use modalkit::editing::store::RegisterError; |
| 8 | use std::process::Command; |
| 8 | 9 | use tokio; |
| 9 | 10 | |
| 10 | 11 | use matrix_sdk::{ |
| @@ -246,14 +247,21 @@ impl ChatState { |
| 246 | 247 | } |
| 247 | 248 | |
| 248 | 249 | let info = if flags.contains(DownloadFlags::OPEN) { |
| 249 | | // open::that may not return until the spawned program closes. |
| 250 | 250 | 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 | } |
| 257 | 265 | } else { |
| 258 | 266 | InfoMessage::from(format!( |
| 259 | 267 | "Attachment downloaded to {}", |
| @@ -844,3 +852,26 @@ impl<'a> StatefulWidget for Chat<'a> { |
| 844 | 852 | scrollback.render(scrollarea, buf, &mut state.scrollback); |
| 845 | 853 | } |
| 846 | 854 | } |
| 855 | |
| 856 | fn 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 | |
| 870 | fn 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 | } |