todoman icon indicating copy to clipboard operation
todoman copied to clipboard

Introduce a sort-by flag that accepts a python comparator function

Open tionis opened this issue 11 months ago • 2 comments

I'd like to add some more complex sorting logic, it would be nice if power users could pass a python function that is used as a comparator to the program.

tionis avatar Jan 19 '25 04:01 tionis

We store data in an sqlite cache, and query that. So your function would need to write SQL to query todos.

I have some vague plans to move caching out into a separate helper, which indexes all todos and provides an API for querying. todoman would become mostly a UI on top of that.

So I'm not sure if I'd implement this right now, or wait until that happens.

WhyNotHugo avatar Jan 21 '25 17:01 WhyNotHugo

This would only influence the sorting of the result though, so something like

from functools import cmp_to_key
from dopy.core import Dopy

# Using dopy to get python without indentation to make it easier to specify the func in a flag
dopy = Dopy()

if flags.sort_by not None:
  processed = dopy.preprocess(source)
  def compare(x, y):
    return exec(processed, namespace={"x": x, "y": y})
  data = sorted(data, key=cmp_to_key(compare))

could work without really increasing processing time or memory footprint significantly.

At least I think so, I haven't tried it yet.

tionis avatar Jan 21 '25 21:01 tionis