Skip to main content

Planning the day on the CLI with tsort

I had a bunch of items on the todo list for the day and wanted to arrange them in order, but I needed to certain items before others:

  • Drop donations off at the resale store, but can't do that before 10:00 when they open
  • Get the daughter ready for soccer camp before dropping her off
  • Drop the daughter off at camp before taking paperwork up to her school
  • cut the son's hair before he takes a shower and cleans his bathroom
  • pick up daughter at camp after she's started camp (duh) and before 10:45
  • take the kids to the library after picking up daughter at camp
  • for obvious reasons, 8:30 → 8:45 → 9:00 → 10:00 → 10:30 → 10:45
So I expressed this in a file where each row contains "X needs to come before Y":
cat todo.txt
10:00 resale
resale 10:30
sunblock take_to_camp
pack_snack take_to_camp
pack_water take_to_camp
pack_towel take_to_camp
take_to_camp 8:30
8:30 soccer_camp
soccer_camp 8:45 
soccer_camp paperwork
8:45 home
home 9:00
paperwork home
home cut_hair
home clean_bathroom
home shower
cut_hair shower
cut_hair pick_up
shower pick_up
clean_bathroom pick_up
cut_hair clean_bathroom
clean_bathroom 10:00
10:45 pick_up
8:30 8:45 
8:45 9:00 
9:00 10:00 
10:00 10:30 
10:30 10:45 
pick_up library

Now it just became a matter of passing these requirements to tsort

tsort todo.txt
pack_towel
pack_water
pack_snack
sunblock
take_to_camp
8:30
soccer_camp
paperwork
8:45
home
cut_hair
9:00
shower
clean_bathroom
10:00
resale
10:30
10:45
pick_up
library
to sift them into the required order of my todo list for the day.