| 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, 2011-2023 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | 1 | ▶ | use v5.26; # signatures |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use Object::Pad 0.800 ':experimental(adjust_params)'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | package Tickit::Console 0.12; | ||
| 11 | class Tickit::Console | ||
| 12 | 0 | ▶ | :isa(Tickit::Widget::VBox); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | |||
| 14 | use Tickit::Widget::Entry; | ||
| 15 | use Tickit::Widget::Scroller 0.04; | ||
| 16 | use Tickit::Widget::Tabbed 0.003; | ||
| 17 | |||
| 18 | use Tickit::Console::Tab; | ||
| 19 | |||
| 20 | use Scalar::Util qw( weaken ); | ||
| 21 | |||
| 22 | =head1 NAME | ||
| 23 | |||
| 24 | C<Tickit::Console> - build full-screen console-style applications | ||
| 25 | |||
| 26 | =head1 SYNOPSIS | ||
| 27 | |||
| 28 | my $console = Tickit::Console->new; | ||
| 29 | |||
| 30 | Tickit->new( root => $console )->run; | ||
| 31 | |||
| 32 | =head1 DESCRIPTION | ||
| 33 | |||
| 34 | A C<Tickit::Console> instance is a subclass of L<Tickit::Widget::VBox> | ||
| 35 | intended to help building a full-screen console-style application which | ||
| 36 | presents the user with one or more scrollable text areas, selectable as tabs | ||
| 37 | on a ribbon, with a text entry area at the bottom of the screen for entering | ||
| 38 | commands or other data. As a L<Tickit::Widget> subclass it can be added | ||
| 39 | anywhere within a widget tree, though normally it would be used as the root | ||
| 40 | widget for a L<Tickit> instance. | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | =head1 CONSTRUCTOR | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | =head2 new | ||
| 49 | |||
| 50 | $console = Tickit::Console->new( %args ); | ||
| 51 | |||
| 52 | Returns a new instance of a C<Tickit::Console>. Takes the following named | ||
| 53 | arguments: | ||
| 54 | |||
| 55 | =over 8 | ||
| 56 | |||
| 57 | =item on_line => CODE | ||
| 58 | |||
| 59 | Callback to invoke when a line of text is entered in the entry widget. | ||
| 60 | |||
| 61 | $on_line->( $active_tab, $text ); | ||
| 62 | |||
| 63 | =item tab_class => STRING | ||
| 64 | |||
| 65 | Optional. If set, gives a class name (which should be a subclass of | ||
| 66 | L<Tickit::Console::Tab>) to construct newly-added tabs with. This setting | ||
| 67 | allows an application to provide new methods in tabs to change behaviours. | ||
| 68 | |||
| 69 | =item timestamp_format, datestamp_format | ||
| 70 | |||
| 71 | Optional. If supplied, these will be stored as default values to pass to the | ||
| 72 | tab constructor in the C<add_tab> method. | ||
| 73 | |||
| 74 | =back | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | field %_default_tab_opts; | ||
| 79 | field $_tabbed; | ||
| 80 | field $_entry; | ||
| 81 | |||
| 82 | ADJUST :params ( | ||
| 83 | :$on_line = undef, | ||
| 84 | :$timestamp_format = undef, | ||
| 85 | :$datestamp_format = undef, | ||
| 86 | :$tab_class = "Tickit::Console::Tab", | ||
| 87 | ) { | ||
| 88 | $_default_tab_opts{timestamp_format} = $timestamp_format; | ||
| 89 | $_default_tab_opts{datestamp_format} = $datestamp_format; | ||
| 90 | |||
| 91 | $self->add( | ||
| 92 | $_tabbed = Tickit::Widget::Tabbed->new( | ||
| 93 | tab_position => "bottom", | ||
| 94 | tab_class => $tab_class, | ||
| 95 | ), | ||
| 96 | expand => 1, | ||
| 97 | ); | ||
| 98 | |||
| 99 | $self->add( | ||
| 100 | $_entry = Tickit::Widget::Entry->new | ||
| 101 | ); | ||
| 102 | |||
| 103 | weaken( my $weakself = $self ); | ||
| 104 | $_entry->set_on_enter( sub ( $entry, @ ) { | ||
| 105 | return unless $weakself; | ||
| 106 | my $line = $entry->text; | ||
| 107 | $entry->set_text( "" ); | ||
| 108 | |||
| 109 | my $tab = $weakself->active_tab; | ||
| 110 | $tab->_on_line( $line ) or | ||
| 111 | $on_line->( $tab, $line ); | ||
| 112 | } ); | ||
| 113 | } | ||
| 114 | |||
| 115 | =head1 METHODS | ||
| 116 | |||
| 117 | =cut | ||
| 118 | |||
| 119 | =head2 add_tab | ||
| 120 | |||
| 121 | $tab = $console->add_tab( %args ); | ||
| 122 | |||
| 123 | Adds a new tab to the console, and returns a L<Tickit::Console::Tab> object | ||
| 124 | representing it. | ||
| 125 | |||
| 126 | Takes the following named arguments: | ||
| 127 | |||
| 128 | =over 8 | ||
| 129 | |||
| 130 | =item name => STRING | ||
| 131 | |||
| 132 | Name for the tab. | ||
| 133 | |||
| 134 | =item on_line => CODE | ||
| 135 | |||
| 136 | Optional. Provides a different callback to invoke when a line of text is | ||
| 137 | entered while this tab is active. Invoked the same way as above. | ||
| 138 | |||
| 139 | =item make_widget => CODE | ||
| 140 | |||
| 141 | Optional. Gives a piece of code used to construct the actual L<Tickit::Widget> | ||
| 142 | used as this tab's child in the ribbon. A C<Tickit::Widget::Scroller> to hold | ||
| 143 | the tab's content will be passed in to this code, which should construct some | ||
| 144 | sort of widget tree with that inside it, and return it. This can be used to | ||
| 145 | apply a decorative frame, place the scroller in a split box or other layout | ||
| 146 | along with other widgets, or various other effects. | ||
| 147 | |||
| 148 | $tab_widget = $make_widget->( $scroller ); | ||
| 149 | |||
| 150 | =back | ||
| 151 | |||
| 152 | Any other named arguments are passed to the tab's constructor. | ||
| 153 | |||
| 154 | =cut | ||
| 155 | |||
| 156 | method add_tab ( %args ) | ||
| 157 | { | ||
| 158 | my $make_widget = delete $args{make_widget}; | ||
| 159 | |||
| 160 | my $scroller = Tickit::Widget::Scroller->new( gravity => "bottom" ); | ||
| 161 | |||
| 162 | my $widget = $make_widget ? $make_widget->( $scroller ) : $scroller; | ||
| 163 | |||
| 164 | my $tab = $_tabbed->add_tab( | ||
| 165 | $widget, | ||
| 166 | label => delete $args{name}, | ||
| 167 | console => $self, | ||
| 168 | scroller => $scroller, | ||
| 169 | %_default_tab_opts, | ||
| 170 | %args, | ||
| 171 | ); | ||
| 172 | |||
| 173 | return $tab; | ||
| 174 | } | ||
| 175 | |||
| 176 | =head2 active_tab_index | ||
| 177 | |||
| 178 | $index = $console->active_tab_index; | ||
| 179 | |||
| 180 | =head2 active_tab | ||
| 181 | |||
| 182 | $tab = $console->active_tab; | ||
| 183 | |||
| 184 | =head2 remove_tab | ||
| 185 | |||
| 186 | $console->remove_tab( $tab_or_index ); | ||
| 187 | |||
| 188 | =head2 move_tab | ||
| 189 | |||
| 190 | $console->move_tab( $tab_or_index, $delta ); | ||
| 191 | |||
| 192 | =head2 activate_tab | ||
| 193 | |||
| 194 | $console->activate_tab( $tab_or_index ); | ||
| 195 | |||
| 196 | =head2 next_tab | ||
| 197 | |||
| 198 | $console->next_tab; | ||
| 199 | |||
| 200 | =head2 prev_tab | ||
| 201 | |||
| 202 | $console->prev_tab; | ||
| 203 | |||
| 204 | These methods are all passed through to the underlying | ||
| 205 | L<Tickit::Widget::Tabbed> object. | ||
| 206 | |||
| 207 | =cut | ||
| 208 | |||
| 209 | BEGIN { | ||
| 210 | use Object::Pad 0.800 ':experimental(mop)'; | ||
| 211 | |||
| 212 | my $meta = Object::Pad::MOP::Class->for_caller; | ||
| 213 | |||
| 214 | foreach my $method (qw( active_tab_index active_tab | ||
| 215 | remove_tab move_tab activate_tab next_tab prev_tab )) { | ||
| 216 | $meta->add_method( $method => method { | ||
| 217 | $_tabbed->$method( @_ ); | ||
| 218 | } ); | ||
| 219 | } | ||
| 220 | } | ||
| 221 | |||
| 222 | =head2 bind_key | ||
| 223 | |||
| 224 | $console->bind_key( $key, $code ); | ||
| 225 | |||
| 226 | Installs a callback to invoke if the given key is pressed, overwriting any | ||
| 227 | previous callback for the same key. The code block is invoked as | ||
| 228 | |||
| 229 | $code->( $console, $key ); | ||
| 230 | |||
| 231 | If C<$code> is missing or C<undef>, any existing callback is removed. | ||
| 232 | |||
| 233 | =cut | ||
| 234 | |||
| 235 | field %_keybindings; | ||
| 236 | |||
| 237 | method bind_key ( $key, $code ) | ||
| 238 | { | ||
| 239 | $_keybindings{$key}[0] = $code; | ||
| 240 | |||
| 241 | $self->_update_key_binding( $key ); | ||
| 242 | } | ||
| 243 | |||
| 244 | method _update_key_binding ( $key ) | ||
| 245 | { | ||
| 246 | my $bindings = $_keybindings{$key}; | ||
| 247 | |||
| 248 | if( $bindings->[0] or $bindings->[1] ) { | ||
| 249 | $_entry->bind_keys( $key => sub ( $entry, $key ) { | ||
| 250 | $entry->parent->_on_key( $key ); | ||
| 251 | }); | ||
| 252 | } | ||
| 253 | else { | ||
| 254 | $_entry->bind_key( $key => undef ); | ||
| 255 | } | ||
| 256 | } | ||
| 257 | |||
| 258 | method _inc_key_binding ( $key ) | ||
| 259 | { | ||
| 260 | $_keybindings{$key}[1]++; | ||
| 261 | $self->_update_key_binding( $key ); | ||
| 262 | } | ||
| 263 | |||
| 264 | method _dec_key_binding ( $key ) | ||
| 265 | { | ||
| 266 | $_keybindings{$key}[1]--; | ||
| 267 | $self->_update_key_binding( $key ); | ||
| 268 | } | ||
| 269 | |||
| 270 | method _on_key ( $key ) | ||
| 271 | { | ||
| 272 | if( my $tab = $self->active_tab ) { | ||
| 273 | return 1 if $tab->_on_key( $key ); | ||
| 274 | } | ||
| 275 | |||
| 276 | my $code = $_keybindings{$key}[0] or return 0; | ||
| 277 | return $code->( $self, $key ); | ||
| 278 | } | ||
| 279 | |||
| 280 | =head1 AUTHOR | ||
| 281 | |||
| 282 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 283 | |||
| 284 | =cut | ||
| 285 | |||
| 286 | 0x55AA; | ||