# UI/Categories: Rotate through category views, and add category watches
#
# List all category views:  python-pyrocore -m pyrocore.ui.categories -l
# Rotate to next view:      python-pyrocore -m pyrocore.ui.categories -qn
# Rotate to previous view:  python-pyrocore -m pyrocore.ui.categories -qp
# Re-filter current view:   python-pyrocore -m pyrocore.ui.categories -qu
#
# See also 'd.category.set' in 'helper-methods'.
#
# Example category definitions:
#
#   echo >>rtorrent.d/categories.rc "pyro.category.add = hdtv"
#   echo >>rtorrent.d/categories.rc "pyro.category.add = movies"
#   echo >>rtorrent.d/categories.rc "pyro.category.add = books"
#

# UI/Keys: Prev / Update / Next [ < | > ]
pyro.bind_key = category_view_prev, <,\
    "execute.nothrow.bg = (cat,(pyro.bin_dir),python-pyrocore), -m, pyrocore.ui.categories, -q, --prev"

pyro.bind_key = category_view_update, |,\
    "execute.nothrow.bg = (cat,(pyro.bin_dir),python-pyrocore), -m, pyrocore.ui.categories, -q, --update"

pyro.bind_key = category_view_next, >,\
    "execute.nothrow.bg = (cat,(pyro.bin_dir),python-pyrocore), -m, pyrocore.ui.categories, -q, --next"


# HELPER: Add a category
method.insert = pyro.category.add, private|simple,\
    "view.add = (cat, category_, (argument.0)) ;\
     pyro.view.collapsed.toggle = (cat, category_, (argument.0))"

# HELPER: Compare item category (label) to a given text (arg.0), for filtering
method.insert = pyro._in_category, simple|private, "equal={d.custom1=,argument.0=}"

# HELPER: Filter a category view
method.insert = pyro.category.update, simple,\
    "view.filter = (cat, category_, (argument.0)), pyro._in_category=$argument.0="


# HELPER: Categorized watches
#
# This allows you to add concise labelled watches like this:
#
#   schedule2 = watch_hdtv, 123, 10, ((load.category.normal, hdtv))
#
# Metafiles are loaded from a subdir with the given category
# name, in the normal watch folder (cfg.watch).
#
# The "category" custom attribute is set to the given label,
# as is "custom1" (the ruTorrent label). Use either in your
# completion handler to create target paths.
#
# Alternatively, you can add `pyro.category.separated.set = 1` to
# your custom config, which causes the download path to be set
# immediately to a categorized one (`…/work/‹category›`).
#

method.insert.value = pyro.category.separated, 0

method.insert = d.category.set, simple,\
    "d.custom1.set = (argument.0) ;\
     pyro.category.update = (d.custom,category) ;\
     d.custom.set = category,(argument.0) ;\
     pyro.category.update = (argument.0)"

method.insert = pyro._category.path, private|simple,\
    "cat = (directory.default), /, (argument.0)"
method.insert = pyro._category.check_set_dir, private|simple,\
    "branch = (pyro.category.separated),\
        ((d.directory.set, (pyro._category.path, (argument.0))))"

method.insert = load.category.normal, private|simple,\
    "load.verbose =\
        (cat,(cfg.watch),(argument.0),/*.torrent),\
        (cat, d.category.set=, (argument.0)),\
        (cat, pyro._category.check_set_dir=, (argument.0)) ;\
     pyro.category.update=(argument.0)"
method.insert = load.category.start, private|simple,\
    "load.start_verbose =\
        (cat, (cfg.watch), (argument.0), /*.torrent),\
        (cat, d.category.set=, (argument.0)),\
        (cat, pyro._category.check_set_dir=, (argument.0)) ;\
     pyro.category.update = (argument.0)"

# END categories
