Skip to content

Commit 75ed800

Browse files
committed
Modify taskwarrior block to support more general filter instead of filter_tags
This deprecates `filter_tags`. `filter_tags` prepended with '+' and concatenated them to build `filter`. e.g. filter_tags = [ "tag1", "tag2"] becomes filter = "+tag1 +tag2"
1 parent bb93867 commit 75ed800

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

blocks.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,7 +1322,7 @@ format_singular = "{count} open task"
13221322
format_everything_done = "nothing to do!"
13231323
warning_threshold = 10
13241324
critical_threshold = 20
1325-
filter_tags = ["work", "important"]
1325+
filter = "+work +important"
13261326
```
13271327

13281328
#### Options
@@ -1332,7 +1332,8 @@ Key | Values | Required | Default
13321332
`interval` | Update interval, in seconds. | No | `600` (10min)
13331333
`warning_threshold` | The threshold of pending (or started) tasks when the block turns into a warning state. | No | `10`
13341334
`critical_threshold` | The threshold of pending (or started) tasks when the block turns into a critical state. | No | `20`
1335-
`filter_tags` | A list of tags a task has to have before its counted as a pending task. | No | ```<empty>```
1335+
`filter_tags` | Deprecated in favour of `filter`. A list of tags a task has to have before its counted as a pending task. | No | ```<empty>```
1336+
`filter` | A filter that a task has to match to be counted as a pending task. | No | ```<empty```
13361337
`format` | A string to customise the output of this block. See below for available placeholders. Text may need to be escaped, refer to [Escaping Text](#escaping-text). | No | `"{count}"`
13371338
`format_singular` | Same as `format` but for when exactly one task is pending. | No | `"{count}"`
13381339
`format_everything_done` | Same as `format` but for when all tasks are completed. | No | `"{count}"`

src/blocks/taskwarrior.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub struct Taskwarrior {
2121
update_interval: Duration,
2222
warning_threshold: u32,
2323
critical_threshold: u32,
24-
filter_tags: Vec<String>,
24+
filter: String,
2525
block_mode: TaskwarriorBlockMode,
2626
format: FormatTemplate,
2727
format_singular: FormatTemplate,
@@ -53,9 +53,14 @@ pub struct TaskwarriorConfig {
5353
pub critical_threshold: u32,
5454

5555
/// A list of tags a task has to have before it's used for counting pending tasks
56+
/// (DEPRECATED) use filter instead
5657
#[serde(default = "TaskwarriorConfig::default_filter_tags")]
5758
pub filter_tags: Vec<String>,
5859

60+
/// The search criteria that matching tasks must have to be counted
61+
#[serde(default = "TaskwarriorConfig::default_filter")]
62+
pub filter: String,
63+
5964
/// Format override
6065
#[serde(default = "TaskwarriorConfig::default_format")]
6166
pub format: String,
@@ -96,6 +101,10 @@ impl TaskwarriorConfig {
96101
vec![]
97102
}
98103

104+
fn default_filter() -> String {
105+
String::new()
106+
}
107+
99108
fn default_format() -> String {
100109
"{count}".to_owned()
101110
}
@@ -123,7 +132,11 @@ impl ConfigBlock for Taskwarrior {
123132
update_interval: block_config.interval,
124133
warning_threshold: block_config.warning_threshold,
125134
critical_threshold: block_config.critical_threshold,
126-
filter_tags: block_config.filter_tags,
135+
filter: if block_config.filter_tags.len() > 0 {
136+
tags_to_filter(&block_config.filter_tags)
137+
} else {
138+
block_config.filter
139+
},
127140
block_mode: TaskwarriorBlockMode::OnlyFilteredPendingTasks,
128141
output,
129142
format: FormatTemplate::from_string(&block_config.format).block_error(
@@ -171,14 +184,14 @@ fn tags_to_filter(tags: &[String]) -> String {
171184
.join(" ")
172185
}
173186

174-
fn get_number_of_pending_tasks(tags: &[String]) -> Result<u32> {
187+
fn get_number_of_pending_tasks(filter: &str) -> Result<u32> {
175188
String::from_utf8(
176189
Command::new("sh")
177190
.args(&[
178191
"-c",
179192
&format!(
180193
"task rc.gc=off -COMPLETED -DELETED {} count",
181-
tags_to_filter(tags)
194+
filter
182195
),
183196
])
184197
.output()
@@ -202,11 +215,11 @@ impl Block for Taskwarrior {
202215
if !has_taskwarrior()? {
203216
self.output.set_text("?")
204217
} else {
205-
let filter_tags = match self.block_mode {
206-
TaskwarriorBlockMode::OnlyFilteredPendingTasks => self.filter_tags.clone(),
207-
TaskwarriorBlockMode::AllPendingTasks => vec![],
218+
let filter = match self.block_mode {
219+
TaskwarriorBlockMode::OnlyFilteredPendingTasks => &self.filter,
220+
TaskwarriorBlockMode::AllPendingTasks => "",
208221
};
209-
let number_of_pending_tasks = get_number_of_pending_tasks(&filter_tags)?;
222+
let number_of_pending_tasks = get_number_of_pending_tasks(filter)?;
210223
let values = map!("{count}" => number_of_pending_tasks);
211224
self.output.set_text(match number_of_pending_tasks {
212225
0 => self.format_everything_done.render_static_str(&values)?,

0 commit comments

Comments
 (0)