-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumed.pl
More file actions
320 lines (270 loc) · 7.04 KB
/
numed.pl
File metadata and controls
320 lines (270 loc) · 7.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#! /usr/bin/env perl
#
# Short description for numed.pl
#
# Author Marcin Natanek <natanek.marcin@gmail.com>
# Version 0.1
# Copyright (C) 2015 Marcin Natanek <natanek.marcin@gmail.com>
# Modified On 2015-06-28 00:27
# Created 2015-06-28 00:27
#
use v5.20;
use warnings;
use autodie;
use Curses::UI;
use Data::Dumper;
# ----------------------- VARIABLES SECTION --------------------------
open my $file, "<", shift, or die "$!";
open my $dict, "<", "dictionary", or die "$!";
open my $debug, ">", "debug.out", or die "$!";
my @buffor = <$file>;
my $line = 0;
my $mode = "NORMAL";
my $t9queue = "";
my @dictionary;
my @current_suggestions;
my $suggestion_iterator = 0;
while (<$dict>) {
for my $hue (split) {
push @dictionary, $hue;
}
}
#say $debug Dumper(\@dictionary);
# --------------------- USER INTERFACE SECTION -----------------------
my $cui = new Curses::UI(-color_support => 1);
my @menu = (
{
-label => 'File',
-submenu => [
{
-label => 'Exit ^Q',
-value => \&exit_dialog
}
]
},
);
my $menu = $cui->add(
'menu', 'Menubar',
-menu => \@menu,
-fg => "Black",
);
my $win1 = $cui->add(
'win1', 'Window',
-y => 1,
-bfg => 'white',
);
my $indicator = $win1->add(
"indicator", "TextViewer",
-text => "$mode",
-x => $win1->width() - 20,
-bfg => 'white',
);
my $textfield = $win1->add(
"textfield", "TextEditor",
-text => join("", @buffor),
-pad => 1,
-border => 1,
);
# ----------------------- KEYBINDINGS SECTION --------------------------
# KEYPAD
$cui->set_binding(\&toggle_mode, "/");
$cui->set_binding(\&one, "1");
$cui->set_binding(\&down, "2");
$cui->set_binding(\&three, "3");
$cui->set_binding(\&left, "4");
$cui->set_binding(\&five, "5");
$cui->set_binding(\&right, "6");
$cui->set_binding(\&seven, "7");
$cui->set_binding(\&up, "8");
$cui->set_binding(\&nine, "9");
$cui->set_binding(\&zero, "0");
$cui->set_binding(\&plus, "+");
$cui->set_binding(\&minus, "-");
$cui->set_binding(\&comma, ",");
$cui->set_binding(\&exit_dialog, "");
# OTHER
$cui->set_binding(sub { $menu->focus() }, "\cX");
$cui->set_binding(\&exit_dialog, "\cQ");
$cui->set_binding(sub { exit 0 }, "\cC");
# ----------------------- PROCEDURES SECTION ---------------------------
sub exit_dialog() {
my $return = $cui->dialog(
-message => "Are you sure you want to discard unsaved changes?",
-title => "Exit",
-buttons => [ 'yes', 'no' ]
);
exit(0) if $return;
}
sub toggle_mode() {
if ($mode eq "NORMAL") {
$mode = "INPUT";
} else {
$mode = "NORMAL";
}
update_indicator();
}
sub update_indicator() {
$indicator->text("$mode - $t9queue");
$textfield->focus();
}
sub up() {
if ($mode eq "NORMAL") {
$textfield->cursor_up();
$textfield->focus();
} elsif ($mode eq "INPUT") {
t9input("8");
} elsif ($mode eq "MENU") {
}
}
sub down() {
if ($mode eq "NORMAL") {
$textfield->cursor_down();
$textfield->focus();
} elsif ($mode eq "INPUT") {
t9input("2");
} elsif ($mode eq "MENU") {
}
}
sub left() {
if ($mode eq "NORMAL") {
$textfield->cursor_left();
$textfield->focus();
} elsif ($mode eq "INPUT") {
t9input("4");
} elsif ($mode eq "MENU") {
}
}
sub right() {
if ($mode eq "NORMAL") {
$textfield->cursor_right();
$textfield->focus();
} elsif ($mode eq "INPUT") {
t9input("6");
} elsif ($mode eq "MENU") {
}
}
sub one() {
if ($mode eq "NORMAL") {
$line++ if $line + $textfield->height() < scalar @buffor;
$textfield->text(
join("", @buffor[ $line .. $line + $textfield->height() ]));
$textfield->focus();
} elsif ($mode eq "INPUT") {
t9input("1");
} elsif ($mode eq "MENU") {
}
}
sub three() {
if ($mode eq "NORMAL") {
$line-- if $line;
$textfield->text(
join("", @buffor[ $line .. $line + $textfield->height() ]));
$textfield->focus();
} elsif ($mode eq "INPUT") {
t9input("3");
} elsif ($mode eq "MENU") {
}
}
sub nine() {
if ($mode eq "NORMAL") {
#$textfield->cursor_end(); #no such subrutine?
$textfield->focus();
} elsif ($mode eq "INPUT") {
t9input("9");
} elsif ($mode eq "MENU") {
}
}
sub seven() {
if ($mode eq "NORMAL") {
#$textfield->cursor_home(); #no such subrutine?
$textfield->focus();
} elsif ($mode eq "INPUT") {
t9input("7");
} elsif ($mode eq "MENU") {
}
}
sub plus() {
if ($mode eq "NORMAL") {
} elsif ($mode eq "INPUT") {
if ($suggestion_iterator + 1 < @current_suggestions) {
$suggestion_iterator++;
} else {
$suggestion_iterator = 0;
}
t9input("");
} elsif ($mode eq "MENU") {
}
}
sub minus() {
if ($mode eq "NORMAL") {
$textfield->undo();
$textfield->focus();
} elsif ($mode eq "INPUT") {
if ($suggestion_iterator - 1 > -@current_suggestions) {
$suggestion_iterator--;
} else {
$suggestion_iterator = 0;
}
t9input("");
} elsif ($mode eq "MENU") {
}
}
sub five() {
if ($mode eq "NORMAL") {
} elsif ($mode eq "INPUT") {
t9input("5");
} elsif ($mode eq "MENU") {
}
}
sub zero() {
if ($mode eq "NORMAL") {
} elsif ($mode eq "INPUT") {
$t9queue = "";
$textfield->add_string(" ");
$textfield->focus();
@buffor = split /(?<=\n)/, $textfield->get();
} elsif ($mode eq "MENU") {
}
}
sub comma() {
$textfield->backspace();
#pop $t9queue; #ERROR how to remove last char from string?
$t9queue = substr($t9queue, 0, -1); #hotfix cause no interents :( TODO
$suggestion_iterator = 0;
update_indicator();
$textfield->focus();
}
sub t9input() {
$textfield->backspace() for 1 .. length($t9queue);
$t9queue .= shift;
update_indicator();
say $debug $t9queue;
@current_suggestions = t9_find_words($t9queue, \@dictionary);
say $debug Dumper(\@current_suggestions);
if (@current_suggestions) {
#buggy for 263? FIXME
$textfield->add_string($current_suggestions[$suggestion_iterator]);
} else {
$textfield->add_string($t9queue);
}
$textfield->focus();
@buffor = split /(?<=\n)/, $textfield->get();
}
our @T9NL = ('', '', 'ABC', 'DEF', 'GHI', 'JKL', 'MNO', 'PQRS', 'TUV', 'WXYZ');
sub t9_find_words($$) {
my $num = shift;
my $words = shift;
return () unless $num =~ /^[2-9]+$/;
my $len = length($num);
my $re;
for (0 .. $len - 1) {
$re .= "[" . $T9NL[ substr($num, $_, 1) ] . "]";
}
$re = "^$re\$";
return grep { /$re/i } grep { length($_) == $len } @$words;
}
# -------------------- START MAIN LOOP --------------------------
$textfield->focus();
$cui->mainloop();
close $file;
close $debug;