Tarotplane/UI.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Tarotplane/Cards.pm ↑ Summary Tarotplane.pm »
1 package App::Tarotplane::UI;
2 our $VERSION = '2.02';
3 1 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 require Exporter;
8 our @ISA = qw(Exporter);
9 our @EXPORT_OK = qw(%KEY_BINDINGS);
10  
11 1 ▶use Text::Wrap qw(wrap $columns);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 0 ▶use Curses;
Subroutine BEGIN: called CC=1 SCAR=2.6
14  
15 # Tells whether Curses is running or not, so that multiple objects don't try to
16 # run Curses.
17 my $Cursed = 0;
18  
19 our %KEY_BINDINGS = (
20 Next => [ 'l', KEY_RIGHT, ],
21 Prev => [ 'h', KEY_LEFT, ],
22 Flip => [ 'j', 'k', ' ', KEY_UP, KEY_DOWN, ],
23 First => [ KEY_NPAGE, KEY_END, ],
24 Last => [ KEY_PPAGE, KEY_HOME, ],
25 Quit => [ 'q', ],
26 Help => [ '?', ],
27 );
28  
29 my $CONTROLS_HELP = <<END;
30 Next: right, l
31 Previous: left, h
32 Flip: up/down, j/k, space
33 First card: page up, home
34 Last card: page down, end
35 Quit: q
36 Help: ?
37 END
38  
39 sub init {
40  
41 my $class = shift;
42 my $self = {
43 MainWin => undef,
44 CardWin => undef,
45 InfoWin => undef,
46 CardStr => '',
47 InfoStr => '',
48 };
49  
50 if ($Cursed) {
51 die "Curses is already running";
52 }
53  
54 bless $self, $class;
55  
56 initscr();
57  
58 curs_set(0);
59 cbreak();
60 noecho();
61 keypad(1);
62  
63 $self->{MainWin} = $stdscr;
64  
65 $self->{InfoWin} = newwin(1, $COLS, $LINES - 1, 0);
66 $self->{CardWin} = newwin($LINES - 3, $COLS - 2, 1, 1);
67  
68 $Cursed = 1;
69  
70 return $self;
71  
72 }
73  
74 sub wipe {
75  
76 my $self = shift;
77  
78 erase($self->{MainWin});
79 noutrefresh($self->{MainWin});
80  
81 }
82  
83 sub update {
84  
85 my $self = shift;
86  
87 doupdate();
88  
89 }
90  
91 sub draw_card {
92  
93 my $self = shift;
94 my $str = shift;
95 my $bold = shift;
96  
97 # Make room for the box borders and a single whitespace on each side.
98 my $linemax = getmaxx($self->{CardWin}) - 4;
99  
100 $self->{CardStr} = $str if defined $str;
101  
102 $columns = $linemax;
103 my $text = wrap('', '', $self->{CardStr});
104  
105 if (defined $bold) {
106 attrset($self->{CardWin}, $bold ? A_BOLD : A_NORMAL);
107 }
108  
109 erase($self->{CardWin});
110 box($self->{CardWin}, 0, 0);
111  
112 my $ypos = (getmaxy($self->{CardWin}) / 2) - (($text =~ tr/\n//) / 2);
113  
114 foreach my $l (split /\n/, $text) {
115 $l =~ s/^\s+|\s+$//; # Trim leading/trailing whitespace.
116 $l =~ s/\s+/ /g; # Truncate space.
117 addstr($self->{CardWin}, $ypos++, (($linemax + 4) - length($l)) / 2, $l);
118 }
119  
120 noutrefresh($self->{CardWin});
121  
122 }
123  
124 sub draw_info {
125  
126 my $self = shift;
127 my $info = shift;
128  
129 $self->{InfoStr} = $info if defined $info;
130  
131 erase($self->{InfoWin});
132  
133 addstr($self->{InfoWin}, $self->{InfoStr});
134  
135 noutrefresh($self->{InfoWin});
136  
137 }
138  
139 sub draw_help {
140  
141 my $self = shift;
142  
143 my $y = 0;
144 foreach my $l (split /\n/, $CONTROLS_HELP) {
145 addstr($self->{MainWin}, $y++, 0, $l);
146 }
147  
148 noutrefresh($self->{MainWin});
149  
150 }
151  
152 sub update_size {
153  
154 my $self = shift;
155  
156 $self->wipe();
157  
158 resize($self->{InfoWin}, 1, $COLS);
159 mvwin($self->{InfoWin}, $LINES - 1, 0);
160  
161 resize($self->{CardWin}, $LINES - 3, $COLS - 2);
162 mvwin($self->{CardWin}, 1, 1);
163  
164 $self->draw_card();
165 $self->draw_info();
166  
167 $self->update();
168  
169 }
170  
171 sub poll {
172  
173 my $self = shift;
174  
175 while (my $in = getch()) {
176  
177 if ($in eq KEY_RESIZE) {
178 $self->update_size();
179 next;
180 }
181  
182 foreach my $k (keys %KEY_BINDINGS) {
183 return $k if grep { $in eq $_ } @{$KEY_BINDINGS{$k}};
184 }
185  
186 return undef;
187  
188 }
189  
190 }
191  
192 sub end {
193  
194 my $self = shift;
195  
196 endwin();
197  
198 foreach my $win (grep { /Win$/ } keys %{$self}) {
199 $self->{$win} = undef;
200 }
201  
202 $Cursed = 0;
203  
204 }
205  
206 DESTROY {
207  
208 my $self = shift;
209  
210 $self->end();
211  
212 }
213  
214 1;
215  
216  
217  
218 =head1 NAME
219  
220 App::Tarotplane::UI - tarotplane TUI
221  
222 =head1 SYNOPSIS
223  
224 use App::Tarotplane::UI;
225  
226 $ui = App::Tarotplane::UI->init();
227  
228 $ui->draw_card($cardstr);
229 $ui->draw_info($infostr);
230 $ui->update();
231  
232 $cmd = $ui->poll();
233  
234 $ui->wipe();
235 $ui->update();
236  
237 $ui->end();
238  
239 =head1 DESCRIPTION
240  
241 App::Tarotplane::UI is the component of L<tarotplane> that handles the TUI
242 (text-user interface). It accomplishes this through the L<Curses> module. If
243 you're looking for documentation for L<tarotplane>, consult its manual page.
244  
245 =head1 Object Methods
246  
247 =head2 App::Tarotplane::UI->init()
248  
249 Initializes L<Curses> and returns an App::Tarotplane::UI object. Only one
250 App::Tarotplane::UI object can be initialized at a single time.
251  
252 =head2 $ui->wipe()
253  
254 Wipes screen. Should be called before any re-drawing occurs.
255  
256 =head2 $ui->update()
257  
258 Updates screen with any new drawings. Should be called to display the results
259 of any draw method.
260  
261 =head2 $ui->draw_card([$str, $bold])
262  
263 Draws a new card displaying $str, automatically performing any text wrapping
264 necessary to fit in the card. If $bold is supplied and true, the drawn card
265 will be bold.
266  
267 update() needs to be called to push the drawing to the screen.
268  
269 If $str and/or $bold are not supplied, draw_card()
270 will use whatever was used on a previous call to draw_card().
271  
272 =head2 $ui->draw_info([$str])
273  
274 Draws an info bar at the bottom of the screen containing $str.
275  
276 update() needs to be called to push the drawing to the screen.
277  
278 If $str is not supplied, uses the last string supplied by a previous
279 draw_info() call.
280  
281 =head2 $ui->draw_help()
282  
283 Wipes screen and draws help message.
284  
285 update() needs to be called to push the drawing to the screen.
286  
287 =head2 $ui->update_size()
288  
289 Resizes drawings to fit screen, automatically performing necessary wiping and
290 updating.
291  
292 =head2 $ui->poll()
293  
294 Returns command from user input, or undef if the command does not exist. See
295 the documentation for %KEY_BINDINGS for a list of valid commands.
296  
297 =head2 $ui->end()
298  
299 Ends L<Curses>.
300  
301 =head1 Global Variables
302  
303 =over 4
304  
305 =item %KEY_BINDINGS
306  
307 Hash map of commands and their respective key bindings.
308  
309 =back
310  
311 =head1 AUTHOR
312  
313 Written by Samuel Young E<lt>L<samyoung12788@gmail.com>E<gt>.
314  
315 =head1 COPYRIGHT
316  
317 Copyright 2024-2026, Samuel Young
318  
319 This library is free software; you may redistribute it and/or
320 modify it under the same terms as Perl itself.
321  
322 =head1 SEE ALSO
323  
324 L<tarotplane>, L<Curses>
325  
326 =cut
« Tarotplane/Cards.pm ↑ Summary Tarotplane.pm »