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 = [
11601160name = "iamb"
11611161version = "0.0.4"
11621162dependencies = [
1163 "bitflags",
11631164 "chrono",
11641165 "clap",
11651166 "css-color-parser",
@@ -1172,6 +1173,7 @@ dependencies = [
11721173 "mime",
11731174 "mime_guess",
11741175 "modalkit",
1176 "open",
11751177 "regex",
11761178 "rpassword",
11771179 "serde",
@@ -1765,6 +1767,16 @@ version = "0.3.0"
17651767source = "registry+https://github.com/rust-lang/crates.io-index"
17661768checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
17671769
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
17681780[[package]]
17691781name = "os_str_bytes"
17701782version = "6.4.1"
@@ -1842,6 +1854,12 @@ dependencies = [
18421854 "subtle",
18431855]
18441856
1857[[package]]
1858name = "pathdiff"
1859version = "0.2.1"
1860source = "registry+https://github.com/rust-lang/crates.io-index"
1861checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd"
1862
18451863[[package]]
18461864name = "pbkdf2"
18471865version = "0.11.0"
Cargo.toml +2
@@ -14,6 +14,7 @@ categories = ["command-line-utilities"]
1414rust-version = "1.66"
1515
1616[dependencies]
17bitflags = "1.3.2"
1718chrono = "0.4"
1819clap = {version = "4.0", features = ["derive"]}
1920css-color-parser = "0.1.2"
@@ -23,6 +24,7 @@ html5ever = "0.26.0"
2324markup5ever_rcdom = "0.2.0"
2425mime = "^0.3.16"
2526mime_guess = "^2.0.4"
27open = "3.2.0"
2628regex = "^1.5"
2729rpassword = "^7.2"
2830serde = "^1.0"
src/base.rs +15 −3
@@ -81,9 +81,9 @@ pub enum MessageAction {
8181
8282 /// Download an attachment to the given path.
8383 ///
84 /// The [bool] argument controls whether to overwrite any already existing file at the
85 /// destination path.
86 Download(Option<String>, bool),
84 /// The second argument controls whether to overwrite any already existing file at the
85 /// destination path, or to open the attachment after downloading.
86 Download(Option<String>, DownloadFlags),
8787
8888 /// Edit a sent message.
8989 Edit,
@@ -95,6 +95,18 @@ pub enum MessageAction {
9595 Reply,
9696}
9797
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
98110#[derive(Clone, Debug, Eq, PartialEq)]
99111pub enum RoomField {
100112 Name,
src/commands.rs +25 −1
@@ -10,6 +10,7 @@ use modalkit::{
1010};
1111
1212use crate::base::{
13 DownloadFlags,
1314 IambAction,
1415 IambId,
1516 MessageAction,
@@ -317,7 +318,29 @@ fn iamb_download(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult
317318 return Result::Err(CommandError::InvalidArgument);
318319 }
319320
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);
321344 let iact = IambAction::from(mact);
322345 let step = CommandStep::Continue(iact.into(), ctx.context.take());
323346
@@ -328,6 +351,7 @@ fn add_iamb_commands(cmds: &mut ProgramCommands) {
328351 cmds.add_command(ProgramCommand { names: vec!["cancel".into()], f: iamb_cancel });
329352 cmds.add_command(ProgramCommand { names: vec!["dms".into()], f: iamb_dms });
330353 cmds.add_command(ProgramCommand { names: vec!["download".into()], f: iamb_download });
354 cmds.add_command(ProgramCommand { names: vec!["open".into()], f: iamb_open });
331355 cmds.add_command(ProgramCommand { names: vec!["edit".into()], f: iamb_edit });
332356 cmds.add_command(ProgramCommand { names: vec!["invite".into()], f: iamb_invite });
333357 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;
44use std::ops::Deref;
55use std::path::{Path, PathBuf};
66
7use tokio;
8
79use matrix_sdk::{
810 attachment::AttachmentConfig,
911 media::{MediaFormat, MediaRequest},
@@ -55,6 +57,7 @@ use modalkit::editing::{
5557};
5658
5759use crate::base::{
60 DownloadFlags,
5861 IambAction,
5962 IambBufferId,
6063 IambError,
@@ -155,7 +158,7 @@ impl ChatState {
155158
156159 Ok(None)
157160 },
158 MessageAction::Download(filename, force) => {
161 MessageAction::Download(filename, flags) => {
159162 if let MessageEvent::Original(ev) = &msg.event {
160163 let media = client.media();
161164
@@ -202,29 +205,42 @@ impl ChatState {
202205 },
203206 };
204207
205 if !force && filename.exists() {
206 let msg = format!(
207 "The file {} already exists; use :download! to overwrite it.",
208 filename.display()
209 );
210 let err = UIError::Failure(msg);
208 if filename.exists() {
209 if !flags.contains(DownloadFlags::FORCE) {
210 let msg = format!(
211 "The file {} already exists; add ! to end of command to overwrite it.",
212 filename.display()
213 );
214 let err = UIError::Failure(msg);
211215
212 return Err(err);
213 }
216 return Err(err);
217 }
214218
215 let req = MediaRequest { source, format: MediaFormat::File };
219 let req = MediaRequest { source, format: MediaFormat::File };
216220
217 let bytes =
218 media.get_media_content(&req, true).await.map_err(IambError::from)?;
221 let bytes =
222 media.get_media_content(&req, true).await.map_err(IambError::from)?;
219223
220 fs::write(filename.as_path(), bytes.as_slice())?;
224 fs::write(filename.as_path(), bytes.as_slice())?;
221225
222 msg.downloaded = true;
226 msg.downloaded = true;
227 }
223228
224 let info = InfoMessage::from(format!(
225 "Attachment downloaded to {}",
226 filename.display()
227 ));
229 let info = if flags.contains(DownloadFlags::OPEN) {
230 // open::that may not return until the spawned program closes.
231 let target = filename.clone().into_os_string();
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 };
228244
229245 return Ok(info.into());
230246 }