Skip to content

Commit

Permalink
(fix) parse filters similarly to the CLI
Browse files Browse the repository at this point in the history
The `task` CLI has the general form: `task [<filter>] <command>
[<mods>]`. I'm not interested in supporting the full syntax right now,
but placing filters between `task` and `command` (the report name) makes
the syntax for the code block significantly more similar to the CLI
syntax.

This is a breaking change.
  • Loading branch information
Ashton Eby committed Feb 28, 2024
1 parent 2022b05 commit 3112f97
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This is an [Obsidian](https://obsidian.md) plugin that allows viewing [taskwarri
## Task tables
To include a table of tasks, use a `task-table` code block. The text inside the code block is parsed as YAML; include a key called `command` with the taskwarrior command that should be executed to generate the table. The syntax for this is exactly the same as the taskwarrior CLI syntax except that:

- The report name must immediately follow `task`. For example, `task list +nonsense` is legal, but `task +nonsense list` is not.
- The report name must be the last token, and will not be defaulted if not provided. For example, `task +nonsense list` is legal, but `task list +nonsense` and `task +nonsense` are not.
- Some overrides are provided so the resulting ascii table can be parsed to an HTML table; `rc.detection` is set to `off`, and `rc.defaultWidth` is set to `1000`.

A basic use-case might look like:
Expand Down
40 changes: 34 additions & 6 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,48 @@
import { Plugin, Notice, MarkdownPostProcessor } from 'obsidian';
import { exec, execSync } from 'child_process'
import { parse } from 'yaml'
import { Plugin, parseYaml, PluginSettingTab, Setting, App } from 'obsidian';
import { execSync } from 'child_process'

interface Settings {
taskBinaryPath: string;
}

const DEFAULT_SETTINGS: Settings = {
taskBinaryPath: 'wsl task'
taskBinaryPath: 'task'
}

export class EgoRockSettingsTab extends PluginSettingTab {
plugin: EgoRock

constructor(app: App, plugin: EgoRock) {
super(app, plugin)
this.plugin = plugin
}

display(): void {
let { containerEl } = this

containerEl.empty()

new Setting(containerEl)
.setName('Taskwarrior binary path')
.setDesc('The path to the taskwarrior binary. If task is on the system PATH, "task" should work. Otherwise, provide an absolute path. WSL systems can invoke taskwarrior running in WSL from windows with the path "wsl task".')
.addText((text) =>
text
.setPlaceholder("task")
.setValue(this.plugin.settings.taskBinaryPath)
.onChange(async (value) => {
this.plugin.settings.taskBinaryPath = value
await this.plugin.saveSettings()
})
)
}
}

export default class EgoRock extends Plugin {
settings: Settings;

async onload() {
await this.loadSettings();
this.addSettingTab(new EgoRockSettingsTab(this.app, this))

this.registerMarkdownCodeBlockProcessor('task-table', (source, element, context) => {
this.doCommandReturnString(parseYaml(source).command, element, this.settings.taskBinaryPath)
Expand Down Expand Up @@ -87,9 +115,9 @@ export default class EgoRock extends Plugin {
doCommandReturnString(commandString: string, el: any, taskwarriorBin='wsl task') {
commandString = commandString.replace(/^task /, '')
const reports = this.getReportNames()
const report = commandString.split(' ')[0]
const report = commandString.split(' ').slice(-1)[0]
if (reports.includes(report)) {
const newCommand = `${taskwarriorBin.trim()} rc.detection:off rc.defaultwidth:1000 ${commandString.split(' ').slice(1).join(' ')} ${report}`
const newCommand = `${taskwarriorBin.trim()} rc.detection:off rc.defaultwidth:1000 ${commandString}`
const asciiTable = execSync(newCommand).toString().split('\n')
.filter((line) => {
if (line.match(/^[ -]*$/)) return false
Expand Down

0 comments on commit 3112f97

Please sign in to comment.