cli/show: add possibility to copy the task part

Closes #22
This commit is contained in:
Dmitriy Pleshevskiy 2022-08-18 15:02:18 +03:00
parent 18004c24d0
commit 15b156414e
Signed by: pleshevskiy
GPG Key ID: 1B59187B161C0215
1 changed files with 45 additions and 8 deletions

View File

@ -13,12 +13,18 @@
//! You should have received a copy of the GNU General Public License //! You should have received a copy of the GNU General Public License
//! along with tas. If not, see <https://www.gnu.org/licenses/>. //! along with tas. If not, see <https://www.gnu.org/licenses/>.
//! //!
use std::io::Write;
use std::process::{Command, Stdio};
use crate::cli::print_task_detail; use crate::cli::print_task_detail;
use crate::domain::CurrentTaskInfo; use crate::domain::CurrentTaskInfo;
use crate::repo::Repository; use crate::repo::Repository;
#[derive(clap::Args)] #[derive(clap::Args)]
pub struct Args { pub struct Args {
#[clap(short, long)]
clip: bool,
part: Option<PrintPart>, part: Option<PrintPart>,
} }
@ -55,26 +61,57 @@ pub fn execute(repo: impl Repository, args: Args) {
match args.part { match args.part {
None => { None => {
if args.clip {
eprintln!("[WARNING]: You don't provide part. --clip option will ignore.");
}
println!("Information about your current task:"); println!("Information about your current task:");
print_task_detail(&task); print_task_detail(&task);
} }
Some(PrintPart::Project) => { 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) => { Some(PrintPart::DirPath) => {
println!( let dir_path = task
"{}", .dir_path
task.dir_path .unwrap_or_else(|| std::env::current_dir().expect("Cannot get current dir"));
.unwrap_or_else(|| std::env::current_dir().expect("Cannot get current dir")) if args.clip {
.to_string_lossy() save_to_clipboard(&dir_path.to_string_lossy())
) } else {
println!("{}", dir_path.to_string_lossy())
}
} }
Some(PrintPart::Link) => { Some(PrintPart::Link) => {
if let Some(link) = task.link { if let Some(link) = task.link {
println!("{}", link) if args.clip {
save_to_clipboard(&link);
} else {
println!("{}", link)
}
} else { } else {
eprintln!("The current task doesn't contain link") 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");
}