File Coverage

blib/lib/Term/Graille/Dialog.pm
Criterion Covered Total %
statement 18 117 15.3
branch 0 40 0.0
condition 0 23 0.0
subroutine 6 25 24.0
pod 1 14 7.1
total 25 219 11.4


line stmt bran cond sub pod time code
1             package Term::Graille::Dialog;
2              
3 1     1   1016 use strict;use warnings;
  1     1   4  
  1         27  
  1         5  
  1         2  
  1         25  
4 1     1   5 use lib "../../../lib";
  1         2  
  1         17  
5 1     1   134 use Term::Graille qw/colour printAt clearScreen border cursorAt wrapText/;
  1         2  
  1         66  
6 1     1   7 use base 'Exporter';
  1         2  
  1         133  
7             our @EXPORT_OK = qw/deathSentence confirm prompt caution/;
8 1     1   7 use utf8;
  1         1  
  1         5  
9              
10             our $VERSION=0.10;
11              
12             =head3 Cnew(%params)>
13              
14             Creates a new dialog box; params are
15             C the possible options that may be selected
16             C This is a function to redraws the application screen.
17             The dialog box may overwrite parts of the application screen, and this
18             function needs to be provided to restore the screen.
19             C The dialog does not call any functions itself, instead returns the
20             button value pressed. if the $dialog->{entry} is set this is also sent. In
21             prompts, the entry is populated by user. It is upto the main application
22             to use this data (the callback function supplied)
23             C Optional. The default position is [2,2], but setting this parameter allows
24             the dialog to be placed elsewhere
25             C Optional. The selected item is highlighted default "black on_white"
26             C Optional. The normal colour of text items "white on_black"
27              
28              
29             =cut
30              
31             my %icons=(
32             die=> [[colour("red")."⢰","⠪","⣛","⠍","⠩","⣛","⠝","⡆"],
33             ["⠈","⠑","⢤","⠰","⠆","⡤","⠋","⠀"],
34             ["⠛","⢧","⣀","⠉","⢉","⣡","⠞","⠛"],
35             ["⣤","⡴","⠚","⠋","⠙","⠓","⢤","⣤".colour("reset")]],
36             info=> [[colour("blue")."⠀","⠐","⠿","⠿","⠂","⠀","⠀","⠀"],
37             ["⠀","⠠","⢶","⣶","⠀","⠀","⠀","⠀"],
38             ["⠀","⠀","⢸","⣿","⠀","⠀","⠀","⠀"],
39             ["⠀","⠀","⠘","⢿","⣤","⠼","⠃","⠀".colour("reset")]],
40             error=> [[colour("yellow")."⠀","⠀","⠀","⢀","⡀","⠀","⠀","⠀"],
41             ["⠀","⠀","⠸","⣭","⣿","⠇","⠀","⠀"],
42             ["⠀","⠀","⠀","⠸","⠇","⠀","⠀","⠀"],
43             ["⠀","⠀","⠀","⢴","⡦","⠀","⠀","⠀".colour("reset")]],
44             forbidden=>[[colour("red")."⠀","⠀","⢀","⣀","⣀","⡀","⠀","⠀"],
45             ["⢠","⡞","⣉","⣀","⣀","⣉","⢳","⡄"],
46             ["⠘","⢧","⣉","⠉","⠉","⣉","⡼","⠃"],
47             ["⠀","⠀","⠈","⠉","⠉","⠁","⠀","⠀".colour("reset")]],
48             query=>[[colour("green")."⠀","⠀","⠀","⣀","⡀","⠀","⠀","⠀"],
49             ["⠀","⠀","⠾","⠉","⠉","⣷","⠀","⠀"],
50             ["⠀","⠀","⠀","⢠","⡞","⠉","⠀","⠀"],
51             ["⠀","⠀","⠀","⢨","⡅","⠀","⠀","⠀".colour("reset")]],
52             );
53              
54              
55             sub new{
56 0     0 1   my ($class,%params) = @_;
57 0           my $self={};
58 0           bless $self,$class;
59            
60 0   0       $self->{buttons}=$params{buttons}//[];
61 0   0       $self->{selected}=$params{selected}// ""; #if a button is tob pres elected
62            
63 0   0       $self->{message}=$params{message}// "No Message";
64            
65 0 0         $self->{redraw}=$params{redraw} if (exists $params{redraw}); # function to redraw application
66 0 0         $self->{callback}=$params{callback} if (exists $params{callback}); # function to call after menu item selected
67 0   0       $self->{transient}=$params{transient}//1;
68 0   0       $self->{title}=$params{title}//"Dialog";
69 0   0       $self->{normalColour}=$params{titleColour}//"yellow";
70 0   0       $self->{pos}=$params{pos}//[2,2];
71 0   0       $self->{geometry}=$params{geometry}//[8,40];
72 0           $self->{param}=$params{param}; # optional parameter
73 0           $self->{icon}=$params{icon};
74 0   0       $self->{highlightColour}=$params{highlightColour}//"black on_white";
75 0   0       $self->{normalColour}=$params{normalColour}//"white on_black";
76             $self->{keyAction}={
77 0     0     "[D" =>sub{$self->prevItem()},
78 0     0     "[C" =>sub{$self->nextItem()},
79 0     0     "enter"=>sub{$self->selectItem()},
80 0     0     "esc" =>sub{$self->{close}->()},
81 0     0     others =>sub{my ($obj,$pressed,$gV)=@_;$self->addChar($pressed)},
  0            
82 0           };
83 0           return $self;
84             }
85              
86             sub mode{
87 0     0 0   my ($self,$mode,@etc)=@_;
88 0           for ($mode){
89 0 0         /okc/i && do {$self->{buttons}=[qw/OK Cancel/]; last;};
  0            
  0            
90 0 0         /ok/i && do {$self->{buttons}=[qw/OK/]; last;};
  0            
  0            
91 0 0         /ync/i && do {$self->{buttons}=[qw/Yes No Cancel/]; last;};
  0            
  0            
92 0 0         /input/i && do {$self->{buttons}=[qw/Submit Cancel/];
  0            
93 0           $self->{entry}="";$self->{entryPos}=0; last};
  0            
  0            
94            
95             }
96            
97             }
98              
99             sub draw{
100 0     0 0   my $self=shift;
101 0 0         if (!ref $self->{message}){
102 0           $self->{message}=wrapText( $self->{message},$self->{geometry}->[1]-15);
103             }
104 0 0         my $entryLines=(defined $self->{entry})?2:0;
105 0           my $buttons="";
106 0           foreach my $button (@{$self->{buttons}}){
  0            
107 0 0 0       $buttons.=" ".colour($self->{selected} && $self->{selected} eq $button?"black on_white":"white")."[$button]".colour("reset");
108             }
109 0           border(@{$self->{pos}},$self->{pos}->[0]+max(($self->{icon}?6:0),@{$self->{message}}+$entryLines+1),
  0            
110             $self->{geometry}->[1]+$self->{pos}->[1],
111             "double","green",
112 0 0         $self->{title},$self->{titleColour});
113 0 0         printAt($self->{pos}->[0]+1,6,[@{$self->{message}},($entryLines?($self->{entry},"-"x($self->{geometry}->[1]-4)):()),$buttons]);
  0            
114            
115 0 0         printAt($self->{pos}->[0]+2,$self->{geometry}->[1]-5,$icons{$self->{icon}}) if $self->{icon};
116            
117             }
118              
119              
120             sub addChar{
121 0     0 0   my ($self,$ch)=@_;
122 0 0         return unless exists $self->{entry};
123 0 0         $self->{entry}.=$ch if (length $ch ==1 );
124 0 0         chop $self->{entry} if ($ch =~/back/ );
125 0           $self->draw();
126            
127             }
128              
129             sub selectedPos{
130 0     0 0   my ($self)=@_;
131 0 0         return 0 unless $self->{selected};
132 0           for my $p (0..$#{$self->{buttons}}){
  0            
133 0 0         return $p if ($self->{selected} eq $self->{buttons}->[$p])
134             }
135 0           return 0;
136             }
137              
138             sub nextItem{
139 0     0 0   my ($self)=@_;
140 0           my $p=$self->selectedPos();
141 0 0         $p+=($p<$#{$self->{buttons}})?1:0;
  0            
142 0           $self->{selected}= $self->{buttons}->[$p];
143             #printAt(2,50, " $p $self->{selected} ".$#{$self->{buttons}}." ".join("--",@{$self->{buttons}}));
144 0           $self->draw();
145             }
146              
147             sub prevItem{
148 0     0 0   my ($self)=@_;
149 0           my $p=$self->selectedPos();
150 0 0         $self->{selected}= $self->{buttons}->[$p-($p>0)?1:0];
151             #printAt(2,50, " $p $self->{selected} ".$#{$self->{buttons}}." ".join("--",@{$self->{buttons}}));
152 0           $self->draw();
153             }
154              
155             sub selectItem{
156 0     0 0   my ($self)=@_;
157 0           $self->{param}->{button}=$self->{selected};
158 0           $self->{param}->{entry}=$self->{entry};
159 0 0         if (exists $self->{callback}) {$self->{callback}->($self->{param})};
  0            
160             }
161              
162              
163             sub close{ # what needs to be done before Interact de-activates widget
164 0     0 0   my ($self)=@_;
165 0           $self->{redraw}->();
166             }
167              
168             sub max{
169 0     0 0   return (sort{$a <=> $b}@_)[-1]
  0            
170             }
171              
172             sub min{
173 0     0 0   return (sort{$a <=> $b}@_)[0]
  0            
174             }
175              
176       0 0   sub prompt{
177            
178             }
179              
180              
181             sub deathSentence{ # waiting to be exported
182 0     0 0   my $str=shift;
183 0           my @epitaphs=(
184             "'Forgive him Larry, for he knows not how to code'",
185             "'I told you I was sick'",
186             "'It is a far far better code that I do, than you have ever run'",
187             "'Your references have been counted and found wanting'",
188             "'Goodbye old friend, may the Schwartz be with you'",
189             "'she believed she could, but she couldn\'t'",
190             );
191 0           my $message=[@{wrapText(colour("red").$str.colour("reset"),45)},caller(),
192 0           @{wrapText(colour("yellow").($epitaphs[rand(@epitaphs)]).colour("reset"),45)}];
  0            
193            
194 0           border(10,4,11+@$message,64,"double","green","RIP","yellow");
195 0           printAt(11,6,$message);
196 0           printAt(12,55,$icons{die});
197            
198 0           printAt(18,0,"");
199 0           sleep 10;
200 0           exit;
201             }
202              
203             sub fileSelector{
204 0     0 0   my ($dir,$filter,$callback,$redraw,$parameter)=@_;
205            
206            
207            
208             }
209              
210              
211             1;