From 15b156414ebb4a0d0b35fbb724451fcc9c6dd6fe Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Thu, 18 Aug 2022 15:02:18 +0300 Subject: [PATCH] cli/show: add possibility to copy the task part Closes #22 --- src/cli/show.rs | 53 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/src/cli/show.rs b/src/cli/show.rs index a237c99..7deaaad 100644 --- a/src/cli/show.rs +++ b/src/cli/show.rs @@ -13,12 +13,18 @@ //! You should have received a copy of the GNU General Public License //! along with tas. If not, see . //! +use std::io::Write; +use std::process::{Command, Stdio}; + use crate::cli::print_task_detail; use crate::domain::CurrentTaskInfo; use crate::repo::Repository; #[derive(clap::Args)] pub struct Args { + #[clap(short, long)] + clip: bool, + part: Option, } @@ -55,26 +61,57 @@ pub fn execute(repo: impl Repository, args: Args) { match args.part { None => { + if args.clip { + eprintln!("[WARNING]: You don't provide part. --clip option will ignore."); + } + println!("Information about your current task:"); print_task_detail(&task); } Some(PrintPart::Project) => { - println!("{}", task.project.unwrap_or_else(|| String::from('-'))) + if args.clip { + if let Some(project) = task.project { + save_to_clipboard(&project); + } else { + eprintln!("Task doesn't contain a project"); + } + } else { + println!("{}", task.project.unwrap_or_else(|| String::from('-'))) + } } Some(PrintPart::DirPath) => { - println!( - "{}", - task.dir_path - .unwrap_or_else(|| std::env::current_dir().expect("Cannot get current dir")) - .to_string_lossy() - ) + let dir_path = task + .dir_path + .unwrap_or_else(|| std::env::current_dir().expect("Cannot get current dir")); + if args.clip { + save_to_clipboard(&dir_path.to_string_lossy()) + } else { + println!("{}", dir_path.to_string_lossy()) + } } Some(PrintPart::Link) => { if let Some(link) = task.link { - println!("{}", link) + if args.clip { + save_to_clipboard(&link); + } else { + println!("{}", link) + } } else { eprintln!("The current task doesn't contain link") } } } } + +fn save_to_clipboard(input: &str) { + let xclip_cmd = Command::new("xclip") + .args(["-sel", "clip"]) + .stdin(Stdio::piped()) + .spawn() + .expect("Cannot spawn xclip command"); + + let mut xclip_stdin = xclip_cmd.stdin.expect("Cannot get stdin for xclip command"); + xclip_stdin + .write_all(input.as_bytes()) + .expect("Cannot save data to the clipboard"); +}