Compare commits
5 Commits
e409a2b632
...
da7661fe52
Author | SHA1 | Date |
---|---|---|
Dmitriy Pleshevskiy | da7661fe52 | |
Dmitriy Pleshevskiy | 2c65a7c262 | |
Dmitriy Pleshevskiy | cb52a466db | |
Dmitriy Pleshevskiy | b0cca6a408 | |
Dmitriy Pleshevskiy | 15b156414e |
115
src/cli/show.rs
115
src/cli/show.rs
|
@ -13,16 +13,22 @@
|
|||
//! 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 {
|
||||
part: Option<PrintPart>,
|
||||
#[clap(short, long)]
|
||||
clip: bool,
|
||||
|
||||
rest: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(clap::ValueEnum, Clone)]
|
||||
#[derive(Clone, clap::ValueEnum)]
|
||||
enum PrintPart {
|
||||
Project,
|
||||
DirPath,
|
||||
|
@ -43,38 +49,113 @@ impl std::str::FromStr for PrintPart {
|
|||
}
|
||||
|
||||
pub fn execute(repo: impl Repository, args: Args) {
|
||||
let task = match repo.get_current_task_opt() {
|
||||
Ok(None) => {
|
||||
return eprintln!("You don't have an active task.");
|
||||
let (idx, part) = match args.rest.len() {
|
||||
0 => (None, None),
|
||||
1 => {
|
||||
let first = args.rest.first().unwrap();
|
||||
match first.parse::<usize>() {
|
||||
Ok(idx) => (Some(idx), None),
|
||||
Err(idx_err) => match first.parse::<PrintPart>() {
|
||||
Ok(part) => (None, Some(part)),
|
||||
Err(part_err) => {
|
||||
return eprint!(
|
||||
r#"error: Invalid value "{}": {}; {}"#,
|
||||
first, idx_err, part_err
|
||||
)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
Ok(Some(CurrentTaskInfo { task, .. })) => task,
|
||||
Err(err) => {
|
||||
return eprintln!("Cannot read current task: {}", err);
|
||||
2 => {
|
||||
let mut rest = args.rest.iter();
|
||||
let first = rest.next().unwrap();
|
||||
let second = rest.next().unwrap();
|
||||
|
||||
let idx = match first.parse::<usize>() {
|
||||
Ok(idx) => Some(idx),
|
||||
Err(err) => return eprint!(r#"error: Invalid value "{}": {}"#, first, err),
|
||||
};
|
||||
let part = match second.parse::<PrintPart>() {
|
||||
Ok(part) => Some(part),
|
||||
Err(err) => return eprint!(r#"error: Invalid value "{}": {}"#, second, err),
|
||||
};
|
||||
|
||||
(idx, part)
|
||||
}
|
||||
_ => return eprintln!("error: To much arguments: {}", args.rest.join(",")),
|
||||
};
|
||||
|
||||
let task = if let Some(idx) = idx {
|
||||
match repo.get_task_opt(idx) {
|
||||
Ok(Some(task)) => task,
|
||||
Ok(None) => return eprintln!("The task not found"),
|
||||
Err(err) => return eprintln!("Cannot get the task: {}", err),
|
||||
}
|
||||
} else {
|
||||
match repo.get_current_task_opt() {
|
||||
Ok(None) => {
|
||||
return eprintln!("You don't have an active task.");
|
||||
}
|
||||
Ok(Some(CurrentTaskInfo { task, .. })) => task,
|
||||
Err(err) => {
|
||||
return eprintln!("Cannot read current task: {}", err);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
match args.part {
|
||||
match 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");
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue