Skip to content
Brett Terpstra edited this page Oct 26, 2021 · 12 revisions

Hooks allow you to register commands and script to run when doing performs certain actions. You can register the following events:

  • post_config -- Runs after the configuration is read, including any local .doingrc file located at run time
  • post_read -- Runs after the contents of the doing file are parsed
  • post_write -- Runs after the contents of the doing file are changed. Does not run unless a modification is made

To register a hook, simply place a Ruby file in your plugins folder. This folder is located at ~/.config/doing/plugins by default, but you can specify another location in your config using the plugin_path key.

The ruby file should contain a call to register the hook. Pass a block to perform when the associated event is triggered.

The following runs a shell script called after_doing.sh whenever a change is made to the doing file (post_write event):

# frozen_string_literal: true

Doing::Hooks.register :post_write do |filename|
  res = `/bin/bash ~/scripts/after_doing.sh`.strip
  Doing.logger.debug('Hooks:', res)
end

That's all there is to it. As an example, I use the above shell script to update my iTerm status bar and my Touch Bar (BetterTouchTool widget) with my current task whenever the file changes. Any time I add or complete an entry, my status bars update.

:post_config

The post_config hook receives a full copy of the configuration object. This is a read-only Hash for reference. Changes to it will not affect the in-memory configuration.

Doing::Hooks.register :post_config do |config|
  # Actions
end

:post_read

The post_read hook receives the full content hash. This contains keys for each section, and their values include an items key with an array of all of the items in that section. Each item has values for (Date)date, (String)title, section(String), and note(Note < Array).

Doing::Hooks.register :post_read do |content|
  # Actions
end

:post_write

The post_write hook receives the filename that was saved to.

Doing::Hooks.register :post_write do |filename|
  # Actions
end