serhii.net

In the middle of the desert you can say anything you want

20 Jan 2022

taskwarrior modify tasks' hierarchical project names using taskwarrior filters and export

Wanted to rename all tasks belonging to a certain project from a certain timeframe.

TL;DR

  • Use filters to select tasks within a timeframe
  • If you use hierarchical projects (pro:w.one.two) heavily and want to keep the children names:
    • Export them and use JSON parsing magic to get a unique list of project names
    • Bash loop to manually rename each of these project

Final command I used:

for p in $(task export "\(pro.is:w or pro:w.\) entry.after:2019-04-30 entry.before:2021-12-31"  | jq ".[].project" -r | sort | uniq);
	do task entry.after:2019-04-30 entry.before:2021-12-31 pro:$p mod pro:new_project_name$p;
done

Longer version

The task1

Used project:w for work, now new work, makes sense to rename the previous one for cleaner separation.

To list all tasks created in certain dates (task all to cover tasks that aren’t just status:pending as by default):

task all pro:w entry.after:2019-04-30 entry.before:2021-12-31

1213 tasks. Wow.

Remembering when I was using sprints and renaming them at the end, pro:w covers pro:w.test and pro:whatever.

I was disciplined but wanted to cover all pro:w and pro:w.whatever but not pro:whatever just in case, so tested this, same result:

task all "\(pro.is:w or pro:w.\) entry.after:2019-04-30 entry.before:2021-12-31"

How to modify them?

The problem

Okay, got them. How to modify? Complexity: I need to change part of the project, so pro:w.one -> pro:old_w.one instead of changing all tasks’ project to pro:old_w

Attempts

Commands

There’s prepend 2 but seems to work only for descriptions.

There’s t mod /from/to/ syntax3, couldn’t get it to work part of the project.

There’s regex4, but works only for filters if enabled

There’s json export but I don’t feel like parsing JSON, feels too close to day job :)

Listing projects

You can list projects like this:

# currently used
task projects

# all
task rc.list.all.projects=1 projects

This gives hope, if I get the list of projects I can just iterate through them and rename all of them individually.

Can’t find this documented, but task rc.list.all.projects=1 projects pro:w filters the projects by ones starting with w.

Format parses the hierarchy sadly

Project       Tasks
w              1107
  a               1
  aan             1

Can I rename the character used for hierarchy so that I get them as list of separate tags with dots in them? Not exposed through config from what I can see

…alright, JSON export it is

JSON export

It exists, and of course it accepts filters <3

task export "\(pro.is:w or pro:w.\) entry.after:2019-04-30 entry.before:2021-12-31" | wc -l

1215 lines - about the same ballpark as the number of tasks.

JSON output is an array of these objects:

  {
    "id": 0,
    "description": "write attn mechanism also on token features",
    "end": "20191016T143449Z",
    "entry": "20191016T120514Z",
    "est": "PT1H",
    "modified": "20200111T094548Z",
    "project": "w",
    "sprint": "2019-41",
    "status": "completed",
    "uuid": "d3f2b2ac-ec20-4d16-bd16-66b2e1e568f9",
    "urgency": 2
  },

Okay

> task export "\(pro.is:w or pro:w.\) entry.after:2019-04-30 entry.before:2021-12-31"  | jq ".[].project" | uniq


"w.lm"
"w.l.p"
"w.lm"
"w.lm"
"w.l.py"
"w.lm"
"w"

Proud that I wrote that from the first try, as trivial as it is. Thank you ExB for teaching me to parse JSONs.

The quotes - jq -r returns raw output5, so same as above but without quotes.

Final command to get the list of projects:

task export "\(pro.is:w or pro:w.\) entry.after:2019-04-30 entry.before:2021-12-31"  | jq ".[].project" -r | sort | uniq

(Remembering that uniq works only after sort)

And let’s make it a loop, final command:

for p in $(task export "\(pro.is:w or pro:w.\) entry.after:2019-04-30 entry.before:2021-12-31"  | jq ".[].project" -r | sort | uniq);
	do task entry.after:2019-04-30 entry.before:2021-12-31 pro:$p mod pro:new_project_name$p;
done

Nice but forgotten stuff:

Nel mezzo del deserto posso dire tutto quello che voglio.