Skip to content

Commit

Permalink
taskwarrior: revert "Experimentally use format flags instead of forma…
Browse files Browse the repository at this point in the history
…t, format_singular and format_everything_done"
  • Loading branch information
MaxVerevkin committed Feb 21, 2023
1 parent 2207830 commit fca5c28
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 36 deletions.
1 change: 0 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ music | `smart_trim` has been removed | -
net |`hide_missing` and `hide_inactive` are removed. You can set `missing_format = ""` | -
notmuch | `name` option is removed and now you can use `format` to set the name | -
pomodoro | interactive configuration ?? | -
taskwarrior | `format_singular` and `format_everything_done` have been removed, and instead implemented via the new formatter. | Example:
temperature | `collapsed` option is removed and now you can use `format_alt = " $icon "` to maintain the behavior | -
toggle | `text` option is removed and now you can use `format` to set the text | -

Expand Down
74 changes: 39 additions & 35 deletions src/blocks/taskwarrior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,33 @@
//! `interval` | Update interval in seconds | `600` (10min)
//! `warning_threshold` | The threshold of pending (or started) tasks when the block turns into a warning state | `10`
//! `critical_threshold` | The threshold of pending (or started) tasks when the block turns into a critical state | `20`
//! `hide_when_zero` | Whethere to hide the block when the number of tasks is zero | `false`
//! `filters` | A list of tables with the keys `name` and `filter`. `filter` specifies the criteria that must be met for a task to be counted towards this filter. | ```[{name = "pending", filter = "-COMPLETED -DELETED"}]```
//! `format` | A string to customise the output of this block. See below for available placeholders. | `" $icon $done\|$count.eng(w:1) "`
//! `format` | A string to customise the output of this block. See below for available placeholders. | `" $icon $count.eng(w:1) "`
//! `format_singular` | Same as `format` but for when exactly one task is pending. | `" $icon $count.eng(w:1) "`
//! `format_everything_done` | Same as `format` but for when all tasks are completed. | `" $icon $count.eng(w:1) "`
//! `data_location`| Directory in which taskwarrior stores its data files. Supports path expansions e.g. `~`. | `"~/.task"`
//!
//! Placeholder | Value | Type | Unit
//! --------------|---------------------------------------------|--------|-----
//! `icon` | A static icon | Icon | -
//! `count` | The number of tasks matching current filter | Number | -
//! `filter_name` | The name of current filter | Text | -
//! `done` | Present only if `count` is zero | Flag | -
//! `single` | Present only if `count` is one | Flag | -
//!
//! Action | Default button
//! --------------|---------------
//! `next_filter` | Right
//!
//! # Example
//!
//! In this example, block will display "All done" if `count` is zero, "One task" if `count` is one
//! and "Tasks: N" if there are more than one task.
//! In this example, block will be hidden if `count` is zero.
//!
//! ```toml
//! [[block]]
//! block = "taskwarrior"
//! interval = 60
//! format = " $icon $done{All done}|$single{One task}|Tasks: $count.eng(w:1) "
//! format = " $icon count.eng(w:1) tasks "
//! format_singular = " $icon 1 task "
//! format_everything_done = ""
//! warning_threshold = 10
//! critical_threshold = 20
//! [[block.filters]]
Expand All @@ -59,9 +59,10 @@ pub struct Config {
interval: Seconds,
warning_threshold: u32,
critical_threshold: u32,
hide_when_zero: bool,
filters: Vec<Filter>,
format: FormatConfig,
format_singular: FormatConfig,
format_everything_done: FormatConfig,
data_location: ShellString,
}

Expand All @@ -71,12 +72,13 @@ impl Default for Config {
interval: Seconds::new(600),
warning_threshold: 10,
critical_threshold: 20,
hide_when_zero: false,
filters: vec![Filter {
name: "pending".into(),
filter: "-COMPLETED -DELETED".into(),
}],
format: default(),
format_singular: default(),
format_everything_done: default(),
data_location: ShellString::new("~/.task"),
}
}
Expand All @@ -86,11 +88,15 @@ pub async fn run(config: Config, mut api: CommonApi) -> Result<()> {
api.set_default_actions(&[(MouseButton::Right, None, "next_filter")])
.await?;

let mut widget = Widget::new().with_format(
config
.format
.with_default(" $icon $done|$count.eng(w:1) ")?,
);
let format = config.format.with_default(" $icon $count.eng(w:1) ")?;
let format_singular = config
.format_singular
.with_default(" $icon $count.eng(w:1) ")?;
let format_everything_done = config
.format_everything_done
.with_default(" $icon $count.eng(w:1) ")?;

let mut widget = Widget::new();

let mut filters = config.filters.iter().cycle();
let mut filter = filters.next().error("`filters` is empty")?;
Expand All @@ -106,27 +112,25 @@ pub async fn run(config: Config, mut api: CommonApi) -> Result<()> {
loop {
let number_of_tasks = get_number_of_tasks(&filter.filter).await?;

if number_of_tasks != 0 || !config.hide_when_zero {
widget.set_values(map! {
"icon" => Value::icon(api.get_icon("tasks")?),
"count" => Value::number(number_of_tasks),
"filter_name" => Value::text(filter.name.clone()),
[if number_of_tasks == 0] "done" => Value::flag(),
[if number_of_tasks == 1] "single" => Value::flag(),
});

widget.state = if number_of_tasks >= config.critical_threshold {
State::Critical
} else if number_of_tasks >= config.warning_threshold {
State::Warning
} else {
State::Idle
};

api.set_widget(&widget).await?;
} else {
api.hide().await?;
}
widget.set_format(match number_of_tasks {
0 => format_everything_done.clone(),
1 => format_singular.clone(),
_ => format.clone(),
});

widget.set_values(map! {
"icon" => Value::icon(api.get_icon("tasks")?),
"count" => Value::number(number_of_tasks),
"filter_name" => Value::text(filter.name.clone()),
});

widget.state = match number_of_tasks {
x if x >= config.critical_threshold => State::Critical,
x if x >= config.warning_threshold => State::Warning,
_ => State::Idle,
};

api.set_widget(&widget).await?;

select! {
_ = sleep(config.interval.0) =>(),
Expand Down

0 comments on commit fca5c28

Please sign in to comment.