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 = [ |
| 1160 | 1160 | name = "iamb" |
| 1161 | 1161 | version = "0.0.4" |
| 1162 | 1162 | dependencies = [ |
| 1163 | "bitflags", |
| 1163 | 1164 | "chrono", |
| 1164 | 1165 | "clap", |
| 1165 | 1166 | "css-color-parser", |
| @@ -1172,6 +1173,7 @@ dependencies = [ |
| 1172 | 1173 | "mime", |
| 1173 | 1174 | "mime_guess", |
| 1174 | 1175 | "modalkit", |
| 1176 | "open", |
| 1175 | 1177 | "regex", |
| 1176 | 1178 | "rpassword", |
| 1177 | 1179 | "serde", |
| @@ -1765,6 +1767,16 @@ version = "0.3.0" |
| 1765 | 1767 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 1766 | 1768 | checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" |
| 1767 | 1769 | |
| 1770 | [[package]] |
| 1771 | name = "open" |
| 1772 | version = "3.2.0" |
| 1773 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 1774 | checksum = "2078c0039e6a54a0c42c28faa984e115fb4c2d5bf2208f77d1961002df8576f8" |
| 1775 | dependencies = [ |
| 1776 | "pathdiff", |
| 1777 | "windows-sys", |
| 1778 | ] |
| 1779 | |
| 1768 | 1780 | [[package]] |
| 1769 | 1781 | name = "os_str_bytes" |
| 1770 | 1782 | version = "6.4.1" |
| @@ -1842,6 +1854,12 @@ dependencies = [ |
| 1842 | 1854 | "subtle", |
| 1843 | 1855 | ] |
| 1844 | 1856 | |
| 1857 | [[package]] |
| 1858 | name = "pathdiff" |
| 1859 | version = "0.2.1" |
| 1860 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 1861 | checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd" |
| 1862 | |
| 1845 | 1863 | [[package]] |
| 1846 | 1864 | name = "pbkdf2" |
| 1847 | 1865 | version = "0.11.0" |
Cargo.toml
+2
| @@ -14,6 +14,7 @@ categories = ["command-line-utilities"] |
| 14 | 14 | rust-version = "1.66" |
| 15 | 15 | |
| 16 | 16 | [dependencies] |
| 17 | bitflags = "1.3.2" |
| 17 | 18 | chrono = "0.4" |
| 18 | 19 | clap = {version = "4.0", features = ["derive"]} |
| 19 | 20 | css-color-parser = "0.1.2" |
| @@ -23,6 +24,7 @@ html5ever = "0.26.0" |
| 23 | 24 | markup5ever_rcdom = "0.2.0" |
| 24 | 25 | mime = "^0.3.16" |
| 25 | 26 | mime_guess = "^2.0.4" |
| 27 | open = "3.2.0" |
| 26 | 28 | regex = "^1.5" |
| 27 | 29 | rpassword = "^7.2" |
| 28 | 30 | serde = "^1.0" |
src/base.rs
+15 −3
| @@ -81,9 +81,9 @@ pub enum MessageAction { |
| 81 | 81 | |
| 82 | 82 | /// Download an attachment to the given path. |
| 83 | 83 | /// |
| 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), |
| 87 | 87 | |
| 88 | 88 | /// Edit a sent message. |
| 89 | 89 | Edit, |
| @@ -95,6 +95,18 @@ pub enum MessageAction { |
| 95 | 95 | Reply, |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | bitflags::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 | 110 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 99 | 111 | pub enum RoomField { |
| 100 | 112 | Name, |
src/commands.rs
+25 −1
| @@ -10,6 +10,7 @@ use modalkit::{ |
| 10 | 10 | }; |
| 11 | 11 | |
| 12 | 12 | use crate::base::{ |
| 13 | DownloadFlags, |
| 13 | 14 | IambAction, |
| 14 | 15 | IambId, |
| 15 | 16 | MessageAction, |
| @@ -317,7 +318,29 @@ fn iamb_download(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult |
| 317 | 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 | |
| 332 | fn 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 | 344 | let iact = IambAction::from(mact); |
| 322 | 345 | let step = CommandStep::Continue(iact.into(), ctx.context.take()); |
| 323 | 346 | |
| @@ -328,6 +351,7 @@ fn add_iamb_commands(cmds: &mut ProgramCommands) { |
| 328 | 351 | cmds.add_command(ProgramCommand { names: vec!["cancel".into()], f: iamb_cancel }); |
| 329 | 352 | cmds.add_command(ProgramCommand { names: vec!["dms".into()], f: iamb_dms }); |
| 330 | 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 | 355 | cmds.add_command(ProgramCommand { names: vec!["edit".into()], f: iamb_edit }); |
| 332 | 356 | cmds.add_command(ProgramCommand { names: vec!["invite".into()], f: iamb_invite }); |
| 333 | 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; |
| 4 | 4 | use std::ops::Deref; |
| 5 | 5 | use std::path::{Path, PathBuf}; |
| 6 | 6 | |
| 7 | use tokio; |
| 8 | |
| 7 | 9 | use matrix_sdk::{ |
| 8 | 10 | attachment::AttachmentConfig, |
| 9 | 11 | media::{MediaFormat, MediaRequest}, |
| @@ -55,6 +57,7 @@ use modalkit::editing::{ |
| 55 | 57 | }; |
| 56 | 58 | |
| 57 | 59 | use crate::base::{ |
| 60 | DownloadFlags, |
| 58 | 61 | IambAction, |
| 59 | 62 | IambBufferId, |
| 60 | 63 | IambError, |
| @@ -155,7 +158,7 @@ impl ChatState { |
| 155 | 158 | |
| 156 | 159 | Ok(None) |
| 157 | 160 | }, |
| 158 | | MessageAction::Download(filename, force) => { |
| 161 | MessageAction::Download(filename, flags) => { |
| 159 | 162 | if let MessageEvent::Original(ev) = &msg.event { |
| 160 | 163 | let media = client.media(); |
| 161 | 164 | |
| @@ -202,29 +205,42 @@ impl ChatState { |
| 202 | 205 | }, |
| 203 | 206 | }; |
| 204 | 207 | |
| 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); |
| 211 | 215 | |
| 212 | | return Err(err); |
| 213 | | } |
| 216 | return Err(err); |
| 217 | } |
| 214 | 218 | |
| 215 | | let req = MediaRequest { source, format: MediaFormat::File }; |
| 219 | let req = MediaRequest { source, format: MediaFormat::File }; |
| 216 | 220 | |
| 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)?; |
| 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!( |
| 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 | }; |
| 228 | 244 | |
| 229 | 245 | return Ok(info.into()); |
| 230 | 246 | } |