| 1 | # ABSTRACT: Show board summary | ||
| 2 | |||
| 3 | package App::karr::Cmd::Board; | ||
| 4 | our $VERSION = '0.602'; | ||
| 5 | 1 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use MooX::Cmd; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use MooX::Options ( |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | usage_string => 'USAGE: karr board [--json] [--compact] [--tags] [--done] [--group-by FIELD]', | |
| 9 | 1 | ); | |
| 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::Role::Color; | ||
| 14 | use App::karr::Board; | ||
| 15 | use App::karr::Task; | ||
| 16 | use App::karr::Config; | ||
| 17 | use App::karr::Error qw( user_error ); | ||
| 18 | use Term::ANSIColor qw( colored ); | ||
| 19 | |||
| 20 | with 'App::karr::Role::BoardAccess', 'App::karr::Role::Output', | ||
| 21 | 'App::karr::Role::CompactOutput', 'App::karr::Role::Color'; | ||
| 22 | |||
| 23 | option tags => ( | ||
| 24 | is => 'ro', | ||
| 25 | doc => 'Show each task\'s tags on an extra indented line', | ||
| 26 | ); | ||
| 27 | |||
| 28 | option done => ( | ||
| 29 | is => 'ro', | ||
| 30 | doc => 'Include the board\'s final column (hidden by default)', | ||
| 31 | ); | ||
| 32 | |||
| 33 | # The complete set of --group-by keys, in the order the usage message lists | ||
| 34 | # them. Single source for the option doc, the usage message, and the | ||
| 35 | # validation. The same five kanban-md's GroupBy takes | ||
| 36 | # (internal/board/group.go). | ||
| 37 | my @GROUP_FIELDS = qw( assignee tag class priority status ); | ||
| 38 | |||
| 39 | option group_by => ( | ||
| 40 | is => 'ro', | ||
| 41 | format => 's', | ||
| 42 | doc => 'Group by: ' . join(', ', @GROUP_FIELDS), | ||
| 43 | ); | ||
| 44 | |||
| 45 | |||
| 46 | my %STATUS_COLOR = ( | ||
| 47 | backlog => 'bright_black', | ||
| 48 | todo => 'cyan', | ||
| 49 | 'in-progress' => 'yellow', | ||
| 50 | review => 'magenta', | ||
| 51 | done => 'green', | ||
| 52 | ); | ||
| 53 | |||
| 54 | my %PRIORITY_COLOR = ( | ||
| 55 | critical => 'bold red', | ||
| 56 | high => 'red', | ||
| 57 | medium => 'yellow', | ||
| 58 | low => 'bright_black', | ||
| 59 | ); | ||
| 60 | |||
| 61 | sub execute { | ||
| 62 | my ($self, $args_ref, $chain_ref) = @_; | ||
| 63 | |||
| 64 | # Before anything is rendered: a repository with no board here would | ||
| 65 | # otherwise print the default config over an empty task list, which is | ||
| 66 | # byte-identical to a board that simply has no cards (#135). No sync -- see | ||
| 67 | # App::karr::Role::BoardDiscovery/require_local_board. | ||
| 68 | $self->require_local_board; | ||
| 69 | $self->_validate_options; | ||
| 70 | |||
| 71 | my $ec = $self->store->effective_config; | ||
| 72 | |||
| 73 | # `board` shows the board, and the archive is what a card is taken off the | ||
| 74 | # board into: `archived` is not one of the columns, and archived cards are | ||
| 75 | # dropped here -- before anything below groups, counts, renders or hides | ||
| 76 | # them. kanban-md arrives at the same place in two steps (cmd/board.go | ||
| 77 | # filters the task list with IsArchivedStatus, board.Summary iterates | ||
| 78 | # cfg.BoardStatuses()), but unlike the context block of #229 this view is no | ||
| 79 | # interop contract, so the reason it holds here is karr's own (#234): | ||
| 80 | # | ||
| 81 | # * The footer has to count something a reader can name. With the archive | ||
| 82 | # in it, this repository's own board ended in "241 tasks (231 done | ||
| 83 | # hidden)" over ten rendered cards, one of which was a card deliberately | ||
| 84 | # taken off the board. "241 tasks" here and "241 tasks" from the | ||
| 85 | # reference counted different things, and nothing on the line said which | ||
| 86 | # of them was meant. | ||
| 87 | # * Once the archive is not counted, still rendering it would put cards on | ||
| 88 | # screen that the total leaves out -- the exact inverse of the "(N done | ||
| 89 | # hidden)" contract, where what is withheld is counted and the footer | ||
| 90 | # says so. A view that disagrees with its own total in both directions | ||
| 91 | # at once is worse than either whole answer. | ||
| 92 | # * --compact and --json never hid the empty archived column at all, so | ||
| 93 | # "the archive is not a column" is also the first rule about it that all | ||
| 94 | # three output modes share. | ||
| 95 | # | ||
| 96 | # Nothing is lost: `karr list --archived` reads the archive, `karr show ID` a | ||
| 97 | # single filed card. As in #229, `archived` is archived whether or not the | ||
| 98 | # board configures the column -- kanban-md's IsArchivedStatus additionally | ||
| 99 | # requires it in the config, and two answers to "is this archived?" in one | ||
| 100 | # distribution would be worse than the divergence. | ||
| 101 | my @statuses = grep { $_ ne App::karr::Config->ARCHIVED_STATUS } | ||
| 102 | $self->store->all_status_names; | ||
| 103 | my @tasks = grep { $_->status ne App::karr::Config->ARCHIVED_STATUS } | ||
| 104 | $self->load_tasks; | ||
| 105 | |||
| 106 | my %by_status; | ||
| 107 | for my $t (@tasks) { | ||
| 108 | push @{$by_status{$t->status}}, $t; | ||
| 109 | } | ||
| 110 | |||
| 111 | if ($self->json) { | ||
| 112 | my $board_name = $ec->{board}{name} // 'Kanban Board'; | ||
| 113 | my %board_data = ( | ||
| 114 | name => $board_name, | ||
| 115 | total => scalar @tasks, | ||
| 116 | columns => [], | ||
| 117 | ); | ||
| 118 | for my $status (@statuses) { | ||
| 119 | my $tasks_in_status = $by_status{$status} // []; | ||
| 120 | # Hide the finished column's payloads by default (keep the column and | ||
| 121 | # its real count so the all-columns shape and total stay intact); --done | ||
| 122 | # reveals them. Which column is finished is the board's own decision and | ||
| 123 | # never the literal `done` -- an imported board ends in whatever it | ||
| 124 | # named its last status, and asking for the literal here left `--done` | ||
| 125 | # with nothing to do on such a board (#67, #234). | ||
| 126 | my $hide = $self->store->is_terminal_status($status) && !$self->done; | ||
| 127 | my %col = ( | ||
| 128 | status => $status, | ||
| 129 | count => scalar @$tasks_in_status, | ||
| 130 | tasks => $hide ? [] : [ map { $_->to_frontmatter } @$tasks_in_status ], | ||
| 131 | ); | ||
| 132 | push @{$board_data{columns}}, \%col; | ||
| 133 | } | ||
| 134 | $self->print_json(\%board_data); | ||
| 135 | return; | ||
| 136 | } | ||
| 137 | |||
| 138 | if ($self->compact) { | ||
| 139 | for my $status (@statuses) { | ||
| 140 | my $tasks_in_status = $by_status{$status} // []; | ||
| 141 | my $count = scalar @$tasks_in_status; | ||
| 142 | my $ids = join(',', map { $_->id } @$tasks_in_status); | ||
| 143 | printf "%s(%d): %s\n", $status, $count, $ids || '-'; | ||
| 144 | } | ||
| 145 | return; | ||
| 146 | } | ||
| 147 | |||
| 148 | my $board_name = $ec->{board}{name} // 'Kanban Board'; | ||
| 149 | |||
| 150 | # Colour only when writing to a real terminal — piped or redirected output | ||
| 151 | # stays clean plaintext so the board diffs, greps, and pastes cleanly. The | ||
| 152 | # tty/NO_COLOR/--no-color decision lives in App::karr::Role::Color. | ||
| 153 | my $color = $self->_want_color; | ||
| 154 | my $c = sub { | ||
| 155 | my ($text, $spec) = @_; | ||
| 156 | return $color ? colored($text, $spec) : $text; | ||
| 157 | }; | ||
| 158 | my $sep = $c->('|', 'bright_black'); | ||
| 159 | |||
| 160 | print $c->("# $board_name", 'bold cyan'), "\n"; | ||
| 161 | |||
| 162 | # Hide the board's finished column unless --done was given (the footer still | ||
| 163 | # says how many cards it withheld). Asked of the store, so a board whose | ||
| 164 | # last column is `shipped` hides shipped work instead of a `done` it does not | ||
| 165 | # have (#67, #234). `archived` is not in @statuses at all -- see the top. | ||
| 166 | my @display_statuses = grep { | ||
| 167 | $self->done || !$self->store->is_terminal_status($_) | ||
| 168 | } @statuses; | ||
| 169 | |||
| 170 | my $board = App::karr::Board->new( store => $self->store ); | ||
| 171 | |||
| 172 | if ($self->group_by) { | ||
| 173 | # --group-by replaces the status sections with one section per group, the | ||
| 174 | # same card rows below each heading. The group keys and their order come | ||
| 175 | # from App::karr::Board, the same answers `karr list --group-by` uses, so | ||
| 176 | # the two commands cannot drift (ticket #279). | ||
| 177 | # | ||
| 178 | # The rows are the flat view's rows, so the finished column is withheld | ||
| 179 | # here exactly as the status sections withhold it: a grouped view that | ||
| 180 | # printed the done cards while its footer said "(N done hidden)" would | ||
| 181 | # disagree with its own total in both directions at once. --done reveals | ||
| 182 | # them, as it does in the flat view. | ||
| 183 | my %groups; | ||
| 184 | for my $t (@tasks) { | ||
| 185 | next if !$self->done && $self->store->is_terminal_status( $t->status ); | ||
| 186 | push @{ $groups{$_} }, $t for $board->group_keys( $t, $self->group_by ); | ||
| 187 | } | ||
| 188 | for my $key ( $board->group_order( \%groups, $self->group_by ) ) { | ||
| 189 | print "\n", $c->("## $key", 'bold'), "\n"; | ||
| 190 | $self->_render_card( $_, $c, $sep ) for @{ $groups{$key} }; | ||
| 191 | } | ||
| 192 | } else { | ||
| 193 | for my $status (@display_statuses) { | ||
| 194 | my $tasks = $by_status{$status} // []; | ||
| 195 | my $label = join ' ', map { ucfirst } split /-/, $status; | ||
| 196 | my $accent = $STATUS_COLOR{$status} // 'white'; | ||
| 197 | print "\n", $c->("## $label", "bold $accent"), "\n"; | ||
| 198 | $self->_render_card( $_, $c, $sep ) for @$tasks; | ||
| 199 | } | ||
| 200 | } | ||
| 201 | |||
| 202 | # Summary footer. Every count here is over @tasks, which is the board | ||
| 203 | # without its archive (see the top) -- so the total says how many cards are | ||
| 204 | # on this board, and `blocked` how many of those are stuck, rather than | ||
| 205 | # either number silently including work that was filed away. | ||
| 206 | # Same test as the per-card `@claimant` token above, so the footer can never | ||
| 207 | # count a claim the board itself does not show. | ||
| 208 | my $claimed = grep { $_->has_claimed_by && !$self->store->is_terminal_status($_->status) } @tasks; | ||
| 209 | # At most one status left in @statuses is terminal once `archived` is gone, | ||
| 210 | # and it is the column @display_statuses withheld. The hint names it, so it | ||
| 211 | # reads "(3 shipped hidden)" on a board that calls it that. The count is | ||
| 212 | # App::karr::Board's, the same one `karr list`'s footer prints (ticket #275). | ||
| 213 | my $final_status = $board->final_status; | ||
| 214 | my $hidden = ( defined $final_status && !$self->done ) | ||
| 215 | ? $board->hidden_done_count( \@tasks ) : 0; | ||
| 216 | # Count only live blocked cards as blocked -- the same terminal-status test | ||
| 217 | # the claimed count uses, so the number matches the blocked cards the board | ||
| 218 | # actually shows and a dead card cannot linger as a perpetual blocker. A card | ||
| 219 | # blocked in the withheld final column is not lost: its block_reason stays put | ||
| 220 | # as provenance (#223/#224) and the annotation below surfaces it as | ||
| 221 | # "(M in <final>)" so a done+blocked card cannot stay invisible (ticket #295). | ||
| 222 | my $blocked = grep { $_->has_blocked && !$self->store->is_terminal_status($_->status) } @tasks; | ||
| 223 | my $blocked_hidden = ( defined $final_status && !$self->done ) | ||
| 224 | ? scalar grep { $_->has_blocked && $_->status eq $final_status } @tasks : 0; | ||
| 225 | my $total_label = scalar(@tasks) . ' tasks'; | ||
| 226 | $total_label .= " ($hidden $final_status hidden)" if $hidden; | ||
| 227 | my @summary = ( $total_label ); | ||
| 228 | push @summary, "$claimed claimed" if $claimed; | ||
| 229 | if ( $blocked || $blocked_hidden ) { | ||
| 230 | my $blocked_label = "$blocked blocked"; | ||
| 231 | $blocked_label .= " ($blocked_hidden in $final_status)" if $blocked_hidden; | ||
| 232 | push @summary, $blocked_label; | ||
| 233 | } | ||
| 234 | print "\n", $c->(join(' ', @summary), 'bold'), "\n"; | ||
| 235 | } | ||
| 236 | |||
| 237 | # One card row. Extracted from execute so the grouped view prints exactly the | ||
| 238 | # rows the status view does (ticket #279). | ||
| 239 | sub _render_card { | ||
| 240 | my ( $self, $t, $c, $sep ) = @_; | ||
| 241 | my @meta; | ||
| 242 | if ($t->priority && $t->priority ne 'medium') { | ||
| 243 | push @meta, $c->('priority:' . $t->priority, $PRIORITY_COLOR{$t->priority} // 'white'); | ||
| 244 | } | ||
| 245 | # A claim is only worth showing while the work is still live, and which | ||
| 246 | # columns count as finished is the board's decision -- a board imported | ||
| 247 | # from kanban-md can end in `shipped`, and every finished card there | ||
| 248 | # still carried its claimant into the board (ticket #98, following #67). | ||
| 249 | if ($t->has_claimed_by && !$self->store->is_terminal_status($t->status)) { | ||
| 250 | push @meta, $c->('@' . $t->claimed_by, 'cyan'); | ||
| 251 | } | ||
| 252 | if ($t->has_blocked) { | ||
| 253 | my $reason = $t->has_block_reason ? $t->block_reason : undef; | ||
| 254 | $reason = substr($reason, 0, 40) . '...' if defined $reason && length $reason > 43; | ||
| 255 | push @meta, $c->( | ||
| 256 | defined $reason && length $reason ? "blocked:$reason" : 'blocked', 'bold red'); | ||
| 257 | } | ||
| 258 | if ($t->has_due) { | ||
| 259 | push @meta, $c->('due:' . $t->due, 'yellow'); | ||
| 260 | } | ||
| 261 | |||
| 262 | my $line = join ' ', $c->('-', 'bright_black'), $t->id, $sep, $t->title; | ||
| 263 | $line .= " $sep " . join(" $sep ", @meta) if @meta; | ||
| 264 | print $line, "\n"; | ||
| 265 | |||
| 266 | if ($self->tags && @{$t->tags}) { | ||
| 267 | print ' ', $c->(join(' ', map { "#$_" } @{$t->tags}), 'bright_black'), "\n"; | ||
| 268 | } | ||
| 269 | } | ||
| 270 | |||
| 271 | # The one usage error this command can raise that does not need a task in | ||
| 272 | # hand, decided before the first ref is read: whether an invocation is well | ||
| 273 | # formed is not a question about what happens to be on the board. | ||
| 274 | sub _validate_options { | ||
| 275 | my ($self) = @_; | ||
| 276 | |||
| 277 | # --group-by is a rendering key, validated like `karr list --sort`: an | ||
| 278 | # unknown field is a usage error (exit 2) rather than a silent "no grouping | ||
| 279 | # happened". The accepted set is the same five kanban-md's GroupBy takes | ||
| 280 | # (internal/board/group.go). | ||
| 281 | if ( defined $self->group_by ) { | ||
| 282 | my $field = $self->group_by; | ||
| 283 | user_error( "Usage: karr board --group-by ", | ||
| 284 | join('|', @GROUP_FIELDS), " (got '$field')" ) | ||
| 285 | unless grep { $_ eq $field } @GROUP_FIELDS; | ||
| 286 | } | ||
| 287 | } | ||
| 288 | |||
| 289 | 1; | ||
| 290 | |||
| 291 | __END__ | ||