App/financeta/gui/editor.pm

filter: Statementstmt 91.3% 21 / 23 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 93.5% 29 / 31 CC 1 SCAR 0.4
CC
1
Cov
91%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« App/financeta/gui/security_wizard.pm ↑ Summary App/financeta/gui/tradereport.pm »
1 package App::financeta::gui::editor;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 our $VERSION = '0.16';
7 $VERSION = eval $VERSION;
8 1 ▶use App::financeta::mo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use App::financeta::utils qw(dumper log_filter);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Log::Any '$log', filter => \&App::financeta::utils::log_filter;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::ShareDir 'dist_file';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 if ($^O !~ /win32/i) {
13 eval {
14 require POE;
15 require POE::Kernel;
16 POE::Kernel->import({loop => 'Prima'});
17 require POE::Session;
18 } or die "Unable to load POE::Loop::Prima";
19 }
20 0 ▶use Prima qw(Application Edit MsgBox sys::GUIException);
Subroutine BEGIN: called CC=1 SCAR=2.6
21 use Try::Tiny;
22 use App::financeta::language;
23  
24 $| = 1;
25 has debug => 0;
26 has parent => undef;
27 has main => (builder => '_build_main');
28 has brand => __PACKAGE__;
29 has tab_name => undef;
30 has compiler => (default => sub {
31 return App::financeta::language->new(debug => 0);
32 });
33  
34 sub _build_main {
35 my $self = shift;
36 my $mw = new Prima::Window(
37 name => 'editor',
38 text => $self->brand,
39 size => [640, 480],
40 owner => $self->parent->main,
41 centered => 1,
42 # force border styles for consistency
43 borderIcons => bi::All,
44 borderStyle => bs::Sizeable,
45 windowState => ws::Normal,
46 # origin
47 left => 10,
48 top => 0,
49 visible => 0,
50 menuItems => [[
51 '~Action' => [
52 [
53 'save_rules', '~Save', 'Ctrl+S', '^S',
54 sub {
55 my ($win, $item) = @_;
56 my $ed = $win->menu->options($item);
57 my $txt = $win->editor_edit->text;
58 $ed->parent->save_editor($txt, $ed->tab_name, 0);
59 },
60 $self,
61 ],
62 [
63 'close_rules', '~Close', 'Ctrl+W', '^W',
64 sub {
65 my ($win, $item) = @_;
66 my $ed = $win->menu->options($item);
67 my $txt = $win->editor_edit->text;
68 $ed->parent->save_editor($txt, $ed->tab_name, 1);
69 $ed->parent->close_editor($ed->tab_name); # force it
70 $win->close;
71 },
72 $self,
73 ],
74 [],
75 [
76 'compile_rules', 'Compile', 'Ctrl+B', '^B',
77 sub {
78 my ($win, $item) = @_;
79 my $ed = $win->menu->options($item);
80 my $txt = $win->editor_edit->text;
81 $ed->parent->save_editor($txt, $ed->tab_name, 0);
82 my $output = $ed->compile($txt);
83 #TODO: do something with the output
84 message_box('Compiled Output', $output,
85 mb::Ok | mb::Information) if defined $output;
86 },
87 $self,
88 ],
89 [
90 'execute_rules', 'Execute', 'Ctrl+R', '^R',
91 sub {
92 my ($win, $item) = @_;
93 my $ed = $win->menu->options($item);
94 my $txt = $win->editor_edit->text;
95 $ed->parent->save_editor($txt, $ed->tab_name, 0);
96 $ed->execute($txt);
97 },
98 $self,
99 ],
100 ]], [
101 '~Edit' => [
102 [
103 'edit_cut', 'Cut', 'Ctrl+X', '^X',
104 sub { $_[0]->editor_edit->cut },
105 ],
106 [
107 'edit_copy', 'Copy', 'Ctrl+C', '^C',
108 sub { $_[0]->editor_edit->copy },
109 ],
110 [
111 'edit_paste', 'Paste', 'Ctrl+V', '^V',
112 sub { $_[0]->editor_edit->paste},
113 ],
114 [
115 'edit_del', 'Delete', 'Ctrl+Del', '',
116 sub { $_[0]->editor_edit->delete_block },
117 ],
118 [],
119 [
120 'edit_undo', 'Undo', '', '',
121 sub { $_[0]->editor_edit->undo },
122 ],
123 [
124 'edit_redo', 'Redo', '', '',
125 sub { $_[0]->editor_edit->redo },
126 ],
127 [
128 'edit_select', 'Select All', 'Ctrl+A', '^A',
129 sub { $_[0]->editor_edit->select_all },
130 ],
131 ],
132 ]],
133 onDestroy => sub {
134 if ($self->parent and $self->tab_name) {
135 $self->parent->close_editor($self->tab_name);
136 }
137 },
138 );
139 my $is_dark_mode = (cl::to_gray_byte($mw->map_color($mw->color)) > cl::to_gray_byte($mw->map_color($mw->backColor))) ? 1 : 0;
140 $log->debug("Editor window dark mode test: $is_dark_mode\n");
141 my @sz = $mw->size;
142 $sz[0] *= 0.98;
143 $sz[1] *= 0.98;
144 my $ed = $mw->insert(Edit => name => 'editor_edit',
145 text => '#This line is auto-generated',
146 pack => { expand => 1, fill => 'both' },
147 syntaxHilite => 1,
148 hScroll => 1,
149 growMode => gm::Client | gm::GrowHiX | gm::GrowHiY,
150 hiliteNumbers => cl::Red,
151 hiliteQStrings => cl::Red,
152 hiliteQQStrings => cl::Red,
153 hiliteChars => [ ';:?/,~`!@#$%^&*()-_+=\|]}{[./', $is_dark_mode ? cl::White : cl::Black],
154 # hiliteIDs => [$keywords, cl::Green],
155 tabIndent => 4,
156 size => \@sz,
157 visible => 1,
158 # check these
159 cursorWrap => 1,
160 persistentBlock => 1,
161 wantTabs => 1,
162 );
163 my $regexes = $self->compiler->get_grammar_regexes($is_dark_mode);
164 my @arr = ();
165 my %color_scheme = (
166 conditions => $is_dark_mode ? cl::LightCyan : cl::Blue,
167 keywords => $is_dark_mode ? cl::Yellow : cl::Green,
168 booleans => $is_dark_mode ? cl::LightGreen : cl::Brown,
169 variables => $is_dark_mode ? cl::Magenta : cl::Red,
170 plain => $is_dark_mode ? cl::White : cl::Black,
171 );
172 foreach my $k (keys %$regexes) {
173 if (ref $regexes->{$k} eq 'ARRAY') {
174 push @arr, '(?i:(' . join('|', @{$regexes->{$k}}) . '))';
175 } else {
176 push @arr, $regexes->{$k};
177 }
178 push @arr, $color_scheme{$k} // $color_scheme{plain},
179 }
180 my $hlres = $ed->hiliteREs;
181 $ed->hiliteREs([@arr, @$hlres]);
182 return $mw;
183 }
184  
185 sub update_editor {
186 my ($self, $rules, $tabname, $vars, $hidden) = @_;
187 $self->tab_name($tabname) if defined $tabname;
188 $self->compiler->preset_vars($vars) if defined $vars;
189 $self->main->editor_edit->text($rules);
190 unless ($hidden) {
191 $self->main->show;
192 $self->main->bring_to_front;
193 }
194 1;
195 }
196  
197 sub close {
198 my $self = shift;
199 #my $win = $self->main;
200 #my $txt = $win->editor_edit->text;
201 #$self->parent->save_editor($txt, $self->tab_name, 1);
202 if ($self->parent) {
203 $self->parent->close_editor($self->tab_name);
204 }
205 $self->main->close;
206 }
207  
208 sub get_text {
209 my $self = shift;
210 return $self->main->editor_edit->text;
211 }
212  
213 sub compile {
214 my ($self, $txt) = @_;
215 my $output;
216 try {
217 $output = $self->compiler->compile($txt);
218 } catch {
219 my $err = $_;
220 $log->error("Compiler error:\n$err");
221 #TODO: create a better window
222 message("Compiler Error\n$err");
223 };
224 return $output;
225 }
226  
227 sub execute {
228 my ($self, $txt) = @_;
229 my $code = $self->compile($txt);
230 return unless $code;
231 try {
232 my $coderef = $self->compiler->generate_coderef($code);
233 if ($self->parent) {
234 $log->info("Executing rules for tab name: ", $self->tab_name);
235 $self->parent->execute_rules($self->tab_name, $coderef);
236 }
237 } catch {
238 my $err = $_;
239 $log->error("Error executing generated code:\n$err");
240 $log->debug("Erroneous code: $code");
241 #TODO: create a better window
242 message("Error executing generated code:\n$err");
243 };
244 }
245  
246 1;
247 __END__
« App/financeta/gui/security_wizard.pm ↑ Summary App/financeta/gui/tradereport.pm »