Просмотр исходного кода

bin/tau: prompt in editor for comments as well, like desc in editor

Dastan-glitch 3 лет назад
Родитель
Сommit
7e2bb06fc8
3 измененных файлов с 33 добавлено и 30 удалено
  1. 20 13
      bin/tau/tau-cli/src/main.rs
  2. 4 8
      bin/tau/tau-cli/src/util.rs
  3. 9 9
      bin/tau/tau-cli/src/view.rs

+ 20 - 13
bin/tau/tau-cli/src/main.rs

@@ -39,8 +39,10 @@ mod view;
 use drawdown::{drawdown, to_naivedate};
 use filter::{apply_filter, get_ids, no_filter_warn};
 use primitives::{task_from_cli, State, TaskEvent};
-use util::{desc_in_editor, due_as_timestamp};
-use view::{comments_as_string, print_task_info, print_task_list};
+use util::{due_as_timestamp, prompt_text};
+use view::{print_task_info, print_task_list};
+
+use crate::primitives::TaskInfo;
 
 const DEFAULT_PATH: &str = "~/tau_exported_tasks";
 
@@ -199,7 +201,7 @@ async fn main() -> Result<()> {
                 };
 
                 if task.desc.is_none() {
-                    task.desc = desc_in_editor(task.clone())?;
+                    task.desc = prompt_text(TaskInfo::from(task.clone()), "description")?;
                 };
 
                 if task.clone().desc.unwrap().trim().is_empty() {
@@ -292,17 +294,22 @@ async fn main() -> Result<()> {
                     no_filter_warn()
                 }
                 for task in tasks {
-                    if content.is_empty() {
-                        let task = tau.get_task_by_id(task.id.into()).await?;
-                        let comments = comments_as_string(task.comments);
-                        println!("Comments {}:\n{}", task.id, comments);
+                    let comment = if content.is_empty() {
+                        prompt_text(task.clone(), "comment")?
                     } else {
-                        let res = tau.set_comment(task.id.into(), &content.join(" ")).await?;
-                        if res {
-                            let tsk = tau.get_task_by_id(task.id.into()).await?;
-                            print_task_info(tsk)?;
-                            println!()
-                        }
+                        Some(content.join(" "))
+                    };
+
+                    if comment.clone().unwrap().trim().is_empty() || comment.is_none() {
+                        error!("Abort due to empty comment.");
+                        exit(1)
+                    }
+
+                    let res = tau.set_comment(task.id.into(), &comment.unwrap()).await?;
+                    if res {
+                        let tsk = tau.get_task_by_id(task.id.into()).await?;
+                        print_task_info(tsk)?;
+                        println!()
                     }
                 }
                 Ok(())

+ 4 - 8
bin/tau/tau-cli/src/util.rs

@@ -28,10 +28,7 @@ use log::error;
 
 use darkfi::{util::time::Timestamp, Result};
 
-use crate::{
-    primitives::{BaseTask, TaskInfo},
-    view::helper_taskinfo_func,
-};
+use crate::{primitives::TaskInfo, view::helper_taskinfo_func};
 
 /// Parse due date (e.g. "1503" for 15 March) as i64 timestamp.
 pub fn due_as_timestamp(due: &str) -> Option<i64> {
@@ -62,17 +59,16 @@ pub fn due_as_timestamp(due: &str) -> Option<i64> {
 }
 
 /// Start up the preferred editor to edit a task's description.
-pub fn desc_in_editor(task: BaseTask) -> Result<Option<String>> {
-    let task_info = TaskInfo::from(task);
+pub fn prompt_text(task_info: TaskInfo, what: &str) -> Result<Option<String>> {
     // Create a temporary file with some comments inside.
     let mut file_path = env::temp_dir();
     let file_name = format!("tau-{}", Timestamp::current_time().0);
     file_path.push(file_name);
     let mut file = File::create(&file_path)?;
 
-    writeln!(file, "\n# Write your task description here.")?;
+    writeln!(file, "\n# Write your task {what} above this line.")?;
     writeln!(file, "# Lines starting with \"#\" will be removed.")?;
-    writeln!(file, "# An empty description aborts adding the task.")?;
+    writeln!(file, "# An empty {what} aborts the operation.")?;
     writeln!(file, "\n# ------------------------ >8 ------------------------")?;
     writeln!(file, "# Do not modify or remove the line above.")?;
     writeln!(file, "# Everything below it will be ignored.")?;

+ 9 - 9
bin/tau/tau-cli/src/view.rs

@@ -30,7 +30,7 @@ use darkfi::{
 };
 
 use crate::{
-    primitives::{Comment, State, TaskInfo},
+    primitives::{State, TaskInfo},
     TaskEvent,
 };
 
@@ -165,14 +165,14 @@ pub fn print_task_info(taskinfo: TaskInfo) -> Result<()> {
     Ok(())
 }
 
-pub fn comments_as_string(comments: Vec<Comment>) -> String {
-    let mut comments_str = String::new();
-    for comment in comments {
-        writeln!(comments_str, "{}", comment).unwrap();
-    }
-    comments_str.pop();
-    comments_str
-}
+// pub fn comments_as_string(comments: Vec<Comment>) -> String {
+//     let mut comments_str = String::new();
+//     for comment in comments {
+//         writeln!(comments_str, "{}", comment).unwrap();
+//     }
+//     comments_str.pop();
+//     comments_str
+// }
 
 pub fn events_as_string(events: Vec<TaskEvent>) -> (String, String) {
     let mut events_str = String::new();