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
321
322
| #!/usr/bin/env perl
# Perl v5.36 以降
# 外部依存: Moo
use v5.36;
package Editor {
use Moo;
has text => (
is => 'rw',
default => '',
);
};
package Command::Role {
use Moo::Role;
requires 'execute';
requires 'undo';
};
package InsertCommand {
use Moo;
with 'Command::Role';
has editor => (
is => 'ro',
required => 1,
);
has position => (
is => 'ro',
required => 1,
);
has string => (
is => 'ro',
required => 1,
);
sub execute ($self) {
my $editor = $self->editor;
my $position = $self->position;
my $string = $self->string;
my $current = $editor->text;
my $new_text = substr($current, 0, $position)
. $string
. substr($current, $position);
$editor->text($new_text);
}
sub undo ($self) {
my $editor = $self->editor;
my $position = $self->position;
my $length = length($self->string);
my $current = $editor->text;
my $new_text = substr($current, 0, $position)
. substr($current, $position + $length);
$editor->text($new_text);
}
};
package DeleteCommand {
use Moo;
with 'Command::Role';
has editor => (
is => 'ro',
required => 1,
);
has position => (
is => 'ro',
required => 1,
);
has length => (
is => 'ro',
required => 1,
);
has _deleted_string => (
is => 'rw',
default => '',
);
sub execute ($self) {
my $editor = $self->editor;
my $position = $self->position;
my $length = $self->length;
my $current = $editor->text;
my $deleted = substr($current, $position, $length);
$self->_deleted_string($deleted);
my $new_text = substr($current, 0, $position)
. substr($current, $position + $length);
$editor->text($new_text);
}
sub undo ($self) {
my $editor = $self->editor;
my $position = $self->position;
my $deleted = $self->_deleted_string;
my $current = $editor->text;
my $new_text = substr($current, 0, $position)
. $deleted
. substr($current, $position);
$editor->text($new_text);
}
};
package MacroCommand {
use Moo;
with 'Command::Role';
has commands => (
is => 'ro',
default => sub { [] },
);
sub add_command ($self, $command) {
push $self->commands->@*, $command;
}
sub execute ($self) {
for my $cmd ($self->commands->@*) {
$cmd->execute;
}
}
sub undo ($self) {
for my $cmd (reverse $self->commands->@*) {
$cmd->undo;
}
}
};
package History {
use Moo;
has undo_stack => (
is => 'ro',
default => sub { [] },
);
has redo_stack => (
is => 'ro',
default => sub { [] },
);
sub execute_command ($self, $command) {
$command->execute;
push $self->undo_stack->@*, $command;
$self->redo_stack->@* = ();
}
sub undo ($self) {
return unless $self->undo_stack->@*;
my $command = pop $self->undo_stack->@*;
$command->undo;
push $self->redo_stack->@*, $command;
}
sub redo ($self) {
return unless $self->redo_stack->@*;
my $command = pop $self->redo_stack->@*;
$command->execute;
push $self->undo_stack->@*, $command;
}
};
# ヘルパー関数
sub show_text ($editor) {
say "テキスト: '" . $editor->text . "'";
}
sub do_insert ($editor, $history) {
print "挿入位置: ";
my $pos_input = <STDIN>;
return unless defined $pos_input;
chomp $pos_input;
my $position = int($pos_input);
my $max_pos = length($editor->text);
if ($position < 0 || $position > $max_pos) {
say "エラー: 位置は0〜$max_posの範囲で指定してください";
return;
}
print "挿入文字列: ";
my $string = <STDIN>;
return unless defined $string;
chomp $string;
if ($string eq '') {
say "エラー: 挿入文字列が空です";
return;
}
my $cmd = InsertCommand->new(
editor => $editor,
position => $position,
string => $string,
);
$history->execute_command($cmd);
show_text($editor);
}
sub do_delete ($editor, $history) {
if ($editor->text eq '') {
say "エラー: テキストが空です";
return;
}
print "削除開始位置: ";
my $pos_input = <STDIN>;
return unless defined $pos_input;
chomp $pos_input;
my $position = int($pos_input);
my $max_pos = length($editor->text) - 1;
if ($position < 0 || $position > $max_pos) {
say "エラー: 位置は0〜$max_posの範囲で指定してください";
return;
}
print "削除文字数: ";
my $len_input = <STDIN>;
return unless defined $len_input;
chomp $len_input;
my $length = int($len_input);
my $max_length = length($editor->text) - $position;
if ($length < 1 || $length > $max_length) {
say "エラー: 削除文字数は1〜$max_lengthの範囲で指定してください";
return;
}
my $cmd = DeleteCommand->new(
editor => $editor,
position => $position,
length => $length,
);
$history->execute_command($cmd);
show_text($editor);
}
sub do_undo ($editor, $history) {
if ($history->undo_stack->@* == 0) {
say "Undoする操作がありません";
return;
}
$history->undo;
say "Undo実行";
show_text($editor);
}
sub do_redo ($editor, $history) {
if ($history->redo_stack->@* == 0) {
say "Redoする操作がありません";
return;
}
$history->redo;
say "Redo実行";
show_text($editor);
}
# メイン処理
sub main {
say "=== 簡易テキストエディタ ===";
say "コマンド: i(挿入), d(削除), u(undo), r(redo), p(表示), q(終了)";
say "";
my $editor = Editor->new;
my $history = History->new;
while (1) {
print "> ";
my $input = <STDIN>;
last unless defined $input;
chomp $input;
my $cmd = lc($input);
if ($cmd eq 'q') {
say "終了します。";
last;
}
elsif ($cmd eq 'p') {
show_text($editor);
}
elsif ($cmd eq 'i') {
do_insert($editor, $history);
}
elsif ($cmd eq 'd') {
do_delete($editor, $history);
}
elsif ($cmd eq 'u') {
do_undo($editor, $history);
}
elsif ($cmd eq 'r') {
do_redo($editor, $history);
}
else {
say "不明なコマンド: '$input'" if $input ne '';
}
}
}
main();
|