blib/lib/Term/Fabulous/Widget/FileDialog/FolderLine.pm untested

filter: Statementstmt 0.0% 0 / 71 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod n/a totaltotal 0.0% 0 / 91 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Term::Fabulous::Widget::FileDialog::FolderLine;
2  
3 use v5.32;
4 use warnings;
5 use feature 'signatures';
6 no warnings 'experimental::signatures';
7  
8 use Object::Pad 0.825;
9  
10 use Term::Fabulous::Widget::Display;
11  
12 our $VERSION = '0.02';
13  
14 class Term::Fabulous::Widget::FileDialog::FolderLine
15 :isa(Term::Fabulous::Widget::Display)
16 :strict(params)
17 {
18 use Clay::XS qw(sizing_grow);
19 use List::Util qw(max min);
20 use Term::Fabulous::Check qw(string);
21 use Term::Fabulous::Unicode qw(cluster_columns grapheme_clusters string_columns);
22  
23 use constant CUT => '...';
24 use constant PATH_ROOM => 10;
25  
26 field $label :param = 'Folder: ';
27 field $path :param = '';
28 field $after :param = '';
29  
30 ADJUST {
31 $label = string( $self, label => $label );
32 $path = string( $self, path => $path );
33 $after = string( $self, after => $after );
34 }
35  
36 method path (@new) {
37 return $path unless @new;
38 $path = string( $self, path => $new[0] );
39 $self->mark_changed;
40 return $path;
41 }
42  
43 # Sets the three parts at once, for a line that says something new.
44 method say ( $new_label, $new_path = '', $new_after = '' ) {
45 $label = string( $self, label => $new_label );
46 $path = string( $self, path => $new_path );
47 $after = string( $self, after => $new_after );
48 $self->mark_changed;
49 return $self;
50 }
51  
52 method text () {
53 return $label . $path . $after;
54 }
55  
56 method natural_size () {
57 return ( sizing_grow(), 1 );
58 }
59  
60 # The label, then as much of the end of the path as fits in front of
61 # the text after it. On a narrow line the path keeps some room
62 # (PATH_ROOM columns) and the text after it is cut at the right.
63 method paint () {
64 my $color = $self->color_attr( $self->family_look( text => 'color' ) );
65 my $x = $self->paint_text( 0, 0, $label, $color, undef );
66 my $room = $self->columns - $x;
67 $x = $self->paint_text( $x, 0, cut_from_the_left( $path, max( $room - string_columns($after), min( $room, PATH_ROOM ) ) ), $color, undef );
68 $self->paint_text( $x, 0, $after, $color, undef );
69 return;
70 }
71  
72 # The end of a text that fits a number of columns, after "..." when
73 # its start had to go.
74 sub cut_from_the_left ( $text, $room ) {
75 return '' if $room <= 0;
76 return $text if string_columns($text) <= $room;
77 my $room_left = $room - string_columns(CUT);
78 my ( $kept, $width ) = ( '', 0 );
79 foreach my $cluster ( reverse grapheme_clusters($text) ) {
80 my $columns = cluster_columns($cluster);
81 last if $width + $columns > $room_left;
82 ( $kept, $width ) = ( $cluster . $kept, $width + $columns );
83 }
84 return CUT . $kept;
85 }
86 }
87  
88 1;
89  
90 __END__