| 1 | # ABSTRACT: Multi-board overview of every karr board under a directory tree | ||
| 2 | |||
| 3 | package App::karr::Cmd::Dashboard; | ||
| 4 | our $VERSION = '0.602'; | ||
| 5 | use Moo; | ||
| 6 | use MooX::Cmd; | ||
| 7 | use MooX::Options ( | ||
| 8 | usage_string => 'USAGE: karr dashboard [PATH] [--depth N] [--hide-no-board] [--show-no-board] [--json] [--compact]', | ||
| 9 | ); | ||
| 10 | use Path::Tiny (); | ||
| 11 | use Term::ANSIColor qw( colored ); | ||
| 12 | use App::karr::Git; | ||
| 13 | use App::karr::BoardStore; | ||
| 14 | use App::karr::Role::CliArgs; | ||
| 15 | use App::karr::Role::ExitCodes; | ||
| 16 | use App::karr::Role::Output; | ||
| 17 | use App::karr::Role::CompactOutput; | ||
| 18 | use App::karr::Role::Color; | ||
| 19 | |||
| 20 | # Board-less on purpose (ticket #220): this walks a directory tree for | ||
| 21 | # repositories and opens each board directly through App::karr::Git / | ||
| 22 | # App::karr::BoardStore, the way App::karr::Foundation/_is_karr_board_root | ||
| 23 | # does. It composes neither App::karr::Role::BoardDiscovery nor | ||
| 24 | # App::karr::Role::BoardAccess -- there is no single board to discover here, | ||
| 25 | # and no --dir either: the positional PATH below already names the search | ||
| 26 | # root, and a second option with an unrelated meaning would only confuse. | ||
| 27 | # _reject_root_dir in execute() is what makes that decision hold for the root | ||
| 28 | # placement as well (#225); without it the option was accepted there and | ||
| 29 | # silently dropped. | ||
| 30 | with 'App::karr::Role::CliArgs', 'App::karr::Role::ExitCodes', | ||
| 31 | 'App::karr::Role::Output', 'App::karr::Role::CompactOutput', | ||
| 32 | 'App::karr::Role::Color'; | ||
| 33 | |||
| 34 | |||
| 35 | option depth => ( | ||
| 36 | is => 'ro', | ||
| 37 | format => 'i', | ||
| 38 | default => sub { 4 }, | ||
| 39 | doc => 'Maximum recursion depth below PATH (default 4)', | ||
| 40 | ); | ||
| 41 | |||
| 42 | option hide_no_board => ( | ||
| 43 | is => 'ro', | ||
| 44 | doc => 'Hide the summarized list of repositories with no karr board', | ||
| 45 | ); | ||
| 46 | |||
| 47 | option show_no_board => ( | ||
| 48 | is => 'ro', | ||
| 49 | doc => 'Always list the board-less repositories by name, wrapped over several lines', | ||
| 50 | ); | ||
| 51 | |||
| 52 | # Same status -> colour mapping App::karr::Cmd::Board uses (see its own | ||
| 53 | # comment there): the same status must not look different between the two | ||
| 54 | # commands of this one distribution. | ||
| 55 | my %STATUS_COLOR = ( | ||
| 56 | backlog => 'bright_black', | ||
| 57 | todo => 'cyan', | ||
| 58 | 'in-progress' => 'yellow', | ||
| 59 | review => 'magenta', | ||
| 60 | done => 'green', | ||
| 61 | archived => 'bright_black', | ||
| 62 | ); | ||
| 63 | |||
| 64 | # Decision (documented on ticket #220, karr edit 220 -a "..."): blocked cards | ||
| 65 | # are pulled out of their status colour into their own trailing bar segment, | ||
| 66 | # always bold red -- the same colour App::karr::Cmd::Board uses for | ||
| 67 | # `blocked:reason` -- so a blocked card reads as blocked at a glance instead | ||
| 68 | # of disappearing into whichever status colour it happened to be sitting in. | ||
| 69 | use constant BLOCKED_COLOR => 'bold red'; | ||
| 70 | |||
| 71 | # One block character per (unscaled) open task. Decision: capped at 10 blocks | ||
| 72 | # per bar (see _scale_segments) so a busy board's entry stays short enough for | ||
| 73 | # several columns to fit side by side at an ordinary 80-column terminal -- | ||
| 74 | # the whole point of "mehrspaltig" being multiple repos per screen row, not | ||
| 75 | # one very wide repo taking the row on its own. | ||
| 76 | use constant BLOCK_CHAR => "\x{2588}"; | ||
| 77 | use constant MAX_BAR_BLOCKS => 10; | ||
| 78 | use constant COLUMN_GAP => 2; | ||
| 79 | |||
| 80 | sub execute { | ||
| 81 | my ($self, $args_ref, $chain_ref) = @_; | ||
| 82 | |||
| 83 | $self->_reject_root_dir($chain_ref); | ||
| 84 | $self->check_positional_args($args_ref, 1); | ||
| 85 | my ($pos) = $self->positional_args($args_ref); | ||
| 86 | |||
| 87 | $self->usage_error( sprintf '--depth must be 0 or greater (got %d)', $self->depth ) | ||
| 88 | if $self->depth < 0; | ||
| 89 | |||
| 90 | my $start = Path::Tiny::path( defined $pos ? $pos : '.' )->absolute; | ||
| 91 | $self->usage_error("not a directory: $start") unless $start->is_dir; | ||
| 92 | |||
| 93 | my @repo_dirs = sort { "$a" cmp "$b" } $self->_find_repos( $start, $self->depth ); | ||
| 94 | |||
| 95 | my ( @boards, @no_board ); | ||
| 96 | for my $dir (@repo_dirs) { | ||
| 97 | my $info = $self->_probe_repo($dir); | ||
| 98 | if ($info) { push @boards, $info } | ||
| 99 | else { push @no_board, $dir } | ||
| 100 | } | ||
| 101 | @boards = sort { $a->{name} cmp $b->{name} || "$a->{dir}" cmp "$b->{dir}" } @boards; | ||
| 102 | |||
| 103 | my $total_open = 0; | ||
| 104 | $total_open += $_->{open} for @boards; | ||
| 105 | |||
| 106 | if ( $self->json ) { | ||
| 107 | my %doc = ( | ||
| 108 | root => "$start", | ||
| 109 | summary => { | ||
| 110 | repos => scalar(@repo_dirs), | ||
| 111 | boards => scalar(@boards), | ||
| 112 | open => $total_open, | ||
| 113 | }, | ||
| 114 | boards => [ | ||
| 115 | map { | ||
| 116 | my $b = $_; | ||
| 117 | +{ | ||
| 118 | path => "$b->{dir}", | ||
| 119 | name => $b->{name}, | ||
| 120 | board_name => $b->{board_name}, | ||
| 121 | open => $b->{open}, | ||
| 122 | blocked => $b->{blocked}, | ||
| 123 | # Named $status, not $_: this is a map nested inside the outer | ||
| 124 | # one, and reusing $_ here would shadow $b's own $_ alias with | ||
| 125 | # the status name, breaking $b->{counts}{$_} silently. | ||
| 126 | statuses => { map { my $status = $_; ( $status => $b->{counts}{$status} // 0 ) } @{ $b->{order} } }, | ||
| 127 | } | ||
| 128 | } @boards | ||
| 129 | ], | ||
| 130 | ( $self->hide_no_board ? () : ( no_board => [ map { "$_" } @no_board ] ) ), | ||
| 131 | ); | ||
| 132 | $self->print_json( \%doc ); | ||
| 133 | return; | ||
| 134 | } | ||
| 135 | |||
| 136 | if ( $self->compact ) { | ||
| 137 | for my $b (@boards) { | ||
| 138 | my @tokens = map { "$_:" . ( $b->{counts}{$_} // 0 ) } @{ $b->{order} }; | ||
| 139 | push @tokens, 'blocked:' . $b->{blocked}; | ||
| 140 | printf "%s\t%s\n", $b->{name}, join( ',', @tokens ); | ||
| 141 | } | ||
| 142 | unless ( $self->hide_no_board ) { | ||
| 143 | printf "%s\tno-board\n", $_->basename for @no_board; | ||
| 144 | } | ||
| 145 | return; | ||
| 146 | } | ||
| 147 | |||
| 148 | my $color = $self->_want_color; | ||
| 149 | my $c = sub { | ||
| 150 | my ( $text, $spec ) = @_; | ||
| 151 | return $color ? colored( $text, $spec ) : $text; | ||
| 152 | }; | ||
| 153 | |||
| 154 | # Every line printed below is fitted to this one width. Nothing may exceed | ||
| 155 | # it: a line that is one character too long soft-wraps in the terminal, and | ||
| 156 | # a soft-wrapped grid row destroys the column alignment this command exists | ||
| 157 | # for -- worse than a plain one-column list would have been. | ||
| 158 | my $width = $self->_term_width; | ||
| 159 | |||
| 160 | print $c->( $self->_truncate( "Dashboard: $start", $width ), 'bold cyan' ), "\n\n"; | ||
| 161 | |||
| 162 | if (@boards) { | ||
| 163 | my $name_width = 0; | ||
| 164 | $name_width = length( $_->{name} ) > $name_width ? length( $_->{name} ) : $name_width | ||
| 165 | for @boards; | ||
| 166 | |||
| 167 | # The count column is the only other variable-width part; measure it so | ||
| 168 | # the name cap below is exact rather than guessed. | ||
| 169 | my $count_w = 0; | ||
| 170 | for my $b (@boards) { | ||
| 171 | my $l = length( '(' . $b->{open} . ')' ); | ||
| 172 | $count_w = $l if $l > $count_w; | ||
| 173 | } | ||
| 174 | |||
| 175 | # A cell is name + 2 spaces + bar + 1 space + count. On a terminal too | ||
| 176 | # narrow to hold even one whole cell the grid falls back to a single | ||
| 177 | # column, and a single column cannot shrink below its own content -- so | ||
| 178 | # the content itself has to shrink. Names are capped first (they are the | ||
| 179 | # only unbounded part), then the bar, so both stay inside $width. At any | ||
| 180 | # ordinary width neither cap is reached and nothing is truncated. | ||
| 181 | my $name_cap = $width - 3 - MAX_BAR_BLOCKS - $count_w; | ||
| 182 | $name_cap = 4 if $name_cap < 4; | ||
| 183 | $name_width = $name_cap if $name_width > $name_cap; | ||
| 184 | |||
| 185 | my $bar_max = $width - $name_width - 3 - $count_w; | ||
| 186 | $bar_max = 1 if $bar_max < 1; | ||
| 187 | $bar_max = MAX_BAR_BLOCKS if $bar_max > MAX_BAR_BLOCKS; | ||
| 188 | |||
| 189 | my @cells = map { $self->_entry_for_board( $_, $name_width, $bar_max, $c ) } @boards; | ||
| 190 | print $self->_render_grid( \@cells, $width ); | ||
| 191 | } | ||
| 192 | else { | ||
| 193 | print $c->( '(no boards found)', 'bright_black' ), "\n"; | ||
| 194 | } | ||
| 195 | |||
| 196 | # The board-less repositories are a footnote, and on a big tree there are | ||
| 197 | # more of them than boards (46 of 91 under one real /home/getty/dev scan). | ||
| 198 | # Joined into one line that was 837 characters -- seven soft-wrapped | ||
| 199 | # terminal lines that buried the summary underneath them. Three cases now: | ||
| 200 | # it fits on one line, or --show-no-board wraps it properly, or it collapses | ||
| 201 | # to a count that says how to see the names. | ||
| 202 | unless ( $self->hide_no_board || !@no_board ) { | ||
| 203 | my @names = map { $_->basename } @no_board; | ||
| 204 | my $one_line = 'No board: ' . join( ', ', @names ); | ||
| 205 | print "\n"; | ||
| 206 | if ( length($one_line) <= $width ) { | ||
| 207 | print $c->( $one_line, 'bright_black' ), "\n"; | ||
| 208 | } | ||
| 209 | elsif ( $self->show_no_board ) { | ||
| 210 | print $c->( $_, 'bright_black' ), "\n" | ||
| 211 | for $self->_wrap_items( 'No board: ', \@names, $width, 10 ); | ||
| 212 | } | ||
| 213 | else { | ||
| 214 | # The hint is worth a line only while it fits beside the count; on a | ||
| 215 | # narrow terminal the count alone is what survives. | ||
| 216 | my $hint = sprintf( 'No board: %d repos (--show-no-board to list them)', scalar @names ); | ||
| 217 | my $short = sprintf( 'No board: %d repos', scalar @names ); | ||
| 218 | $hint = $short if length($hint) > $width; | ||
| 219 | print $c->( $self->_truncate( $hint, $width ), 'bright_black' ), "\n"; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | print "\n", $c->( | ||
| 224 | $self->_truncate( | ||
| 225 | sprintf( '%d repos %d boards %d open', scalar(@repo_dirs), scalar(@boards), $total_open ), | ||
| 226 | $width ), | ||
| 227 | 'bold' | ||
| 228 | ), "\n"; | ||
| 229 | } | ||
| 230 | |||
| 231 | # `karr dashboard --dir PATH` was always rejected by MooX::Options (this | ||
| 232 | # command declares no such option) -- but `karr --dir PATH dashboard` was not: | ||
| 233 | # --dir is declared on App::karr::Role::BoardDiscovery, the root command | ||
| 234 | # composes it via BoardAccess, and MooX::Cmd leaves the parsed value on the | ||
| 235 | # root instance in the command chain, where nothing here ever looked. So the | ||
| 236 | # option was swallowed without a word and the scan ran on the current | ||
| 237 | # directory instead -- and a list of repositories with counts behind them | ||
| 238 | # looks like a valid answer no matter which tree it came from (#225). | ||
| 239 | # | ||
| 240 | # It is refused rather than adopted because the two paths are not the same | ||
| 241 | # path: --dir seeds a walk UPWARD to one repository's root | ||
| 242 | # (App::karr::Role::BoardDiscovery/_build_git_root, which is why it may name | ||
| 243 | # any directory inside the target repository), while the positional PATH here | ||
| 244 | # is the root of a walk DOWNWARD across a tree of repositories (_find_repos, | ||
| 245 | # bounded by --depth, never entering a repository's own work tree). Handed the | ||
| 246 | # same argument, the two would answer about different directories. | ||
| 247 | # | ||
| 248 | # The root is read from $chain_ref the way App::karr::Cmd::GetRefs reads it | ||
| 249 | # for the opposite purpose; a directly constructed instance (no MooX::Cmd | ||
| 250 | # dispatch, hence an empty chain) has no root option to reject. | ||
| 251 | sub _reject_root_dir { | ||
| 252 | my ($self, $chain_ref) = @_; | ||
| 253 | return unless $chain_ref && @$chain_ref; | ||
| 254 | my $root = $chain_ref->[0]; | ||
| 255 | return unless $root && $root->can('has_dir') && $root->has_dir; | ||
| 256 | # Wrapped to stay inside 80 columns with usage_error's own "Usage error: " | ||
| 257 | # prefix on the first line: what to type comes first, the reason after it. | ||
| 258 | $self->usage_error( | ||
| 259 | "dashboard does not take --dir; its scan root is an argument:\n" | ||
| 260 | . "karr dashboard PATH\n" | ||
| 261 | . "(--dir seeds the search upward for one repository's board, while dashboard\n" | ||
| 262 | . "searches downward for every board under a directory.)" | ||
| 263 | ); | ||
| 264 | } | ||
| 265 | |||
| 266 | # --------------------------------------------------------------------------- | ||
| 267 | # Discovery | ||
| 268 | # --------------------------------------------------------------------------- | ||
| 269 | |||
| 270 | # Iterative (not recursive) so a very deep or wide tree cannot blow the Perl | ||
| 271 | # call stack; order within a directory is irrelevant to the caller, which | ||
| 272 | # sorts the whole result afterwards. | ||
| 273 | sub _find_repos { | ||
| 274 | my ( $self, $start, $max_depth ) = @_; | ||
| 275 | my @found; | ||
| 276 | my @stack = ( [ $start, 0 ] ); | ||
| 277 | |||
| 278 | while (@stack) { | ||
| 279 | my ( $dir, $depth ) = @{ shift @stack }; | ||
| 280 | |||
| 281 | if ( $dir->child('.git')->exists ) { | ||
| 282 | push @found, $dir; | ||
| 283 | next; # never search inside a repository's own working tree | ||
| 284 | } | ||
| 285 | next if $depth >= $max_depth; | ||
| 286 | |||
| 287 | my @children = eval { grep { $_->is_dir } $dir->children }; | ||
| 288 | next unless @children; # unreadable directory: skip, do not abort | ||
| 289 | |||
| 290 | for my $child ( sort { "$a" cmp "$b" } @children ) { | ||
| 291 | next if -l $child; # never follow a symlinked directory (loops) | ||
| 292 | push @stack, [ $child, $depth + 1 ]; | ||
| 293 | } | ||
| 294 | } | ||
| 295 | return @found; | ||
| 296 | } | ||
| 297 | |||
| 298 | # undef when $dir is not a Git repository at all, or holds none of | ||
| 299 | # refs/karr/*; a probing failure (a corrupt repository, an unreadable ref) is | ||
| 300 | # treated the same as "no board" rather than aborting the whole dashboard -- | ||
| 301 | # App::karr::Foundation's own per-repo processing follows the same rule, see | ||
| 302 | # its "one broken board cannot stop the rest of the run". The actual work is | ||
| 303 | # in _probe_repo_or_die below, called through eval here rather than inline -- | ||
| 304 | # a bare `return` inside `eval BLOCK` returns from the enclosing sub, not the | ||
| 305 | # eval, so the early `return undef`s that method needs would otherwise skip | ||
| 306 | # this catch entirely; a real named sub has no such surprise. | ||
| 307 | sub _probe_repo { | ||
| 308 | my ( $self, $dir ) = @_; | ||
| 309 | my $info = eval { $self->_probe_repo_or_die($dir) }; | ||
| 310 | if ($@) { | ||
| 311 | print STDERR "karr dashboard: skipping $dir: $@"; | ||
| 312 | return undef; | ||
| 313 | } | ||
| 314 | return $info; | ||
| 315 | } | ||
| 316 | |||
| 317 | sub _probe_repo_or_die { | ||
| 318 | my ( $self, $dir ) = @_; | ||
| 319 | |||
| 320 | my $git = App::karr::Git->new( dir => "$dir" ); | ||
| 321 | return undef unless $git->is_repo; | ||
| 322 | return undef unless $git->ref_exists('refs/karr/config'); | ||
| 323 | |||
| 324 | my $store = App::karr::BoardStore->new( git => $git ); | ||
| 325 | my $ec = $store->effective_config; | ||
| 326 | my @order = grep { !$store->is_terminal_status($_) } $store->all_status_names; | ||
| 327 | my %in_order = map { $_ => 1 } @order; | ||
| 328 | |||
| 329 | my %counts; | ||
| 330 | my $blocked = 0; | ||
| 331 | for my $t ( $store->load_tasks ) { | ||
| 332 | next unless $in_order{ $t->status }; | ||
| 333 | if ( $t->has_blocked ) { $blocked++ } | ||
| 334 | else { $counts{ $t->status }++ } | ||
| 335 | } | ||
| 336 | my $open = $blocked; | ||
| 337 | $open += ( $counts{$_} // 0 ) for @order; | ||
| 338 | |||
| 339 | return { | ||
| 340 | dir => $dir, | ||
| 341 | name => $dir->basename, | ||
| 342 | board_name => $ec->{board}{name} // 'Kanban Board', | ||
| 343 | order => \@order, | ||
| 344 | counts => \%counts, | ||
| 345 | blocked => $blocked, | ||
| 346 | open => $open, | ||
| 347 | }; | ||
| 348 | } | ||
| 349 | |||
| 350 | # --------------------------------------------------------------------------- | ||
| 351 | # Bar rendering | ||
| 352 | # --------------------------------------------------------------------------- | ||
| 353 | |||
| 354 | # [ [ label, count, color ], ... ] for one board -- every non-terminal status | ||
| 355 | # with at least one open task, in the board's own status order, plus a | ||
| 356 | # trailing synthetic 'blocked' segment (see BLOCKED_COLOR above). | ||
| 357 | sub _bar_segments { | ||
| 358 | my ( $self, $info ) = @_; | ||
| 359 | my @segments; | ||
| 360 | for my $status ( @{ $info->{order} } ) { | ||
| 361 | my $n = $info->{counts}{$status} // 0; | ||
| 362 | push @segments, [ $status, $n, $STATUS_COLOR{$status} // 'white' ] if $n; | ||
| 363 | } | ||
| 364 | push @segments, [ 'blocked', $info->{blocked}, BLOCKED_COLOR ] if $info->{blocked}; | ||
| 365 | return @segments; | ||
| 366 | } | ||
| 367 | |||
| 368 | # Scales @segments down to sum to at most $max blocks (largest-remainder / | ||
| 369 | # Hamilton apportionment), unscaled when they already fit. A nonzero | ||
| 370 | # 'blocked' segment is never rounded away to zero -- see BLOCKED_COLOR above | ||
| 371 | # for why that one category specifically must stay visible; every other | ||
| 372 | # segment is allowed to round down to nothing under heavy compression, which | ||
| 373 | # only loses precision the exact counts ( --json / --compact / the "N open" | ||
| 374 | # summary) still carry. | ||
| 375 | sub _scale_segments { | ||
| 376 | my ( $self, $max, @segments ) = @_; | ||
| 377 | my $total = 0; | ||
| 378 | $total += $_->[1] for @segments; | ||
| 379 | return @segments if $total <= $max; | ||
| 380 | |||
| 381 | my @work; | ||
| 382 | my $allocated = 0; | ||
| 383 | for my $seg (@segments) { | ||
| 384 | my $share = $seg->[1] * $max / $total; | ||
| 385 | my $floor = int $share; | ||
| 386 | push @work, { seg => $seg, blocks => $floor, remainder => $share - $floor }; | ||
| 387 | $allocated += $floor; | ||
| 388 | } | ||
| 389 | my $left = $max - $allocated; | ||
| 390 | for my $w ( sort { $b->{remainder} <=> $a->{remainder} } @work ) { | ||
| 391 | last unless $left > 0; | ||
| 392 | $w->{blocks}++; | ||
| 393 | $left--; | ||
| 394 | } | ||
| 395 | for my $w (@work) { | ||
| 396 | next unless $w->{seg}[0] eq 'blocked'; | ||
| 397 | next if $w->{blocks} > 0 || $w->{seg}[1] == 0; | ||
| 398 | my ($donor) = sort { $b->{blocks} <=> $a->{blocks} } grep { $_->{blocks} > 0 } @work; | ||
| 399 | # $donor is always found here: $max blocks were allocated somewhere among | ||
| 400 | # @work, and this branch only runs when blocked's own share is 0 of them, | ||
| 401 | # so at least one other segment holds the rest. The guard is defensive. | ||
| 402 | next unless $donor; | ||
| 403 | $donor->{blocks}--; | ||
| 404 | $w->{blocks}++; | ||
| 405 | } | ||
| 406 | return map { [ $_->{seg}[0], $_->{blocks}, $_->{seg}[2] ] } @work; | ||
| 407 | } | ||
| 408 | |||
| 409 | # One grid cell for a board: { width => visible character width, text => the | ||
| 410 | # (possibly coloured) string to print }. Names are left-padded to | ||
| 411 | # $name_width -- the longest name across the whole grid -- so every bar in | ||
| 412 | # every column starts at the same horizontal offset (a cheap readability win | ||
| 413 | # over plain per-cell width alignment, which only aligns whole entries). | ||
| 414 | sub _entry_for_board { | ||
| 415 | my ( $self, $info, $name_width, $bar_max, $c ) = @_; | ||
| 416 | |||
| 417 | my @segments = $self->_scale_segments( $bar_max, $self->_bar_segments($info) ); | ||
| 418 | |||
| 419 | my $bar_plain = ''; | ||
| 420 | my $bar_colored = ''; | ||
| 421 | for my $seg (@segments) { | ||
| 422 | my ( undef, $n, $color ) = @$seg; | ||
| 423 | next unless $n; | ||
| 424 | my $blocks = BLOCK_CHAR x $n; | ||
| 425 | $bar_plain .= $blocks; | ||
| 426 | $bar_colored .= $c->( $blocks, $color ); | ||
| 427 | } | ||
| 428 | |||
| 429 | my $name = $self->_truncate( $info->{name}, $name_width ); | ||
| 430 | my $name_padded = $name . ( ' ' x ( $name_width - length $name ) ); | ||
| 431 | my $count_str = '(' . $info->{open} . ')'; | ||
| 432 | |||
| 433 | my $plain = $name_padded . ' ' . $bar_plain . ' ' . $count_str; | ||
| 434 | my $text = $name_padded . ' ' . $bar_colored . ' ' . $c->( $count_str, 'bright_black' ); | ||
| 435 | |||
| 436 | return { width => length($plain), text => $text }; | ||
| 437 | } | ||
| 438 | |||
| 439 | # --------------------------------------------------------------------------- | ||
| 440 | # Grid layout | ||
| 441 | # --------------------------------------------------------------------------- | ||
| 442 | |||
| 443 | # ls -C style: one shared cell width (the widest entry) across the whole | ||
| 444 | # grid, as many columns as fit the terminal width, filled left to right, top | ||
| 445 | # to bottom (reading order) rather than ls's own top-to-bottom-then-across -- | ||
| 446 | # a dashboard is read start to finish, not scanned by column. | ||
| 447 | sub _render_grid { | ||
| 448 | my ( $self, $cells, $width ) = @_; | ||
| 449 | return '' unless @$cells; | ||
| 450 | |||
| 451 | my $cell_w = 0; | ||
| 452 | $cell_w = $_->{width} > $cell_w ? $_->{width} : $cell_w for @$cells; | ||
| 453 | |||
| 454 | my $ncols = int( ( $width + COLUMN_GAP ) / ( $cell_w + COLUMN_GAP ) ); | ||
| 455 | $ncols = 1 if $ncols < 1; | ||
| 456 | |||
| 457 | my $out = ''; | ||
| 458 | my $i = 0; | ||
| 459 | for my $cell (@$cells) { | ||
| 460 | $i++; | ||
| 461 | if ( $i % $ncols == 0 || $i == @$cells ) { | ||
| 462 | $out .= $cell->{text} . "\n"; | ||
| 463 | } | ||
| 464 | else { | ||
| 465 | $out .= $cell->{text} . ( ' ' x ( $cell_w - $cell->{width} ) ) . ( ' ' x COLUMN_GAP ); | ||
| 466 | } | ||
| 467 | } | ||
| 468 | return $out; | ||
| 469 | } | ||
| 470 | |||
| 471 | # Cuts $text down to at most $max visible characters, marking a cut with a | ||
| 472 | # trailing '..' so a truncated name never reads as a real (shorter) name. | ||
| 473 | # Only ever reached on a terminal too narrow to hold the untruncated content; | ||
| 474 | # at any ordinary width this returns $text unchanged. | ||
| 475 | sub _truncate { | ||
| 476 | my ( $self, $text, $max ) = @_; | ||
| 477 | return $text if length($text) <= $max; | ||
| 478 | return substr( $text, 0, $max ) if $max <= 2; | ||
| 479 | return substr( $text, 0, $max - 2 ) . '..'; | ||
| 480 | } | ||
| 481 | |||
| 482 | # Wraps a comma-separated list to $width, indenting every continuation line by | ||
| 483 | # $indent so the items stay visually grouped under their prefix. Returns the | ||
| 484 | # lines without newlines. A single item longer than the available room is | ||
| 485 | # truncated rather than allowed to overhang -- the whole point here is that no | ||
| 486 | # produced line is ever wider than $width. | ||
| 487 | sub _wrap_items { | ||
| 488 | my ( $self, $prefix, $items, $width, $indent ) = @_; | ||
| 489 | my $pad = ' ' x $indent; | ||
| 490 | my $lead = length($prefix) > $indent ? length($prefix) : $indent; | ||
| 491 | my $room = $width - $lead; | ||
| 492 | $room = 1 if $room < 1; | ||
| 493 | |||
| 494 | my @lines; | ||
| 495 | my $cur = $prefix; | ||
| 496 | my $empty = 1; # current line carries no item yet | ||
| 497 | for my $i ( 0 .. $#$items ) { | ||
| 498 | # Truncate the item AND its separator together: cutting the name to | ||
| 499 | # $room and then appending the comma is one character too many, which is | ||
| 500 | # exactly the kind of off-by-one that soft-wraps a line in the terminal. | ||
| 501 | my $tok = $self->_truncate( $items->[$i] . ( $i < $#$items ? ',' : '' ), $room ); | ||
| 502 | my $add = $empty ? $tok : ' ' . $tok; | ||
| 503 | if ( !$empty && length($cur) + length($add) > $width ) { | ||
| 504 | push @lines, $cur; | ||
| 505 | $cur = $pad . $tok; | ||
| 506 | } | ||
| 507 | else { | ||
| 508 | $cur .= $add; | ||
| 509 | } | ||
| 510 | $empty = 0; | ||
| 511 | } | ||
| 512 | push @lines, $cur unless $empty; | ||
| 513 | return @lines; | ||
| 514 | } | ||
| 515 | |||
| 516 | # Terminal width in columns. Checked ahead of any tty test -- COLUMNS lets a | ||
| 517 | # script or a piped capture (e.g. inside tmux) control the layout without a | ||
| 518 | # real pty, which also makes this deterministic to test. Falls back to `tput | ||
| 519 | # cols` (near-universal on the Unix systems karr already targets via | ||
| 520 | # libgit2/Git::Native) and then to 80, same fallback App::karr::Cmd::Board's | ||
| 521 | # neighbours use for "not determinable". | ||
| 522 | sub _term_width { | ||
| 523 | my ($self) = @_; | ||
| 524 | if ( defined $ENV{COLUMNS} && $ENV{COLUMNS} =~ /\A[0-9]+\z/ && $ENV{COLUMNS} > 0 ) { | ||
| 525 | return $ENV{COLUMNS}; | ||
| 526 | } | ||
| 527 | my $cols = eval { | ||
| 528 | local $SIG{__WARN__} = sub { }; | ||
| 529 | my $out = `tput cols 2>/dev/null`; | ||
| 530 | chomp $out; | ||
| 531 | $out; | ||
| 532 | }; | ||
| 533 | return ( $cols && $cols =~ /\A[0-9]+\z/ && $cols > 0 ) ? $cols : 80; | ||
| 534 | } | ||
| 535 | |||
| 536 | 1; | ||
| 537 | |||
| 538 | __END__ |