| 1 | # ABSTRACT: List tasks with filtering and sorting | ||
| 2 | |||
| 3 | package App::karr::Cmd::List; | ||
| 4 | our $VERSION = '0.602'; | ||
| 5 | 2 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use MooX::Cmd; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 2 | ▶ | use MooX::Options ( |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 2 | usage_string => 'USAGE: karr list [--status LIST] [--priority LIST] [--archived] [--sort FIELD] [--limit N] [--group-by FIELD] [options]', | |
| 9 | 2 | ); | |
| 10 | 0 | ▶ | use App::karr::Role::BoardAccess; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 11 | use App::karr::Role::Output; | ||
| 12 | use App::karr::Role::CompactOutput; | ||
| 13 | use App::karr::Board; | ||
| 14 | # For --unclaimed, and for nothing else: claim_held is the claim test | ||
| 15 | # App::karr::Role::PickRules/pickable applies, so this command and `karr pick` | ||
| 16 | # cannot disagree about who holds a card (ticket #252). Only the claim test: | ||
| 17 | # pick also refuses blocked and backlog cards, and --unclaimed lists them | ||
| 18 | # (ticket k309). The role is composed rather than the predicate rewritten | ||
| 19 | # here, which is the whole point of the option. The rest of what it brings -- | ||
| 20 | # check_claim and its reporting half -- belongs to the mutating commands; | ||
| 21 | # `list` writes nothing and never calls it. | ||
| 22 | use App::karr::Role::ClaimTimeout; | ||
| 23 | use App::karr::Role::ClaimDefault; | ||
| 24 | use App::karr::Task; | ||
| 25 | use App::karr::Config; | ||
| 26 | use App::karr::Error qw( user_error command_hint ); | ||
| 27 | |||
| 28 | with 'App::karr::Role::BoardAccess', 'App::karr::Role::Output', | ||
| 29 | 'App::karr::Role::CompactOutput', 'App::karr::Role::ClaimTimeout', | ||
| 30 | 'App::karr::Role::ClaimDefault'; | ||
| 31 | |||
| 32 | |||
| 33 | option status => ( | ||
| 34 | is => 'ro', | ||
| 35 | format => 's', | ||
| 36 | doc => 'Filter by status (comma-separated)', | ||
| 37 | ); | ||
| 38 | |||
| 39 | option priority => ( | ||
| 40 | is => 'ro', | ||
| 41 | format => 's', | ||
| 42 | doc => 'Filter by priority (comma-separated)', | ||
| 43 | ); | ||
| 44 | |||
| 45 | option assignee => ( | ||
| 46 | is => 'ro', | ||
| 47 | format => 's', | ||
| 48 | doc => 'Filter by assignee', | ||
| 49 | ); | ||
| 50 | |||
| 51 | option tag => ( | ||
| 52 | is => 'ro', | ||
| 53 | format => 's', | ||
| 54 | doc => 'Filter by tag', | ||
| 55 | ); | ||
| 56 | |||
| 57 | option search => ( | ||
| 58 | is => 'ro', | ||
| 59 | format => 's', | ||
| 60 | short => 's', | ||
| 61 | doc => 'Search tasks by title, body, or tags', | ||
| 62 | ); | ||
| 63 | |||
| 64 | option claimed_by => ( | ||
| 65 | is => 'ro', | ||
| 66 | format => 's', | ||
| 67 | doc => 'Filter by claim owner', | ||
| 68 | ); | ||
| 69 | |||
| 70 | # The complete set of --sort keys, in the order the usage message lists them. | ||
| 71 | # Single source for the option doc, the usage message, and _comparators. | ||
| 72 | my @SORT_FIELDS = qw( id title status priority created updated due ); | ||
| 73 | |||
| 74 | option sort => ( | ||
| 75 | is => 'ro', | ||
| 76 | format => 's', | ||
| 77 | default => sub { 'id' }, | ||
| 78 | doc => 'Sort by: ' . join(', ', @SORT_FIELDS), | ||
| 79 | ); | ||
| 80 | |||
| 81 | option reverse => ( | ||
| 82 | is => 'ro', | ||
| 83 | short => 'r', | ||
| 84 | doc => 'Reverse sort order', | ||
| 85 | ); | ||
| 86 | |||
| 87 | option archived => ( | ||
| 88 | is => 'ro', | ||
| 89 | doc => 'Show only archived tasks', | ||
| 90 | ); | ||
| 91 | |||
| 92 | option class => ( | ||
| 93 | is => 'ro', | ||
| 94 | format => 's', | ||
| 95 | doc => 'Filter by class of service', | ||
| 96 | ); | ||
| 97 | |||
| 98 | option blocked => ( | ||
| 99 | is => 'ro', | ||
| 100 | doc => 'Show only blocked tasks', | ||
| 101 | ); | ||
| 102 | |||
| 103 | option not_blocked => ( | ||
| 104 | is => 'ro', | ||
| 105 | doc => 'Show only tasks that are not blocked', | ||
| 106 | ); | ||
| 107 | |||
| 108 | option unclaimed => ( | ||
| 109 | is => 'ro', | ||
| 110 | doc => 'Show only tasks no live claim holds', | ||
| 111 | ); | ||
| 112 | |||
| 113 | option limit => ( | ||
| 114 | is => 'ro', | ||
| 115 | format => 'i', | ||
| 116 | short => 'n', | ||
| 117 | default => sub { 0 }, | ||
| 118 | doc => 'Show at most N tasks after filtering and sorting (0 = no limit)', | ||
| 119 | ); | ||
| 120 | |||
| 121 | # The complete set of --group-by keys, in the order the usage message lists | ||
| 122 | # them. Single source for the option doc, the usage message, and the | ||
| 123 | # validation. The same five kanban-md's GroupBy takes | ||
| 124 | # (internal/board/group.go). | ||
| 125 | my @GROUP_FIELDS = qw( assignee tag class priority status ); | ||
| 126 | |||
| 127 | option group_by => ( | ||
| 128 | is => 'ro', | ||
| 129 | format => 's', | ||
| 130 | doc => 'Group by: ' . join(', ', @GROUP_FIELDS), | ||
| 131 | ); | ||
| 132 | |||
| 133 | sub execute { | ||
| 134 | my ($self, $args_ref, $chain_ref) = @_; | ||
| 135 | # "0 task(s)" and `[]` are answers about a board; a repository with no board | ||
| 136 | # has to say that instead of borrowing them (#135). | ||
| 137 | $self->require_local_board; | ||
| 138 | $self->_validate_options; | ||
| 139 | # The pre-filter list is kept for the footer's hidden-done count: the | ||
| 140 | # default view hides the board's terminal statuses, and the footer has to | ||
| 141 | # say how many cards that filter withheld -- a count over the filtered list | ||
| 142 | # would be "how many of what I already see are done", which is zero by | ||
| 143 | # construction. | ||
| 144 | my @all_tasks = $self->_load_tasks; | ||
| 145 | # One window for the whole run, read once here rather than per card: the | ||
| 146 | # claim test re-reads claim_timeout for every card otherwise, and a board | ||
| 147 | # that changes its timeout mid-list would answer with two different notions | ||
| 148 | # of "held" on one screen. Read only when something will actually test a | ||
| 149 | # claim -- the --unclaimed filter, or a card with a claim to render -- so a | ||
| 150 | # claim-free board's plain list never pays for the parse (ticket #252). | ||
| 151 | my $timeout; | ||
| 152 | if ( $self->unclaimed || grep { $_->has_claimed_by } @all_tasks ) { | ||
| 153 | $timeout = $self->claim_timeout_secs; | ||
| 154 | } | ||
| 155 | # Filter, then sort, then cut. kanban-md's board.List does the three in that | ||
| 156 | # order (internal/board/board.go) and the order is the whole point of the | ||
| 157 | # third: cutting before the sort would keep an arbitrary N and then order | ||
| 158 | # those, so `--sort priority -n 5` would answer with five tasks that are not | ||
| 159 | # the five most urgent ones. | ||
| 160 | my @tasks = $self->_filter( \@all_tasks, $timeout ); | ||
| 161 | @tasks = $self->_sort(\@tasks); | ||
| 162 | @tasks = $self->_limit(\@tasks); | ||
| 163 | |||
| 164 | if ($self->json) { | ||
| 165 | # to_json_hash, not to_frontmatter: the body lives below the frontmatter in | ||
| 166 | # the file format, so the frontmatter view has no body to give and list | ||
| 167 | # --json shipped bodiless cards while show/pick/handoff shipped whole ones | ||
| 168 | # (ticket #129). kanban-md marshals the full task here too, with | ||
| 169 | # `json:"body,omitempty"` on Body (cmd/list.go, internal/task/task.go). | ||
| 170 | $self->print_json([map { $_->to_json_hash } @tasks]); | ||
| 171 | return; | ||
| 172 | } | ||
| 173 | |||
| 174 | # The line itself is L<App::karr::Task/compact_line>, so `show --compact` | ||
| 175 | # prints exactly this and cannot drift from it (#254). | ||
| 176 | if ($self->compact) { | ||
| 177 | for my $t (@tasks) { | ||
| 178 | print $t->compact_line . "\n"; | ||
| 179 | } | ||
| 180 | return; | ||
| 181 | } | ||
| 182 | |||
| 183 | printf "%-5s %10s %s\n", 'ID', 'STATUS', 'TITLE'; | ||
| 184 | printf "%s\n", '-' x 72; | ||
| 185 | my $board = App::karr::Board->new( store => $self->store ); | ||
| 186 | if ($self->group_by) { | ||
| 187 | my %groups; | ||
| 188 | for my $t (@tasks) { | ||
| 189 | push @{ $groups{$_} }, $t for $board->group_keys( $t, $self->group_by ); | ||
| 190 | } | ||
| 191 | for my $key ( $board->group_order( \%groups, $self->group_by ) ) { | ||
| 192 | print "\n## $key\n"; | ||
| 193 | $self->_render_row( $_, $timeout ) for @{ $groups{$key} }; | ||
| 194 | } | ||
| 195 | } else { | ||
| 196 | $self->_render_row( $_, $timeout ) for @tasks; | ||
| 197 | } | ||
| 198 | |||
| 199 | my $footer = scalar(@tasks) . ' task(s)'; | ||
| 200 | # The default view hides the board's terminal statuses; when that filter hid | ||
| 201 | # something, the footer says so and names the column -- the same count | ||
| 202 | # `karr board`'s footer prints, computed by the same helper (ticket #275). | ||
| 203 | # The gate is the default filter itself: --status and --archived replace it, | ||
| 204 | # so nothing was hidden by it, and the hint would be a lie. | ||
| 205 | if ( !$self->status && !$self->archived ) { | ||
| 206 | my $final_status = $board->final_status; | ||
| 207 | my $hidden = defined $final_status ? $board->hidden_done_count( \@all_tasks ) : 0; | ||
| 208 | if ($hidden) { | ||
| 209 | $footer .= " ($hidden $final_status hidden; --status $final_status to include)"; | ||
| 210 | } | ||
| 211 | } | ||
| 212 | print "\n$footer\n"; | ||
| 213 | } | ||
| 214 | |||
| 215 | # One table row. Extracted from execute so the grouped view prints exactly the | ||
| 216 | # rows the flat view does (ticket #279). | ||
| 217 | sub _render_row { | ||
| 218 | my ( $self, $t, $timeout ) = @_; | ||
| 219 | my @meta; | ||
| 220 | push @meta, $t->priority if defined $t->priority && length $t->priority; | ||
| 221 | # An `assignee: ""` from kanban-md satisfies the predicate but names | ||
| 222 | # nobody, and printing it gave every imported card a bare "@" in its meta | ||
| 223 | # list. Empty means absent here as it does in pick (ticket #59). | ||
| 224 | push @meta, '@' . $t->assignee if $t->has_assignee && length $t->assignee; | ||
| 225 | # A claim is only worth showing while the work is still live, and which | ||
| 226 | # columns count as finished is the board's decision -- a board imported from | ||
| 227 | # kanban-md can end in `shipped`, and a claim on a finished card is | ||
| 228 | # provenance, not a lease (CONTEXT.md). claim_held is the same test | ||
| 229 | # --unclaimed and pick apply, so the table cannot disagree with either about | ||
| 230 | # who holds a card (ticket #275). | ||
| 231 | if ( $self->claim_held( $t, $timeout ) | ||
| 232 | && !$self->store->is_terminal_status( $t->status ) ) { | ||
| 233 | push @meta, '@' . $t->claimed_by; | ||
| 234 | } | ||
| 235 | push @meta, 'blocked' if $t->has_blocked; | ||
| 236 | my $title = $t->title; | ||
| 237 | $title .= ' [' . join(', ', @meta) . ']' if @meta; | ||
| 238 | |||
| 239 | printf "#%-4u %10s %s\n", | ||
| 240 | $t->id, | ||
| 241 | $t->status, | ||
| 242 | $title; | ||
| 243 | } | ||
| 244 | |||
| 245 | # The filter flags an invalid --status value is really reaching for. "blocked" | ||
| 246 | # and "not-blocked" read like statuses but are the --blocked/--not-blocked | ||
| 247 | # filters, and agents type `--status blocked` reproducibly (ticket k290). Maps | ||
| 248 | # the bare value to the flag that was meant, so the rejection can name it. | ||
| 249 | my %FILTER_FLAG_FOR = ( | ||
| 250 | 'blocked' => '--blocked', | ||
| 251 | 'not-blocked' => '--not-blocked', | ||
| 252 | ); | ||
| 253 | |||
| 254 | # Every usage error this command can raise that does not need a task in hand, | ||
| 255 | # decided in one place before the first ref is read: whether an invocation is | ||
| 256 | # well formed is not a question about what happens to be on the board, and an | ||
| 257 | # answer that arrives after the load has a chance of arriving after some of the | ||
| 258 | # output too. | ||
| 259 | sub _validate_options { | ||
| 260 | my ($self) = @_; | ||
| 261 | |||
| 262 | # Ticket #235's rule: an invocation that contradicts itself is refused, not | ||
| 263 | # silently resolved for the caller. kanban-md lets --blocked win over | ||
| 264 | # --not-blocked without a word (cmd/list.go tests `if blocked` first), which | ||
| 265 | # is the same silent pick karr already rejects for `edit --claim/--release` | ||
| 266 | # and `move --next/--prev`. Nobody types both on purpose, so the useful | ||
| 267 | # answer is the one that says so. | ||
| 268 | $self->usage_error('cannot use --blocked and --not-blocked together') | ||
| 269 | if $self->blocked && $self->not_blocked; | ||
| 270 | |||
| 271 | # The pair has a common case, and that is precisely why it is refused rather | ||
| 272 | # than answered. --claimed-by NAME is an exact match on the field and so also | ||
| 273 | # matches a claim of NAME's that has expired; --unclaimed is "nobody holds | ||
| 274 | # this now", which an expired claim satisfies. The two therefore intersect on | ||
| 275 | # "cards NAME claimed and no longer holds" -- a real set, non-empty on a | ||
| 276 | # normal board, and a third question nobody typed. Answering it silently is | ||
| 277 | # worse than answering the contradicting pairs #235 collected, not better: a | ||
| 278 | # plausible, non-empty list comes back for an invocation whose author meant | ||
| 279 | # either "free cards" or "NAME's cards" and got neither. So this follows | ||
| 280 | # `edit --claim/--release`, `move --next/--prev` and --blocked/--not-blocked | ||
| 281 | # above and refuses, which also leaves the door open -- if "which of my | ||
| 282 | # claims did I lose" is wanted, it deserves its own spelling rather than | ||
| 283 | # arriving as the accident of two filters meeting. | ||
| 284 | # | ||
| 285 | # Both orders reach this line. `--unclaimed --claimed-by NAME` did not until | ||
| 286 | # #256 -- a bare boolean in front of a dashed option ate its name, so that | ||
| 287 | # spelling died as `Unknown option: claimed-by` before the check could speak. | ||
| 288 | # `defined`, like the --class check below and unlike the truthy guard the | ||
| 289 | # filter itself uses: a value the caller typed is a value the caller typed | ||
| 290 | # (#153, #239, #244), even when it is empty. | ||
| 291 | $self->usage_error('cannot use --unclaimed and --claimed-by together') | ||
| 292 | if $self->unclaimed && defined $self->claimed_by; | ||
| 293 | |||
| 294 | # 0 is the documented "no limit" and stays legal -- it is the default, and | ||
| 295 | # kanban-md spells unlimited that way too. A negative count is not a smaller | ||
| 296 | # list, it is a typo, and kanban-md's `Limit > 0` guard swallows it back into | ||
| 297 | # "unlimited": the caller asks for at most -1 tasks and gets the whole board. | ||
| 298 | # Rejected here for the reason `show`/`log --last` reject theirs (#76, #151, | ||
| 299 | # ADR 0002) -- note those two reject 0 as well, because a count of zero | ||
| 300 | # entries means nothing there while it means "all of them" here. | ||
| 301 | $self->usage_error( sprintf '--limit must be 0 or greater (got %d)', $self->limit ) | ||
| 302 | if $self->limit < 0; | ||
| 303 | |||
| 304 | # --group-by is a rendering key, validated like --sort: an unknown field is | ||
| 305 | # a usage error (exit 2) rather than a silent "no grouping happened". The | ||
| 306 | # accepted set is the same five kanban-md's GroupBy takes | ||
| 307 | # (internal/board/group.go). | ||
| 308 | if ( defined $self->group_by ) { | ||
| 309 | my $field = $self->group_by; | ||
| 310 | user_error( "Usage: karr list --group-by ", | ||
| 311 | join('|', @GROUP_FIELDS), " (got '$field')" ) | ||
| 312 | unless grep { $_ eq $field } @GROUP_FIELDS; | ||
| 313 | } | ||
| 314 | |||
| 315 | # A class the board does not configure is a usage error, naming the classes | ||
| 316 | # that exist -- the same answer `create --class` gives and the same answer | ||
| 317 | # --sort gives an unknown field. Deliberate divergence from kanban-md, whose | ||
| 318 | # filter is a bare string equality (internal/board/filter.go): there | ||
| 319 | # `list --class bogus` prints an empty list, which reads as "no such work" | ||
| 320 | # when the truth is "no such class". Validating needs the board's config, so | ||
| 321 | # this one sits after require_local_board rather than before it. | ||
| 322 | $self->config->validate_class( $self->class ) if defined $self->class; | ||
| 323 | |||
| 324 | # --status and --priority are comma-separated lists, and every element is a | ||
| 325 | # value the board must know -- a typo in one of them used to print an empty | ||
| 326 | # list and exit 0, which reads as "no such work" when the truth is "no such | ||
| 327 | # status" (ticket #271). Same answer --class gives above and `create`/`move` | ||
| 328 | # give the same value. The status filter accepts `archived` even on a board | ||
| 329 | # that does not configure it (App::karr::Config/validate_status_filter); the | ||
| 330 | # priority filter is the plain L<App::karr::Config/validate_priority>. | ||
| 331 | if ( defined $self->status ) { | ||
| 332 | for my $value ( split /,/, $self->status ) { | ||
| 333 | # The hint rides on the rejection, not in front of it: a board could in | ||
| 334 | # principle configure a status literally named `blocked`, so validate | ||
| 335 | # first and only reach for the flag when the value is genuinely refused. | ||
| 336 | # When the refused value is one of the filter-flag concepts, append the | ||
| 337 | # invocation that was meant (ticket k290, k263's answer-goes-last rule); | ||
| 338 | # every other invalid value keeps the plain `(valid: ...)` message. | ||
| 339 | eval { $self->config->validate_status_filter($value); 1 } and next; | ||
| 340 | my $err = $@; | ||
| 341 | my $flag = $FILTER_FLAG_FOR{$value} or die $err; | ||
| 342 | chomp $err; | ||
| 343 | die $err . qq{ -- "$value" is a filter flag, not a status:\n} | ||
| 344 | . command_hint( 'list', $flag ) . "\n"; | ||
| 345 | } | ||
| 346 | } | ||
| 347 | if ( defined $self->priority ) { | ||
| 348 | $self->config->validate_priority($_) for split /,/, $self->priority; | ||
| 349 | } | ||
| 350 | } | ||
| 351 | |||
| 352 | sub _load_tasks { | ||
| 353 | my ($self) = @_; | ||
| 354 | return $self->load_tasks; | ||
| 355 | } | ||
| 356 | |||
| 357 | sub _filter { | ||
| 358 | my ($self, $tasks, $timeout) = @_; | ||
| 359 | my @filtered = @$tasks; | ||
| 360 | |||
| 361 | # Which statuses were asked for, if any. --archived is a status filter and | ||
| 362 | # nothing more, exactly as in kanban-md (cmd/list.go): it replaces --status | ||
| 363 | # rather than intersecting with it, and every other filter below still | ||
| 364 | # applies on top, so `--archived --tag legacy` means what it reads like. | ||
| 365 | my $wanted; | ||
| 366 | if ($self->archived) { | ||
| 367 | $wanted = { App::karr::Config->ARCHIVED_STATUS => 1 }; | ||
| 368 | } elsif ($self->status) { | ||
| 369 | $wanted = { map { $_ => 1 } split /,/, $self->status }; | ||
| 370 | } | ||
| 371 | |||
| 372 | # Nothing asked for: hide the board's terminal statuses, so the default view | ||
| 373 | # is open work. Asked of the store, so a board whose final column is | ||
| 374 | # `shipped` hides shipped work instead of the `done` it does not have | ||
| 375 | # (ticket #67). | ||
| 376 | if ($wanted) { | ||
| 377 | @filtered = grep { $wanted->{$_->status} } @filtered; | ||
| 378 | } else { | ||
| 379 | @filtered = grep { !$self->store->is_terminal_status($_->status) } @filtered; | ||
| 380 | } | ||
| 381 | if ($self->priority) { | ||
| 382 | my %priorities = map { $_ => 1 } split /,/, $self->priority; | ||
| 383 | @filtered = grep { $priorities{$_->priority} } @filtered; | ||
| 384 | } | ||
| 385 | if ($self->assignee) { | ||
| 386 | @filtered = grep { $_->has_assignee && $_->assignee eq $self->assignee } @filtered; | ||
| 387 | } | ||
| 388 | if ($self->tag) { | ||
| 389 | @filtered = grep { | ||
| 390 | my $t = $_; | ||
| 391 | grep { $_ eq $self->tag } @{$t->tags}; | ||
| 392 | } @filtered; | ||
| 393 | } | ||
| 394 | # --claimed-by defaults to KARR_CLAIM when omitted (ADR 0005), so an agent | ||
| 395 | # that exported it sees its own cards from a bare `karr list`. --unclaimed asks | ||
| 396 | # the opposite question and wins outright: the env default is suppressed under | ||
| 397 | # it, and an explicit --claimed-by alongside --unclaimed was already rejected | ||
| 398 | # as a usage error above, so this only steps aside for the env-supplied value. | ||
| 399 | my $claimed_by = $self->unclaimed ? undef : $self->resolved_claimed_by; | ||
| 400 | if ( defined $claimed_by && length $claimed_by ) { | ||
| 401 | @filtered = grep { $_->has_claimed_by && $_->claimed_by eq $claimed_by } @filtered; | ||
| 402 | } | ||
| 403 | # The claim test itself is App::karr::Role::ClaimTimeout/claim_held -- the | ||
| 404 | # one App::karr::Role::PickRules/pickable applies -- so this list and `karr | ||
| 405 | # pick` cannot come to disagree about who holds a card (#59, #198, #252). | ||
| 406 | # One window for the whole run, read once here rather than per card: a | ||
| 407 | # board-wide filter that re-read claim_timeout for every card could in | ||
| 408 | # principle straddle a config change mid-list, and would certainly do the | ||
| 409 | # parse N times. | ||
| 410 | # | ||
| 411 | # Claim only, as kanban-md's IsUnclaimed is: pickable goes on to exclude | ||
| 412 | # backlog, blocked and terminal cards, and none of them is a statement about | ||
| 413 | # who holds the card (k309). --blocked --unclaimed is a real triage query | ||
| 414 | # here, not an empty one. | ||
| 415 | # The window was read once in execute and is passed in, so the filter and | ||
| 416 | # the table's claim display cannot judge the same run against two timeouts. | ||
| 417 | if ($self->unclaimed) { | ||
| 418 | @filtered = grep { !$self->claim_held( $_, $timeout ) } @filtered; | ||
| 419 | } | ||
| 420 | # Plain equality against the card's class, which App::karr::Task always has | ||
| 421 | # (it defaults to `standard`), so there is no unset case to fold in. The | ||
| 422 | # value was checked against the board's classes in _validate_options, so an | ||
| 423 | # empty result here means the board has no card of that class -- not that the | ||
| 424 | # class was misspelled. | ||
| 425 | if (defined $self->class) { | ||
| 426 | @filtered = grep { $_->class eq $self->class } @filtered; | ||
| 427 | } | ||
| 428 | # has_blocked is the whole test on both sides: L<App::karr::Task/BUILD> | ||
| 429 | # normalizes the field so the predicate is true exactly when the card is | ||
| 430 | # blocked, and `blocked: false` from a kanban-md document is not | ||
| 431 | # representable as "set but off" (ticket #58). The pair is mutually | ||
| 432 | # exclusive, rejected in _validate_options, so the elsif cannot hide a | ||
| 433 | # second filter from anybody. | ||
| 434 | if ($self->blocked) { | ||
| 435 | @filtered = grep { $_->has_blocked } @filtered; | ||
| 436 | } elsif ($self->not_blocked) { | ||
| 437 | @filtered = grep { !$_->has_blocked } @filtered; | ||
| 438 | } | ||
| 439 | if ($self->search) { | ||
| 440 | my $q = lc($self->search); | ||
| 441 | @filtered = grep { | ||
| 442 | index(lc($_->title), $q) >= 0 | ||
| 443 | || index(lc($_->body), $q) >= 0 | ||
| 444 | || grep { index(lc($_), $q) >= 0 } @{$_->tags} | ||
| 445 | } @filtered; | ||
| 446 | } | ||
| 447 | return @filtered; | ||
| 448 | } | ||
| 449 | |||
| 450 | # Cut to --limit, last of the three stages and deliberately after the sort. | ||
| 451 | # 0 -- the default -- is no limit rather than an empty list, matching | ||
| 452 | # kanban-md's `if opts.Limit > 0` (internal/board/board.go); a negative value | ||
| 453 | # never reaches here, _validate_options refuses it. The cut is in execute | ||
| 454 | # rather than in either output branch so --json, --compact and the table all | ||
| 455 | # see the same N tasks: a --limit that only applied to the human table would be | ||
| 456 | # a limit exactly where the context it saves does not matter. | ||
| 457 | sub _limit { | ||
| 458 | my ($self, $tasks) = @_; | ||
| 459 | my $limit = $self->limit; | ||
| 460 | return @$tasks unless $limit > 0 && @$tasks > $limit; | ||
| 461 | return @{$tasks}[ 0 .. $limit - 1 ]; | ||
| 462 | } | ||
| 463 | |||
| 464 | sub _sort { | ||
| 465 | my ($self, $tasks) = @_; | ||
| 466 | my $field = $self->sort; | ||
| 467 | |||
| 468 | # Look the key up in an explicit table; never call it as a method. The old | ||
| 469 | # `$a->$field` turned a value straight from argv into a method call on | ||
| 470 | # App::karr::Task, so `--sort slug` and `--sort to_markdown` both ran, and an | ||
| 471 | # unknown key died with "Can't locate object method ... at List.pm line NNN". | ||
| 472 | my $comparators = $self->_comparators; | ||
| 473 | my $cmp = $comparators->{$field} | ||
| 474 | or user_error( "Usage: karr list --sort ", join('|', @SORT_FIELDS), | ||
| 475 | " (got '$field')" ); | ||
| 476 | |||
| 477 | # Tie-break on id so the order is fully determined: Perl's sort is stable in | ||
| 478 | # practice but not by contract, and load_tasks already hands tasks over in | ||
| 479 | # ascending id order, so this pins what stability was silently providing. | ||
| 480 | my @sorted = sort { $cmp->($a, $b) || $a->id <=> $b->id } @$tasks; | ||
| 481 | @sorted = reverse @sorted if $self->reverse; | ||
| 482 | return @sorted; | ||
| 483 | } | ||
| 484 | |||
| 485 | # One comparator per allowed --sort key. Status follows the board config's own | ||
| 486 | # order rather than the alphabet or a hardcoded table, matching kanban-md's | ||
| 487 | # Sort/compareTasks (internal/board/sort.go) which indexes both through | ||
| 488 | # cfg.StatusIndex / cfg.PriorityIndex. Priority deliberately breaks that | ||
| 489 | # symmetry (ticket #91): it walks the config list backwards, so the most | ||
| 490 | # urgent task -- the last name in priorities, critical on a default board -- | ||
| 491 | # sorts first and the top of the list agrees with what pick would take. | ||
| 492 | # kanban-md's ascending order (sort.go:29) put the least urgent task on top, | ||
| 493 | # the exact opposite of pick; it has since taken the same direction (upstream | ||
| 494 | # c783157), one layer up -- cmd/list.go flips the reverse flag for this one key | ||
| 495 | # and sort.go:29 stays ascending -- so the comparators differ but the order a | ||
| 496 | # user sees does not. A value that is not in the config still gets | ||
| 497 | # index -1, as kanban-md's IndexOf does; descending, that keeps it at the | ||
| 498 | # least-urgent end, the same end the ascending order gave it. | ||
| 499 | sub _comparators { | ||
| 500 | my ($self) = @_; | ||
| 501 | my %status = $self->_index_of( $self->config->statuses ); | ||
| 502 | my %priority = $self->_index_of( $self->config->priorities ); | ||
| 503 | return { | ||
| 504 | id => sub { $_[0]->id <=> $_[1]->id }, | ||
| 505 | # Case-insensitively, as kanban-md's compareTasks does via strings.ToLower | ||
| 506 | # (internal/board/sort.go:31). A bare `cmp` would sort every capitalized | ||
| 507 | # title ahead of every lowercase one, which is a second alphabet, not a | ||
| 508 | # sort by title. lc() sees characters here, not octets, so a title outside | ||
| 509 | # ASCII lowercases by Unicode rules -- but cmp still compares codepoints, | ||
| 510 | # so such a title sorts behind every ASCII one rather than beside its | ||
| 511 | # unaccented spelling. No collation is promised, and none is coming: a | ||
| 512 | # collating comparator needs a locale to collate for, boards are read by | ||
| 513 | # agents on machines that share none, and two hosts disagreeing about the | ||
| 514 | # order is worse than one order that is merely not alphabetical. The POD | ||
| 515 | # under FILTERS AND SORTING states this as a non-goal (ticket #237). | ||
| 516 | title => sub { lc($_[0]->title) cmp lc($_[1]->title) }, | ||
| 517 | status => sub { ($status{$_[0]->status} // -1) <=> ($status{$_[1]->status} // -1) }, | ||
| 518 | priority => sub { ($priority{$_[1]->priority} // -1) <=> ($priority{$_[0]->priority} // -1) }, | ||
| 519 | # created/updated are ISO-8601 UTC stamps, so a string compare is | ||
| 520 | # chronological. | ||
| 521 | created => sub { $_[0]->created cmp $_[1]->created }, | ||
| 522 | updated => sub { $_[0]->updated cmp $_[1]->updated }, | ||
| 523 | due => sub { $self->_cmp_due(@_) }, | ||
| 524 | }; | ||
| 525 | } | ||
| 526 | |||
| 527 | sub _index_of { | ||
| 528 | my ($self, @values) = @_; | ||
| 529 | my %index; | ||
| 530 | $index{$values[$_]} //= $_ for 0 .. $#values; | ||
| 531 | return %index; | ||
| 532 | } | ||
| 533 | |||
| 534 | # `due` is optional. kanban-md's compareDue sorts a task without a due date | ||
| 535 | # last; the previous `('' cmp '')` fallback sorted it first. | ||
| 536 | sub _cmp_due { | ||
| 537 | my ($self, $left, $right) = @_; | ||
| 538 | my $l = $self->_due_of($left); | ||
| 539 | my $r = $self->_due_of($right); | ||
| 540 | return 0 unless defined $l || defined $r; | ||
| 541 | return 1 unless defined $l; | ||
| 542 | return -1 unless defined $r; | ||
| 543 | return $l cmp $r; | ||
| 544 | } | ||
| 545 | |||
| 546 | sub _due_of { | ||
| 547 | my ($self, $task) = @_; | ||
| 548 | return undef unless $task->has_due; | ||
| 549 | my $due = $task->due; | ||
| 550 | return ( defined $due && length $due ) ? $due : undef; | ||
| 551 | } | ||
| 552 | |||
| 553 | 1; | ||
| 554 | |||
| 555 | __END__ | ||