Browse Source

Improve clipboard string building

timvisee 6 năm trước cách đây
mục cha
commit
d0a301b300
2 tập tin đã thay đổi với 16 bổ sung12 xóa
  1. 4 11
      src/action/upload.rs
  2. 12 1
      src/cmd/matcher/upload.rs

+ 4 - 11
src/action/upload.rs

@@ -26,8 +26,8 @@ use crate::progress::ProgressBar;
 #[cfg(feature = "clipboard")]
 use crate::util::set_clipboard;
 use crate::util::{
-    bin_name, format_bytes, open_url, print_error, print_error_msg, prompt_yes, quit,
-    quit_error_msg, ErrorHintsBuilder,
+    format_bytes, open_url, print_error, print_error_msg, prompt_yes, quit, quit_error_msg,
+    ErrorHintsBuilder,
 };
 
 /// A file upload action.
@@ -265,18 +265,11 @@ impl<'a> Upload<'a> {
             };
         }
 
-        // Copy the URL in the user's clipboard
+        // Copy the URL or command to the user's clipboard
         #[cfg(feature = "clipboard")]
         {
             if let Some(copy_mode) = matcher_upload.copy() {
-                // Build the string to copy
-                let copy = match copy_mode {
-                    CopyMode::Url => url.as_str().to_owned(),
-                    CopyMode::DownloadCmd => format!("{} download {}", bin_name(), url.as_str()),
-                };
-
-                // Copy to clipboard
-                if let Err(err) = set_clipboard(copy) {
+                if let Err(err) = set_clipboard(copy_mode.build(url.as_str())) {
                     print_error(
                         err.context("failed to copy the share link to the clipboard, ignoring"),
                     );

+ 12 - 1
src/cmd/matcher/upload.rs

@@ -6,7 +6,7 @@ use super::Matcher;
 use crate::cmd::arg::{
     ArgDownloadLimit, ArgGenPassphrase, ArgHost, ArgPassword, CmdArgFlag, CmdArgOption,
 };
-use crate::util::{env_var_present, quit_error_msg, ErrorHintsBuilder};
+use crate::util::{bin_name, env_var_present, quit_error_msg, ErrorHintsBuilder};
 
 /// The upload command matcher.
 pub struct UploadMatcher<'a> {
@@ -120,6 +120,7 @@ impl<'a> Matcher<'a> for UploadMatcher<'a> {
 }
 
 /// The copy mode.
+#[derive(Debug, Copy, Clone)]
 pub enum CopyMode {
     /// Copy the public share link.
     Url,
@@ -127,3 +128,13 @@ pub enum CopyMode {
     /// Copy an ffsend download command.
     DownloadCmd,
 }
+
+impl CopyMode {
+    /// Build the string to copy, based on the given `url` and currend mode.
+    pub fn build(&self, url: &str) -> String {
+        match self {
+            CopyMode::Url => url.into(),
+            CopyMode::DownloadCmd => format!("{} download {}", bin_name(), url),
+        }
+    }
+}