blib/lib/Term/Fabulous/Terminal/Memory.pm untested

filter: Statementstmt 0.0% 0 / 128 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 0.0% 0 / 11 Podpod n/a totaltotal 0.0% 0 / 233 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Term::Fabulous::Terminal::Memory;
2  
3 use v5.32;
4 use warnings;
5 use feature 'signatures';
6 no warnings 'experimental::signatures';
7  
8 our $VERSION = '0.05';
9  
10 use Object::Pad 0.825;
11  
12 use Term::Fabulous::Role::Terminal;
13 use Term::Fabulous::Render::Target::Grid;
14  
15 class Term::Fabulous::Terminal::Memory :does(Term::Fabulous::Role::Terminal) :strict(params) {
16 use Term::Fabulous::Termbox qw(
17 TB_EVENT_KEY TB_EVENT_MOUSE TB_EVENT_RESIZE
18 TB_KEY_MOUSE_LEFT TB_KEY_MOUSE_RELEASE
19 );
20 use Term::Fabulous::Termbox::Event;
21 use Term::Fabulous::Event::KeyPress;
22  
23 use constant OPEN_OPTIONS => qw(inline mouse kitty_keyboard);
24  
25 field $width :param :reader;
26 field $height :param :reader;
27 field $kitty_keyboard :param :reader = 0; # whether the terminal speaks the protocol
28 field $sixel_cell_size :param = undef; # [width, height] in pixels when the terminal shows sixel
29  
30 field $cell_target :reader = Term::Fabulous::Render::Target::Grid->new( sixel_cell_size => $sixel_cell_size );
31 field $is_open :reader = 0;
32 field $session_count :reader = 0;
33 field $mouse_enabled :reader = 0;
34 field $inline_rows :reader = undef;
35 field $kitty_keyboard_active :reader = 0;
36 field $_inline; # the rows the session asked for
37 field $_rows; # the rows of the layout, kept after close for reading the screen
38 field @_queue; # Term::Fabulous::Termbox::Event objects
39 field $_input_has_ended = 0;
40  
41 # A byte in the pipe makes the read end readable while input waits, so
42 # an IO::Async watcher on it wakes up like on a real terminal.
43 field $_wakeup_read;
44 field $_wakeup_write;
45 field $_wakeup_pending = 0;
46  
47 ADJUST {
48 _check_size( width => $width );
49 _check_size( height => $height );
50 $_rows = $height;
51 pipe( $_wakeup_read, $_wakeup_write ) or die "Term::Fabulous::Terminal::Memory: cannot create a pipe: $!\n";
52 $_wakeup_write->autoflush(1);
53 }
54  
55 sub _check_size ( $name, $value ) {
56 die "Term::Fabulous::Terminal::Memory: $name must be a whole number of at least 1, got " . ( defined $value ? "'$value'" : 'undef' )
57 unless defined $value && $value =~ /\A[1-9][0-9]*\z/;
58 return;
59 }
60  
61 method open (%options) {
62 die "Term::Fabulous::Terminal::Memory: the terminal is open already" if $is_open;
63 my %known = map { $_ => 1 } OPEN_OPTIONS;
64 my @unknown = grep { !$known{$_} } sort keys %options;
65 die "Term::Fabulous::Terminal::Memory: open does not accept @unknown (known options: " . join( ', ', OPEN_OPTIONS ) . ")" if @unknown;
66 die "Term::Fabulous::Terminal::Memory: inline must be a whole number of rows of at least 1, got '$options{inline}'"
67 if defined $options{inline} && $options{inline} !~ /\A[1-9][0-9]*\z/;
68  
69 $is_open = 1;
70 $session_count++;
71 $_inline = $options{inline};
72 $mouse_enabled = $options{mouse} ? 1 : 0;
73 $kitty_keyboard_active = $options{kitty_keyboard} && $kitty_keyboard ? 1 : 0;
74 $_rows = $self->_layout_rows($height);
75 $inline_rows = defined $_inline ? $_rows : undef;
76 $cell_target->clear_cells;
77 return;
78 }
79  
80 method _layout_rows ($screen_height) {
81 return $screen_height unless defined $_inline;
82 return $_inline < $screen_height ? $_inline : $screen_height;
83 }
84  
85 method close () {
86 return unless $is_open;
87 $is_open = 0;
88 ( $mouse_enabled, $inline_rows, $kitty_keyboard_active ) = ( 0, undef, 0 );
89 return;
90 }
91  
92 method size () {
93 die "Term::Fabulous::Terminal::Memory: the terminal is not open" unless $is_open;
94 return ( $width, $_rows );
95 }
96  
97 method apply_resize ( $new_width, $new_height ) {
98 die "Term::Fabulous::Terminal::Memory: the terminal is not open" unless $is_open;
99 ( $width, $height ) = ( $new_width, $new_height );
100 $_rows = $self->_layout_rows($height);
101 $inline_rows = $_rows if defined $_inline;
102 return ( $width, $_rows );
103 }
104  
105 method read_handles () {
106 return ($_wakeup_read);
107 }
108  
109 # A terminal that does not report the mouse sends no mouse events.
110 method next_event () {
111 while (@_queue) {
112 my $event = shift @_queue;
113 next if $event->type == TB_EVENT_MOUSE && !$mouse_enabled;
114 return $event;
115 }
116 if ($_wakeup_pending) {
117 sysread( $_wakeup_read, my $byte, 1 ) or die "Term::Fabulous::Terminal::Memory: cannot read its wakeup pipe: $!\n";
118 $_wakeup_pending = 0;
119 }
120 return undef;
121 }
122  
123 method input_ended () {
124 return $_input_has_ended && !@_queue ? 1 : 0;
125 }
126  
127 method push_event (%fields) {
128 my $type = $fields{type} // die "Term::Fabulous::Terminal::Memory: push_event needs a type: TB_EVENT_KEY, TB_EVENT_MOUSE or TB_EVENT_RESIZE";
129 die "Term::Fabulous::Terminal::Memory: unknown event type '$type'; use TB_EVENT_KEY, TB_EVENT_MOUSE or TB_EVENT_RESIZE"
130 unless $type == TB_EVENT_KEY || $type == TB_EVENT_MOUSE || $type == TB_EVENT_RESIZE;
131 die "Term::Fabulous::Terminal::Memory: no input can follow end_input" if $_input_has_ended;
132 push @_queue, Term::Fabulous::Termbox::Event->new(%fields);
133 $self->_wake;
134 return $self;
135 }
136  
137 method type_text ($text) {
138 $self->push_event( type => TB_EVENT_KEY, ch => ord $_ ) foreach split //, $text // '';
139 return $self;
140 }
141  
142 method press_key ($name) {
143 return $self->push_event( type => TB_EVENT_KEY, Term::Fabulous::Event::KeyPress->fields_for_name($name) );
144 }
145  
146 method mouse (%fields) {
147 return $self->push_event( %fields, type => TB_EVENT_MOUSE );
148 }
149  
150 # A release names its button, as the input parser reports it.
151 method click ( $x, $y ) {
152 $self->mouse( key => TB_KEY_MOUSE_LEFT, x => $x, y => $y );
153 $self->mouse( key => TB_KEY_MOUSE_RELEASE, x => $x, y => $y, ch => TB_KEY_MOUSE_LEFT );
154 return $self;
155 }
156  
157 method resize ( $new_width, $new_height ) {
158 _check_size( width => $new_width );
159 _check_size( height => $new_height );
160 return $self->push_event( type => TB_EVENT_RESIZE, w => $new_width, h => $new_height );
161 }
162  
163 method end_input () {
164 $_input_has_ended = 1;
165 $self->_wake;
166 return $self;
167 }
168  
169 method _wake () {
170 return if $_wakeup_pending;
171 syswrite( $_wakeup_write, 'x' ) or die "Term::Fabulous::Terminal::Memory: cannot write its wakeup pipe: $!\n";
172 $_wakeup_pending = 1;
173 return;
174 }
175  
176 method lines (%options) {
177 my @unknown = grep { $_ ne 'colors' } sort keys %options;
178 die "Term::Fabulous::Terminal::Memory: lines does not accept @unknown (known options: colors)" if @unknown;
179 return map { $cell_target->row_text( $_, columns => $width, colors => $options{colors} // 0 ) } 0 .. $_rows - 1;
180 }
181  
182 method cell ( $x, $y ) {
183 return $cell_target->cell( $x, $y );
184 }
185 }
186  
187 1;
188  
189 __END__