From 1c5e9b009b965fee7967a4327931d04f996a33ce Mon Sep 17 00:00:00 2001 From: "Dustin J. Mitchell" Date: Tue, 24 Nov 2020 12:05:30 -0500 Subject: [PATCH] Add Replica::get_working_set_index and use it This is probably ridiculously inefficient, as it will load the working set for each and every task listed. Optimize later! --- cli/src/cmd/info.rs | 9 +++++++-- cli/src/cmd/list.rs | 11 ++++++++--- taskchampion/src/replica.rs | 19 +++++++++++++++++++ 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/cli/src/cmd/info.rs b/cli/src/cmd/info.rs index bcb84d279..cdc39cc11 100644 --- a/cli/src/cmd/info.rs +++ b/cli/src/cmd/info.rs @@ -30,11 +30,16 @@ define_subcommand! { subcommand_invocation! { fn run(&self, command: &CommandInvocation) -> Fallible<()> { - let task = shared::get_task(&mut command.get_replica(), &self.task)?; + let mut replica = command.get_replica(); + let task = shared::get_task(&mut replica, &self.task)?; + let uuid = task.get_uuid(); let mut t = Table::new(); t.set_format(table::format()); - t.add_row(row![b->"Uuid", task.get_uuid()]); + t.add_row(row![b->"Uuid", uuid]); + if let Some(i) = replica.get_working_set_index(uuid)? { + t.add_row(row![b->"Id", i]); + } t.add_row(row![b->"Description", task.get_description()]); t.add_row(row![b->"Status", task.get_status()]); t.printstd(); diff --git a/cli/src/cmd/list.rs b/cli/src/cmd/list.rs index 2d51484e9..d29227c92 100644 --- a/cli/src/cmd/list.rs +++ b/cli/src/cmd/list.rs @@ -23,11 +23,16 @@ define_subcommand! { subcommand_invocation! { fn run(&self, command: &CommandInvocation) -> Fallible<()> { + let mut replica = command.get_replica(); let mut t = Table::new(); t.set_format(table::format()); - t.set_titles(row![b->"uuid", b->"description"]); - for (uuid, task) in command.get_replica().all_tasks().unwrap() { - t.add_row(row![uuid, task.get_description()]); + t.set_titles(row![b->"id", b->"description"]); + for (uuid, task) in replica.all_tasks().unwrap() { + let mut id = uuid.to_string(); + if let Some(i) = replica.get_working_set_index(&uuid)? { + id = i.to_string(); + } + t.add_row(row![id, task.get_description()]); } t.printstd(); Ok(()) diff --git a/taskchampion/src/replica.rs b/taskchampion/src/replica.rs index 19937eb2c..1485a9e0b 100644 --- a/taskchampion/src/replica.rs +++ b/taskchampion/src/replica.rs @@ -105,6 +105,19 @@ impl Replica { return Ok(None); } + /// Get the working set index for the given task uuid + pub fn get_working_set_index(&mut self, uuid: &Uuid) -> Fallible> { + let working_set = self.taskdb.working_set()?; + for (i, u) in working_set.iter().enumerate() { + if let Some(ref u) = u { + if u == uuid { + return Ok(Some(i)); + } + } + } + Ok(None) + } + /// Create a new task. The task must not already exist. pub fn new_task(&mut self, uuid: Uuid, status: Status, description: String) -> Fallible { // check that it doesn't exist; this is a convenience check, as the task @@ -223,6 +236,10 @@ mod tests { let t = rep.get_task(&uuid).unwrap().unwrap(); assert_eq!(t.get_status(), Status::Deleted); assert_eq!(t.get_description(), "gone"); + + rep.gc().unwrap(); + + assert!(rep.get_working_set_index(t.get_uuid()).unwrap().is_none()); } #[test] @@ -241,6 +258,8 @@ mod tests { assert_eq!(ws.len(), 2); assert!(ws[0].is_none()); assert_eq!(ws[1].as_ref().unwrap().get_uuid(), &uuid); + + assert_eq!(rep.get_working_set_index(t.get_uuid()).unwrap().unwrap(), 1); } #[test]