diff options
| author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2026-06-03 18:26:23 +0200 |
|---|---|---|
| committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2026-06-03 18:26:23 +0200 |
| commit | 821ada0b8c3ffef3d4286f3c4fc15871ff9b5f65 (patch) | |
| tree | ccda5ab8d3cc298c85422cf83c3d95274de77747 /pkgs/by-name/ts/tskm/src | |
| parent | pkgs/*/update.sh: Perform more through cargo updates (diff) | |
| download | nixos-config-821ada0b8c3ffef3d4286f3c4fc15871ff9b5f65.zip | |
pkgs/tskm: Update `taskchampion` to 3.x
Diffstat (limited to 'pkgs/by-name/ts/tskm/src')
| -rw-r--r-- | pkgs/by-name/ts/tskm/src/browser/mod.rs | 45 | ||||
| -rw-r--r-- | pkgs/by-name/ts/tskm/src/cli.rs | 75 | ||||
| -rw-r--r-- | pkgs/by-name/ts/tskm/src/interface/input/handle.rs | 7 | ||||
| -rw-r--r-- | pkgs/by-name/ts/tskm/src/interface/neorg/handle.rs | 13 | ||||
| -rw-r--r-- | pkgs/by-name/ts/tskm/src/interface/open/handle.rs | 38 | ||||
| -rw-r--r-- | pkgs/by-name/ts/tskm/src/main.rs | 11 | ||||
| -rw-r--r-- | pkgs/by-name/ts/tskm/src/state.rs | 26 | ||||
| -rw-r--r-- | pkgs/by-name/ts/tskm/src/task/mod.rs | 60 |
8 files changed, 176 insertions, 99 deletions
diff --git a/pkgs/by-name/ts/tskm/src/browser/mod.rs b/pkgs/by-name/ts/tskm/src/browser/mod.rs index 2129982f..fd90b820 100644 --- a/pkgs/by-name/ts/tskm/src/browser/mod.rs +++ b/pkgs/by-name/ts/tskm/src/browser/mod.rs @@ -14,7 +14,7 @@ use url::Url; use crate::{state::State, task}; #[allow(clippy::too_many_lines)] -pub fn open_in_browser<U>( +pub async fn open_in_browser<U>( selected_project: &task::Project, state: &mut State, urls: Option<Vec<U>>, @@ -24,8 +24,9 @@ where { let old_project: Option<task::Project> = task::Project::get_current().context("Failed to get currently active project")?; - let old_task: Option<task::Task> = - task::Task::get_current(state).context("Failed to get currently active task")?; + let old_task: Option<task::Task> = task::Task::get_current(state) + .await + .context("Failed to get currently active task")?; selected_project.activate().with_context(|| { format!( @@ -35,25 +36,36 @@ where })?; let tracking_task = { - let all_tasks = selected_project.get_tasks(state).with_context(|| { + let all_tasks = selected_project.get_tasks(state).await.with_context(|| { format!( "Failed to get assoctiated tasks for project: '{}'", selected_project.to_project_display() ) })?; - let tracking_task = all_tasks.into_iter().find(|t| { - let maybe_desc = t.description(state); - if let Ok(desc) = maybe_desc { - desc == "tracking" - } else { - error!( - "Getting task description returned error: {}", - maybe_desc.expect_err("We already check for Ok") - ); - false + let tracking_task = { + let mut output = None; + + for t in all_tasks { + let maybe_desc = t.description(state).await; + let found = if let Ok(desc) = maybe_desc { + desc == "tracking" + } else { + error!( + "Getting task description returned error: {}", + maybe_desc.expect_err("We already check for Ok") + ); + false + }; + + if found { + output = Some(t); + break; + } } - }); + + output + }; if let Some(task) = tracking_task { info!( @@ -61,6 +73,7 @@ where selected_project.to_project_display() ); task.start(state) + .await .with_context(|| format!("Failed to start task {task}"))?; } tracking_task @@ -180,10 +193,12 @@ where if let Some(task) = tracking_task { task.stop(state) + .await .with_context(|| format!("Failed to stop task {task}"))?; } if let Some(task) = old_task { task.start(state) + .await .with_context(|| format!("Failed to start task {task}"))?; } diff --git a/pkgs/by-name/ts/tskm/src/cli.rs b/pkgs/by-name/ts/tskm/src/cli.rs index a347f5ce..3dc1181d 100644 --- a/pkgs/by-name/ts/tskm/src/cli.rs +++ b/pkgs/by-name/ts/tskm/src/cli.rs @@ -8,11 +8,12 @@ // You should have received a copy of the License along with this program. // If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>. -use std::{ffi::OsStr, path::PathBuf}; +use std::{ffi::OsStr, path::PathBuf, thread}; -use anyhow::{Result, bail}; -use clap::{ArgAction, Parser, Subcommand, ValueEnum, builder::StyledStr}; +use anyhow::{bail, Result}; +use clap::{builder::StyledStr, ArgAction, Parser, Subcommand, ValueEnum}; use clap_complete::{ArgValueCompleter, CompletionCandidate}; +use tokio::runtime::Runtime; use crate::{ interface::{ @@ -23,6 +24,43 @@ use crate::{ state, task, }; +macro_rules! as_sync { + ( + wrap $old_name_1:ident($($arg_name_1:ident : $arg_type_1:ty),*) -> $output_1:ty => $new_name_1:ident; + $( + wrap $old_name:ident($($arg_name:ident : $arg_type:ty),*) -> $output:ty => $new_name:ident; + )+ + ) => { + as_sync!( + wrap $old_name_1($($arg_name_1 : $arg_type_1),*) -> $output_1 => $new_name_1; + ); + as_sync!( + $( + wrap $old_name($($arg_name : $arg_type),*) -> $output => $new_name; + )+ + ); + }; + ( + wrap $old_name:ident($($arg_name:ident : $arg_type:ty),*) -> $output:ty => $new_name:ident $(;)? + ) => { + fn $new_name($($arg_name: $arg_type),*) -> $output { + $( + let $arg_name = $arg_name.to_owned(); + ),* + + let handle: std::thread::JoinHandle<$output> = thread::spawn(move || { + let rt = Runtime::new().expect("No runtime issue"); + + let output = rt.block_on($old_name($($arg_name.as_ref()),*)); + + output + }); + + handle.join().expect("The thread should be joinable") + } + }; +} + #[derive(Parser, Debug)] #[command(author, version, about, long_about, verbatim_doc_comment)] /// This is the core interface to the system-integrated task management @@ -94,16 +132,21 @@ pub enum NeorgCommand { /// Open the `neorg` project associated with id of the task. Task { /// The working set id of the task - #[arg(value_name = "ID", value_parser = task_from_working_set_id, add = ArgValueCompleter::new(complete_task_id))] + #[arg(value_name = "ID", value_parser = task_from_working_set_id_sync, add = ArgValueCompleter::new(complete_task_id_sync))] task: task::Task, }, } -fn task_from_working_set_id(id: &str) -> Result<task::Task> { +as_sync!( + wrap task_from_working_set_id(id: &str) -> Result<task::Task> => task_from_working_set_id_sync; + wrap complete_task_id(current: &OsStr) -> Vec<CompletionCandidate> => complete_task_id_sync; +); + +async fn task_from_working_set_id(id: &str) -> Result<task::Task> { let id: usize = id.parse()?; - let mut state = state::State::new_ro()?; + let mut state = state::State::new_ro().await?; - let Some(task) = task::Task::from_working_set(id, &mut state)? else { + let Some(task) = task::Task::from_working_set(id, &mut state).await? else { bail!("Working set id '{id}' is not valid!") }; Ok(task) @@ -201,14 +244,14 @@ pub enum InputCommand { Tags {}, } -fn complete_task_id(current: &OsStr) -> Vec<CompletionCandidate> { - fn format_task( +async fn complete_task_id(current: &OsStr) -> Vec<CompletionCandidate> { + async 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 { + let Ok(base) = task.working_set_id(state).await else { return None; }; base.to_string() @@ -219,7 +262,7 @@ fn complete_task_id(current: &OsStr) -> Vec<CompletionCandidate> { } let description = { - let Ok(base) = task.description(state) else { + let Ok(base) = task.description(state).await else { return None; }; StyledStr::from(base) @@ -234,11 +277,11 @@ fn complete_task_id(current: &OsStr) -> Vec<CompletionCandidate> { return output; }; - let Ok(mut state) = state::State::new_ro() else { + let Ok(mut state) = state::State::new_ro().await else { return output; }; - let Ok(pending) = state.replica().pending_tasks() else { + let Ok(pending) = state.replica().pending_tasks().await else { return output; }; @@ -249,9 +292,9 @@ fn complete_task_id(current: &OsStr) -> Vec<CompletionCandidate> { 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 let Ok(project) = task.project(&mut state).await { if project == current_project { - if let Some(out) = format_task(task, current, &mut state) { + if let Some(out) = format_task(task, current, &mut state).await { output.push(out); } } @@ -260,7 +303,7 @@ fn complete_task_id(current: &OsStr) -> Vec<CompletionCandidate> { } else { for t in pending { let task = task::Task::from(&t); - if let Some(out) = format_task(task, current, &mut state) { + if let Some(out) = format_task(task, current, &mut state).await { output.push(out); } } diff --git a/pkgs/by-name/ts/tskm/src/interface/input/handle.rs b/pkgs/by-name/ts/tskm/src/interface/input/handle.rs index d9904657..cd868f7a 100644 --- a/pkgs/by-name/ts/tskm/src/interface/input/handle.rs +++ b/pkgs/by-name/ts/tskm/src/interface/input/handle.rs @@ -28,7 +28,7 @@ use super::{Input, Tag}; /// # Panics /// When internal assertions fail. #[allow(clippy::too_many_lines)] -pub fn handle(command: InputCommand, state: &mut State) -> Result<()> { +pub async fn handle(command: InputCommand, state: &mut State) -> Result<()> { match command { InputCommand::Add { inputs } => { for input in inputs { @@ -79,10 +79,11 @@ pub fn handle(command: InputCommand, state: &mut State) -> Result<()> { &project, state, Some(all.iter().map(|f| f.url.clone()).collect()), - )?; + ) + .await?; { - use std::io::{Write, stdin, stdout}; + use std::io::{stdin, stdout, Write}; let mut s = String::new(); eprint!("Continue? (y/N) "); 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 4e433143..12a0180d 100644 --- a/pkgs/by-name/ts/tskm/src/interface/neorg/handle.rs +++ b/pkgs/by-name/ts/tskm/src/interface/neorg/handle.rs @@ -10,19 +10,19 @@ use std::{ env, - fs::{self, File, OpenOptions, read_to_string}, + fs::{self, read_to_string, File, OpenOptions}, io::Write, process::Command, }; -use anyhow::{Context, Result, bail}; +use anyhow::{bail, Context, Result}; use crate::{cli::NeorgCommand, state::State}; -pub fn handle(command: NeorgCommand, state: &mut State) -> Result<()> { +pub async fn handle(command: NeorgCommand, state: &mut State) -> Result<()> { match command { NeorgCommand::Task { task } => { - let project = task.project(state)?; + let project = task.project(state).await?; let base = dirs::data_local_dir() .expect("This should exists") .join("tskm/notes"); @@ -46,7 +46,8 @@ pub fn handle(command: NeorgCommand, state: &mut State) -> Result<()> { let mut file = options.open(&path)?; file.write_all( - format!("* {} (% {})", task.description(state)?, task.uuid()).as_bytes(), + format!("* {} (% {})", task.description(state).await?, task.uuid()) + .as_bytes(), ) .with_context(|| { format!("Failed to write task uuid to file: '{}'", path.display()) @@ -92,7 +93,7 @@ pub fn handle(command: NeorgCommand, state: &mut State) -> Result<()> { } { - task.mark_neorg_data(state)?; + task.mark_neorg_data(state).await?; } } } 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 15f749c5..5b9100bc 100644 --- a/pkgs/by-name/ts/tskm/src/interface/open/handle.rs +++ b/pkgs/by-name/ts/tskm/src/interface/open/handle.rs @@ -10,7 +10,7 @@ use std::str::FromStr; -use anyhow::{Context, Result, bail}; +use anyhow::{bail, Context, Result}; use log::{error, info}; use url::Url; @@ -31,7 +31,7 @@ fn is_empty(project: &task::Project) -> Result<bool> { } #[allow(clippy::too_many_lines)] -pub fn handle(command: OpenCommand, state: &mut State) -> Result<()> { +pub async fn handle(command: OpenCommand, state: &mut State) -> Result<()> { match command { OpenCommand::Review { non_empty } => { for project in task::Project::all().context("Failed to get all project files")? { @@ -43,12 +43,14 @@ pub fn handle(command: OpenCommand, state: &mut State) -> Result<()> { project.to_project_display(), if is_empty { "is empty" } else { "is not empty" } ); - open_in_browser(project, state, None::<Vec<Url>>).with_context(|| { - format!( - "Failed to open project ('{}') in qutebrowser", - project.to_project_display() - ) - })?; + open_in_browser(project, state, None::<Vec<Url>>) + .await + .with_context(|| { + format!( + "Failed to open project ('{}') in qutebrowser", + project.to_project_display() + ) + })?; if project.is_touched() { project.untouch().with_context(|| { @@ -63,9 +65,11 @@ pub fn handle(command: OpenCommand, state: &mut State) -> Result<()> { } OpenCommand::Project { project, urls } => { project.touch().context("Failed to touch project")?; - open_in_browser(&project, state, urls).with_context(|| { - format!("Failed to open project: {}", project.to_project_display()) - })?; + open_in_browser(&project, state, urls) + .await + .with_context(|| { + format!("Failed to open project: {}", project.to_project_display()) + })?; } OpenCommand::Select { urls } => { let selected_project: task::Project = task::Project::from_project_string( @@ -85,7 +89,9 @@ pub fn handle(command: OpenCommand, state: &mut State) -> Result<()> { .touch() .context("Failed to touch project")?; - open_in_browser(&selected_project, state, urls).context("Failed to open project")?; + open_in_browser(&selected_project, state, urls) + .await + .context("Failed to open project")?; } OpenCommand::ListTabs { projects, mode } => { let projects = { @@ -152,7 +158,13 @@ pub fn handle(command: OpenCommand, state: &mut State) -> Result<()> { }; for (active, url) in tabs { - let is_selected = { if active { "🔻 " } else { " " } }; + let is_selected = { + if active { + "🔻 " + } else { + " " + } + }; println!("{is_selected}{url}"); } } diff --git a/pkgs/by-name/ts/tskm/src/main.rs b/pkgs/by-name/ts/tskm/src/main.rs index e6113111..a852bd7b 100644 --- a/pkgs/by-name/ts/tskm/src/main.rs +++ b/pkgs/by-name/ts/tskm/src/main.rs @@ -24,7 +24,8 @@ pub mod rofi; pub mod state; pub mod task; -fn main() -> Result<(), anyhow::Error> { +#[tokio::main] +async fn main() -> Result<(), anyhow::Error> { clap_complete::CompleteEnv::with_factory(CliArgs::command).complete(); let args = CliArgs::parse(); @@ -38,12 +39,12 @@ fn main() -> Result<(), anyhow::Error> { .init() .expect("Let's just hope that this does not panic"); - let mut state = State::new_rw()?; + let mut state = State::new_rw().await?; match args.command { - Command::Inputs { command } => input::handle(command, &mut state)?, - Command::Neorg { command } => neorg::handle(command, &mut state)?, - Command::Open { command } => open::handle(command, &mut state)?, + Command::Inputs { command } => input::handle(command, &mut state).await?, + Command::Neorg { command } => neorg::handle(command, &mut state).await?, + Command::Open { command } => open::handle(command, &mut state).await?, 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 index ae71764e..57495bb8 100644 --- a/pkgs/by-name/ts/tskm/src/state.rs +++ b/pkgs/by-name/ts/tskm/src/state.rs @@ -11,10 +11,13 @@ use std::path::PathBuf; use anyhow::Result; -use taskchampion::{Replica, StorageConfig, storage::AccessMode}; +use taskchampion::{ + storage::{sqlite::SqliteStorage, AccessMode}, + Replica, +}; pub struct State { - replica: Replica, + replica: Replica<SqliteStorage>, } impl std::fmt::Debug for State { @@ -28,28 +31,23 @@ impl State { 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()?; + async fn new(taskdb_dir: PathBuf, access_mode: AccessMode) -> Result<Self> { + let storage = SqliteStorage::new(taskdb_dir, access_mode, false).await?; let replica = Replica::new(storage); Ok(Self { replica }) } - pub fn new_ro() -> Result<Self> { - Self::new(Self::taskdb_dir(), AccessMode::ReadOnly) + pub async fn new_ro() -> Result<Self> { + Self::new(Self::taskdb_dir(), AccessMode::ReadOnly).await } - pub fn new_rw() -> Result<Self> { - Self::new(Self::taskdb_dir(), AccessMode::ReadWrite) + pub async fn new_rw() -> Result<Self> { + Self::new(Self::taskdb_dir(), AccessMode::ReadWrite).await } #[must_use] - pub fn replica(&mut self) -> &mut Replica { + pub fn replica(&mut self) -> &mut Replica<SqliteStorage> { &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 5e223e33..1362615d 100644 --- a/pkgs/by-name/ts/tskm/src/task/mod.rs +++ b/pkgs/by-name/ts/tskm/src/task/mod.rs @@ -10,14 +10,14 @@ use std::{ fmt::Display, - fs::{self, File, read_to_string}, + fs::{self, read_to_string, File}, path::PathBuf, process::Command, str::FromStr, sync::OnceLock, }; -use anyhow::{Context, Result, bail}; +use anyhow::{bail, Context, Result}; use log::{debug, info, trace}; use taskchampion::Tag; @@ -45,18 +45,20 @@ impl From<&taskchampion::TaskData> for Task { } impl Task { - pub fn from_working_set(id: usize, state: &mut State) -> Result<Option<Self>> { + pub async fn from_working_set(id: usize, state: &mut State) -> Result<Option<Self>> { Ok(state .replica() - .working_set()? + .working_set() + .await? .by_index(id) .map(|uuid| Self { uuid })) } - pub fn get_current(state: &mut State) -> Result<Option<Self>> { + pub async fn get_current(state: &mut State) -> Result<Option<Self>> { let tasks = state .replica() - .pending_tasks()? + .pending_tasks() + .await? .into_iter() .filter(taskchampion::Task::is_active) .collect::<Vec<_>>(); @@ -76,62 +78,65 @@ impl Task { pub fn uuid(&self) -> &taskchampion::Uuid { &self.uuid } - pub fn working_set_id(&self, state: &mut State) -> Result<usize> { + pub async fn working_set_id(&self, state: &mut State) -> Result<usize> { Ok(state .replica() - .working_set()? + .working_set() + .await? .by_uuid(self.uuid) .expect("The task should be in the working set")) } - fn as_task(&self, state: &mut State) -> Result<taskchampion::Task> { + async fn as_task(&self, state: &mut State) -> Result<taskchampion::Task> { Ok(state .replica() - .get_task(self.uuid)? + .get_task(self.uuid) + .await? .expect("We have the task from this replica, it should still be in it")) } /// 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<()> { + pub async fn mark_neorg_data(&self, state: &mut State) -> Result<()> { let mut ops = vec![]; - self.as_task(state)? + self.as_task(state) + .await? .add_tag(&Tag::from_str("neorg_data").expect("Is valid"), &mut ops)?; - state.replica().commit_operations(ops)?; + state.replica().commit_operations(ops).await?; Ok(()) } /// Try to start this task. /// It will stop previously active tasks. - pub fn start(&self, state: &mut State) -> Result<()> { + pub async fn start(&self, state: &mut State) -> Result<()> { info!("Activating {self}"); - if let Some(active) = Self::get_current(state)? { - active.stop(state)?; + if let Some(active) = Self::get_current(state).await? { + active.stop(state).await?; } let mut ops = vec![]; - self.as_task(state)?.start(&mut ops)?; - state.replica().commit_operations(ops)?; + self.as_task(state).await?.start(&mut ops)?; + state.replica().commit_operations(ops).await?; Ok(()) } /// Stops this task. - pub fn stop(&self, state: &mut State) -> Result<()> { + pub async fn stop(&self, state: &mut State) -> Result<()> { info!("Stopping {self}"); let mut ops = vec![]; - self.as_task(state)?.stop(&mut ops)?; - state.replica().commit_operations(ops)?; + self.as_task(state).await?.stop(&mut ops)?; + state.replica().commit_operations(ops).await?; Ok(()) } - pub fn description(&self, state: &mut State) -> Result<String> { - Ok(self.as_task(state)?.get_description().to_owned()) + pub async fn description(&self, state: &mut State) -> Result<String> { + Ok(self.as_task(state).await?.get_description().to_owned()) } - pub fn project(&self, state: &mut State) -> Result<Project> { + pub async fn project(&self, state: &mut State) -> Result<Project> { let output = { - let task = self.as_task(state)?; + let task = self.as_task(state).await?; let task_data = task.into_task_data(); task_data .get("project") @@ -303,10 +308,11 @@ impl Project { /// # Errors /// When `task` execution fails. - pub fn get_tasks(&self, state: &mut State) -> Result<Vec<Task>> { + pub async fn get_tasks(&self, state: &mut State) -> Result<Vec<Task>> { Ok(state .replica() - .pending_task_data()? + .pending_task_data() + .await? .into_iter() .filter(|t| t.get("project").expect("Is set") == self.to_project_display()) .map(|t| Task::from(&t)) |
