Featured image of post 第9回-完成!簡易エディタ - 統合と仕上げ - Mooで作る簡易テキストエディタ

第9回-完成!簡易エディタ - 統合と仕上げ - Mooで作る簡易テキストエディタ

これまで作成したEditor、コマンドクラス、History、MacroCommandを統合し、対話的に操作できるCLIベースの簡易テキストエディタを完成させます。

@nqounetです。

シリーズ「Mooで作る簡易テキストエディタ」の第9回です。

前回の振り返り

前回は、複数の操作を1つにまとめるMacroCommandクラスを実装しました。

MacroCommandを使えば、複数の操作を1回のUndo/Redoでまとめて処理できます。

1
2
3
4
5
6
7
my $macro = MacroCommand->new;
$macro->add_command($cmd1);
$macro->add_command($cmd2);
$macro->add_command($cmd3);

$history->execute_command($macro);  # 3つの操作を一括実行
$history->undo;                     # 3つの操作を一括Undo

今回は、これまで作成した全機能を統合し、対話的に操作できる簡易エディタを完成させます。

完成イメージ

最終的に、以下のような対話型CLIエディタを作ります。

 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
=== 簡易テキストエディタ ===
コマンド: i(挿入), d(削除), u(undo), r(redo), p(表示), q(終了)

> p
テキスト: ''

> i
挿入位置: 0
挿入文字列: Hello
テキスト: 'Hello'

> i
挿入位置: 5
挿入文字列:  World
テキスト: 'Hello World'

> u
Undo実行
テキスト: 'Hello'

> r
Redo実行
テキスト: 'Hello World'

> q
終了します。

対話ループを作成する

まず、ユーザーからの入力を受け付ける対話ループを作成します。

 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
# Perl v5.36 以降
# 外部依存: Moo

use v5.36;

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;  # Ctrl+Dで終了

        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'";
        }
    }
}

sub show_text ($editor) {
    say "テキスト: '" . $editor->text . "'";
}

各コマンドハンドラを実装する

次に、各コマンドに対応するハンドラ関数を実装します。

挿入コマンド (i)

 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
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);
}

挿入位置を検証し、範囲外ならエラーメッセージを表示します。

削除コマンド (d)

 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
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);
}

削除位置と削除文字数を検証し、範囲外ならエラーメッセージを表示します。

Undoコマンド (u)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
sub do_undo ($editor, $history) {
    if ($history->undo_stack->@* == 0) {
        say "Undoする操作がありません";
        return;
    }

    $history->undo;
    say "Undo実行";
    show_text($editor);
}

Undoスタックが空の場合は、メッセージを表示して何もしません。

Redoコマンド (r)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
sub do_redo ($editor, $history) {
    if ($history->redo_stack->@* == 0) {
        say "Redoする操作がありません";
        return;
    }

    $history->redo;
    say "Redo実行";
    show_text($editor);
}

Redoスタックが空の場合は、メッセージを表示して何もしません。

完成したエディタスクリプト

では、すべてを統合した完成版のエディタスクリプトを見てみましょう。

  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();

実行例とデモ

このスクリプトをeditor.plとして保存し、実行してみましょう。

1
$ perl editor.pl

以下は実行例です。

 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
=== 簡易テキストエディタ ===
コマンド: i(挿入), d(削除), u(undo), r(redo), p(表示), q(終了)

> p
テキスト: ''

> i
挿入位置: 0
挿入文字列: Hello
テキスト: 'Hello'

> i
挿入位置: 5
挿入文字列:  World
テキスト: 'Hello World'

> i
挿入位置: 11
挿入文字列: !
テキスト: 'Hello World!'

> p
テキスト: 'Hello World!'

> u
Undo実行
テキスト: 'Hello World'

> u
Undo実行
テキスト: 'Hello'

> r
Redo実行
テキスト: 'Hello World'

> d
削除開始位置: 5
削除文字数: 6
テキスト: 'Hello'

> u
Undo実行
テキスト: 'Hello World'

> q
終了します。

対話的に操作でき、Undo/Redoも正しく動作していることが確認できます。

エラー処理のデモ

エラー処理も確認してみましょう。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
> i
挿入位置: 100
エラー: 位置は0〜0の範囲で指定してください

> d
エラー: テキストが空です

> u
Undoする操作がありません

> r
Redoする操作がありません

範囲外の入力やスタックが空の場合に、適切なエラーメッセージが表示されます。

今回作成した完成コード

上記の完成したエディタスクリプトが、今回の完成コードです。

このスクリプトには、これまでのシリーズで作成した以下のクラスがすべて含まれています。

クラス役割
Editorテキストを保持する
Command::Roleコマンドの共通インターフェース
InsertCommand文字列を挿入する
DeleteCommand文字列を削除する
MacroCommand複数のコマンドをまとめる
HistoryUndo/Redo履歴を管理する

そして、対話的なCLIインターフェースを追加しました。

まとめ

  • これまで作成した全クラスを統合し、対話型エディタを完成させた
  • 対話ループでユーザー入力を受け付け、各コマンドに振り分ける
  • 入力値の検証を行い、エラー時は適切なメッセージを表示する
  • Undo/Redoスタックが空の場合もハンドリングする

次回予告

対話型エディタが完成しました!

次回は最終回として、これまで作ってきたエディタの設計が実はあるデザインパターンだったことをお話しします。シリーズ全体を振り返り、学んだことを整理します。

お楽しみに。

comments powered by Disqus
Hugo で構築されています。
テーマ StackJimmy によって設計されています。