From 15890f4d6539735bb8c0aba12dd85f41aed9ecde Mon Sep 17 00:00:00 2001 From: Dastan-glitch Date: Thu, 10 Mar 2022 22:55:51 -0500 Subject: [PATCH] bin/tau-cli: reading title and desc from user input if not specified in the command --- bin/tau-cli/src/main.rs | 65 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 57 insertions(+), 8 deletions(-) diff --git a/bin/tau-cli/src/main.rs b/bin/tau-cli/src/main.rs index b619713aa..1f35ffe39 100644 --- a/bin/tau-cli/src/main.rs +++ b/bin/tau-cli/src/main.rs @@ -1,6 +1,12 @@ use clap::{AppSettings, IntoApp, Parser, Subcommand}; use log::{debug, error}; +use std::{ + env::{temp_dir, var}, + fs::{self, File}, + io::{Read, Write}, +}; + use darkfi::{ rpc::jsonrpc::{self, JsonResult}, util::cli::log_config, @@ -8,6 +14,7 @@ use darkfi::{ }; use serde_json::{json, Value}; use simplelog::{ColorChoice, TermLogger, TerminalMode}; +use std::{io, process::Command}; use url::Url; #[derive(Subcommand)] @@ -15,9 +22,9 @@ pub enum CliTauSubCommands { /// Add a new task Add { #[clap(short, long)] - title: String, + title: Option, #[clap(long)] - desc: String, + desc: Option, #[clap(short, long)] assign: Option, #[clap(short, long)] @@ -25,7 +32,7 @@ pub enum CliTauSubCommands { #[clap(short, long)] due: Option, #[clap(short, long)] - rank: Option, + rank: Option, }, } @@ -73,12 +80,12 @@ async fn request(r: jsonrpc::JsonRequest, url: String) -> Result { // <-- {"jsonrpc": "2.0", "result": true, "id": 1} async fn add( url: String, - title: String, - desc: String, + title: Option, + desc: Option, assign: Option, project: Option, due: Option, - rank: Option, + rank: Option, ) -> Result { let req = jsonrpc::request(json!("add"), json!([title, desc, assign, project, due, rank])); Ok(request(req, url).await?) @@ -89,8 +96,50 @@ async fn start(options: CliTau) -> Result<()> { if let Some(CliTauSubCommands::Add { title, desc, assign, project, due, rank }) = options.command { - add(rpc_addr.to_string(), title.clone(), desc, assign, project, due, rank).await?; - println!("Added task: {}", title); + let t = if title.is_none() { + print!("Title: "); + io::stdout().flush().unwrap(); + let mut t = String::new(); + io::stdin().read_line(&mut t)?; + if &t[(t.len() - 1)..] == "\n" { + t.pop(); + } + Some(t) + } else { + title + }; + + let des = if desc.is_none() { + let editor = var("EDITOR").unwrap(); + let mut file_path = temp_dir(); + file_path.push("temp_file"); + File::create(&file_path)?; + fs::write( + &file_path, + "\n# Write task description above this line\n# These lines will be removed\n", + )?; + + Command::new(editor).arg(&file_path).status()?; + + let mut lines = String::new(); + File::open(file_path)?.read_to_string(&mut lines)?; + + let mut description = String::new(); + for line in lines.split('\n') { + if !line.starts_with('#') { + description.push_str(line) + } + } + + Some(description) + } else { + desc + }; + + let r = if rank.is_none() { Some(0) } else { rank }; + + add(rpc_addr.to_string(), t, des, assign, project, due, r).await?; + //println!("Added task: {:#?}", t); return Ok(()) } error!("Please run 'tau help' to see usage.");