Tarotplane.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Tarotplane/UI.pm ↑ Summary
1 package App::Tarotplane;
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(%CARD_SORT);
10  
11 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use App::Tarotplane::Cards;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 0 ▶use App::Tarotplane::UI;
Subroutine BEGIN: called CC=1 SCAR=2.6
15  
16 my $PRGNAM = 'tarotplane';
17 my $PRGVER = $VERSION;
18  
19 my $HELP_MSG = <<END;
20 $PRGNAM - $PRGVER
21 Usage: $0 [options] file ...
22  
23 Options:
24 -o [<by>] --order=[<by>] Order cards alphabetically by terms or definitions
25 -r --random Randomize order cards appear in
26 -t --terms-first Show terms first rather than definitions
27 -h --help Print help message and exit
28 -v --version Print version and copyright info, then exit
29 END
30  
31 my $VER_MSG = <<END;
32 $PRGNAM - $PRGVER
33  
34 Copyright 2024-2026, Samuel Young
35  
36 This program is free software; you may redistribute it and/or
37 modify it under the same terms as Perl itself.
38 END
39  
40 my $TTY_PATH = $^O eq 'MSWin32' ? 'CONIN$' : '/dev/tty';
41  
42 our %CARD_SORT = (
43 None => 0,
44 Random => 1,
45 Order => 2,
46 );
47  
48 sub init {
49  
50 my $class = shift;
51 my $self = {
52 Files => [],
53 Sort => $CARD_SORT{None},
54 OrderBy => '',
55 First => 'Definition',
56 };
57  
58 my $order = undef;
59  
60 Getopt::Long::config('bundling');
61 GetOptions(
62 'order|o:s' => \$order,
63 'random|r' => sub { $self->{Sort} = $CARD_SORT{Random} },
64 'terms-first|t' => sub { $self->{First} = 'Term' },
65 'help|h' => sub { print $HELP_MSG; exit 0 },
66 'version|v' => sub { print $VER_MSG; exit 0 },
67 ) or die "Error in command line arguments\n";
68  
69 die $HELP_MSG unless @ARGV;
70  
71 $self->{Files} = [ @ARGV ];
72 if (scalar(grep { $_ eq '-' } @{$self->{Files}}) > 1) {
73 die "Standard input ('-') can only be passed once to the command-line\n";
74 }
75  
76 if (defined $order) {
77  
78 $order = fc $order;
79  
80 if ($order eq fc 'term' or $order eq '') {
81 $self->{OrderBy} = 'Term';
82 } elsif ($order eq fc 'definition') {
83 $self->{OrderBy} = 'Definition';
84 } else {
85 die "Cards must be sorted by either 'Term' or 'Definition'\n";
86 }
87  
88 $self->{Sort} = $CARD_SORT{Order};
89  
90 }
91  
92 bless $self, $class;
93 return $self;
94  
95 }
96  
97 sub run {
98  
99 my $self = shift;
100  
101 my $deck = App::Tarotplane::Cards->new(@{$self->{Files}});
102  
103 if (not -t STDIN) {
104 open STDIN, '<', $TTY_PATH or die "Failed to open $TTY_PATH: $!\n";
105 }
106  
107 if ($self->{Sort} == $CARD_SORT{Random}) {
108 $deck->shuffle_deck();
109 } elsif ($self->{Sort} == $CARD_SORT{Order}) {
110 $deck->order_deck($self->{OrderBy});
111 }
112  
113 my $filestr = join(" ", @{$self->{Files}});
114  
115 my $card = 0;
116 my $side = $self->{First};
117  
118 my $ui = App::Tarotplane::UI->init();
119  
120 $ui->wipe();
121  
122 $ui->draw_card(
123 $deck->card_side($card, $side),
124 $side eq 'Term' ? 1 : 0
125 );
126 $ui->draw_info(
127 sprintf("[%d/%d] %s", $card + 1, $deck->get('CardNum'), $filestr)
128 );
129  
130 $ui->update();
131  
132 while (1) {
133  
134 my $cmd = $ui->poll();
135  
136 # Do nothing if we can't recognize the command
137 next unless defined $cmd;
138  
139 if ($cmd eq 'Next') {
140 $card++ if $card < $deck->get('CardNum') - 1;
141 $side = $self->{First};
142 } elsif ($cmd eq 'Prev') {
143 $card-- if $card > 0;
144 $side = $self->{First};
145 } elsif ($cmd eq 'Flip') {
146 $side = $side eq 'Term' ? 'Definition' : 'Term';
147 } elsif ($cmd eq 'First') {
148 $card = 0;
149 $side = $self->{First};
150 } elsif ($cmd eq 'Last') {
151 $card = $deck->get('CardNum') - 1;
152 $side = $self->{First};
153 } elsif ($cmd eq 'Quit') {
154 last;
155 } elsif ($cmd eq 'Help') {
156 $ui->wipe();
157 $ui->draw_help();
158 $ui->update();
159 $ui->poll();
160 }
161  
162 $ui->wipe();
163 $ui->draw_card(
164 $deck->card_side($card, $side),
165 $side eq 'Term' ? 1 : 0
166 );
167 $ui->draw_info(
168 sprintf("[%d/%d] %s", $card + 1, $deck->get('CardNum'), $filestr)
169 );
170 $ui->update();
171  
172 }
173  
174 $ui->end();
175  
176 }
177  
178 sub get {
179  
180 my $self = shift;
181 my $get = shift;
182  
183 return $self->{$get};
184  
185 }
186  
187 1;
188  
189  
190  
191 =head1 NAME
192  
193 App::Tarotplane - Curses flashcard program
194  
195 =head1 SYNOPSIS
196  
197 use App::Tarotplane;
198  
199 $tarotplane = App::Tarotplane->init();
200 $tarotplane->run();
201  
202 =head1 DESCRIPTION
203  
204 App::Tarotplane is the module that does all of the work for L<tarotplane>.
205 If you're looking for L<tarotplane> documentation, you should consult its
206 manual page instead of this one.
207  
208 =head1 Object Methods
209  
210 =head2 App::Tarotplane->init()
211  
212 Reads @ARGV and returns an initialized App::Tarotplane object. Read the
213 documentation for L<tarotplane> for a list of what options are available to
214 tarotplane.
215  
216 =head2 $tarotplane->run()
217  
218 Runs tarotplane.
219  
220 =head2 $tarotplane->get($get)
221  
222 Get member $get from $tarotplane. The following are valid gets:
223  
224 =over 4
225  
226 =item Files
227  
228 Array ref of supplied file arguments.
229  
230 =item Sort
231  
232 Sort method used, see %CARD_SORT below.
233  
234 =item OrderBy
235  
236 If ordering cards, what to order by. Can either be 'Term' or 'Definition'.
237  
238 =item First
239  
240 Which card side to display first. Can either be 'Term' or 'Definition'.
241  
242 =back
243  
244 get() primarily exists for testing purposes.
245  
246 =head1 Global Variables
247  
248 =over 4
249  
250 =item $App::Tarotplane::VERSION
251  
252 tarotplane version.
253  
254 =item %CARD_SORT
255  
256 use App::Tarotplane qw(%CARD_SORT);
257  
258 Hash map of different ways tarotplane can sort cards.
259  
260 =over 4
261  
262 =item None
263  
264 Cards are sorted as they appear in the given files.
265  
266 =item Random
267  
268 Cards are sorted in random order.
269  
270 =item Order
271  
272 Cards are sorted in alphabetical order.
273  
274 =back
275  
276 =back
277  
278 =head1 AUTHOR
279  
280 Written by Samuel Young E<lt>L<samyoung12788@gmail.com>E<gt>.
281  
282 =head1 COPYRIGHT
283  
284 Copyright 2024-2026, Samuel Young
285  
286 This library is free software; you may redistribute it and/or
287 modify it under the same terms as Perl itself.
288  
289 =head1 SEE ALSO
290  
291 L<tarotplane>
292  
293 =cut
« Tarotplane/UI.pm ↑ Summary