diff options
Diffstat (limited to '')
-rw-r--r-- | pkgs/by-name/ts/tskm/src/cli.rs | 166 | ||||
-rw-r--r-- | pkgs/by-name/ts/tskm/src/interface/neorg/handle.rs | 51 | ||||
-rw-r--r-- | pkgs/by-name/ts/tskm/src/interface/neorg/mod.rs | 9 | ||||
-rw-r--r-- | pkgs/by-name/ts/tskm/src/interface/open/handle.rs | 115 | ||||
-rw-r--r-- | pkgs/by-name/ts/tskm/src/main.rs | 55 | ||||
-rw-r--r-- | pkgs/by-name/ts/tskm/src/state.rs | 45 | ||||
-rw-r--r-- | pkgs/by-name/ts/tskm/src/task/mod.rs | 186 |
7 files changed, 439 insertions, 188 deletions
diff --git a/pkgs/by-name/ts/tskm/src/cli.rs b/pkgs/by-name/ts/tskm/src/cli.rs index bf0af7fb..c1eba387 100644 --- a/pkgs/by-name/ts/tskm/src/cli.rs +++ b/pkgs/by-name/ts/tskm/src/cli.rs @@ -1,10 +1,13 @@ -use std::path::PathBuf; +use std::{ffi::OsStr, path::PathBuf}; -use clap::{Parser, Subcommand}; +use anyhow::{bail, Result}; +use clap::{builder::StyledStr, ArgAction, Parser, Subcommand}; +use clap_complete::{ArgValueCompleter, CompletionCandidate}; +use url::Url; use crate::{ interface::{input::Input, project::ProjectName}, - task, + state, task, }; #[derive(Parser, Debug)] @@ -14,13 +17,23 @@ use crate::{ /// `tskm` effectively combines multiple applications together: /// - `taskwarrior` projects are raised connected to `firefox` profiles, making it possible to “open” /// a project. +/// /// - Every `taskwarrior` project has a determined `neorg` path, so that extra information for a /// `project` can be stored in this `norg` file. +/// /// - `tskm` can track inputs for you. These are URLs with optional tags which you can that /// “review” to open tasks based on them. pub struct CliArgs { #[command(subcommand)] pub command: Command, + + /// Increase message verbosity + #[arg(long="verbose", short = 'v', action = ArgAction::Count, default_value_t = 2)] + pub verbosity: u8, + + /// Silence all output + #[arg(long, short = 'q')] + pub quiet: bool, } #[derive(Subcommand, Debug)] @@ -66,7 +79,21 @@ pub enum ProjectCommand { #[derive(Subcommand, Debug, Clone, Copy)] pub enum NeorgCommand { /// Open the `neorg` project associated with id of the task. - Task { id: task::Id }, + Task { + /// The working set id of the task + #[arg(value_parser = task_from_working_set_id, add = ArgValueCompleter::new(complete_task_id))] + id: task::Task, + }, +} + +fn task_from_working_set_id(id: &str) -> Result<task::Task> { + let id: usize = id.parse()?; + let mut state = state::State::new_ro()?; + + let Some(task) = task::Task::from_working_set(id, &mut state)? else { + bail!("Working set id '{id}' is not valid!") + }; + Ok(task) } #[derive(Subcommand, Debug)] @@ -79,22 +106,28 @@ pub enum OpenCommand { /// Opens Firefox with either the supplied project or the currently active project profile. Project { /// The project to open. - #[arg(value_parser = task::Project::from_project_string)] - project: Option<task::Project>, + #[arg(value_parser = task::Project::from_project_string, add = ArgValueCompleter::new(complete_project))] + project: task::Project, + + /// The URL to open. + url: Option<Url>, }, /// Open a selected project in it's Firefox profile. /// /// This will use rofi's dmenu mode to select one project from the list of all registered /// projects. - Select, + Select { + /// The URL to open. + url: Option<Url>, + }, /// List all open tabs in the project. ListTabs { /// The project to open. - #[arg(value_parser = task::Project::from_project_string)] + #[arg(value_parser = task::Project::from_project_string, add = ArgValueCompleter::new(complete_project))] project: Option<task::Project>, - } + }, } #[derive(Subcommand, Debug)] @@ -102,7 +135,10 @@ pub enum InputCommand { /// Add URLs as inputs to be categorized. Add { inputs: Vec<Input> }, /// Remove URLs - Remove { inputs: Vec<Input> }, + Remove { + #[arg(add = ArgValueCompleter::new(complete_input_url))] + inputs: Vec<Input>, + }, /// Add all URLs in the file as inputs to be categorized. /// @@ -113,10 +149,118 @@ pub enum InputCommand { /// It takes a project in which to open the URLs. Review { /// Opens all the URLs in this project. - #[arg(value_parser = task::Project::from_project_string)] + #[arg(value_parser = task::Project::from_project_string, add = ArgValueCompleter::new(complete_project))] project: task::Project, }, /// List all the previously added inputs. List, } + +fn complete_task_id(current: &OsStr) -> Vec<CompletionCandidate> { + fn format_task( + task: task::Task, + current: &str, + state: &mut state::State, + ) -> Option<CompletionCandidate> { + let id = { + let Ok(base) = task.working_set_id(state) else { + return None; + }; + base.to_string() + }; + + if !id.starts_with(current) { + return None; + } + + let description = { + let Ok(base) = task.description(state) else { + return None; + }; + StyledStr::from(base) + }; + + Some(CompletionCandidate::new(id).help(Some(description))) + } + + let mut output = vec![]; + + let Some(current) = current.to_str() else { + return output; + }; + + let Ok(mut state) = state::State::new_ro() else { + return output; + }; + + let Ok(pending) = state.replica().pending_tasks() else { + return output; + }; + + let Ok(current_project) = task::Project::get_current() else { + return output; + }; + + if let Some(current_project) = current_project { + for t in pending { + let task = task::Task::from(&t); + if let Ok(project) = task.project(&mut state) { + if project == current_project { + if let Some(out) = format_task(task, current, &mut state) { + output.push(out); + } else { + continue; + } + } + } + } + } else { + for t in pending { + let task = task::Task::from(&t); + if let Some(out) = format_task(task, current, &mut state) { + output.push(out); + } + } + } + + output +} +fn complete_project(current: &OsStr) -> Vec<CompletionCandidate> { + let mut output = vec![]; + + let Some(current) = current.to_str() else { + return output; + }; + + let Ok(all) = task::Project::all() else { + return output; + }; + + for a in all { + if a.to_project_display().starts_with(current) { + output.push(CompletionCandidate::new(a.to_project_display())); + } + } + + output +} +fn complete_input_url(current: &OsStr) -> Vec<CompletionCandidate> { + let mut output = vec![]; + + let Some(current) = current.to_str() else { + return output; + }; + + let Ok(all) = Input::all() else { + return output; + }; + + for a in all { + if a.to_string().starts_with(current) { + output.push(CompletionCandidate::new(a.to_string())); + } + } + + output +} diff --git a/pkgs/by-name/ts/tskm/src/interface/neorg/handle.rs b/pkgs/by-name/ts/tskm/src/interface/neorg/handle.rs index a9a46ee7..d904b12e 100644 --- a/pkgs/by-name/ts/tskm/src/interface/neorg/handle.rs +++ b/pkgs/by-name/ts/tskm/src/interface/neorg/handle.rs @@ -1,33 +1,46 @@ use std::{ env, - fs::{self, read_to_string, OpenOptions}, + fs::{self, read_to_string, File, OpenOptions}, io::Write, process::Command, }; -use anyhow::{bail, Result}; +use anyhow::{bail, Context, Result}; -use crate::cli::NeorgCommand; +use crate::{cli::NeorgCommand, state::State}; -pub fn handle(command: NeorgCommand) -> Result<()> { +pub fn handle(command: NeorgCommand, state: &mut State) -> Result<()> { match command { NeorgCommand::Task { id } => { - let project = id.project()?; - let path = dirs::data_local_dir() + let project = id.project(state)?; + let base = dirs::data_local_dir() .expect("This should exists") - .join("notes") - .join(project.get_neorg_path()?); + .join("tskm/notes"); + let path = base.join(project.get_neorg_path()?); fs::create_dir_all(path.parent().expect("This should exist"))?; { - let contents = read_to_string(&path)?; - if contents.contains(format!("% {}", id.to_uuid()?).as_str()) { + let contents = if path.exists() { + read_to_string(&path) + .with_context(|| format!("Failed to read file: '{}'", path.display()))? + } else { + File::create(&path) + .with_context(|| format!("Failed to create file: '{}'", path.display()))?; + String::new() + }; + + if !contents.contains(format!("% {}", id.uuid()).as_str()) { let mut options = OpenOptions::new(); - options.append(true).create(true); + options.append(true).create(false); let mut file = options.open(&path)?; - file.write_all(format!("* TITLE (% {})", id.to_uuid()?).as_bytes())?; + file.write_all(format!("* TITLE (% {})", id.uuid()).as_bytes()) + .with_context(|| { + format!("Failed to write task uuid to file: '{}'", path.display()) + })?; + file.flush() + .with_context(|| format!("Failed to flush file: '{}'", path.display()))?; } } @@ -36,7 +49,7 @@ pub fn handle(command: NeorgCommand) -> Result<()> { .args([ path.to_str().expect("Should be a utf-8 str"), "-c", - format!("/% {}", id.to_uuid()?).as_str(), + format!("/% {}", id.uuid()).as_str(), ]) .status()?; if !status.success() { @@ -46,7 +59,7 @@ pub fn handle(command: NeorgCommand) -> Result<()> { { let status = Command::new("git") .args(["add", "."]) - .current_dir(&path) + .current_dir(path.parent().expect("Will exist")) .status()?; if !status.success() { bail!("Git add . failed!"); @@ -56,14 +69,10 @@ pub fn handle(command: NeorgCommand) -> Result<()> { .args([ "commit", "--message", - format!( - "chore({}): Update", - path.parent().expect("Should have a parent").display() - ) - .as_str(), + format!("chore({}): Update", project.get_neorg_path()?.display()).as_str(), "--no-gpg-sign", ]) - .current_dir(&path) + .current_dir(path.parent().expect("Will exist")) .status()?; if !status.success() { bail!("Git commit failed!"); @@ -71,7 +80,7 @@ pub fn handle(command: NeorgCommand) -> Result<()> { } { - id.annotate("[neorg data]")?; + id.mark_neorg_data(state)?; } } } diff --git a/pkgs/by-name/ts/tskm/src/interface/neorg/mod.rs b/pkgs/by-name/ts/tskm/src/interface/neorg/mod.rs index dc5cdf19..51d58ab3 100644 --- a/pkgs/by-name/ts/tskm/src/interface/neorg/mod.rs +++ b/pkgs/by-name/ts/tskm/src/interface/neorg/mod.rs @@ -8,11 +8,18 @@ pub mod handle; pub use handle::handle; impl Project { + /// Return the stored neorg path of this project. + /// The returned path will never start with a slash (/). pub(super) fn get_neorg_path(&self) -> Result<PathBuf> { let project_path = run_task(&[ "_get", format!("rc.context.{}.rc.neorg_path", self.to_context_display()).as_str(), ])?; - Ok(PathBuf::from(project_path.as_str())) + + let final_path = project_path + .strip_prefix('/') + .unwrap_or(project_path.as_str()); + + Ok(PathBuf::from(final_path)) } } diff --git a/pkgs/by-name/ts/tskm/src/interface/open/handle.rs b/pkgs/by-name/ts/tskm/src/interface/open/handle.rs index dc0d165d..4d7341b2 100644 --- a/pkgs/by-name/ts/tskm/src/interface/open/handle.rs +++ b/pkgs/by-name/ts/tskm/src/interface/open/handle.rs @@ -1,17 +1,23 @@ -use std::process; +use std::{ + fs, + net::{IpAddr, Ipv4Addr}, + path::PathBuf, + process, +}; use anyhow::{bail, Context, Result}; -use log::{error, info}; +use log::{error, info, warn}; +use url::Url; -use crate::{cli::OpenCommand, rofi, task}; +use crate::{cli::OpenCommand, rofi, state::State, task}; -pub fn handle(command: OpenCommand) -> Result<()> { +pub fn handle(command: OpenCommand, state: &mut State) -> Result<()> { match command { OpenCommand::Review => { for project in task::Project::all().context("Failed to get all project files")? { if project.is_touched() { info!("Reviewing project: '{}'", project.to_project_display()); - open_in_browser(project).with_context(|| { + open_in_browser(project, state, None).with_context(|| { format!( "Failed to open project ('{}') in Firefox", project.to_project_display() @@ -26,23 +32,13 @@ pub fn handle(command: OpenCommand) -> Result<()> { } } } - OpenCommand::Project { project } => { - let project = if let Some(p) = project { - p - } else if let Some(p) = - task::Project::get_current().context("Failed to get currently focused project")? - { - p - } else { - bail!("You need to either supply a project or have a project active!"); - }; - + OpenCommand::Project { project, url } => { project.touch().context("Failed to touch project")?; - open_in_browser(&project).with_context(|| { + open_in_browser(&project, state, url).with_context(|| { format!("Failed to open project: {}", project.to_project_display()) })?; } - OpenCommand::Select => { + OpenCommand::Select { url } => { let selected_project: task::Project = task::Project::from_project_string( &rofi::select( task::Project::all() @@ -60,7 +56,7 @@ pub fn handle(command: OpenCommand) -> Result<()> { .touch() .context("Failed to touch project")?; - open_in_browser(&selected_project).context("Failed to open project")?; + open_in_browser(&selected_project, state, url).context("Failed to open project")?; } OpenCommand::ListTabs { project } => { let project = if let Some(p) = project { @@ -109,12 +105,15 @@ pub fn handle(command: OpenCommand) -> Result<()> { Ok(()) } -fn open_in_browser(selected_project: &task::Project) -> Result<()> { +fn open_in_browser( + selected_project: &task::Project, + state: &mut State, + url: Option<Url>, +) -> Result<()> { let old_project: Option<task::Project> = task::Project::get_current().context("Failed to get currently active project")?; - // We have ensured that only one task may be active - let old_task: Option<task::Id> = - task::Id::get_current().context("Failed to get currently active task")?; + let old_task: Option<task::Task> = + task::Task::get_current(state).context("Failed to get currently active task")?; selected_project.activate().with_context(|| { format!( @@ -124,7 +123,7 @@ fn open_in_browser(selected_project: &task::Project) -> Result<()> { })?; let tracking_task = { - let all_tasks = selected_project.get_tasks().with_context(|| { + let all_tasks = selected_project.get_tasks(state).with_context(|| { format!( "Failed to get assoctiated tasks for project: '{}'", selected_project.to_project_display() @@ -132,7 +131,7 @@ fn open_in_browser(selected_project: &task::Project) -> Result<()> { })?; let tracking_task = all_tasks.into_iter().find(|t| { - let maybe_desc = t.description(); + let maybe_desc = t.description(state); if let Ok(desc) = maybe_desc { desc == "tracking" } else { @@ -149,31 +148,75 @@ fn open_in_browser(selected_project: &task::Project) -> Result<()> { "Starting task {} -> tracking", selected_project.to_project_display() ); - task.start() + task.start(state) .with_context(|| format!("Failed to start task {task}"))?; } tracking_task }; - let status = process::Command::new("firefox") - .args([ - "-P", - selected_project.to_project_display().as_str(), - "about:newtab", - ]) - .status() - .context("Failed to start firefox")?; + let status = { + let mut args = vec!["-P".to_owned(), selected_project.to_project_display()]; + if let Some(url) = url { + args.push(url.to_string()); + } else { + let lock_file = dirs::home_dir() + .expect("Exists") + .join(".mozilla/firefox") + .join(selected_project.to_project_display()) + .join("lock"); + + if lock_file.exists() { + let (ip, pid): (IpAddr, u32) = { + let link = fs::read_link(&lock_file).with_context(|| { + format!("Failed to readlink lock at '{}'", lock_file.display()) + })?; + + let (ip, pid) = link + .to_str() + .expect("Should work") + .split_once(':') + .expect("The split works"); + + ( + ip.parse().expect("Should be a valid ip address"), + pid.parse().expect("Should be a valid pid"), + ) + }; + + if ip != Ipv4Addr::new(127, 0, 0, 2) { + warn!("Your ip is weird.."); + } + + if PathBuf::from("/proc").join(pid.to_string()).exists() { + // Another Firefox instance has already been started for this project + // Add a buffer URL to force Firefox to open it in the already open instance + args.push("about:newtab".to_owned()); + } else { + // This project does not yet have another Firefox instance + // We do not need to add anything to the arguments, Firefox will open a new + // instance. + } + } else { + // There is no lock file and thus no instance already open. + } + }; + + process::Command::new("firefox") + .args(args) + .status() + .context("Failed to start firefox")? + }; if !status.success() { error!("Firefox run exited with error."); } if let Some(task) = tracking_task { - task.stop() + task.stop(state) .with_context(|| format!("Failed to stop task {task}"))?; } if let Some(task) = old_task { - task.start() + task.start(state) .with_context(|| format!("Failed to start task {task}"))?; } diff --git a/pkgs/by-name/ts/tskm/src/main.rs b/pkgs/by-name/ts/tskm/src/main.rs index 7fc9c0d4..77f2dcca 100644 --- a/pkgs/by-name/ts/tskm/src/main.rs +++ b/pkgs/by-name/ts/tskm/src/main.rs @@ -1,64 +1,39 @@ -#![allow(clippy::missing_panics_doc)] -#![allow(clippy::missing_errors_doc)] - use anyhow::Result; -use clap::Parser; +use clap::{CommandFactory, Parser}; -use crate::interface::{input, neorg, open, project}; +use crate::{ + cli::{CliArgs, Command}, + interface::{input, neorg, open, project}, + state::State, +}; pub mod cli; pub mod interface; pub mod rofi; +pub mod state; pub mod task; -use crate::cli::{CliArgs, Command}; - fn main() -> Result<(), anyhow::Error> { - // TODO: Support these completions for the respective types <2025-04-04> - // - // ID_GENERATION_FUNCTION - // ```sh - // context="$(task _get rc.context)" - // if [ "$context" ]; then - // filter="project:$context" - // else - // filter="0-10000" - // fi - // tasks="$(task "$filter" _ids)" - // - // if [ "$tasks" ]; then - // echo "$tasks" | xargs task _zshids | awk -F: -v q="'" '{gsub(/'\''/, q "\\" q q ); print $1 ":" q $2 q}' - // fi - // ``` - // - // ARGUMENTS: - // ID | *([0-9]) := [[%ID_GENERATION_FUNCTION]] - // The function displays all possible IDs of the eligible tasks. - // - // WS := %ALL_WORKSPACES - // All possible workspaces. - // - // P := %ALL_PROJECTS_PIPE - // The possible project. - // - // F := [[fd . --max-depth 3]] - // A URL-Input file to use as source. + clap_complete::CompleteEnv::with_factory(CliArgs::command).complete(); + let args = CliArgs::parse(); stderrlog::new() .module(module_path!()) - .quiet(false) + .quiet(args.quiet) .show_module_names(true) .color(stderrlog::ColorChoice::Auto) - .verbosity(5) + .verbosity(usize::from(args.verbosity)) .timestamp(stderrlog::Timestamp::Off) .init() .expect("Let's just hope that this does not panic"); + let mut state = State::new_rw()?; + match args.command { Command::Inputs { command } => input::handle(command)?, - Command::Neorg { command } => neorg::handle(command)?, - Command::Open { command } => open::handle(command)?, + Command::Neorg { command } => neorg::handle(command, &mut state)?, + Command::Open { command } => open::handle(command, &mut state)?, Command::Projects { command } => project::handle(command)?, } diff --git a/pkgs/by-name/ts/tskm/src/state.rs b/pkgs/by-name/ts/tskm/src/state.rs new file mode 100644 index 00000000..175a7f03 --- /dev/null +++ b/pkgs/by-name/ts/tskm/src/state.rs @@ -0,0 +1,45 @@ +use std::path::PathBuf; + +use anyhow::Result; +use taskchampion::{storage::AccessMode, Replica, StorageConfig}; + +pub struct State { + replica: Replica, +} + +impl std::fmt::Debug for State { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "State") + } +} + +impl State { + fn taskdb_dir() -> PathBuf { + dirs::data_local_dir().expect("Should exist").join("task") + } + + fn new(taskdb_dir: PathBuf, access_mode: AccessMode) -> Result<Self> { + let storage = StorageConfig::OnDisk { + taskdb_dir, + create_if_missing: false, + access_mode, + } + .into_storage()?; + + let replica = Replica::new(storage); + + Ok(Self { replica }) + } + + pub fn new_ro() -> Result<Self> { + Self::new(Self::taskdb_dir(), AccessMode::ReadOnly) + } + pub fn new_rw() -> Result<Self> { + Self::new(Self::taskdb_dir(), AccessMode::ReadWrite) + } + + #[must_use] + pub fn replica(&mut self) -> &mut Replica { + &mut self.replica + } +} diff --git a/pkgs/by-name/ts/tskm/src/task/mod.rs b/pkgs/by-name/ts/tskm/src/task/mod.rs index c3a6d614..989f273a 100644 --- a/pkgs/by-name/ts/tskm/src/task/mod.rs +++ b/pkgs/by-name/ts/tskm/src/task/mod.rs @@ -9,109 +9,144 @@ use std::{ use anyhow::{bail, Context, Result}; use log::{debug, info, trace}; +use taskchampion::Tag; -use crate::interface::project::ProjectName; +use crate::{interface::project::ProjectName, state::State}; /// The `taskwarrior` id of a task. #[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Ord, Eq)] -pub struct Id { - id: u64, +pub struct Task { + uuid: taskchampion::Uuid, } -impl Id { - /// # Errors - /// When `task` execution fails - pub fn get_current() -> Result<Option<Self>> { - // We have ensured that only one task may be active - let self_str = run_task(&["+ACTIVE", "_ids"])?; - if self_str.is_empty() { - Ok(None) +impl From<&taskchampion::Task> for Task { + fn from(value: &taskchampion::Task) -> Self { + Self { + uuid: value.get_uuid(), + } + } +} +impl From<&taskchampion::TaskData> for Task { + fn from(value: &taskchampion::TaskData) -> Self { + Self { + uuid: value.get_uuid(), + } + } +} + +impl Task { + pub fn from_working_set(id: usize, state: &mut State) -> Result<Option<Self>> { + Ok(state + .replica() + .working_set()? + .by_index(id) + .map(|uuid| Self { uuid })) + } + + pub fn get_current(state: &mut State) -> Result<Option<Self>> { + let tasks = state + .replica() + .pending_tasks()? + .into_iter() + .filter(taskchampion::Task::is_active) + .collect::<Vec<_>>(); + + assert!( + tasks.len() <= 1, + "We have ensured that only one task may be active, via a hook" + ); + if let Some(active) = tasks.first() { + Ok(Some(Self::from(active))) } else { - Self::from_str(&self_str).map(Some) + Ok(None) } } - /// # Errors - /// When `task` execution fails - pub fn to_uuid(&self) -> Result<String> { - let uuid = run_task(&[self.to_string().as_str(), "uuids"])?; + #[must_use] + pub fn uuid(&self) -> &taskchampion::Uuid { + &self.uuid + } + #[must_use] + pub fn working_set_id(&self, state: &mut State) -> Result<usize> { + Ok(state + .replica() + .working_set()? + .by_uuid(self.uuid) + .expect("The task should be in the working set")) + } - Ok(uuid) + fn as_task(&self, state: &mut State) -> Result<taskchampion::Task> { + Ok(state + .replica() + .get_task(self.uuid)? + .expect("We have the task from this replica, it should still be in it")) } - /// # Panics - /// When internal assertions fail. - /// # Errors - /// When `task` execution fails - pub fn annotate(&self, message: &str) -> Result<()> { - run_task(&["annotate", self.to_string().as_str(), "--", message])?; + /// Adds a tag to the task, to show the user that it has additional neorg data. + pub fn mark_neorg_data(&self, state: &mut State) -> Result<()> { + let mut ops = vec![]; + self.as_task(state)? + .add_tag(&Tag::from_str("neorg_data").expect("Is valid"), &mut ops)?; + state.replica().commit_operations(ops)?; Ok(()) } - /// # Panics - /// When internal assertions fail. - /// # Errors - /// When `task` execution fails - pub fn start(&self) -> Result<()> { + /// Try to start this task. + /// It will stop previously active tasks. + pub fn start(&self, state: &mut State) -> Result<()> { info!("Activating {self}"); - let output = run_task(&["start", self.to_string().as_str()])?; - assert!(output.is_empty()); + if let Some(active) = Self::get_current(state)? { + active.stop(state)?; + } + + let mut ops = vec![]; + self.as_task(state)?.start(&mut ops)?; + state.replica().commit_operations(ops)?; Ok(()) } - /// # Panics - /// When internal assertions fail. - /// # Errors - /// When `task` execution fails - pub fn stop(&self) -> Result<()> { + + /// Stops this task. + pub fn stop(&self, state: &mut State) -> Result<()> { info!("Stopping {self}"); - let output = run_task(&["stop", self.to_string().as_str()])?; - assert!(output.is_empty()); + let mut ops = vec![]; + self.as_task(state)?.stop(&mut ops)?; + state.replica().commit_operations(ops)?; Ok(()) } - /// # Panics - /// When internal assertions fail. - /// # Errors - /// When `task` execution fails - pub fn description(&self) -> Result<String> { - let output = run_task(&["rc.context=none", "_zshids", self.to_string().as_str()])?; - let (id, desc) = output - .split_once(':') - .expect("The output should always contain one colon"); - assert_eq!(id.parse::<Id>().expect("This should be a valid id"), *self); - Ok(desc.to_owned()) + pub fn description(&self, state: &mut State) -> Result<String> { + Ok(self.as_task(state)?.get_description().to_owned()) } - /// # Panics - /// When internal assertions fail. - /// # Errors - /// When `task` execution fails - pub fn project(&self) -> Result<Project> { - let output = run_task(&[ - "rc.context=none", - "_get", - format!("{self}.project").as_str(), - ])?; - let project = Project::from_project_string(output.as_str()) + pub fn project(&self, state: &mut State) -> Result<Project> { + let output = { + let task = self.as_task(state)?; + let task_data = task.into_task_data(); + task_data + .get("project") + .expect("Every task should have a project") + .to_owned() + }; + let project = Project::from_project_string(output.as_str().trim()) .expect("This comes from tw, it should be valid"); Ok(project) } } -impl Display for Id { +impl Display for Task { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - self.id.fmt(f) + self.uuid.fmt(f) } } -impl FromStr for Id { +impl FromStr for Task { type Err = anyhow::Error; fn from_str(s: &str) -> Result<Self, Self::Err> { - let id = u64::from_str(s)?; - Ok(Self { id }) + let uuid = taskchampion::Uuid::from_str(s)?; + Ok(Self { uuid }) } } @@ -259,21 +294,14 @@ impl Project { /// # Errors /// When `task` execution fails. - pub fn get_tasks(&self) -> Result<Vec<Id>> { - let output = run_task(&[ - "rc.context=none", - format!("project:{}", self.to_project_display()).as_str(), - "_ids", - ])?; - - if output.is_empty() { - Ok(vec![]) - } else { - output - .lines() - .map(Id::from_str) - .collect::<Result<Vec<Id>>>() - } + pub fn get_tasks(&self, state: &mut State) -> Result<Vec<Task>> { + Ok(state + .replica() + .pending_task_data()? + .into_iter() + .filter(|t| t.get("project").expect("Is set") == self.to_project_display()) + .map(|t| Task::from(&t)) + .collect()) } /// # Errors |