-
Notifications
You must be signed in to change notification settings - Fork 8
PowerShell Tricks
If you've looked at how the scripts work, you may have wondered why they go to all the trouble to parse the todo.txt file into a set of objects instead of just using Select-String/Set-Content (the PowerShell equivalent of sed/awk) to do text manipulation. It's certainly possible to do everything we need using text manipulation in PowerShell, and it can be done in about the same amount of code that we'd use for the sh equivalent. But PowerShell has an affinity for .NET objects; that's where it really stands out as a CLI. In addition to the 'todo' function (familiar to todo.sh users), the todo.txt-PowerShell module exports functions for dealing directly with the Task and TaskList objects which are created when the todo.txt file is parsed. When you use the 'todo' command:
todo
The results come back as a stream of .NET objects (specifically, Task
objects). Using the '|' (pipe), you can pipe the objects into other PowerShell commands. For example, you can filter out all of your tasks which don't have any projects:
todo | where {-not $_.Projects.Count}
You can do the same sort of thing with Contexts. Let's say you wanted to see all of your tasks with multiple Contexts:
todo | where {$_.Contexts.Count -gt 1}
You can bascially pipe the Tasks into any PowerShell cmdlet; for example, to sort them alphabetically by the body of the task, you can
todo | sort -property Body
Or you can get all of the tasks with item numbers between 40 and 52:
todo | where {($_.ItemNumber -lt 52) -and ($_.ItemNumber -gt 40)}
Or count how many of your tasks have a particular context:
(todo | ? { $_.Contexts -contains '@home' } | measure).Count
You can also pipe them into a WPF-based grid view:
todo | Out-GridView