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
//! 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::domain::CurrentTaskInfo;
use crate::repo::Repository;
#[derive(clap::Args)]
pub struct Args {
#[clap(short, long)]
clip: bool,
part: Option<PrintPart>,
}
@ -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");
}