Commit 1d93461183
Unregistered key
Layout: unified · split
Cargo.lock +18
| @@ -1160,6 +1160,7 @@ dependencies = [ | |||
| 1160 | name = "iamb" | 1160 | name = "iamb" |
| 1161 | version = "0.0.4" | 1161 | version = "0.0.4" |
| 1162 | dependencies = [ | 1162 | dependencies = [ |
| 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" | |||
| 1765 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1767 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 1766 | checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" | 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 | [[package]] | 1780 | [[package]] |
| 1769 | name = "os_str_bytes" | 1781 | name = "os_str_bytes" |
| 1770 | version = "6.4.1" | 1782 | version = "6.4.1" |
| @@ -1842,6 +1854,12 @@ dependencies = [ | |||
| 1842 | "subtle", | 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 | [[package]] | 1863 | [[package]] |
| 1846 | name = "pbkdf2" | 1864 | name = "pbkdf2" |
| 1847 | version = "0.11.0" | 1865 | version = "0.11.0" |
Cargo.toml +2
| @@ -14,6 +14,7 @@ categories = ["command-line-utilities"] | |||
| 14 | rust-version = "1.66" | 14 | rust-version = "1.66" |
| 15 | 15 | ||
| 16 | [dependencies] | 16 | [dependencies] |
| 17 | bitflags = "1.3.2" | ||
| 17 | chrono = "0.4" | 18 | chrono = "0.4" |
| 18 | clap = {version = "4.0", features = ["derive"]} | 19 | clap = {version = "4.0", features = ["derive"]} |
| 19 | css-color-parser = "0.1.2" | 20 | css-color-parser = "0.1.2" |
| @@ -23,6 +24,7 @@ html5ever = "0.26.0" | |||
| 23 | markup5ever_rcdom = "0.2.0" | 24 | markup5ever_rcdom = "0.2.0" |
| 24 | mime = "^0.3.16" | 25 | mime = "^0.3.16" |
| 25 | mime_guess = "^2.0.4" | 26 | mime_guess = "^2.0.4" |
| 27 | open = "3.2.0" | ||
| 26 | regex = "^1.5" | 28 | regex = "^1.5" |
| 27 | rpassword = "^7.2" | 29 | rpassword = "^7.2" |
| 28 | serde = "^1.0" | 30 | serde = "^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 | ||
| 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 | #[derive(Clone, Debug, Eq, PartialEq)] | 110 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 99 | pub enum RoomField { | 111 | pub enum RoomField { |
| 100 | Name, | 112 | Name, |
src/commands.rs +25 −1
| @@ -10,6 +10,7 @@ use modalkit::{ | |||
| 10 | }; | 10 | }; |
| 11 | 11 | ||
| 12 | use crate::base::{ | 12 | use 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 | |||
| 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 | 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; | |||
| 4 | use std::ops::Deref; | 4 | use std::ops::Deref; |
| 5 | use std::path::{Path, PathBuf}; | 5 | use std::path::{Path, PathBuf}; |
| 6 | 6 | ||
| 7 | use tokio; | ||
| 8 | |||
| 7 | use matrix_sdk::{ | 9 | use 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 | ||
| 57 | use crate::base::{ | 59 | use 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 | } |