summaryrefslogtreecommitdiff
path: root/examples/text.pl
blob: 00ceb35ad9b4bfb45edfe636b1170fcf0a0b587f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use strict;
use warnings;

use Tickit;

use Tickit::Widget::VBox;
use Tickit::Widget::Entry;

use Tickit::Widget::Scroller;
use Tickit::Widget::Scroller::Item::Text;

my $scroller = Tickit::Widget::Scroller->new(
   gravity => "bottom",
   gen_top_indicator => sub {
      my $self = shift;
      my $lines = $self->lines_above or return;
      return sprintf "+ %d more", $lines;
   },
   gen_bottom_indicator => sub {
      my $self = shift;
      my $lines = $self->lines_below or return;
      return sprintf "+ %d more", $lines;
   },
);

for my $i ( 0 .. 100 ) {
   my $text = "<Rand $i>: ";
   for ( 0 .. rand( 30 ) + 3 ) {
      $text .= chr( rand( 26 ) + 0x40 ) x ( rand( 10 ) + 5 );
      $text .= " ";
   }

   $scroller->push(
      Tickit::Widget::Scroller::Item::Text->new( $text, indent => 4 ),
   );
}

my $entry = Tickit::Widget::Entry->new(
   on_enter => sub {
      my ( $self, $line ) = @_;

      $scroller->push(
         Tickit::Widget::Scroller::Item::Text->new( "You wrote: $line" )
      );

      $self->set_text( "" );
   },

   fg => 0,
   bg => 2,
);

my $tickit = Tickit->new;

my $vbox = Tickit::Widget::VBox->new;

$vbox->add( $scroller, expand => 1 );
$vbox->add( $entry );

$tickit->set_root_widget( $vbox );

$tickit->run;