obsidian-tasks-group / obsidian-tasks

Task management for the Obsidian knowledge base.
https://publish.obsidian.md/tasks/
MIT License
2.23k stars 222 forks source link

Add query/search feature to API to allow other plugins to query tasks based on task's query language #2459

Open mkirkland4874 opened 7 months ago

mkirkland4874 commented 7 months ago

⚠️ Please check that this feature request hasn't been suggested before.

🔖 Feature description

Add a query/search feature to the API to allow other plugins to query tasks based on the task query language.

const tasksApi = this.app.plugins.plugins['obsidian-tasks-plugin'].apiV1;
let tasks = await tasksApi.search("not done");

This would allow plugins like Full Calendar to be able to query and display tasks on a read-only basis. A related discussion on the Full Calendar discussion thread:

https://github.com/davish/obsidian-full-calendar/discussions/376#discussioncomment-7711424

✔️ Solution

There is some discussion around a search feature in section 5.2 at https://github.com/obsidian-tasks-group/obsidian-tasks/discussions/1646#discussion-4836417 with similar code below.

const tasksPlugin = this.app.plugins.getPlugin("obsidian-tasks-plugin")  

// Return the number of matching tasks, as a comma-separated string
function getQueryTasksCount(source) {
   return tasksPlugin.search(source).totalTasksCount().toLocaleString();
}

const today = new Date().toISOString().split('T')[0]
const total = getQueryTasksCount('');
const open = getQueryTasksCount('not done');
const done = getQueryTasksCount('done');

dv.el('pre', `| ${today} |             ${total} |             ${open} |            ${done} |                         n,nnn |`)

Here is a link to that search function. Some form of the search feature would need to be added to the API.

https://github.com/claremacrae/obsidian-tasks/blob/c302c57ae4a5ad2f1aa2d071c3d8565f24229e12/src/main.ts#L76-L81

❓ Alternatives

No response

📝 Additional Context

No response

claremacrae commented 7 months ago

Brilliant - thank you. And I had forgotten that I had written that up.

josephcrawfordSRH commented 3 months ago

I also like the idea of integrating these numbers with the calendar, but I also think it would be nice if I could access this information from Templater as well, I would like to have some text generated in my weekly, monthly and yearly templates to say something like the following

This year I have completed 534 tasks and left 87 tasks unfinished. This month I have completed 79 tasks and left 12 tasks unfinished. This week I have completed 24 tasks and left 2 tasks unfinished.

claremacrae commented 1 week ago

Whilst I have a working prototype for this in my fork, I don't consider it releasable yet because of the poor usability of the QueryResult class. It would need to be improved before release.

For example, accessing all the tasks via TaskGroups is really inconvenient.

I would also want to make sure that any stored arrays of values were returned readonly - maybe.