| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # (C) Paul Evans, 2023-2024 -- leonerd@leonerd.org.uk | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use v5.26; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Object::Pad 0.805; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | package App::sdview::Output::Tickit 0.10; | ||||||||||||||||||||||||||||
| 13 | class App::sdview::Output::Tickit | ||||||||||||||||||||||||||||
| 14 | :strict(params); | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use constant format => "tickit"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use App::sdview::Style 0.12; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use String::Tagged 0.21; # safe to ->delete_tag during ->iter_extents | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 NAME | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | C<App::sdview::Output::Tickit> - interactive terminal-based viewer for L<App::sdview> | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | $ sdview README.pod -o tickit | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | (though this is likely the default output mode if this module is installed) | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | This output module provides an interactive terminal-viewer for rendering | ||||||||||||||||||||||||||||
| 34 | documents in L<App::sdview>, showing formatted information on the terminal. | ||||||||||||||||||||||||||||
| 35 | It uses L<Tickit> to provide the terminal interactions. | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =head2 Keybindings | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =over 4 | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =item * | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | C<Home> or C<< < >> - scroll to top | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =item * | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | C<Backspace> - scroll up a page | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =item * | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | C<PageUp> - scroll up half a page | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =item * | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | C<[> - scroll up to the previous item | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =item * | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | C<Up> - scroll up a line | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =item * | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | C<Down> - scroll down a line | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =item * | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | C<]> - scroll up to the previous item | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =item * | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | C<PageDown> - scroll down half a page | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =item * | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | C<Space> - scroll down a page | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =item * | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | C<End> or C<< > >> - scroll to bottom | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =item * | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | C<F9> - open the outline view popup. See Below. | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =item * | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | C</> - start a regexp search in the document body. See Below. | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =item | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | C<q> - exit | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =back | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =head2 Outline View | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | The outline view displays an overview of all the section headings in the | ||||||||||||||||||||||||||||
| 98 | document. | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | Within the outline view, the mouse wheel will scroll the list, and clicking an | ||||||||||||||||||||||||||||
| 101 | entry will jump directly to it, dismissing the view. | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | Typing text with the outline view open will filter it to just those headings | ||||||||||||||||||||||||||||
| 104 | matching the typed text. Pressing the C<< <Enter> >> key will jump directly to | ||||||||||||||||||||||||||||
| 105 | the first highlighted heading, again dismissing the view. | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =head2 Regexp Searching | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | Typing into the main search box enters text that forms a (perl) regexp pattern | ||||||||||||||||||||||||||||
| 110 | to be tested against the body text of the document. Each paragraph is tested | ||||||||||||||||||||||||||||
| 111 | individually and all matches are highlighted. Pressing C<< <Enter> >> will | ||||||||||||||||||||||||||||
| 112 | select the first match. Use the C<< <n> >> and C<< <p> >> keys to jump between | ||||||||||||||||||||||||||||
| 113 | them. Press C<< <Escape> >> to clear the highlights. Press C<< <Alt-i> >> to | ||||||||||||||||||||||||||||
| 114 | toggle case-insensitivity. Press C<< <Alt-w> >> to toggle whole-word matching. | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =cut | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | # Override default output format | ||||||||||||||||||||||||||||
| 119 | require App::sdview; | ||||||||||||||||||||||||||||
| 120 | $App::sdview::DEFAULT_OUTPUT = "tickit" | ||||||||||||||||||||||||||||
| 121 | if $App::sdview::DEFAULT_OUTPUT eq "terminal" and -t STDOUT; | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | my @HIGHLIGHT_PEN = ( | ||||||||||||||||||||||||||||
| 124 | fg => 16, # avoid bold-black | ||||||||||||||||||||||||||||
| 125 | bg => "magenta", | ||||||||||||||||||||||||||||
| 126 | b => 1, | ||||||||||||||||||||||||||||
| 127 | ); | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | my @SELECT_PEN = ( | ||||||||||||||||||||||||||||
| 130 | bg => "green", | ||||||||||||||||||||||||||||
| 131 | ); | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | field $t; | ||||||||||||||||||||||||||||
| 134 | field $scroller; | ||||||||||||||||||||||||||||
| 135 | field $outlinetree; | ||||||||||||||||||||||||||||
| 136 | field @items; | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | # Related to searching | ||||||||||||||||||||||||||||
| 139 | field $matchposstatic; | ||||||||||||||||||||||||||||
| 140 | field $matchidx; | ||||||||||||||||||||||||||||
| 141 | field @matches; | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | ADJUST | ||||||||||||||||||||||||||||
| 144 | { | ||||||||||||||||||||||||||||
| 145 | # Lazy load all the Tickit modules in here | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | require Tickit; | ||||||||||||||||||||||||||||
| 148 | require Tickit::Utils; | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | $t = Tickit->new; | ||||||||||||||||||||||||||||
| 151 | $t->term->await_started( 0.050 ); | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | $t->bind_key( q => sub { $t->stop; } ); | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | require Tickit::Widget::Scroller; | ||||||||||||||||||||||||||||
| 156 | Tickit::Widget::Scroller->VERSION( '0.33' ); | ||||||||||||||||||||||||||||
| 157 | $scroller = Tickit::Widget::Scroller->new; | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | $scroller->set_gen_bottom_indicator( | ||||||||||||||||||||||||||||
| 160 | sub ( $scroller ) { | ||||||||||||||||||||||||||||
| 161 | # It feels better for progress if we claim the percentage of ofscreen | ||||||||||||||||||||||||||||
| 162 | # lines that are above the screen. | ||||||||||||||||||||||||||||
| 163 | my $lines_above = $scroller->lines_above; | ||||||||||||||||||||||||||||
| 164 | my $lines_total = $lines_above + $scroller->lines_below; | ||||||||||||||||||||||||||||
| 165 | return "" if $lines_total < 1; | ||||||||||||||||||||||||||||
| 166 | return sprintf( "%d of %d (%d%%)", | ||||||||||||||||||||||||||||
| 167 | $lines_above, $lines_total, 100 * $lines_above / $lines_total ); | ||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | ); | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | # Ugh | ||||||||||||||||||||||||||||
| 172 | $scroller->set_style( | ||||||||||||||||||||||||||||
| 173 | '<Home>' => "scroll_to_top", | ||||||||||||||||||||||||||||
| 174 | '<Space>' => "scroll_down_page", | ||||||||||||||||||||||||||||
| 175 | '<Backspace>' => "scroll_up_page", | ||||||||||||||||||||||||||||
| 176 | '<End>' => "scroll_to_bottom", | ||||||||||||||||||||||||||||
| 177 | '<<>' => "scroll_to_top", | ||||||||||||||||||||||||||||
| 178 | '<>>' => "scroll_to_bottom", | ||||||||||||||||||||||||||||
| 179 | ); | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | $outlinetree = App::sdview::Output::Tickit::_OutlineTree->new; | ||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 0 | ▶ | method output ( @paragraphs ) | ||||||||||||||||||||||||||
|
Subroutine
output: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 185 | { | ||||||||||||||||||||||||||||
| 186 | 0 | require Tickit::Widget::Scroller::Item::Text; | |||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 0 | foreach my $para ( @paragraphs ) { | |||||||||||||||||||||||||||
| 189 | 0 | ▶ | my $code = $self->can( "output_" . ( $para->type =~ s/-/_/gr ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | or die "TODO: Unhandled paragraph type " . $para->type; | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 0 | $self->$code( $para ); | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 0 | require Tickit::Widget::FloatBox; | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 0 | $t->set_root_widget( my $fb = Tickit::Widget::FloatBox->new | |||||||||||||||||||||||||||
| 198 | ->set_base_child( $scroller ) | ||||||||||||||||||||||||||||
| 199 | ); | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 0 | my $outlinefloat = $fb->add_float( | |||||||||||||||||||||||||||
| 202 | child => $outlinetree, | ||||||||||||||||||||||||||||
| 203 | hidden => 1, # initially hidden | ||||||||||||||||||||||||||||
| 204 | top => 0, bottom => -1, | ||||||||||||||||||||||||||||
| 205 | left => 0, right => 25, # ideally we want to set "30%" or somesuch here | ||||||||||||||||||||||||||||
| 206 | ); | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | $t->bind_key( | ||||||||||||||||||||||||||||
| 209 | 0 | ▶ | 'F9' => sub { $outlinefloat->is_visible ? $outlinefloat->hide : $outlinefloat->show }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 0 | ); | |||||||||||||||||||||||||||
| 211 | $t->bind_key( | ||||||||||||||||||||||||||||
| 212 | 0 | ▶ | ']' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 213 | 0 | my $item = $outlinetree->next_item( +1 ); | |||||||||||||||||||||||||||
| 214 | 0 | ▶ | $scroller->scroll_to( 0, $item->itemidx, 0 ) if $item; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | 0 | ); | |||||||||||||||||||||||||||
| 217 | $t->bind_key( | ||||||||||||||||||||||||||||
| 218 | 0 | ▶ | '[' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 219 | 0 | my $item = $outlinetree->current_item; | |||||||||||||||||||||||||||
| 220 | # Scroll back up to that item if it's not visible | ||||||||||||||||||||||||||||
| 221 | 0 | my ( undef, $offscreen ) = $scroller->item2line( $item->itemidx, 0 ); | |||||||||||||||||||||||||||
| 222 | 0 | ▶ | if( defined $offscreen and $offscreen eq "above" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $offscreen and $offscreen eq "above"
MC/DC0%0/2
defined $offscreen and $offscreen eq "above"
defined $offscreen
$offscreen eq "above"
Truth table0%0/3
A and B
A= defined $offscreenB= $offscreen eq "above"
| |||||||||||||||||||||||||||||
| 223 | 0 | $scroller->scroll_to( 0, $item->itemidx, 0 ); | |||||||||||||||||||||||||||
| 224 | 0 | return; | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 0 | $item = $outlinetree->next_item( -1 ); | |||||||||||||||||||||||||||
| 228 | 0 | ▶ | $scroller->scroll_to( 0, $item->itemidx, 0 ) if $item; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | 0 | ); | |||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 0 | ▶ | $scroller->set_on_scrolled( sub ( $scroller, $ ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 233 | 0 | my $itemidx = $scroller->line2item( 0 ); | |||||||||||||||||||||||||||
| 234 | 0 | $outlinetree->set_current_itemidx( $itemidx ); | |||||||||||||||||||||||||||
| 235 | 0 | } ); | |||||||||||||||||||||||||||
| 236 | 0 | $outlinetree->set_current_itemidx( 0 ); # initial | |||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 0 | ▶ | $outlinetree->set_on_select_item( sub ( %params ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 239 | 0 | $scroller->scroll_to( 0, $params{itemidx}, 0 ); | |||||||||||||||||||||||||||
| 240 | 0 | $outlinefloat->hide; | |||||||||||||||||||||||||||
| 241 | 0 | } ); | |||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 0 | require Tickit::Widget::Static; | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 0 | $matchposstatic = Tickit::Widget::Static->new( | |||||||||||||||||||||||||||
| 246 | text => "", | ||||||||||||||||||||||||||||
| 247 | style => { | ||||||||||||||||||||||||||||
| 248 | rv => 1, | ||||||||||||||||||||||||||||
| 249 | fg => "green", | ||||||||||||||||||||||||||||
| 250 | }, | ||||||||||||||||||||||||||||
| 251 | ); | ||||||||||||||||||||||||||||
| 252 | 0 | my $matchposfloat = $fb->add_float( | |||||||||||||||||||||||||||
| 253 | child => $matchposstatic, | ||||||||||||||||||||||||||||
| 254 | hidden => 1, | ||||||||||||||||||||||||||||
| 255 | top => -2, bottom => -1, | ||||||||||||||||||||||||||||
| 256 | left => 0, right => 10, | ||||||||||||||||||||||||||||
| 257 | ); | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | my $searchbox = App::sdview::Output::Tickit::_SearchBox->new( | ||||||||||||||||||||||||||||
| 260 | 0 | ▶ | on_incremental => sub ( $searchbox, $searchre ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 261 | 0 | my @hlmatches; | |||||||||||||||||||||||||||
| 262 | 0 | foreach my $item ( @items ) { | |||||||||||||||||||||||||||
| 263 | 0 | push @hlmatches, $item->apply_highlight( $searchre ); | |||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | 0 | foreach my $match ( @hlmatches ) { | |||||||||||||||||||||||||||
| 266 | 0 | my ( $item, $pos ) = @$match; | |||||||||||||||||||||||||||
| 267 | 0 | my $startline = $item->line_for_pos( $pos ); | |||||||||||||||||||||||||||
| 268 | 0 | ▶ | my $where = ( $scroller->item2line( $item, $startline ) )[1] // ""; | ||||||||||||||||||||||||||
|
Condition0%0/2
($scroller->item2line($item, $startline))[1] // ""
MC/DC0%0/1
($scroller->item2line($item, $startline))[1] // ""
($scroller->item2line($item, $startline))[1]
Truth table0%0/2
A
A= ($scroller->item2line($item, $startline))[1]
| |||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 0 | ▶ | next if $where eq "above"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 0 | ▶ | $scroller->scroll_to( 2, $item, $startline ) if $where eq "below"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | 0 | last; | |||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | 0 | $scroller->redraw; | |||||||||||||||||||||||||||
| 276 | 0 | $searchbox->set_matchcount( scalar @hlmatches ); | |||||||||||||||||||||||||||
| 277 | }, | ||||||||||||||||||||||||||||
| 278 | 0 | ▶ | on_enter => sub ( $searchbox, $searchre ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 279 | 0 | $matchposfloat->show; | |||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | 0 | undef @matches; | |||||||||||||||||||||||||||
| 282 | 0 | foreach my $item ( @items ) { | |||||||||||||||||||||||||||
| 283 | 0 | push @matches, $item->apply_highlight( $searchre ); | |||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||
| 285 | 0 | foreach my $match ( @matches ) { | |||||||||||||||||||||||||||
| 286 | 0 | my ( $item, $pos ) = @$match; | |||||||||||||||||||||||||||
| 287 | 0 | my $startline = $item->line_for_pos( $pos ); | |||||||||||||||||||||||||||
| 288 | 0 | ▶ | my $where = ( $scroller->item2line( $item, $startline ) )[1] // ""; | ||||||||||||||||||||||||||
|
Condition0%0/2
($scroller->item2line($item, $startline))[1] // ""
MC/DC0%0/1
($scroller->item2line($item, $startline))[1] // ""
($scroller->item2line($item, $startline))[1]
Truth table0%0/2
A
A= ($scroller->item2line($item, $startline))[1]
| |||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | 0 | ▶ | next if $where eq "above"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 0 | ▶ | $scroller->scroll_to( 2, $item, $startline ) if $where eq "below"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 293 | 0 | last; | |||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | 0 | ▶ | $matchposfloat->hide if !@matches; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | 0 | $scroller->redraw; | |||||||||||||||||||||||||||
| 299 | }, | ||||||||||||||||||||||||||||
| 300 | 0 | ); | |||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | 0 | my $searchfloat = $fb->add_float( | |||||||||||||||||||||||||||
| 303 | child => $searchbox, | ||||||||||||||||||||||||||||
| 304 | hidden => 1, | ||||||||||||||||||||||||||||
| 305 | top => -2, bottom => -1, | ||||||||||||||||||||||||||||
| 306 | left => 0, right => -1, | ||||||||||||||||||||||||||||
| 307 | ); | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 0 | $searchbox->set_float( $searchfloat ); | |||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | # TODO: use ->bind_keys of Tickit 0.75 once released | ||||||||||||||||||||||||||||
| 312 | $t->bind_key( | ||||||||||||||||||||||||||||
| 313 | 0 | ▶ | '/' => sub { $searchbox->show }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 314 | 0 | ); | |||||||||||||||||||||||||||
| 315 | $t->bind_key( | ||||||||||||||||||||||||||||
| 316 | 0 | ▶ | 'n' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 317 | 0 | ▶ | @matches or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 318 | 0 | $self->select_and_jump( ( $matchidx + 1 ) % @matches ); | |||||||||||||||||||||||||||
| 319 | }, | ||||||||||||||||||||||||||||
| 320 | 0 | ); | |||||||||||||||||||||||||||
| 321 | $t->bind_key( | ||||||||||||||||||||||||||||
| 322 | 0 | ▶ | 'p' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 323 | 0 | ▶ | @matches or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | 0 | $self->select_and_jump( ( $matchidx - 1 ) % @matches ); | |||||||||||||||||||||||||||
| 325 | }, | ||||||||||||||||||||||||||||
| 326 | 0 | ); | |||||||||||||||||||||||||||
| 327 | $t->bind_key( | ||||||||||||||||||||||||||||
| 328 | 0 | ▶ | 'Escape' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 329 | 0 | $matchposfloat->hide; | |||||||||||||||||||||||||||
| 330 | 0 | foreach my $item ( @items ) { | |||||||||||||||||||||||||||
| 331 | 0 | $item->apply_highlight( undef ); | |||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | 0 | $self->select_and_jump( undef ); | |||||||||||||||||||||||||||
| 334 | 0 | undef @matches; | |||||||||||||||||||||||||||
| 335 | 0 | $scroller->redraw; | |||||||||||||||||||||||||||
| 336 | }, | ||||||||||||||||||||||||||||
| 337 | 0 | ); | |||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 0 | $t->run; | |||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | 0 | ▶ | method select_and_jump ( $new_idx ) | ||||||||||||||||||||||||||
|
Subroutine
select_and_jump: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 343 | { | ||||||||||||||||||||||||||||
| 344 | 0 | ▶ | if( defined $matchidx ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 345 | 0 | $matches[$matchidx][2] = 0; | |||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | 0 | $matchidx = $new_idx; | |||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | 0 | ▶ | if( defined $matchidx ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 351 | 0 | $matches[$matchidx][2] = 1; | |||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 0 | $matchposstatic->set_text( sprintf "%d of %d", $matchidx+1, scalar @matches ); | |||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | 0 | my ( $item, $pos ) = $matches[$matchidx]->@*; | |||||||||||||||||||||||||||
| 356 | 0 | my $startline = $item->line_for_pos( $pos ); | |||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | 0 | $scroller->scroll_to_visible( $item, $startline, margin => 2 ); | |||||||||||||||||||||||||||
| 359 | 0 | $scroller->redraw; # to adjust highlights | |||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | # Most paragraphs are handled in a uniform way | ||||||||||||||||||||||||||||
| 364 | *output_head1 = \&_output_para; | ||||||||||||||||||||||||||||
| 365 | *output_head2 = \&_output_para; | ||||||||||||||||||||||||||||
| 366 | *output_head3 = \&_output_para; | ||||||||||||||||||||||||||||
| 367 | *output_head4 = \&_output_para; | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | *output_plain = \&_output_para; | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | *output_verbatim = \&_output_para; | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | *output_table = \&_output_para; | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | *output_item = \&_output_para; | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | field $_nextblank; | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | 0 | ▶ | method _output_para ( $para, %opts ) | ||||||||||||||||||||||||||
|
Subroutine
_output_para: not called CC=17 SCAR=57.2
| |||||||||||||||||||||||||||||
| 380 | { | ||||||||||||||||||||||||||||
| 381 | 0 | ▶ | my $margin = $opts{margin} // 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$opts{"margin"} // 0
MC/DC0%0/1
$opts{"margin"} // 0
$opts{"margin"}
Truth table0%0/2
A
A= $opts{"margin"}
| |||||||||||||||||||||||||||||
| 382 | 0 | my $leader = $opts{leader}; | |||||||||||||||||||||||||||
| 383 | 0 | my $indent = $opts{indent}; | |||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | 0 | ▶ | $scroller->push( Tickit::Widget::Scroller::Item::Text->new( "" ) ) if $_nextblank; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | 0 | my %parastyle = App::sdview::Style->para_style( $para->type )->%*; | |||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | 0 | my $itempen = Tickit::Pen->new; | |||||||||||||||||||||||||||
| 391 | 0 | ▶ | $itempen->chattr( b => 1 ) if defined $parastyle{bold}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 392 | 0 | ▶ | $itempen->chattr( u => 1 ) if defined $parastyle{under}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 393 | 0 | ▶ | $itempen->chattr( i => 1 ) if defined $parastyle{italic}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 394 | 0 | ▶ | $itempen->chattr( af => 1 ) if defined $parastyle{monospace}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | $itempen->chattr( fg => $parastyle{fg}->as_xterm->index ) | ||||||||||||||||||||||||||||
| 397 | 0 | ▶ | if defined $parastyle{fg}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 398 | $itempen->chattr( bg => $parastyle{bg}->as_xterm->index ) | ||||||||||||||||||||||||||||
| 399 | 0 | ▶ | if defined $parastyle{bg}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | 0 | ▶ | $margin += ( $parastyle{margin} // 0 ); | ||||||||||||||||||||||||||
|
Condition0%0/2
$parastyle{"margin"} // 0
MC/DC0%0/1
$parastyle{"margin"} // 0
$parastyle{"margin"}
Truth table0%0/2
A
A= $parastyle{"margin"}
| |||||||||||||||||||||||||||||
| 402 | 0 | ▶ | $indent //= 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$indent //= 0
MC/DC0%0/1
$indent //= 0
$indent
Truth table0%0/2
A
A= $indent
| |||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | 0 | ▶ | if( defined $leader ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 405 | 0 | my $leaderlen = Tickit::Utils::textwidth( $leader ); | |||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | 0 | my %leaderstyle = App::sdview::Style->para_style( "leader" )->%*; | |||||||||||||||||||||||||||
| 408 | $leaderstyle{$_} and $leader->apply_tag( 0, $leaderlen, $_ => $leaderstyle{$_} ) | ||||||||||||||||||||||||||||
| 409 | 0 | ▶ | for qw( fg bg bold under italic monospace ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | 0 | my $item; | |||||||||||||||||||||||||||
| 413 | 0 | ▶ | if( $para->type eq "verbatim" ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 414 | # This handily deals with the paragraph bg too | ||||||||||||||||||||||||||||
| 415 | 0 | my $text = App::sdview::Style->convert_str( $para->text ); | |||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | 0 | $item = App::sdview::Output::Tickit::_FixedWidthItem->new( | |||||||||||||||||||||||||||
| 418 | text => $text, | ||||||||||||||||||||||||||||
| 419 | margin_left => $margin + $indent, | ||||||||||||||||||||||||||||
| 420 | margin_right => 1, | ||||||||||||||||||||||||||||
| 421 | pen => $itempen, | ||||||||||||||||||||||||||||
| 422 | ); | ||||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||
| 424 | elsif( $para->type eq "table" ) { | ||||||||||||||||||||||||||||
| 425 | 0 | $item = App::sdview::Output::Tickit::_TableItem->new( | |||||||||||||||||||||||||||
| 426 | rows => [ $para->rows ], | ||||||||||||||||||||||||||||
| 427 | margin_left => $margin + $indent, | ||||||||||||||||||||||||||||
| 428 | margin_right => 1, | ||||||||||||||||||||||||||||
| 429 | pen => $itempen, | ||||||||||||||||||||||||||||
| 430 | ); | ||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||
| 432 | else { | ||||||||||||||||||||||||||||
| 433 | 0 | my $text = App::sdview::Style->convert_str( $para->text ); | |||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | 0 | $item = App::sdview::Output::Tickit::_ParagraphItem->new( | |||||||||||||||||||||||||||
| 436 | text => $text, | ||||||||||||||||||||||||||||
| 437 | leader => $leader, | ||||||||||||||||||||||||||||
| 438 | indent => $indent, | ||||||||||||||||||||||||||||
| 439 | margin_left => $margin, | ||||||||||||||||||||||||||||
| 440 | margin_right => 1, | ||||||||||||||||||||||||||||
| 441 | pen => $itempen, | ||||||||||||||||||||||||||||
| 442 | ) | ||||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | 0 | ▶ | if( $para->type =~ m/^head(\d+)/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 446 | 0 | my @lines = $para->text->split( qr/\n/ ); | |||||||||||||||||||||||||||
| 447 | 0 | my $level = $1; | |||||||||||||||||||||||||||
| 448 | 0 | my $itemidx = $scroller->items; | |||||||||||||||||||||||||||
| 449 | 0 | $outlinetree->add_item( "$lines[0]", $level, $itemidx ); | |||||||||||||||||||||||||||
| 450 | } | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | 0 | push @items, $item; | |||||||||||||||||||||||||||
| 453 | 0 | $scroller->push( $item ); | |||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | 0 | $_nextblank = !!$parastyle{blank_after}; | |||||||||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | 0 | ▶ | method output_list_bullet ( $para, %opts ) { $self->_output_list( bullet => $para, %opts ); } | ||||||||||||||||||||||||||
|
Subroutine
output_list_bullet: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 459 | 0 | ▶ | method output_list_number ( $para, %opts ) { $self->_output_list( number => $para, %opts ); } | ||||||||||||||||||||||||||
|
Subroutine
output_list_number: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 460 | 0 | ▶ | method output_list_text ( $para, %opts ) { $self->_output_list( text => $para, %opts ); } | ||||||||||||||||||||||||||
|
Subroutine
output_list_text: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | 0 | ▶ | method _output_list ( $listtype, $para, %opts ) | ||||||||||||||||||||||||||
|
Subroutine
_output_list: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||
| 463 | { | ||||||||||||||||||||||||||||
| 464 | 0 | my $n = $para->initial; | |||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | 0 | ▶ | my $margin = $opts{margin} // 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$opts{"margin"} // 0
MC/DC0%0/1
$opts{"margin"} // 0
$opts{"margin"}
Truth table0%0/2
A
A= $opts{"margin"}
| |||||||||||||||||||||||||||||
| 467 | 0 | ▶ | $margin += App::sdview::Style->para_style( "list" )->{margin} // 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
"App::sdview::Style"->para_style("list")->{"margin"} // 0
MC/DC0%0/1
"App::sdview::Style"->para_style("list")->{"margin"} // 0
"App::sdview::Style"->para_style("list")->{"margin"}
Truth table0%0/2
A
A= "App::sdview::Style"->para_style("list")->{"margin"}
| |||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | 0 | foreach my $item ( $para->items ) { | |||||||||||||||||||||||||||
| 470 | 0 | my $leader; | |||||||||||||||||||||||||||
| 471 | 0 | ▶ | if( $item->type eq "plain" ) { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 472 | # plain paragraphs in list are treated like items with no leader | ||||||||||||||||||||||||||||
| 473 | $self->output_item( $item, | ||||||||||||||||||||||||||||
| 474 | # make sure not to double-count the margin | ||||||||||||||||||||||||||||
| 475 | margin => $margin - App::sdview::Style->para_style( "plain" )->{margin}, | ||||||||||||||||||||||||||||
| 476 | 0 | indent => $para->indent, | |||||||||||||||||||||||||||
| 477 | ); | ||||||||||||||||||||||||||||
| 478 | 0 | next; | |||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||
| 480 | elsif( $item->type ne "item" ) { | ||||||||||||||||||||||||||||
| 481 | # non-items just stand as they are + indent | ||||||||||||||||||||||||||||
| 482 | } | ||||||||||||||||||||||||||||
| 483 | elsif( $listtype eq "bullet" ) { | ||||||||||||||||||||||||||||
| 484 | 0 | $leader = String::Tagged->new( "•" ); | |||||||||||||||||||||||||||
| 485 | } | ||||||||||||||||||||||||||||
| 486 | elsif( $listtype eq "number" ) { | ||||||||||||||||||||||||||||
| 487 | 0 | $leader = String::Tagged->from_sprintf( "%d.", $n++ ); | |||||||||||||||||||||||||||
| 488 | } | ||||||||||||||||||||||||||||
| 489 | elsif( $listtype eq "text" ) { | ||||||||||||||||||||||||||||
| 490 | 0 | $leader = App::sdview::Style->convert_str( $item->term ); | |||||||||||||||||||||||||||
| 491 | } | ||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||
| 493 | 0 | ▶ | my $code = $self->can( "output_" . ( $item->type =~ s/-/_/gr ) ) or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 494 | die "TODO: Unhandled item type " . $item->type; | ||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||
| 496 | 0 | $self->$code( $item, | |||||||||||||||||||||||||||
| 497 | margin => $margin, | ||||||||||||||||||||||||||||
| 498 | indent => $para->indent, | ||||||||||||||||||||||||||||
| 499 | leader => $leader, | ||||||||||||||||||||||||||||
| 500 | ); | ||||||||||||||||||||||||||||
| 501 | } | ||||||||||||||||||||||||||||
| 502 | } | ||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||
| 504 | # Logic kindof stolen from T:W:Scroller::Item::(Rich)Text but modified | ||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | 0 | ▶ | sub _convert_color_tag ($n, $v) | ||||||||||||||||||||||||||
|
Subroutine
_convert_color_tag: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 507 | { | ||||||||||||||||||||||||||||
| 508 | 0 | return $n => $v->as_xterm->index; | |||||||||||||||||||||||||||
| 509 | } | ||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||
| 511 | my %convert_tags = ( | ||||||||||||||||||||||||||||
| 512 | bold => "b", | ||||||||||||||||||||||||||||
| 513 | under => "u", | ||||||||||||||||||||||||||||
| 514 | italic => "i", | ||||||||||||||||||||||||||||
| 515 | strike => "strike", | ||||||||||||||||||||||||||||
| 516 | blink => "blink", | ||||||||||||||||||||||||||||
| 517 | monospace => sub ($, $v) { "af" => ( $v ? 1 : 0 ) }, | ||||||||||||||||||||||||||||
| 518 | reverse => "rv", | ||||||||||||||||||||||||||||
| 519 | fg => \&_convert_color_tag, | ||||||||||||||||||||||||||||
| 520 | bg => \&_convert_color_tag, | ||||||||||||||||||||||||||||
| 521 | ); | ||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||
| 523 | class App::sdview::Output::Tickit::_ParagraphItem | ||||||||||||||||||||||||||||
| 524 | :strict(params) | ||||||||||||||||||||||||||||
| 525 | { | ||||||||||||||||||||||||||||
| 526 | 0 | ▶ | use Tickit::Utils qw( textwidth ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||
| 528 | field $_pen :param = undef; | ||||||||||||||||||||||||||||
| 529 | field $_margin_left :param = 0; | ||||||||||||||||||||||||||||
| 530 | field $_margin_right :param = 0; | ||||||||||||||||||||||||||||
| 531 | field $_indent :param = 0; | ||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||
| 533 | field $_leader; | ||||||||||||||||||||||||||||
| 534 | ADJUST :params ( :$leader = undef ) | ||||||||||||||||||||||||||||
| 535 | { | ||||||||||||||||||||||||||||
| 536 | $_leader = $leader->clone( convert_tags => \%convert_tags ) if defined $leader; | ||||||||||||||||||||||||||||
| 537 | } | ||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||
| 539 | field $_leaderlen; | ||||||||||||||||||||||||||||
| 540 | field $_has_leaderline = 0; | ||||||||||||||||||||||||||||
| 541 | ADJUST { | ||||||||||||||||||||||||||||
| 542 | $_leaderlen = Tickit::Utils::textwidth( $_leader ) if defined $_leader; | ||||||||||||||||||||||||||||
| 543 | $_has_leaderline = 1 if $_leaderlen and $_leaderlen + 1 > $_indent; | ||||||||||||||||||||||||||||
| 544 | } | ||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||
| 546 | field $_text; | ||||||||||||||||||||||||||||
| 547 | field @_chunks; # => [ $start, $end, $width, $is_softhyphen ] | ||||||||||||||||||||||||||||
| 548 | ADJUST :params ( :$text ) | ||||||||||||||||||||||||||||
| 549 | { | ||||||||||||||||||||||||||||
| 550 | $_text = String::Tagged->new( "" ); | ||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||
| 552 | my $textplain = "$text"; | ||||||||||||||||||||||||||||
| 553 | pos( $textplain ) = 0; | ||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||
| 555 | while( pos( $textplain ) < length $textplain ) { | ||||||||||||||||||||||||||||
| 556 | $textplain =~ m/\G\s+/gc and next; # skip whitespace | ||||||||||||||||||||||||||||
| 557 | $textplain =~ m/\G\xAD/gc and | ||||||||||||||||||||||||||||
| 558 | $_chunks[-1][3] = 1, next; | ||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||
| 560 | my $chunkstart = pos( $textplain ); | ||||||||||||||||||||||||||||
| 561 | # Find the next chunk by ignoring NBSP | ||||||||||||||||||||||||||||
| 562 | $textplain =~ m/\G(?[ \S & !\xAD + \xA0 ])+/gc or last; | ||||||||||||||||||||||||||||
| 563 | my $chunklen = pos( $textplain ) - $chunkstart; | ||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||
| 565 | $_text .= " " if @_chunks and !$_chunks[-1][3]; | ||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||
| 567 | my $chunk = $text->substr( $chunkstart, $chunklen ) | ||||||||||||||||||||||||||||
| 568 | ->clone( convert_tags => \%convert_tags ); | ||||||||||||||||||||||||||||
| 569 | my $chunkwidth = textwidth $chunk; | ||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||
| 571 | my $pos = length $_text; | ||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||
| 573 | # Convert NBSP to regular space since it's now been used | ||||||||||||||||||||||||||||
| 574 | foreach my $e ( $chunk->match_extents( qr/\xA0/ ) ) { | ||||||||||||||||||||||||||||
| 575 | $chunk->set_substr( $e->start, $e->length, " " ); | ||||||||||||||||||||||||||||
| 576 | } | ||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||
| 578 | $_text .= $chunk; | ||||||||||||||||||||||||||||
| 579 | push @_chunks, [ $pos, $pos + $chunklen, $chunkwidth ]; | ||||||||||||||||||||||||||||
| 580 | } | ||||||||||||||||||||||||||||
| 581 | } | ||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||
| 583 | field @_lineruns; | ||||||||||||||||||||||||||||
| 584 | method _pushline ( $start, $end, $is_softhyphen ) | ||||||||||||||||||||||||||||
| 585 | { | ||||||||||||||||||||||||||||
| 586 | push @_lineruns, [ $start, $end, $is_softhyphen ]; | ||||||||||||||||||||||||||||
| 587 | } | ||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||
| 589 | method line_for_pos ( $pos ) | ||||||||||||||||||||||||||||
| 590 | { | ||||||||||||||||||||||||||||
| 591 | my $char = $pos->start; | ||||||||||||||||||||||||||||
| 592 | my $line = 0; | ||||||||||||||||||||||||||||
| 593 | $line++ while $line < $#_lineruns and $_lineruns[$line][1] < $char; | ||||||||||||||||||||||||||||
| 594 | return $line; | ||||||||||||||||||||||||||||
| 595 | } | ||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||
| 597 | field $_cached_width; | ||||||||||||||||||||||||||||
| 598 | method height_for_width ( $width ) | ||||||||||||||||||||||||||||
| 599 | { | ||||||||||||||||||||||||||||
| 600 | $_cached_width = $width; | ||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||
| 602 | $width -= $_margin_left + $_margin_right; | ||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||
| 604 | @_lineruns = (); | ||||||||||||||||||||||||||||
| 605 | push @_lineruns, undef if $_has_leaderline; | ||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||
| 607 | # Operate on pos() within textplain as a proxy for the position within | ||||||||||||||||||||||||||||
| 608 | # the String::Tagged instance | ||||||||||||||||||||||||||||
| 609 | my $textplain = "$_text"; | ||||||||||||||||||||||||||||
| 610 | pos( $textplain ) = 0; | ||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||
| 612 | my $linestart = 0; | ||||||||||||||||||||||||||||
| 613 | my $lineend = 0; | ||||||||||||||||||||||||||||
| 614 | my $linewidth = 0; | ||||||||||||||||||||||||||||
| 615 | my $was_softhyphen; | ||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||
| 617 | foreach my $chunk ( @_chunks ) { | ||||||||||||||||||||||||||||
| 618 | my ( $startpos, $endpos, $chunkwidth, $is_softhyphen ) = @$chunk; | ||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||
| 620 | $linewidth += 1 if $linewidth; | ||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||
| 622 | if( $linewidth + $chunkwidth > $width ) { | ||||||||||||||||||||||||||||
| 623 | $self->_pushline( $linestart, $lineend, $was_softhyphen // 0 ); | ||||||||||||||||||||||||||||
| 624 | $linestart = $startpos; | ||||||||||||||||||||||||||||
| 625 | $linewidth = 0; | ||||||||||||||||||||||||||||
| 626 | } | ||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||
| 628 | $linewidth += $_indent if !$linewidth; | ||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||
| 630 | $linewidth += $chunkwidth; | ||||||||||||||||||||||||||||
| 631 | $lineend = $endpos; | ||||||||||||||||||||||||||||
| 632 | $was_softhyphen = $is_softhyphen; | ||||||||||||||||||||||||||||
| 633 | } | ||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||
| 635 | $self->_pushline( $linestart, $lineend, 0 ) if $linewidth; | ||||||||||||||||||||||||||||
| 636 | return scalar @_lineruns; | ||||||||||||||||||||||||||||
| 637 | } | ||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||
| 639 | method render ( $rb, %args ) | ||||||||||||||||||||||||||||
| 640 | { | ||||||||||||||||||||||||||||
| 641 | my $width = $args{width}; | ||||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||
| 643 | $self->height_for_width( $width ) if $width != $_cached_width; | ||||||||||||||||||||||||||||
| 644 | |||||||||||||||||||||||||||||
| 645 | foreach my $line ( $args{firstline} .. $args{lastline} ) { | ||||||||||||||||||||||||||||
| 646 | $rb->goto( $line, 0 ); | ||||||||||||||||||||||||||||
| 647 | $rb->erase( $_margin_left ) if $_margin_left; | ||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||
| 649 | if( $_pen ) { | ||||||||||||||||||||||||||||
| 650 | $rb->savepen; | ||||||||||||||||||||||||||||
| 651 | $rb->setpen( $_pen ); | ||||||||||||||||||||||||||||
| 652 | } | ||||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||
| 654 | if( $line == 0 and $_leaderlen ) { | ||||||||||||||||||||||||||||
| 655 | $_leader->iter_substr_nooverlap( | ||||||||||||||||||||||||||||
| 656 | sub ( $substr, %tags ) | ||||||||||||||||||||||||||||
| 657 | { | ||||||||||||||||||||||||||||
| 658 | if( exists $tags{highlight} ) { | ||||||||||||||||||||||||||||
| 659 | %tags = ( %tags, @HIGHLIGHT_PEN ); | ||||||||||||||||||||||||||||
| 660 | %tags = ( %tags, @SELECT_PEN ) if $tags{highlight}->$*; | ||||||||||||||||||||||||||||
| 661 | } | ||||||||||||||||||||||||||||
| 662 | my $pen = Tickit::Pen::Immutable->new_from_attrs( \%tags ); | ||||||||||||||||||||||||||||
| 663 | $rb->text( $substr, $pen ); | ||||||||||||||||||||||||||||
| 664 | }, | ||||||||||||||||||||||||||||
| 665 | ); | ||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||
| 667 | goto next_line if $_has_leaderline; | ||||||||||||||||||||||||||||
| 668 | } | ||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||
| 670 | $rb->erase_to( $_margin_left + $_indent ) if $_indent; | ||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||
| 672 | my ( $start, $end, $is_softhyphen ) = $_lineruns[$line]->@*; | ||||||||||||||||||||||||||||
| 673 | $_text->iter_substr_nooverlap( | ||||||||||||||||||||||||||||
| 674 | sub ( $substr, %tags ) | ||||||||||||||||||||||||||||
| 675 | { | ||||||||||||||||||||||||||||
| 676 | if( exists $tags{highlight} ) { | ||||||||||||||||||||||||||||
| 677 | %tags = ( %tags, @HIGHLIGHT_PEN ); | ||||||||||||||||||||||||||||
| 678 | %tags = ( %tags, @SELECT_PEN ) if $tags{highlight}->$*; | ||||||||||||||||||||||||||||
| 679 | } | ||||||||||||||||||||||||||||
| 680 | my $pen = Tickit::Pen::Immutable->new_from_attrs( \%tags ); | ||||||||||||||||||||||||||||
| 681 | $rb->text( $substr, $pen ); | ||||||||||||||||||||||||||||
| 682 | }, | ||||||||||||||||||||||||||||
| 683 | start => $start, | ||||||||||||||||||||||||||||
| 684 | end => $end, | ||||||||||||||||||||||||||||
| 685 | ); | ||||||||||||||||||||||||||||
| 686 | $rb->text( "-" ) if $is_softhyphen; | ||||||||||||||||||||||||||||
| 687 | |||||||||||||||||||||||||||||
| 688 | next_line: | ||||||||||||||||||||||||||||
| 689 | if( $_pen ) { | ||||||||||||||||||||||||||||
| 690 | $rb->erase_to( $width - $_margin_right ); | ||||||||||||||||||||||||||||
| 691 | $rb->restore; | ||||||||||||||||||||||||||||
| 692 | $rb->erase_to( $width ) if $_margin_right; | ||||||||||||||||||||||||||||
| 693 | } | ||||||||||||||||||||||||||||
| 694 | else { | ||||||||||||||||||||||||||||
| 695 | $rb->erase_to( $width ); | ||||||||||||||||||||||||||||
| 696 | } | ||||||||||||||||||||||||||||
| 697 | } | ||||||||||||||||||||||||||||
| 698 | } | ||||||||||||||||||||||||||||
| 699 | |||||||||||||||||||||||||||||
| 700 | method apply_highlight ( $re ) | ||||||||||||||||||||||||||||
| 701 | { | ||||||||||||||||||||||||||||
| 702 | my $redraw_needed; | ||||||||||||||||||||||||||||
| 703 | |||||||||||||||||||||||||||||
| 704 | $_leader->iter_extents( | ||||||||||||||||||||||||||||
| 705 | sub ( $e, $t, $v ) { | ||||||||||||||||||||||||||||
| 706 | $_leader->delete_tag( $e, $t ); | ||||||||||||||||||||||||||||
| 707 | $redraw_needed++; | ||||||||||||||||||||||||||||
| 708 | }, | ||||||||||||||||||||||||||||
| 709 | only => [qw( highlight )], | ||||||||||||||||||||||||||||
| 710 | ) if defined $_leader; | ||||||||||||||||||||||||||||
| 711 | |||||||||||||||||||||||||||||
| 712 | $_text->iter_extents( | ||||||||||||||||||||||||||||
| 713 | sub ( $e, $t, $v ) { | ||||||||||||||||||||||||||||
| 714 | $_text->delete_tag( $e, $t ); | ||||||||||||||||||||||||||||
| 715 | $redraw_needed++; | ||||||||||||||||||||||||||||
| 716 | }, | ||||||||||||||||||||||||||||
| 717 | only => [qw( highlight )], | ||||||||||||||||||||||||||||
| 718 | ); | ||||||||||||||||||||||||||||
| 719 | |||||||||||||||||||||||||||||
| 720 | return unless defined $re; | ||||||||||||||||||||||||||||
| 721 | |||||||||||||||||||||||||||||
| 722 | my @ret; | ||||||||||||||||||||||||||||
| 723 | |||||||||||||||||||||||||||||
| 724 | foreach my $e ( defined $_leader ? $_leader->match_extents( $re ) : () ) { | ||||||||||||||||||||||||||||
| 725 | push @ret, [ $self, $e, 0 ]; | ||||||||||||||||||||||||||||
| 726 | $_leader->apply_tag( $e, highlight => \$ret[-1][2] ); | ||||||||||||||||||||||||||||
| 727 | } | ||||||||||||||||||||||||||||
| 728 | |||||||||||||||||||||||||||||
| 729 | foreach my $e ( $_text->match_extents( $re ) ) { | ||||||||||||||||||||||||||||
| 730 | push @ret, [ $self, $e, 0 ]; | ||||||||||||||||||||||||||||
| 731 | $_text->apply_tag( $e, highlight => \$ret[-1][2] ); | ||||||||||||||||||||||||||||
| 732 | } | ||||||||||||||||||||||||||||
| 733 | |||||||||||||||||||||||||||||
| 734 | return @ret; | ||||||||||||||||||||||||||||
| 735 | } | ||||||||||||||||||||||||||||
| 736 | } | ||||||||||||||||||||||||||||
| 737 | |||||||||||||||||||||||||||||
| 738 | class App::sdview::Output::Tickit::_FixedWidthItem | ||||||||||||||||||||||||||||
| 739 | :strict(params) | ||||||||||||||||||||||||||||
| 740 | { | ||||||||||||||||||||||||||||
| 741 | use Tickit::Utils qw( textwidth ); | ||||||||||||||||||||||||||||
| 742 | |||||||||||||||||||||||||||||
| 743 | field $_text; | ||||||||||||||||||||||||||||
| 744 | field @_lineruns; | ||||||||||||||||||||||||||||
| 745 | field $_maxwidth; | ||||||||||||||||||||||||||||
| 746 | ADJUST :params ( | ||||||||||||||||||||||||||||
| 747 | :$text, | ||||||||||||||||||||||||||||
| 748 | ) { | ||||||||||||||||||||||||||||
| 749 | $_text = $text->clone( convert_tags => \%convert_tags ); | ||||||||||||||||||||||||||||
| 750 | |||||||||||||||||||||||||||||
| 751 | my $start = 0; | ||||||||||||||||||||||||||||
| 752 | while( $text =~ m/\n|$/g ) { | ||||||||||||||||||||||||||||
| 753 | push @_lineruns, [ $start, $-[0] ]; | ||||||||||||||||||||||||||||
| 754 | $start = $+[0]; | ||||||||||||||||||||||||||||
| 755 | } | ||||||||||||||||||||||||||||
| 756 | $_maxwidth = 0; | ||||||||||||||||||||||||||||
| 757 | $_ > $_maxwidth and $_maxwidth = $_ for map { textwidth(substr($text, $_->[0], $_->[1]-$_->[0])) } @_lineruns; | ||||||||||||||||||||||||||||
| 758 | } | ||||||||||||||||||||||||||||
| 759 | |||||||||||||||||||||||||||||
| 760 | field $_pen :param = undef; | ||||||||||||||||||||||||||||
| 761 | field $_margin_left :param = 0; | ||||||||||||||||||||||||||||
| 762 | field $_margin_right :param = 0; | ||||||||||||||||||||||||||||
| 763 | |||||||||||||||||||||||||||||
| 764 | method height_for_width ( $ ) { return scalar @_lineruns; } | ||||||||||||||||||||||||||||
| 765 | |||||||||||||||||||||||||||||
| 766 | method line_for_pos ( $pos ) | ||||||||||||||||||||||||||||
| 767 | { | ||||||||||||||||||||||||||||
| 768 | my $char = $pos->start; | ||||||||||||||||||||||||||||
| 769 | my $line = 0; | ||||||||||||||||||||||||||||
| 770 | $line++ while $line < $#_lineruns and $_lineruns[$line][1] < $char; | ||||||||||||||||||||||||||||
| 771 | return $line; | ||||||||||||||||||||||||||||
| 772 | } | ||||||||||||||||||||||||||||
| 773 | |||||||||||||||||||||||||||||
| 774 | method render ( $rb, %args ) | ||||||||||||||||||||||||||||
| 775 | { | ||||||||||||||||||||||||||||
| 776 | my $cols = $args{width}; | ||||||||||||||||||||||||||||
| 777 | |||||||||||||||||||||||||||||
| 778 | foreach my $lineidx ( $args{firstline} .. $args{lastline} ) { | ||||||||||||||||||||||||||||
| 779 | $rb->goto( $lineidx, 0 ); | ||||||||||||||||||||||||||||
| 780 | $rb->erase( $_margin_left ) if $_margin_left; | ||||||||||||||||||||||||||||
| 781 | |||||||||||||||||||||||||||||
| 782 | if( $_pen ) { | ||||||||||||||||||||||||||||
| 783 | $rb->savepen; | ||||||||||||||||||||||||||||
| 784 | $rb->setpen( $_pen ); | ||||||||||||||||||||||||||||
| 785 | } | ||||||||||||||||||||||||||||
| 786 | |||||||||||||||||||||||||||||
| 787 | # TODO: truncation if text wider than margin | ||||||||||||||||||||||||||||
| 788 | |||||||||||||||||||||||||||||
| 789 | my ( $start, $end, $is_softhyphen ) = $_lineruns[$lineidx]->@*; | ||||||||||||||||||||||||||||
| 790 | $_text->iter_substr_nooverlap( | ||||||||||||||||||||||||||||
| 791 | sub ( $substr, %tags ) | ||||||||||||||||||||||||||||
| 792 | { | ||||||||||||||||||||||||||||
| 793 | if( exists $tags{highlight} ) { | ||||||||||||||||||||||||||||
| 794 | %tags = ( %tags, @HIGHLIGHT_PEN ); | ||||||||||||||||||||||||||||
| 795 | %tags = ( %tags, @SELECT_PEN ) if $tags{highlight}->$*; | ||||||||||||||||||||||||||||
| 796 | } | ||||||||||||||||||||||||||||
| 797 | my $pen = Tickit::Pen::Immutable->new_from_attrs( \%tags ); | ||||||||||||||||||||||||||||
| 798 | $rb->text( $substr, $pen ); | ||||||||||||||||||||||||||||
| 799 | }, | ||||||||||||||||||||||||||||
| 800 | start => $start, | ||||||||||||||||||||||||||||
| 801 | end => $end, | ||||||||||||||||||||||||||||
| 802 | ); | ||||||||||||||||||||||||||||
| 803 | $rb->erase_to( $_maxwidth + $_margin_left ); | ||||||||||||||||||||||||||||
| 804 | |||||||||||||||||||||||||||||
| 805 | if( $_pen ) { | ||||||||||||||||||||||||||||
| 806 | $rb->restore; | ||||||||||||||||||||||||||||
| 807 | } | ||||||||||||||||||||||||||||
| 808 | |||||||||||||||||||||||||||||
| 809 | $rb->erase_to( $cols ); | ||||||||||||||||||||||||||||
| 810 | } | ||||||||||||||||||||||||||||
| 811 | } | ||||||||||||||||||||||||||||
| 812 | |||||||||||||||||||||||||||||
| 813 | method apply_highlight ( $re ) | ||||||||||||||||||||||||||||
| 814 | { | ||||||||||||||||||||||||||||
| 815 | $_text->iter_extents( | ||||||||||||||||||||||||||||
| 816 | sub ( $e, $t, $v ) { | ||||||||||||||||||||||||||||
| 817 | $_text->delete_tag( $e, $t ); | ||||||||||||||||||||||||||||
| 818 | }, | ||||||||||||||||||||||||||||
| 819 | only => [qw( highlight )], | ||||||||||||||||||||||||||||
| 820 | ); | ||||||||||||||||||||||||||||
| 821 | |||||||||||||||||||||||||||||
| 822 | return unless defined $re; | ||||||||||||||||||||||||||||
| 823 | |||||||||||||||||||||||||||||
| 824 | my @ret; | ||||||||||||||||||||||||||||
| 825 | |||||||||||||||||||||||||||||
| 826 | foreach my $e ( $_text->match_extents( $re ) ) { | ||||||||||||||||||||||||||||
| 827 | push @ret, [ $self, $e, 0 ]; | ||||||||||||||||||||||||||||
| 828 | $_text->apply_tag( $e, highlight => \$ret[-1][2] ); | ||||||||||||||||||||||||||||
| 829 | } | ||||||||||||||||||||||||||||
| 830 | |||||||||||||||||||||||||||||
| 831 | return @ret; | ||||||||||||||||||||||||||||
| 832 | } | ||||||||||||||||||||||||||||
| 833 | } | ||||||||||||||||||||||||||||
| 834 | |||||||||||||||||||||||||||||
| 835 | class App::sdview::Output::Tickit::_TableItem | ||||||||||||||||||||||||||||
| 836 | :strict(params) | ||||||||||||||||||||||||||||
| 837 | { | ||||||||||||||||||||||||||||
| 838 | use Tickit::Utils qw( textwidth ); | ||||||||||||||||||||||||||||
| 839 | use Tickit::RenderBuffer qw( LINE_SINGLE ); | ||||||||||||||||||||||||||||
| 840 | |||||||||||||||||||||||||||||
| 841 | field @_rows; | ||||||||||||||||||||||||||||
| 842 | ADJUST :params ( :$rows, ) { @_rows = @$rows; } | ||||||||||||||||||||||||||||
| 843 | |||||||||||||||||||||||||||||
| 844 | field $_pen :param = undef; | ||||||||||||||||||||||||||||
| 845 | field $_margin_left :param = 0; | ||||||||||||||||||||||||||||
| 846 | field $_margin_right :param = 0; | ||||||||||||||||||||||||||||
| 847 | |||||||||||||||||||||||||||||
| 848 | field @_colwidth; | ||||||||||||||||||||||||||||
| 849 | field @_colstart; | ||||||||||||||||||||||||||||
| 850 | |||||||||||||||||||||||||||||
| 851 | method line_for_pos ( $pos ) | ||||||||||||||||||||||||||||
| 852 | { | ||||||||||||||||||||||||||||
| 853 | my ( $rowidx, $colidx, $e ) = @$pos; | ||||||||||||||||||||||||||||
| 854 | # TODO: this logic will get a lot more complex when cell wrapping is | ||||||||||||||||||||||||||||
| 855 | # implemented, but for now it's very simple | ||||||||||||||||||||||||||||
| 856 | return 1 + 2 * $rowidx; | ||||||||||||||||||||||||||||
| 857 | } | ||||||||||||||||||||||||||||
| 858 | |||||||||||||||||||||||||||||
| 859 | method height_for_width ( $width ) | ||||||||||||||||||||||||||||
| 860 | { | ||||||||||||||||||||||||||||
| 861 | @_colwidth = (); | ||||||||||||||||||||||||||||
| 862 | foreach my $row ( @_rows ) { | ||||||||||||||||||||||||||||
| 863 | foreach my $colidx ( 0 .. $#$row ) { | ||||||||||||||||||||||||||||
| 864 | my $cellwidth = textwidth $row->[$colidx]->text; | ||||||||||||||||||||||||||||
| 865 | $_colwidth[$colidx] = $cellwidth if $cellwidth > ( $_colwidth[$colidx] // 0 ); | ||||||||||||||||||||||||||||
| 866 | } | ||||||||||||||||||||||||||||
| 867 | } | ||||||||||||||||||||||||||||
| 868 | |||||||||||||||||||||||||||||
| 869 | @_colstart = ( $_margin_left + 1 ); | ||||||||||||||||||||||||||||
| 870 | $_colstart[$_+1] = $_colstart[$_] + $_colwidth[$_] + 3 for 0 .. $#_colwidth; | ||||||||||||||||||||||||||||
| 871 | |||||||||||||||||||||||||||||
| 872 | return 1 + 2 * @_rows; | ||||||||||||||||||||||||||||
| 873 | } | ||||||||||||||||||||||||||||
| 874 | |||||||||||||||||||||||||||||
| 875 | method render ( $rb, %args ) | ||||||||||||||||||||||||||||
| 876 | { | ||||||||||||||||||||||||||||
| 877 | $rb->erase_at( $_, 0, $args{width} ) for $args{firstline} .. $args{lastline}; | ||||||||||||||||||||||||||||
| 878 | |||||||||||||||||||||||||||||
| 879 | my $width = $_colstart[ scalar @_colwidth ]; | ||||||||||||||||||||||||||||
| 880 | my $height = 1 + 2 * @_rows; | ||||||||||||||||||||||||||||
| 881 | |||||||||||||||||||||||||||||
| 882 | # Draw all the lines first | ||||||||||||||||||||||||||||
| 883 | foreach my $rowidx ( 0 .. $#_rows+1 ) { | ||||||||||||||||||||||||||||
| 884 | $rb->hline_at( 2 * $rowidx, $_margin_left, $width-1, LINE_SINGLE ); | ||||||||||||||||||||||||||||
| 885 | } | ||||||||||||||||||||||||||||
| 886 | foreach my $colidx ( 0 .. $#_colwidth+1 ) { | ||||||||||||||||||||||||||||
| 887 | $rb->vline_at( 0, $height-1, $_colstart[$colidx]-1, LINE_SINGLE ); | ||||||||||||||||||||||||||||
| 888 | } | ||||||||||||||||||||||||||||
| 889 | |||||||||||||||||||||||||||||
| 890 | foreach my $rowidx ( 0 .. $#_rows ) { | ||||||||||||||||||||||||||||
| 891 | foreach my $colidx ( 0 .. $#_colwidth ) { | ||||||||||||||||||||||||||||
| 892 | my $cell = $_rows[$rowidx]->[$colidx]; | ||||||||||||||||||||||||||||
| 893 | my $text = $cell->text->clone; | ||||||||||||||||||||||||||||
| 894 | |||||||||||||||||||||||||||||
| 895 | my %cellstyle; | ||||||||||||||||||||||||||||
| 896 | %cellstyle = ( App::sdview::Style->para_style( "table-heading" )->%*, %cellstyle ) | ||||||||||||||||||||||||||||
| 897 | if $cell->heading; | ||||||||||||||||||||||||||||
| 898 | |||||||||||||||||||||||||||||
| 899 | defined $cellstyle{$_} and $text->apply_tag( 0, -1, $_ => $cellstyle{$_} ) | ||||||||||||||||||||||||||||
| 900 | for keys %cellstyle; | ||||||||||||||||||||||||||||
| 901 | |||||||||||||||||||||||||||||
| 902 | $text = $text->clone( convert_tags => \%convert_tags ); | ||||||||||||||||||||||||||||
| 903 | |||||||||||||||||||||||||||||
| 904 | $rb->goto( 1 + 2 * $rowidx, $_colstart[$colidx] ); | ||||||||||||||||||||||||||||
| 905 | |||||||||||||||||||||||||||||
| 906 | $rb->erase( 1 ); | ||||||||||||||||||||||||||||
| 907 | my $align = $cell->align; | ||||||||||||||||||||||||||||
| 908 | if( $align ne "left" ) { | ||||||||||||||||||||||||||||
| 909 | my $spare = $_colwidth[$colidx] - textwidth $text; | ||||||||||||||||||||||||||||
| 910 | $spare /= 2 if $align eq "centre"; | ||||||||||||||||||||||||||||
| 911 | $rb->erase( $spare ); | ||||||||||||||||||||||||||||
| 912 | } | ||||||||||||||||||||||||||||
| 913 | |||||||||||||||||||||||||||||
| 914 | $text->iter_substr_nooverlap( | ||||||||||||||||||||||||||||
| 915 | sub ( $substr, %tags ) | ||||||||||||||||||||||||||||
| 916 | { | ||||||||||||||||||||||||||||
| 917 | if( exists $tags{highlight} ) { | ||||||||||||||||||||||||||||
| 918 | %tags = ( %tags, @HIGHLIGHT_PEN ); | ||||||||||||||||||||||||||||
| 919 | %tags = ( %tags, @SELECT_PEN ) if $tags{highlight}->$*; | ||||||||||||||||||||||||||||
| 920 | } | ||||||||||||||||||||||||||||
| 921 | my $pen = Tickit::Pen::Immutable->new_from_attrs( \%tags ); | ||||||||||||||||||||||||||||
| 922 | $rb->text( $substr, $pen ); | ||||||||||||||||||||||||||||
| 923 | }, | ||||||||||||||||||||||||||||
| 924 | ); | ||||||||||||||||||||||||||||
| 925 | } | ||||||||||||||||||||||||||||
| 926 | } | ||||||||||||||||||||||||||||
| 927 | } | ||||||||||||||||||||||||||||
| 928 | |||||||||||||||||||||||||||||
| 929 | method apply_highlight ( $re ) | ||||||||||||||||||||||||||||
| 930 | { | ||||||||||||||||||||||||||||
| 931 | foreach my $row ( @_rows ) { | ||||||||||||||||||||||||||||
| 932 | foreach my $cell ( @$row ) { | ||||||||||||||||||||||||||||
| 933 | my $text = $cell->text; | ||||||||||||||||||||||||||||
| 934 | $text->iter_extents( | ||||||||||||||||||||||||||||
| 935 | sub ( $e, $t, $v ) { | ||||||||||||||||||||||||||||
| 936 | $text->delete_tag( $e, $t ); | ||||||||||||||||||||||||||||
| 937 | }, | ||||||||||||||||||||||||||||
| 938 | only => [qw( highlight )], | ||||||||||||||||||||||||||||
| 939 | ); | ||||||||||||||||||||||||||||
| 940 | } | ||||||||||||||||||||||||||||
| 941 | } | ||||||||||||||||||||||||||||
| 942 | |||||||||||||||||||||||||||||
| 943 | return unless defined $re; | ||||||||||||||||||||||||||||
| 944 | |||||||||||||||||||||||||||||
| 945 | my @ret; | ||||||||||||||||||||||||||||
| 946 | |||||||||||||||||||||||||||||
| 947 | foreach my $rowidx ( 0 .. $#_rows ) { | ||||||||||||||||||||||||||||
| 948 | foreach my $colidx ( 0 .. $#{ $_rows[$rowidx] } ) { | ||||||||||||||||||||||||||||
| 949 | my $text = $_rows[$rowidx][$colidx]->text; | ||||||||||||||||||||||||||||
| 950 | foreach my $e ( $text->match_extents( $re ) ) { | ||||||||||||||||||||||||||||
| 951 | push @ret, [ $self, [ $rowidx, $colidx, $e ], 0 ]; | ||||||||||||||||||||||||||||
| 952 | $text->apply_tag( $e, highlight => \$ret[-1][2] ); | ||||||||||||||||||||||||||||
| 953 | } | ||||||||||||||||||||||||||||
| 954 | } | ||||||||||||||||||||||||||||
| 955 | } | ||||||||||||||||||||||||||||
| 956 | |||||||||||||||||||||||||||||
| 957 | return @ret; | ||||||||||||||||||||||||||||
| 958 | } | ||||||||||||||||||||||||||||
| 959 | } | ||||||||||||||||||||||||||||
| 960 | |||||||||||||||||||||||||||||
| 961 | class App::sdview::Output::Tickit::_OutlineTree | ||||||||||||||||||||||||||||
| 962 | :isa(Tickit::Widget) | ||||||||||||||||||||||||||||
| 963 | { | ||||||||||||||||||||||||||||
| 964 | use constant WIDGET_PEN_FROM_STYLE => 1; | ||||||||||||||||||||||||||||
| 965 | use constant CAN_FOCUS => 1; | ||||||||||||||||||||||||||||
| 966 | |||||||||||||||||||||||||||||
| 967 | use Tickit::Style; | ||||||||||||||||||||||||||||
| 968 | style_definition base => | ||||||||||||||||||||||||||||
| 969 | bg => "blue", | ||||||||||||||||||||||||||||
| 970 | |||||||||||||||||||||||||||||
| 971 | selected_b => 1, | ||||||||||||||||||||||||||||
| 972 | |||||||||||||||||||||||||||||
| 973 | highlighted_fg => "black", | ||||||||||||||||||||||||||||
| 974 | highlighted_bg => "yellow"; | ||||||||||||||||||||||||||||
| 975 | |||||||||||||||||||||||||||||
| 976 | class App::sdview::Output::Tickit::_OutlineTree::Item { | ||||||||||||||||||||||||||||
| 977 | field $label :reader :param; | ||||||||||||||||||||||||||||
| 978 | field $indent :reader :param; | ||||||||||||||||||||||||||||
| 979 | field $itemidx :reader :param; | ||||||||||||||||||||||||||||
| 980 | } | ||||||||||||||||||||||||||||
| 981 | |||||||||||||||||||||||||||||
| 982 | field @all_items; | ||||||||||||||||||||||||||||
| 983 | |||||||||||||||||||||||||||||
| 984 | field $filter = ""; | ||||||||||||||||||||||||||||
| 985 | |||||||||||||||||||||||||||||
| 986 | field @displayed_items; | ||||||||||||||||||||||||||||
| 987 | field $scrolloff = 0; | ||||||||||||||||||||||||||||
| 988 | |||||||||||||||||||||||||||||
| 989 | method _normalize_scrolloff | ||||||||||||||||||||||||||||
| 990 | { | ||||||||||||||||||||||||||||
| 991 | $scrolloff = 0 if $scrolloff < 0; | ||||||||||||||||||||||||||||
| 992 | |||||||||||||||||||||||||||||
| 993 | my $lines = $self->window->lines; | ||||||||||||||||||||||||||||
| 994 | my $maxoff = scalar @displayed_items - $lines; | ||||||||||||||||||||||||||||
| 995 | $maxoff = 0 if $maxoff < 0; | ||||||||||||||||||||||||||||
| 996 | |||||||||||||||||||||||||||||
| 997 | $scrolloff = $maxoff if $scrolloff > $maxoff; | ||||||||||||||||||||||||||||
| 998 | } | ||||||||||||||||||||||||||||
| 999 | |||||||||||||||||||||||||||||
| 1000 | method resized { $self->_normalize_scrolloff } | ||||||||||||||||||||||||||||
| 1001 | |||||||||||||||||||||||||||||
| 1002 | field $on_select_item :param :writer = undef; | ||||||||||||||||||||||||||||
| 1003 | |||||||||||||||||||||||||||||
| 1004 | field $current; | ||||||||||||||||||||||||||||
| 1005 | |||||||||||||||||||||||||||||
| 1006 | method lines { return scalar @displayed_items } | ||||||||||||||||||||||||||||
| 1007 | method cols { 1 } | ||||||||||||||||||||||||||||
| 1008 | |||||||||||||||||||||||||||||
| 1009 | method add_item ( $label, $level, $itemidx ) | ||||||||||||||||||||||||||||
| 1010 | { | ||||||||||||||||||||||||||||
| 1011 | push @all_items, App::sdview::Output::Tickit::_OutlineTree::Item->new( | ||||||||||||||||||||||||||||
| 1012 | label => $label, | ||||||||||||||||||||||||||||
| 1013 | indent => ($level-1) * 2 + 1, | ||||||||||||||||||||||||||||
| 1014 | itemidx => $itemidx, | ||||||||||||||||||||||||||||
| 1015 | ); | ||||||||||||||||||||||||||||
| 1016 | push @displayed_items, $all_items[-1]; | ||||||||||||||||||||||||||||
| 1017 | } | ||||||||||||||||||||||||||||
| 1018 | |||||||||||||||||||||||||||||
| 1019 | method _update_filter | ||||||||||||||||||||||||||||
| 1020 | { | ||||||||||||||||||||||||||||
| 1021 | if( length $filter ) { | ||||||||||||||||||||||||||||
| 1022 | my $re = qr/\Q$filter/i; | ||||||||||||||||||||||||||||
| 1023 | @displayed_items = grep { $_->label =~ $re } @all_items; | ||||||||||||||||||||||||||||
| 1024 | } | ||||||||||||||||||||||||||||
| 1025 | else { | ||||||||||||||||||||||||||||
| 1026 | @displayed_items = @all_items; | ||||||||||||||||||||||||||||
| 1027 | } | ||||||||||||||||||||||||||||
| 1028 | $self->redraw; | ||||||||||||||||||||||||||||
| 1029 | } | ||||||||||||||||||||||||||||
| 1030 | |||||||||||||||||||||||||||||
| 1031 | method set_current_itemidx ( $itemidx ) | ||||||||||||||||||||||||||||
| 1032 | { | ||||||||||||||||||||||||||||
| 1033 | # TODO: binary search would be faster | ||||||||||||||||||||||||||||
| 1034 | foreach my $i ( reverse 0 .. $#all_items ) { | ||||||||||||||||||||||||||||
| 1035 | my $item = $all_items[$i]; | ||||||||||||||||||||||||||||
| 1036 | next if $item->itemidx > $itemidx; | ||||||||||||||||||||||||||||
| 1037 | |||||||||||||||||||||||||||||
| 1038 | $current = $i; | ||||||||||||||||||||||||||||
| 1039 | last; | ||||||||||||||||||||||||||||
| 1040 | } | ||||||||||||||||||||||||||||
| 1041 | |||||||||||||||||||||||||||||
| 1042 | return unless my $win = $self->window; | ||||||||||||||||||||||||||||
| 1043 | my $bottom = $win->lines - 1; | ||||||||||||||||||||||||||||
| 1044 | if( $current < $scrolloff ) { | ||||||||||||||||||||||||||||
| 1045 | $scrolloff = $current; | ||||||||||||||||||||||||||||
| 1046 | } | ||||||||||||||||||||||||||||
| 1047 | elsif( $current > $scrolloff + $bottom ) { | ||||||||||||||||||||||||||||
| 1048 | $scrolloff = $current - $bottom; | ||||||||||||||||||||||||||||
| 1049 | } | ||||||||||||||||||||||||||||
| 1050 | |||||||||||||||||||||||||||||
| 1051 | $self->redraw if $self->window && $self->window->is_visible; | ||||||||||||||||||||||||||||
| 1052 | } | ||||||||||||||||||||||||||||
| 1053 | |||||||||||||||||||||||||||||
| 1054 | method current_item () | ||||||||||||||||||||||||||||
| 1055 | { | ||||||||||||||||||||||||||||
| 1056 | return $all_items[$current]; | ||||||||||||||||||||||||||||
| 1057 | } | ||||||||||||||||||||||||||||
| 1058 | |||||||||||||||||||||||||||||
| 1059 | method next_item ( $dir ) | ||||||||||||||||||||||||||||
| 1060 | { | ||||||||||||||||||||||||||||
| 1061 | $dir = ( $dir > 0 ) ? 1 : -1; | ||||||||||||||||||||||||||||
| 1062 | |||||||||||||||||||||||||||||
| 1063 | my $next = $current + $dir; | ||||||||||||||||||||||||||||
| 1064 | return if $next < 0 or $next > $#all_items; | ||||||||||||||||||||||||||||
| 1065 | |||||||||||||||||||||||||||||
| 1066 | return $all_items[$next]; | ||||||||||||||||||||||||||||
| 1067 | } | ||||||||||||||||||||||||||||
| 1068 | |||||||||||||||||||||||||||||
| 1069 | method render_to_rb ( $rb, $rect ) | ||||||||||||||||||||||||||||
| 1070 | { | ||||||||||||||||||||||||||||
| 1071 | $rb->eraserect( $rect ); | ||||||||||||||||||||||||||||
| 1072 | |||||||||||||||||||||||||||||
| 1073 | my $re = length $filter ? qr/\Q$filter/i : undef; | ||||||||||||||||||||||||||||
| 1074 | |||||||||||||||||||||||||||||
| 1075 | foreach my $line ( $rect->top .. $rect->bottom - 2 ) { | ||||||||||||||||||||||||||||
| 1076 | ($line + $scrolloff) < @displayed_items or last; | ||||||||||||||||||||||||||||
| 1077 | my $item = $displayed_items[$line + $scrolloff]; | ||||||||||||||||||||||||||||
| 1078 | |||||||||||||||||||||||||||||
| 1079 | $rb->goto( $line, 0 ); | ||||||||||||||||||||||||||||
| 1080 | $rb->savepen; | ||||||||||||||||||||||||||||
| 1081 | |||||||||||||||||||||||||||||
| 1082 | if( ( $line + $scrolloff ) == $current ) { | ||||||||||||||||||||||||||||
| 1083 | $rb->setpen( $self->get_style_pen( 'selected' ) ); | ||||||||||||||||||||||||||||
| 1084 | $rb->text( ">" ); | ||||||||||||||||||||||||||||
| 1085 | } | ||||||||||||||||||||||||||||
| 1086 | $rb->erase_to( $item->indent ); | ||||||||||||||||||||||||||||
| 1087 | |||||||||||||||||||||||||||||
| 1088 | my $label = $item->label; | ||||||||||||||||||||||||||||
| 1089 | if( defined $re ) { | ||||||||||||||||||||||||||||
| 1090 | my $oldpos = 0; | ||||||||||||||||||||||||||||
| 1091 | while( $label =~ m/$re/gc ) { | ||||||||||||||||||||||||||||
| 1092 | $rb->text( substr $label, $oldpos, $-[0]-$oldpos ) if $-[0] > $oldpos; | ||||||||||||||||||||||||||||
| 1093 | |||||||||||||||||||||||||||||
| 1094 | $rb->savepen; | ||||||||||||||||||||||||||||
| 1095 | $rb->setpen( $self->get_style_pen( 'highlighted' ) ); | ||||||||||||||||||||||||||||
| 1096 | $rb->text( substr $label, $-[0], $+[0]-$-[0] ); # TODO: highlight pen | ||||||||||||||||||||||||||||
| 1097 | $rb->restore; | ||||||||||||||||||||||||||||
| 1098 | |||||||||||||||||||||||||||||
| 1099 | $oldpos = $+[0]; | ||||||||||||||||||||||||||||
| 1100 | } | ||||||||||||||||||||||||||||
| 1101 | $rb->text( substr $label, $oldpos ) if $oldpos < length $label; | ||||||||||||||||||||||||||||
| 1102 | } | ||||||||||||||||||||||||||||
| 1103 | else { | ||||||||||||||||||||||||||||
| 1104 | $rb->text( $label ); | ||||||||||||||||||||||||||||
| 1105 | } | ||||||||||||||||||||||||||||
| 1106 | |||||||||||||||||||||||||||||
| 1107 | $rb->restore; | ||||||||||||||||||||||||||||
| 1108 | } | ||||||||||||||||||||||||||||
| 1109 | |||||||||||||||||||||||||||||
| 1110 | $rb->goto( $rect->bottom - 1, 0 ); | ||||||||||||||||||||||||||||
| 1111 | if( length $filter ) { | ||||||||||||||||||||||||||||
| 1112 | $rb->text( "Search: " . $filter ); | ||||||||||||||||||||||||||||
| 1113 | } | ||||||||||||||||||||||||||||
| 1114 | else { | ||||||||||||||||||||||||||||
| 1115 | $rb->savepen; | ||||||||||||||||||||||||||||
| 1116 | $rb->setpen( Tickit::Pen::Immutable->new( i => 1 ) ); | ||||||||||||||||||||||||||||
| 1117 | $rb->text( "Type to search" ); | ||||||||||||||||||||||||||||
| 1118 | $rb->restore; | ||||||||||||||||||||||||||||
| 1119 | } | ||||||||||||||||||||||||||||
| 1120 | |||||||||||||||||||||||||||||
| 1121 | $rb->vline_at( $rect->top, $rect->bottom, $rect->right-1, | ||||||||||||||||||||||||||||
| 1122 | Tickit::RenderBuffer::LINE_SINGLE, undef, Tickit::RenderBuffer::CAP_BOTH ); | ||||||||||||||||||||||||||||
| 1123 | } | ||||||||||||||||||||||||||||
| 1124 | |||||||||||||||||||||||||||||
| 1125 | method on_key ( $ev ) | ||||||||||||||||||||||||||||
| 1126 | { | ||||||||||||||||||||||||||||
| 1127 | if( $ev->type eq "text" ) { | ||||||||||||||||||||||||||||
| 1128 | $filter .= $ev->str; | ||||||||||||||||||||||||||||
| 1129 | $self->_update_filter; | ||||||||||||||||||||||||||||
| 1130 | return 1; | ||||||||||||||||||||||||||||
| 1131 | } | ||||||||||||||||||||||||||||
| 1132 | |||||||||||||||||||||||||||||
| 1133 | my $key = $ev->str; | ||||||||||||||||||||||||||||
| 1134 | if( $key eq "Backspace" ) { | ||||||||||||||||||||||||||||
| 1135 | substr( $filter, -1, 1 ) = "" if length $filter; | ||||||||||||||||||||||||||||
| 1136 | $self->_update_filter; | ||||||||||||||||||||||||||||
| 1137 | } | ||||||||||||||||||||||||||||
| 1138 | elsif( $key eq "Enter" ) { | ||||||||||||||||||||||||||||
| 1139 | my $item = $displayed_items[0]; | ||||||||||||||||||||||||||||
| 1140 | $filter = ""; | ||||||||||||||||||||||||||||
| 1141 | $self->_update_filter; | ||||||||||||||||||||||||||||
| 1142 | $on_select_item->( itemidx => $item->itemidx ); | ||||||||||||||||||||||||||||
| 1143 | } | ||||||||||||||||||||||||||||
| 1144 | else { | ||||||||||||||||||||||||||||
| 1145 | return 0; | ||||||||||||||||||||||||||||
| 1146 | } | ||||||||||||||||||||||||||||
| 1147 | |||||||||||||||||||||||||||||
| 1148 | return 1; | ||||||||||||||||||||||||||||
| 1149 | } | ||||||||||||||||||||||||||||
| 1150 | |||||||||||||||||||||||||||||
| 1151 | method on_mouse ( $ev ) | ||||||||||||||||||||||||||||
| 1152 | { | ||||||||||||||||||||||||||||
| 1153 | if( $ev->type eq "wheel" ) { | ||||||||||||||||||||||||||||
| 1154 | $scrolloff += 5 if $ev->button eq "down"; | ||||||||||||||||||||||||||||
| 1155 | $scrolloff -= 5 if $ev->button eq "up"; | ||||||||||||||||||||||||||||
| 1156 | $self->_normalize_scrolloff; | ||||||||||||||||||||||||||||
| 1157 | $self->redraw; | ||||||||||||||||||||||||||||
| 1158 | } | ||||||||||||||||||||||||||||
| 1159 | elsif( $ev->type eq "press" && $ev->button == 1 ) { | ||||||||||||||||||||||||||||
| 1160 | if( my $item = $displayed_items[$ev->line + $scrolloff] ) { | ||||||||||||||||||||||||||||
| 1161 | $on_select_item->( itemidx => $item->itemidx ); | ||||||||||||||||||||||||||||
| 1162 | } | ||||||||||||||||||||||||||||
| 1163 | } | ||||||||||||||||||||||||||||
| 1164 | |||||||||||||||||||||||||||||
| 1165 | return 1; | ||||||||||||||||||||||||||||
| 1166 | } | ||||||||||||||||||||||||||||
| 1167 | } | ||||||||||||||||||||||||||||
| 1168 | |||||||||||||||||||||||||||||
| 1169 | class App::sdview::Output::Tickit::_SearchBox | ||||||||||||||||||||||||||||
| 1170 | :isa(Tickit::Widget) | ||||||||||||||||||||||||||||
| 1171 | { | ||||||||||||||||||||||||||||
| 1172 | use constant WIDGET_PEN_FROM_STYLE => 1; | ||||||||||||||||||||||||||||
| 1173 | |||||||||||||||||||||||||||||
| 1174 | use constant CAN_FOCUS => 1; | ||||||||||||||||||||||||||||
| 1175 | |||||||||||||||||||||||||||||
| 1176 | use Tickit::Style; | ||||||||||||||||||||||||||||
| 1177 | style_definition base => | ||||||||||||||||||||||||||||
| 1178 | bg => "grey", | ||||||||||||||||||||||||||||
| 1179 | fg => 16, # colour 16 is black but doesn't become grey on bold | ||||||||||||||||||||||||||||
| 1180 | b => 1, | ||||||||||||||||||||||||||||
| 1181 | |||||||||||||||||||||||||||||
| 1182 | bad_fg => 1; | ||||||||||||||||||||||||||||
| 1183 | |||||||||||||||||||||||||||||
| 1184 | method lines { 1 } | ||||||||||||||||||||||||||||
| 1185 | method cols { 1 } | ||||||||||||||||||||||||||||
| 1186 | |||||||||||||||||||||||||||||
| 1187 | field $float :writer; | ||||||||||||||||||||||||||||
| 1188 | |||||||||||||||||||||||||||||
| 1189 | field $leader = "Search: "; | ||||||||||||||||||||||||||||
| 1190 | field $text = ""; | ||||||||||||||||||||||||||||
| 1191 | |||||||||||||||||||||||||||||
| 1192 | field $is_ignorecase; | ||||||||||||||||||||||||||||
| 1193 | field $is_wholeword; | ||||||||||||||||||||||||||||
| 1194 | |||||||||||||||||||||||||||||
| 1195 | field $ok = 1; | ||||||||||||||||||||||||||||
| 1196 | |||||||||||||||||||||||||||||
| 1197 | field $matchcount = 0; | ||||||||||||||||||||||||||||
| 1198 | method set_matchcount ( $_count ) { $matchcount = $_count; $self->redraw; } | ||||||||||||||||||||||||||||
| 1199 | |||||||||||||||||||||||||||||
| 1200 | field $searchre; | ||||||||||||||||||||||||||||
| 1201 | |||||||||||||||||||||||||||||
| 1202 | field $on_incremental :param; | ||||||||||||||||||||||||||||
| 1203 | field $on_enter :param; | ||||||||||||||||||||||||||||
| 1204 | |||||||||||||||||||||||||||||
| 1205 | method show () | ||||||||||||||||||||||||||||
| 1206 | { | ||||||||||||||||||||||||||||
| 1207 | $text = ""; | ||||||||||||||||||||||||||||
| 1208 | $float->show; | ||||||||||||||||||||||||||||
| 1209 | $self->window->cursor_at( 0, length($leader) ); | ||||||||||||||||||||||||||||
| 1210 | $self->take_focus; | ||||||||||||||||||||||||||||
| 1211 | } | ||||||||||||||||||||||||||||
| 1212 | |||||||||||||||||||||||||||||
| 1213 | method dismiss () | ||||||||||||||||||||||||||||
| 1214 | { | ||||||||||||||||||||||||||||
| 1215 | $float->hide; | ||||||||||||||||||||||||||||
| 1216 | } | ||||||||||||||||||||||||||||
| 1217 | |||||||||||||||||||||||||||||
| 1218 | method render_to_rb ( $rb, $rect ) | ||||||||||||||||||||||||||||
| 1219 | { | ||||||||||||||||||||||||||||
| 1220 | $rb->eraserect( $rect ); | ||||||||||||||||||||||||||||
| 1221 | |||||||||||||||||||||||||||||
| 1222 | $rb->goto( 0, 0 ); | ||||||||||||||||||||||||||||
| 1223 | $rb->text( $leader ); | ||||||||||||||||||||||||||||
| 1224 | |||||||||||||||||||||||||||||
| 1225 | $rb->text( $text, $ok ? undef : $self->get_style_pen( "bad" ) ); | ||||||||||||||||||||||||||||
| 1226 | $self->window->cursor_at( 0, length($leader) + length($text) ); | ||||||||||||||||||||||||||||
| 1227 | |||||||||||||||||||||||||||||
| 1228 | my $counttext = sprintf " (%d)", $matchcount; | ||||||||||||||||||||||||||||
| 1229 | if( $is_ignorecase ) { | ||||||||||||||||||||||||||||
| 1230 | $rb->goto( 0, $self->window->right - 2 - length $counttext ); | ||||||||||||||||||||||||||||
| 1231 | $rb->text( "/i" ); | ||||||||||||||||||||||||||||
| 1232 | } | ||||||||||||||||||||||||||||
| 1233 | if( $is_wholeword ) { | ||||||||||||||||||||||||||||
| 1234 | $rb->goto( 0, $self->window->right - 4 - length $counttext ); | ||||||||||||||||||||||||||||
| 1235 | $rb->text( "W" ); | ||||||||||||||||||||||||||||
| 1236 | } | ||||||||||||||||||||||||||||
| 1237 | $rb->goto( 0, $self->window->right - length $counttext ); | ||||||||||||||||||||||||||||
| 1238 | $rb->text( $counttext ); | ||||||||||||||||||||||||||||
| 1239 | } | ||||||||||||||||||||||||||||
| 1240 | |||||||||||||||||||||||||||||
| 1241 | method on_key ( $ev ) | ||||||||||||||||||||||||||||
| 1242 | { | ||||||||||||||||||||||||||||
| 1243 | if( $ev->type eq "text" ) { | ||||||||||||||||||||||||||||
| 1244 | $text .= $ev->str; | ||||||||||||||||||||||||||||
| 1245 | $self->_update_pattern; | ||||||||||||||||||||||||||||
| 1246 | $self->redraw; | ||||||||||||||||||||||||||||
| 1247 | return 1; | ||||||||||||||||||||||||||||
| 1248 | } | ||||||||||||||||||||||||||||
| 1249 | |||||||||||||||||||||||||||||
| 1250 | my $key = $ev->str; | ||||||||||||||||||||||||||||
| 1251 | if( $key eq "Backspace" ) { | ||||||||||||||||||||||||||||
| 1252 | substr( $text, -1, 1 ) = ""; | ||||||||||||||||||||||||||||
| 1253 | $self->_update_pattern; | ||||||||||||||||||||||||||||
| 1254 | $self->redraw; | ||||||||||||||||||||||||||||
| 1255 | } | ||||||||||||||||||||||||||||
| 1256 | elsif( $key eq "Escape" ) { | ||||||||||||||||||||||||||||
| 1257 | undef $text; | ||||||||||||||||||||||||||||
| 1258 | $self->_update_pattern; | ||||||||||||||||||||||||||||
| 1259 | $self->dismiss; | ||||||||||||||||||||||||||||
| 1260 | } | ||||||||||||||||||||||||||||
| 1261 | elsif( $key eq "Enter" ) { | ||||||||||||||||||||||||||||
| 1262 | $on_enter->( $self, $searchre ) if $on_enter; | ||||||||||||||||||||||||||||
| 1263 | $self->dismiss; | ||||||||||||||||||||||||||||
| 1264 | } | ||||||||||||||||||||||||||||
| 1265 | elsif( $key eq "M-i" ) { | ||||||||||||||||||||||||||||
| 1266 | $is_ignorecase = !$is_ignorecase; | ||||||||||||||||||||||||||||
| 1267 | $self->_update_pattern; | ||||||||||||||||||||||||||||
| 1268 | $self->redraw; | ||||||||||||||||||||||||||||
| 1269 | } | ||||||||||||||||||||||||||||
| 1270 | elsif( $key eq "M-w" ) { | ||||||||||||||||||||||||||||
| 1271 | $is_wholeword = !$is_wholeword; | ||||||||||||||||||||||||||||
| 1272 | $self->_update_pattern; | ||||||||||||||||||||||||||||
| 1273 | $self->redraw; | ||||||||||||||||||||||||||||
| 1274 | } | ||||||||||||||||||||||||||||
| 1275 | else { | ||||||||||||||||||||||||||||
| 1276 | return 0; | ||||||||||||||||||||||||||||
| 1277 | } | ||||||||||||||||||||||||||||
| 1278 | |||||||||||||||||||||||||||||
| 1279 | return 1; | ||||||||||||||||||||||||||||
| 1280 | } | ||||||||||||||||||||||||||||
| 1281 | |||||||||||||||||||||||||||||
| 1282 | method _update_pattern () | ||||||||||||||||||||||||||||
| 1283 | { | ||||||||||||||||||||||||||||
| 1284 | my $patternok = 0; | ||||||||||||||||||||||||||||
| 1285 | if( defined $text and length $text ) { | ||||||||||||||||||||||||||||
| 1286 | my $flags = ""; | ||||||||||||||||||||||||||||
| 1287 | $flags .= "i" if $is_ignorecase; | ||||||||||||||||||||||||||||
| 1288 | my $re = "(?$flags:$text)"; | ||||||||||||||||||||||||||||
| 1289 | $re = "\\b$re\\b" if $is_wholeword; | ||||||||||||||||||||||||||||
| 1290 | |||||||||||||||||||||||||||||
| 1291 | # Compiling the pattern might not succeed; if not just keep the previous | ||||||||||||||||||||||||||||
| 1292 | eval { $searchre = qr/$re/; $patternok = 1 }; | ||||||||||||||||||||||||||||
| 1293 | } | ||||||||||||||||||||||||||||
| 1294 | |||||||||||||||||||||||||||||
| 1295 | $self->redraw if $patternok != $ok; $ok = $patternok; | ||||||||||||||||||||||||||||
| 1296 | |||||||||||||||||||||||||||||
| 1297 | $on_incremental->( $self, $searchre ) if $on_incremental; | ||||||||||||||||||||||||||||
| 1298 | } | ||||||||||||||||||||||||||||
| 1299 | } | ||||||||||||||||||||||||||||
| 1300 | |||||||||||||||||||||||||||||
| 1301 | =head1 TODO | ||||||||||||||||||||||||||||
| 1302 | |||||||||||||||||||||||||||||
| 1303 | =over 4 | ||||||||||||||||||||||||||||
| 1304 | |||||||||||||||||||||||||||||
| 1305 | =item * | ||||||||||||||||||||||||||||
| 1306 | |||||||||||||||||||||||||||||
| 1307 | Line-editing and history in the C</> search entry box. | ||||||||||||||||||||||||||||
| 1308 | |||||||||||||||||||||||||||||
| 1309 | =item * | ||||||||||||||||||||||||||||
| 1310 | |||||||||||||||||||||||||||||
| 1311 | Follow links, possibly internally implementing a history mechanism or | ||||||||||||||||||||||||||||
| 1312 | multiple pages. | ||||||||||||||||||||||||||||
| 1313 | |||||||||||||||||||||||||||||
| 1314 | =back | ||||||||||||||||||||||||||||
| 1315 | |||||||||||||||||||||||||||||
| 1316 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 1317 | |||||||||||||||||||||||||||||
| 1318 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||||||||||
| 1319 | |||||||||||||||||||||||||||||
| 1320 | =cut | ||||||||||||||||||||||||||||
| 1321 | |||||||||||||||||||||||||||||
| 1322 | 0x55AA; | ||||||||||||||||||||||||||||