Commit 1d93461183

1d93461183c100cd7a36aecfebaf69f5453acb60

parent: a1574c6b8d

Unregistered key

Benjamin Große <ste3ls@gmail.com> · 2023-01-28 12:29 UTC
committer: <git@ulyssa.dev>

Add :open attachments command (#31)

Fixes #27

Layout: unified · split

Cargo.lock +18
@@ -1160,6 +1160,7 @@ dependencies = [
1160name = "iamb" 1160name = "iamb"
1161version = "0.0.4" 1161version = "0.0.4"
1162dependencies = [ 1162dependencies = [
1163 "bitflags",
1163 "chrono", 1164 "chrono",
1164 "clap", 1165 "clap",
1165 "css-color-parser", 1166 "css-color-parser",
@@ -1172,6 +1173,7 @@ dependencies = [
1172 "mime", 1173 "mime",
1173 "mime_guess", 1174 "mime_guess",
1174 "modalkit", 1175 "modalkit",
1176 "open",
1175 "regex", 1177 "regex",
1176 "rpassword", 1178 "rpassword",
1177 "serde", 1179 "serde",
@@ -1765,6 +1767,16 @@ version = "0.3.0"
1765source = "registry+https://github.com/rust-lang/crates.io-index" 1767source = "registry+https://github.com/rust-lang/crates.io-index"
1766checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" 1768checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
1767 1769
1770[[package]]
1771name = "open"
1772version = "3.2.0"
1773source = "registry+https://github.com/rust-lang/crates.io-index"
1774checksum = "2078c0039e6a54a0c42c28faa984e115fb4c2d5bf2208f77d1961002df8576f8"
1775dependencies = [
1776 "pathdiff",
1777 "windows-sys",
1778]
1779
1768[[package]] 1780[[package]]
1769name = "os_str_bytes" 1781name = "os_str_bytes"
1770version = "6.4.1" 1782version = "6.4.1"
@@ -1842,6 +1854,12 @@ dependencies = [
1842 "subtle", 1854 "subtle",
1843] 1855]
1844 1856
1857[[package]]
1858name = "pathdiff"
1859version = "0.2.1"
1860source = "registry+https://github.com/rust-lang/crates.io-index"
1861checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd"
1862
1845[[package]] 1863[[package]]
1846name = "pbkdf2" 1864name = "pbkdf2"
1847version = "0.11.0" 1865version = "0.11.0"
Cargo.toml +2
@@ -14,6 +14,7 @@ categories = ["command-line-utilities"]
14rust-version = "1.66" 14rust-version = "1.66"
15 15
16[dependencies] 16[dependencies]
17bitflags = "1.3.2"
17chrono = "0.4" 18chrono = "0.4"
18clap = {version = "4.0", features = ["derive"]} 19clap = {version = "4.0", features = ["derive"]}
19css-color-parser = "0.1.2" 20css-color-parser = "0.1.2"
@@ -23,6 +24,7 @@ html5ever = "0.26.0"
23markup5ever_rcdom = "0.2.0" 24markup5ever_rcdom = "0.2.0"
24mime = "^0.3.16" 25mime = "^0.3.16"
25mime_guess = "^2.0.4" 26mime_guess = "^2.0.4"
27open = "3.2.0"
26regex = "^1.5" 28regex = "^1.5"
27rpassword = "^7.2" 29rpassword = "^7.2"
28serde = "^1.0" 30serde = "^1.0"
src/base.rs +15 −3
@@ -81,9 +81,9 @@ pub enum MessageAction {
81 81
82 /// Download an attachment to the given path. 82 /// Download an attachment to the given path.
83 /// 83 ///
84 /// The [bool] argument controls whether to overwrite any already existing file at the 84 /// The second argument controls whether to overwrite any already existing file at the
85 /// destination path. 85 /// destination path, or to open the attachment after downloading.
86 Download(Option<String>, bool), 86 Download(Option<String>, DownloadFlags),
87 87
88 /// Edit a sent message. 88 /// Edit a sent message.
89 Edit, 89 Edit,
@@ -95,6 +95,18 @@ pub enum MessageAction {
95 Reply, 95 Reply,
96} 96}
97 97
98bitflags::bitflags! {
99 pub struct DownloadFlags: u32 {
100 const NONE = 0b00000000;
101
102 /// Overwrite file if it already exists.
103 const FORCE = 0b00000001;
104
105 /// Open file after downloading.
106 const OPEN = 0b00000010;
107 }
108}
109
98#[derive(Clone, Debug, Eq, PartialEq)] 110#[derive(Clone, Debug, Eq, PartialEq)]
99pub enum RoomField { 111pub enum RoomField {
100 Name, 112 Name,
src/commands.rs +25 −1
@@ -10,6 +10,7 @@ use modalkit::{
10}; 10};
11 11
12use crate::base::{ 12use crate::base::{
13 DownloadFlags,
13 IambAction, 14 IambAction,
14 IambId, 15 IambId,
15 MessageAction, 16 MessageAction,
@@ -317,7 +318,29 @@ fn iamb_download(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult
317 return Result::Err(CommandError::InvalidArgument); 318 return Result::Err(CommandError::InvalidArgument);
318 } 319 }
319 320
320 let mact = MessageAction::Download(args.pop(), desc.bang); 321 let mut flags = DownloadFlags::NONE;
322 if desc.bang {
323 flags |= DownloadFlags::FORCE;
324 };
325 let mact = MessageAction::Download(args.pop(), flags);
326 let iact = IambAction::from(mact);
327 let step = CommandStep::Continue(iact.into(), ctx.context.take());
328
329 return Ok(step);
330}
331
332fn iamb_open(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
333 let mut args = desc.arg.strings()?;
334
335 if args.len() > 1 {
336 return Result::Err(CommandError::InvalidArgument);
337 }
338
339 let mut flags = DownloadFlags::OPEN;
340 if desc.bang {
341 flags |= DownloadFlags::FORCE;
342 };
343 let mact = MessageAction::Download(args.pop(), flags);
321 let iact = IambAction::from(mact); 344 let iact = IambAction::from(mact);
322 let step = CommandStep::Continue(iact.into(), ctx.context.take()); 345 let step = CommandStep::Continue(iact.into(), ctx.context.take());
323 346
@@ -328,6 +351,7 @@ fn add_iamb_commands(cmds: &mut ProgramCommands) {
328 cmds.add_command(ProgramCommand { names: vec!["cancel".into()], f: iamb_cancel }); 351 cmds.add_command(ProgramCommand { names: vec!["cancel".into()], f: iamb_cancel });
329 cmds.add_command(ProgramCommand { names: vec!["dms".into()], f: iamb_dms }); 352 cmds.add_command(ProgramCommand { names: vec!["dms".into()], f: iamb_dms });
330 cmds.add_command(ProgramCommand { names: vec!["download".into()], f: iamb_download }); 353 cmds.add_command(ProgramCommand { names: vec!["download".into()], f: iamb_download });
354 cmds.add_command(ProgramCommand { names: vec!["open".into()], f: iamb_open });
331 cmds.add_command(ProgramCommand { names: vec!["edit".into()], f: iamb_edit }); 355 cmds.add_command(ProgramCommand { names: vec!["edit".into()], f: iamb_edit });
332 cmds.add_command(ProgramCommand { names: vec!["invite".into()], f: iamb_invite }); 356 cmds.add_command(ProgramCommand { names: vec!["invite".into()], f: iamb_invite });
333 cmds.add_command(ProgramCommand { names: vec!["join".into()], f: iamb_join }); 357 cmds.add_command(ProgramCommand { names: vec!["join".into()], f: iamb_join });
src/windows/room/chat.rs +34 −18
@@ -4,6 +4,8 @@ use std::fs;
4use std::ops::Deref; 4use std::ops::Deref;
5use std::path::{Path, PathBuf}; 5use std::path::{Path, PathBuf};
6 6
7use tokio;
8
7use matrix_sdk::{ 9use matrix_sdk::{
8 attachment::AttachmentConfig, 10 attachment::AttachmentConfig,
9 media::{MediaFormat, MediaRequest}, 11 media::{MediaFormat, MediaRequest},
@@ -55,6 +57,7 @@ use modalkit::editing::{
55}; 57};
56 58
57use crate::base::{ 59use crate::base::{
60 DownloadFlags,
58 IambAction, 61 IambAction,
59 IambBufferId, 62 IambBufferId,
60 IambError, 63 IambError,
@@ -155,7 +158,7 @@ impl ChatState {
155 158
156 Ok(None) 159 Ok(None)
157 }, 160 },
158 MessageAction::Download(filename, force) => { 161 MessageAction::Download(filename, flags) => {
159 if let MessageEvent::Original(ev) = &msg.event { 162 if let MessageEvent::Original(ev) = &msg.event {
160 let media = client.media(); 163 let media = client.media();
161 164
@@ -202,29 +205,42 @@ impl ChatState {
202 }, 205 },
203 }; 206 };
204 207
205 if !force && filename.exists() { 208 if filename.exists() {
206 let msg = format!( 209 if !flags.contains(DownloadFlags::FORCE) {
207 "The file {} already exists; use :download! to overwrite it.", 210 let msg = format!(
208 filename.display() 211 "The file {} already exists; add ! to end of command to overwrite it.",
209 ); 212 filename.display()
210 let err = UIError::Failure(msg); 213 );
214 let err = UIError::Failure(msg);
211 215
212 return Err(err); 216 return Err(err);
213 } 217 }
214 218
215 let req = MediaRequest { source, format: MediaFormat::File }; 219 let req = MediaRequest { source, format: MediaFormat::File };
216 220
217 let bytes = 221 let bytes =
218 media.get_media_content(&req, true).await.map_err(IambError::from)?; 222 media.get_media_content(&req, true).await.map_err(IambError::from)?;
219 223
220 fs::write(filename.as_path(), bytes.as_slice())?; 224 fs::write(filename.as_path(), bytes.as_slice())?;
221 225
222 msg.downloaded = true; 226 msg.downloaded = true;
227 }
223 228
224 let info = InfoMessage::from(format!( 229 let info = if flags.contains(DownloadFlags::OPEN) {
225 "Attachment downloaded to {}", 230 // open::that may not return until the spawned program closes.
226 filename.display() 231 let target = filename.clone().into_os_string();
227 )); 232 tokio::task::spawn_blocking(move || open::that(target));
233
234 InfoMessage::from(format!(
235 "Attachment downloaded to {} and opened",
236 filename.display()
237 ))
238 } else {
239 InfoMessage::from(format!(
240 "Attachment downloaded to {}",
241 filename.display()
242 ))
243 };
228 244
229 return Ok(info.into()); 245 return Ok(info.into());
230 } 246 }