Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tasks completed #890

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased (on main branch only)

- Pull labels from API if not provided when performing `list label`
- Show number of tasks completed today when using `list process`

## 2024-10-30 v0.6.14

Expand Down
45 changes: 45 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ use tokio::fs;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::sync::mpsc::UnboundedSender;

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Completed {
count: u32,
date: String,
}

/// App configuration, serialized as json in $XDG_CONFIG_HOME/tod.cfg
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Config {
Expand Down Expand Up @@ -39,6 +45,7 @@ pub struct Config {
pub spinners: Option<bool>,
#[serde(default)]
pub disable_links: bool,
pub completed: Option<Completed>,
pub verbose: Option<bool>,
/// Don't ask for sections
pub no_sections: Option<bool>,
Expand Down Expand Up @@ -190,6 +197,29 @@ impl Config {
Config { next_id, ..self }
}

/// Increase the completed count for today
pub fn increment_completed(&self) -> Result<Config, Error> {
let date = time::today_date(self)?.to_string();
let completed = match &self.completed {
None => Some(Completed { date, count: 1 }),
Some(completed) => {
if completed.date == date {
Some(Completed {
count: completed.count + 1,
..completed.clone()
})
} else {
Some(Completed { date, count: 1 })
}
}
};

Ok(Config {
completed,
..self.clone()
})
}

pub async fn create(self) -> Result<Config, Error> {
let json = json!(self).to_string();
let mut file = fs::File::create(&self.path).await?;
Expand Down Expand Up @@ -227,6 +257,7 @@ impl Config {
bell_on_failure: true,
sort_value: Some(SortValue::default()),
timezone: None,
completed: None,
disable_links: false,
spinners: Some(true),
mock_url: None,
Expand Down Expand Up @@ -306,6 +337,20 @@ impl Config {
..self.clone()
}
}

pub fn tasks_completed(&self) -> Result<u32, Error> {
let date = time::today_date(self)?.to_string();
match &self.completed {
None => Ok(0),
Some(completed) => {
if completed.date == date {
Ok(completed.count)
} else {
Ok(0)
}
}
}
}
}

pub async fn get_or_create(
Expand Down
2 changes: 1 addition & 1 deletion src/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ pub async fn process_tasks(config: &Config, project: &Project) -> Result<String,
let mut handles = Vec::new();
for task in tasks {
println!(" ");
match tasks::process_task(&config.reload().await?, task, &mut task_count, false).await {
match tasks::process_task(config, task, &mut task_count, false).await {
Some(handle) => handles.push(handle),
None => return Ok(color::green_string("Exited")),
}
Expand Down
18 changes: 15 additions & 3 deletions src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,20 @@ pub async fn process_task(
.map(|s| s.to_string())
.collect();
let formatted_task = task.fmt(config, FormatType::Single, with_project);
println!("{formatted_task}{task_count} task(s) remaining");
let mut reloaded_config = config
.reload()
.await
.expect("Could not reload config")
.increment_completed()
.expect("Could not increment config");
let tasks_completed = reloaded_config.tasks_completed().unwrap_or_default();
println!("{formatted_task}{tasks_completed} completed today, {task_count} remaining");
*task_count -= 1;
match input::select("Select an option", options, config.mock_select) {
Ok(string) => {
if string == "Complete" {
Some(spawn_complete_task(config.clone(), task))
reloaded_config.save().await.expect("Could not save config");
Some(spawn_complete_task(reloaded_config, task))
} else if string == "Delete" {
Some(spawn_delete_task(config.clone(), task))
} else if string == "Schedule" {
Expand Down Expand Up @@ -1008,7 +1016,11 @@ mod tests {
let config = test::fixtures::config()
.await
.mock_url(server.url())
.mock_select(0);
.mock_select(0)
.create()
.await
.unwrap();

let mut task_count = 3;
process_task(&config, task, &mut task_count, true)
.await
Expand Down
1 change: 1 addition & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub mod fixtures {
token: String::from("alreadycreated"),
sort_value: Some(SortValue::default()),
disable_links: false,
completed: None,
bell_on_success: false,
bell_on_failure: true,
internal: Internal { tx: tx() },
Expand Down
2 changes: 1 addition & 1 deletion src/todoist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ pub async fn complete_task(config: &Config, task_id: &str, spinner: bool) -> Res
request::post_todoist_sync(config, url, body, spinner).await?;

if !cfg!(test) {
config.clone().clear_next_id().save().await?;
config.reload().await?.clear_next_id().save().await?;
}

// Does not pass back a task
Expand Down
Loading