Term/Ribbon.pm

filter: Statementstmt 100% 11 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 100% 15 / 15 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Term/Widget/Label.pm ↑ Summary
1 # You may distribute under the terms of the GNU General Public License
2 #
3 # (C) Paul Evans, 2012-2023 -- leonerd@leonerd.org.uk
4  
5 1 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 package Circle::FE::Term::Ribbon 0.240250;
10  
11 1 ▶use base qw( Tickit::Widget::Tabbed::Ribbon );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 class Circle::FE::Term::Ribbon::horizontal
14 :isa(Tickit::Widget::Tabbed::Ribbon::horizontal);
15  
16 use Syntax::Keyword::Match;
17  
18 use Tickit::Utils qw( textwidth );
19 use List::Util qw( max first );
20 Tickit::Widget->VERSION( '0.35' ); # ->render_to_rb
21  
22 use Tickit::Style;
23  
24 style_definition base =>
25 activity_fg => "cyan";
26  
27 use constant orientation => "horizontal";
28  
29 sub lines { 1 }
30 sub cols { 1 }
31  
32 sub render_to_rb
33 {
34 my $self = shift;
35 my ( $rb, $rect ) = @_;
36  
37 my $win = $self->window or return;
38  
39 my @tabs = $self->tabs;
40 my $active = $self->active_tab;
41  
42 $rb->goto( 0, 0 );
43  
44 my $col = 0;
45 my $printed;
46  
47 if( $active ) {
48 $rb->text( $printed = sprintf( "%d", $active->index + 1 ), $active->pen );
49 $col += textwidth $printed;
50  
51 $rb->text( $printed = sprintf( ":%s | ", $active->label ) );
52 $col += textwidth $printed;
53 }
54  
55 my $rhs = sprintf " | total: %d", scalar @tabs;
56 my $rhswidth = textwidth $rhs;
57  
58 $self->{tabpos} = \my @tabpos;
59  
60 if( grep { $_ != $active and $_->level > 0 } @tabs ) {
61 my @used;
62 # Output formats: [0] = full text
63 # [1] = initialise level<2 names
64 # [2] = initialise level<3 names
65 # [3] = initialise all names
66 # [4] = hide level<2 names, initialise others
67 # [5] = hide all names
68 # [6] = hide all names, hide level<2 tabs entirely
69 # [7] = hide all names, hide level<3 tabs entirely
70  
71 foreach my $idx ( 0 .. $#tabs ) {
72 my $tab = $tabs[$idx];
73 next if $tab == $active;
74  
75 next unless my $level = $tab->level;
76  
77 my $width_full = textwidth sprintf "%d:%s", $idx + 1, $tab->label;
78 my $width_short = textwidth sprintf "%d:%s", $idx + 1, $tab->label_short;
79 my $width_hide = textwidth sprintf "%d", $idx + 1;
80  
81 $used[0] += 1 + $width_full;
82 $used[1] += 1 + $level < 2 ? $width_short : $width_full;
83 $used[2] += 1 + $level < 3 ? $width_short : $width_full;
84 $used[3] += 1 + $width_short;
85 $used[4] += 1 + $level < 2 ? $width_hide : $width_short;
86 $used[5] += 1 + $width_hide;
87 $used[6] += 1 + $width_hide if $level >= 2;
88 $used[7] += 1 + $width_hide if $level >= 3;
89 }
90  
91 my $space = $win->cols - $col - $rhswidth;
92  
93 my $format;
94 match( Circle::FE::Term->get_theme_var( "label_format" ) : eq ) {
95 case( "name_and_number" ) { $format = 0 }
96 case( "initial" ) { $format = 3 }
97 case( "number" ) { $format = 5 }
98 default { die "Unrecognised label_format $_"; $format = 0 }
99 }
100  
101 $format++ while $format < $#used and $used[$format] > $space;
102  
103 my $first = 1;
104 my $hiddencount = 0;
105  
106 TAB: foreach my $idx ( 0 .. $#tabs ) {
107 my $tab = $tabs[$idx];
108 next if $tab == $active;
109  
110 next unless my $level = $tab->level;
111  
112 my $label;
113 match( $format : == ) {
114 case( 0 ) { $label = $tab->label }
115 case( 1 ) { $label = $level < 2 ? $tab->label_short : $tab->label }
116 case( 2 ) { $label = $level < 3 ? $tab->label_short : $tab->label }
117 case( 3 ) { $label = $tab->label_short }
118 case( 4 ) { $label = $level < 2 ? undef : $tab->label_short }
119 case( 5 ) { }
120 case( 6 ) { $level >= 2 or $hiddencount++, next TAB }
121 case( 7 ) { $level >= 3 or $hiddencount++, next TAB }
122 }
123  
124 my $text = sprintf "%d", $idx + 1;
125 $text .= ":$label" if defined $label;
126  
127 {
128 $rb->savepen;
129  
130 if( !$first ) {
131 $rb->setpen( $self->get_style_pen( "activity" ) );
132 $rb->text( "," );
133 $col++;
134 }
135  
136 $rb->setpen( $tab->pen );
137 $rb->text( $text );
138 my $width = textwidth $text;
139  
140 push @tabpos, [ $idx, $col, $width ];
141  
142 $col += $width;
143  
144 $rb->restore;
145 }
146  
147 $first = 0;
148 }
149  
150 if( $hiddencount ) {
151 my $hiddenlevel = $format >= 7 ? "level2" : "level1";
152 $rb->savepen;
153 $rb->setpen( Circle::FE::Term->get_theme_pen( $hiddenlevel ) );
154 $rb->text( " + $hiddencount more" );
155 $rb->restore;
156 }
157 }
158  
159 $rb->erase_to( $win->cols - $rhswidth );
160  
161 $rb->text( $rhs );
162 }
163  
164 sub scroll_to_visible { }
165  
166 sub activate_next
167 {
168 my $self = shift;
169  
170 my @tabs = $self->tabs;
171 @tabs = ( @tabs[$self->active_tab_index + 1 .. $#tabs], @tabs[0 .. $self->active_tab_index - 1] );
172  
173 my $max_level = max map { $_->level } @tabs;
174 return unless $max_level > 0;
175  
176 my $next_tab = first { $_->level == $max_level } @tabs;
177  
178 $next_tab->activate if $next_tab;
179 }
180  
181 my $tab_shortcuts = "1234567890" .
182 "qwertyuiop" .
183 "sdfghjkl;'" .
184 "zxcvbnm,./";
185  
186 sub on_key
187 {
188 my $self = shift;
189 my ( $ev ) = @_;
190  
191 if( $ev->type eq "key" and $ev->str eq "M-a" ) {
192 $self->activate_next;
193 return 1;
194 }
195 elsif( $ev->type eq "key" and $ev->str =~ m/^M-(.)$/ and
196 ( my $idx = index $tab_shortcuts, $1 ) > -1 ) {
197 eval { $self->activate_tab( $idx ) }; # ignore croak on invalid index
198 return 1;
199 }
200  
201 return 0;
202 }
203  
204 sub on_mouse
205 {
206 my $self = shift;
207 my ( $ev ) = @_;
208  
209 return 0 unless $ev->line == 0;
210  
211 if( $ev->type eq "press" and $ev->button == 1 ) {
212 foreach my $pos ( @{ $self->{tabpos} } ) {
213 $self->activate_tab( $pos->[0] ), return 1 if $ev->col >= $pos->[1] and $ev->col < $pos->[1] + $pos->[2];
214 }
215 }
216 elsif( $ev->type eq "wheel" ) {
217 $self->prev_tab if $ev->button eq "up";
218 $self->next_tab if $ev->button eq "down";
219 return 1;
220 }
221 }
222  
223 0x55AA;
« Term/Widget/Label.pm ↑ Summary