| 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, 2014-2023 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | use v5.26; # signatures | ||
| 7 | use warnings; | ||
| 8 | use Object::Pad 0.800 ':experimental(init_expr)'; | ||
| 9 | |||
| 10 | use Tickit::Widget::Tabbed 0.024; | ||
| 11 | |||
| 12 | package Tickit::Console::Tab 0.12; | ||
| 13 | class Tickit::Console::Tab | ||
| 14 | :isa(Tickit::Widget::Tabbed::Tab) | ||
| 15 | :strict(params); | ||
| 16 | |||
| 17 | use Tickit::Widget::Scroller::Item::Text; | ||
| 18 | use Tickit::Widget::Scroller::Item::RichText; | ||
| 19 | |||
| 20 | use String::Tagged 0.10; | ||
| 21 | |||
| 22 | use POSIX (); | ||
| 23 | use Scalar::Util qw( blessed weaken ); | ||
| 24 | |||
| 25 | =head1 NAME | ||
| 26 | |||
| 27 | C<Tickit::Console::Tab> - represent a single tab on a C<Tickit::Console> | ||
| 28 | |||
| 29 | =head1 DESCRIPTION | ||
| 30 | |||
| 31 | Objects in this class represent a single switchable tab within a | ||
| 32 | L<Tickit::Console>. They are not constructed directly, but instead are | ||
| 33 | returned by the C<add_tab> method of the underlying C<Tickit::Console> object. | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | =head1 PARAMETERS | ||
| 38 | |||
| 39 | The following extra parameters may be passed to the constructor, or via the | ||
| 40 | C<add_tab> method on the C<Tickit::Console> object: | ||
| 41 | |||
| 42 | =over 8 | ||
| 43 | |||
| 44 | =item timestamp_format => STRING or String::Tagged | ||
| 45 | |||
| 46 | If defined, every line is prefixed with a timestamp built by applying the | ||
| 47 | C<POSIX::strftime> function to this string. If a L<String::Tagged> instance is | ||
| 48 | applied it will preserve all the formatting from it. | ||
| 49 | |||
| 50 | =item datestamp_format => STRING or String::Tagged | ||
| 51 | |||
| 52 | If defined, every time a line is added to the buffer, if it starts a new day | ||
| 53 | since the previous message (because the format yields a different string), | ||
| 54 | this message is added as well to the scroller. | ||
| 55 | |||
| 56 | =item localtime => CODE | ||
| 57 | |||
| 58 | If defined, provides an alternative function to C<CORE::localtime> for | ||
| 59 | converting an epoch value into a timestamp. For example, this may be set to | ||
| 60 | |||
| 61 | sub { gmtime $_[0] } | ||
| 62 | |||
| 63 | to generate timestamps in UTC instead of using the local timezone. | ||
| 64 | |||
| 65 | =back | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | field $_scroller :param; | ||
| 70 | field $_console :param :weak; | ||
| 71 | field $_on_line :param = undef; | ||
| 72 | |||
| 73 | field $_timestamp_format :param; | ||
| 74 | field $_datestamp_format :param; | ||
| 75 | field $_localtime :param = sub ( $time ) { localtime $time }; | ||
| 76 | |||
| 77 | =head1 METHODS | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | =head2 name | ||
| 82 | |||
| 83 | =head2 set_name | ||
| 84 | |||
| 85 | $name = $tab->name; | ||
| 86 | |||
| 87 | $tab->set_name( $name ); | ||
| 88 | |||
| 89 | Returns or sets the tab name text | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | method name () | ||
| 94 | { | ||
| 95 | return $self->label; | ||
| 96 | } | ||
| 97 | |||
| 98 | method set_name ( $name ) | ||
| 99 | { | ||
| 100 | $self->set_label( $name ); | ||
| 101 | } | ||
| 102 | |||
| 103 | =head2 append_line | ||
| 104 | |||
| 105 | $tab->append_line( $string, %opts ); | ||
| 106 | |||
| 107 | Appends a line of text to the tab. C<$string> may either be a plain perl | ||
| 108 | string, or an instance of L<String::Tagged> containing formatting tags, as | ||
| 109 | specified by L<Tickit::Widget::Scroller>. Options will be passed to the | ||
| 110 | L<Tickit::Widget::Scroller::Item::Line> used to contain the string. | ||
| 111 | |||
| 112 | Also recognises the following options: | ||
| 113 | |||
| 114 | =over 8 | ||
| 115 | |||
| 116 | =item time => NUM | ||
| 117 | |||
| 118 | Overrides the epoch C<time()> value used to generate a timestamp for this line | ||
| 119 | |||
| 120 | =item timestamp_format => STRING or String::Tagged | ||
| 121 | |||
| 122 | Overrides the stored format for generating a timestamp string. | ||
| 123 | |||
| 124 | =item datestamp_format => STRING or String::Tagged | ||
| 125 | |||
| 126 | Overrides the stored format for generating a datestamp string. | ||
| 127 | |||
| 128 | =back | ||
| 129 | |||
| 130 | =cut | ||
| 131 | |||
| 132 | sub strftime ( $format, @t ) | ||
| 133 | { | ||
| 134 | if( blessed $format and $format->isa( "String::Tagged" ) ) { | ||
| 135 | my $fplain = $format->str; | ||
| 136 | my $ret = String::Tagged->new; | ||
| 137 | |||
| 138 | # Iterate format specifiers and other literal text | ||
| 139 | foreach my $m ( $format->matches( qr/%[_0#^-]?[OE]?.|[^%]+/ ) ) { | ||
| 140 | if( $m =~ m/^%/ ) { | ||
| 141 | # Format specifier | ||
| 142 | $ret->append_tagged( POSIX::strftime( $m, @t ), | ||
| 143 | %{ $m->get_tags_at( 0 ) } | ||
| 144 | ); | ||
| 145 | } | ||
| 146 | else { | ||
| 147 | # Literal | ||
| 148 | $ret->append( $m ); | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | return $ret; | ||
| 153 | } | ||
| 154 | else { | ||
| 155 | return POSIX::strftime( $format, @t ); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | sub _make_item ( $string, %opts ) | ||
| 160 | { | ||
| 161 | if( blessed $string and $string->isa( "String::Tagged" ) ) { | ||
| 162 | return Tickit::Widget::Scroller::Item::RichText->new( $string, %opts ); | ||
| 163 | } | ||
| 164 | else { | ||
| 165 | return Tickit::Widget::Scroller::Item::Text->new( $string, %opts ); | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | field $_dusk_datestamp; | ||
| 170 | field $_dawn_datestamp; | ||
| 171 | |||
| 172 | method _make_item_with_timestamp ( $string, %opts ) | ||
| 173 | { | ||
| 174 | if( my $timestamp_format = delete $opts{timestamp_format} // $_timestamp_format ) { | ||
| 175 | my $time = delete $opts{time} // time(); | ||
| 176 | my $timestamp = strftime( $timestamp_format, $_localtime->( $time ) ); | ||
| 177 | |||
| 178 | $string = $timestamp . $string; | ||
| 179 | } | ||
| 180 | |||
| 181 | return _make_item( $string, %opts ); | ||
| 182 | } | ||
| 183 | |||
| 184 | method append_line ( $string, %opts ) | ||
| 185 | { | ||
| 186 | if( my $datestamp_format = delete $opts{datestamp_format} // $_datestamp_format ) { | ||
| 187 | my $time = $opts{time} //= time(); | ||
| 188 | my $plain = POSIX::strftime( $datestamp_format, my @t = $_localtime->( $time ) ); | ||
| 189 | |||
| 190 | if( ( $_dusk_datestamp // "" ) ne $plain ) { | ||
| 191 | my $datestamp = strftime( $datestamp_format, @t ); | ||
| 192 | $_scroller->push( _make_item( $datestamp ) ); | ||
| 193 | |||
| 194 | $_dusk_datestamp = $plain; | ||
| 195 | $_dawn_datestamp //= $plain; | ||
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | $_scroller->push( $self->_make_item_with_timestamp( $string, %opts ) ); | ||
| 200 | } | ||
| 201 | |||
| 202 | *add_line = \&append_line; | ||
| 203 | |||
| 204 | =head2 prepend_line | ||
| 205 | |||
| 206 | $tab->prepend_line( $string, %opts ); | ||
| 207 | |||
| 208 | As C<append_line>, but prepends it at the beginning of the scroller. | ||
| 209 | |||
| 210 | =cut | ||
| 211 | |||
| 212 | method prepend_line ( $string, %opts ) | ||
| 213 | { | ||
| 214 | my $datestamp_item; | ||
| 215 | if( my $datestamp_format = delete $opts{datestamp_format} // $_datestamp_format ) { | ||
| 216 | my $time = $opts{time} //= time(); | ||
| 217 | my $plain = POSIX::strftime( $datestamp_format, my @t = $_localtime->( $time ) ); | ||
| 218 | |||
| 219 | $_scroller->shift if ( $_dawn_datestamp // "" ) eq $plain; | ||
| 220 | |||
| 221 | my $datestamp = strftime( $datestamp_format, @t ); | ||
| 222 | $datestamp_item = _make_item( $datestamp ); | ||
| 223 | |||
| 224 | $_dawn_datestamp = $plain; | ||
| 225 | $_dusk_datestamp //= $plain; | ||
| 226 | } | ||
| 227 | |||
| 228 | $_scroller->unshift( $self->_make_item_with_timestamp( $string, %opts ) ); | ||
| 229 | $_scroller->unshift( $datestamp_item ) if $datestamp_item; | ||
| 230 | } | ||
| 231 | |||
| 232 | =head2 bind_key | ||
| 233 | |||
| 234 | $tab->bind_key( $key, $code ); | ||
| 235 | |||
| 236 | Installs a callback to invoke if the given key is pressed while this tab has | ||
| 237 | focus, overwriting any previous callback for the same key. The code block is | ||
| 238 | invoked as | ||
| 239 | |||
| 240 | $result = $code->( $tab, $key ); | ||
| 241 | |||
| 242 | If C<$code> is missing or C<undef>, any existing callback is removed. | ||
| 243 | |||
| 244 | This callback will be invoked before one defined on the console object itself, | ||
| 245 | if present. If it returns a false value, then the one on the console will be | ||
| 246 | invoked instead. | ||
| 247 | |||
| 248 | =cut | ||
| 249 | |||
| 250 | field %_keybindings; | ||
| 251 | |||
| 252 | method bind_key ( $key, $code ) | ||
| 253 | { | ||
| 254 | if( not $_keybindings{$key} and $code ) { | ||
| 255 | $_console->_inc_key_binding( $key ); | ||
| 256 | } | ||
| 257 | elsif( $_keybindings{$key} and not $code ) { | ||
| 258 | $_console->_dec_key_binding( $key ); | ||
| 259 | } | ||
| 260 | |||
| 261 | $_keybindings{$key} = $code; | ||
| 262 | } | ||
| 263 | |||
| 264 | method _on_line ( $line ) | ||
| 265 | { | ||
| 266 | $_on_line or return 0; | ||
| 267 | |||
| 268 | $_on_line->( $self, $line ); | ||
| 269 | |||
| 270 | return 1; | ||
| 271 | } | ||
| 272 | |||
| 273 | method _on_key ( $key ) | ||
| 274 | { | ||
| 275 | return 1 if $_keybindings{$key} and | ||
| 276 | $_keybindings{$key}->( $self, $key ); | ||
| 277 | return 0; | ||
| 278 | } | ||
| 279 | |||
| 280 | =head1 AUTHOR | ||
| 281 | |||
| 282 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 283 | |||
| 284 | =cut | ||
| 285 | |||
| 286 | 0x55AA; |