text_editor 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +3 -0
  3. data/.text_editor.rb +81 -2
  4. data/Gemfile.lock +1 -0
  5. data/README.org +103 -0
  6. data/bin/console +1 -1
  7. data/lib/core_ext/hash.rb +2 -0
  8. data/lib/core_ext/pathname.rb +2 -0
  9. data/lib/text_editor.rb +43 -13
  10. data/lib/text_editor/buffer.rb +3 -0
  11. data/lib/text_editor/clipboard.rb +1 -1
  12. data/lib/text_editor/clipboard/cygwin.rb +0 -4
  13. data/lib/text_editor/clipboard/linux.rb +0 -4
  14. data/lib/text_editor/clipboard/mac.rb +0 -4
  15. data/lib/text_editor/clipboard/unknown.rb +0 -4
  16. data/lib/text_editor/clipboard/windows.rb +0 -4
  17. data/lib/text_editor/command.rb +4 -4
  18. data/lib/text_editor/command/append_line_and_insert.rb +11 -0
  19. data/lib/text_editor/command/change_mode.rb +9 -0
  20. data/lib/text_editor/command/cursor_end_and_insert.rb +9 -0
  21. data/lib/text_editor/command/cursor_left.rb +1 -8
  22. data/lib/text_editor/command/cursor_left_with_wrap.rb +16 -0
  23. data/lib/text_editor/command/cursor_right.rb +2 -9
  24. data/lib/text_editor/command/cursor_right_and_insert.rb +9 -0
  25. data/lib/text_editor/command/cursor_right_with_wrap.rb +19 -0
  26. data/lib/text_editor/command/cursor_start_without_whitespace.rb +11 -0
  27. data/lib/text_editor/command/cursor_up.rb +1 -1
  28. data/lib/text_editor/command/forward_word.rb +16 -0
  29. data/lib/text_editor/command/insert_tab.rb +1 -1
  30. data/lib/text_editor/command/prepend_line_and_insert.rb +11 -0
  31. data/lib/text_editor/command/replace_line.rb +13 -0
  32. data/lib/text_editor/command/resolver.rb +3 -2
  33. data/lib/text_editor/component.rb +1 -1
  34. data/lib/text_editor/configuration.rb +11 -3
  35. data/lib/text_editor/configuration/bootstrap.rb +9 -3
  36. data/lib/text_editor/configuration/flavor.rb +20 -0
  37. data/lib/text_editor/configuration/log.rb +7 -0
  38. data/lib/text_editor/configuration/mode.rb +28 -0
  39. data/lib/text_editor/configuration/operating_system.rb +21 -0
  40. data/lib/text_editor/configuration/resolver.rb +1 -1
  41. data/lib/text_editor/configuration/tab.rb +11 -0
  42. data/lib/text_editor/cursor.rb +2 -0
  43. data/lib/text_editor/keyboard.rb +1 -1
  44. data/lib/text_editor/logger.rb +11 -0
  45. data/lib/text_editor/reactor.rb +5 -3
  46. data/lib/text_editor/terminal.rb +2 -19
  47. data/lib/text_editor/window.rb +34 -5
  48. data/spec/spec_helper.rb +3 -0
  49. data/spec/support/command_helper.rb +43 -0
  50. data/spec/text_editor/clipboard_spec.rb +0 -7
  51. data/spec/text_editor/command/append_line_and_insert_spec.rb +38 -0
  52. data/spec/text_editor/command/backspace_spec.rb +54 -0
  53. data/spec/text_editor/command/change_mode_spec.rb +17 -0
  54. data/spec/text_editor/command/cursor_down_spec.rb +57 -0
  55. data/spec/text_editor/command/cursor_end_and_insert_spec.rb +35 -0
  56. data/spec/text_editor/command/cursor_end_spec.rb +30 -0
  57. data/spec/text_editor/command/cursor_left_spec.rb +46 -0
  58. data/spec/text_editor/command/cursor_left_with_wrap_spec.rb +46 -0
  59. data/spec/text_editor/command/cursor_right_and_insert_spec.rb +58 -0
  60. data/spec/text_editor/command/cursor_right_spec.rb +53 -0
  61. data/spec/text_editor/command/cursor_right_with_wrap_spec.rb +54 -0
  62. data/spec/text_editor/command/cursor_start_spec.rb +33 -0
  63. data/spec/text_editor/command/cursor_start_without_whitespace_spec.rb +46 -0
  64. data/spec/text_editor/command/cursor_up_spec.rb +56 -0
  65. data/spec/text_editor/command/forward_word_spec.rb +8 -0
  66. data/spec/text_editor/command/insert_char_spec.rb +24 -0
  67. data/spec/text_editor/command/insert_line_spec.rb +26 -0
  68. data/spec/text_editor/command/insert_tab_spec.rb +27 -0
  69. data/spec/text_editor/command/prepend_line_and_insert_spec.rb +38 -0
  70. data/spec/text_editor/command/replace_line_spec.rb +34 -0
  71. data/spec/text_editor/command/resolver_spec.rb +2 -2
  72. data/spec/text_editor/command/write_file_spec.rb +25 -0
  73. data/spec/text_editor/command_spec.rb +8 -8
  74. data/spec/text_editor/component_spec.rb +7 -0
  75. data/spec/text_editor/configuration/log_spec.rb +26 -0
  76. data/spec/text_editor/{operating_system_spec.rb → configuration/operating_system_spec.rb} +1 -1
  77. data/spec/text_editor/configuration/tab_spec.rb +41 -0
  78. data/spec/text_editor/configuration_spec.rb +11 -4
  79. data/spec/text_editor/logger_spec.rb +17 -0
  80. data/text_editor.gemspec +2 -1
  81. metadata +84 -7
  82. data/lib/text_editor/mode.rb +0 -23
  83. data/lib/text_editor/mode/nano.rb +0 -30
  84. data/lib/text_editor/operating_system.rb +0 -19
@@ -0,0 +1,46 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe TextEditor::Command::CursorLeftWithWrap do
4
+ include CommandHelper
5
+
6
+ describe "#run" do
7
+ context "with an empty buffer" do
8
+ before do
9
+ expect_buffer("")
10
+ expect_cursor(0, 0)
11
+ end
12
+
13
+ it "does not move cursor" do
14
+ subject.run
15
+ expect_buffer("")
16
+ expect_cursor(0, 0)
17
+ end
18
+ end
19
+
20
+ context "with an non-empty buffer" do
21
+ before do
22
+ update_buffer("ab")
23
+ update_cursor(0, 1)
24
+ end
25
+
26
+ it "moves cursor to the left" do
27
+ subject.run
28
+ expect_buffer("ab")
29
+ expect_cursor(0, 0)
30
+ end
31
+ end
32
+
33
+ context "with a buffer containing multiple lines" do
34
+ before do
35
+ update_buffer("ab\ncd")
36
+ update_cursor(1, 0)
37
+ end
38
+
39
+ it "wraps to previous line" do
40
+ subject.run
41
+ expect_buffer("ab\ncd")
42
+ expect_cursor(0, 2)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,58 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe TextEditor::Command::CursorRightAndInsert do
4
+ include CommandHelper
5
+
6
+ let(:args) { %w(--flavor vim) }
7
+
8
+ before { expect_mode(:normal) }
9
+ after { expect_mode(:insert) }
10
+
11
+ describe "#run" do
12
+ context "with an empty buffer" do
13
+ before do
14
+ expect_buffer("")
15
+ expect_cursor(0, 0)
16
+ end
17
+
18
+ it "does not move cursor" do
19
+ subject.run
20
+ expect_buffer("")
21
+ expect_cursor(0, 0)
22
+ end
23
+ end
24
+
25
+ context "with an non-empty buffer" do
26
+ before { update_buffer("ab") }
27
+ after { expect_buffer("ab") }
28
+
29
+ it "moves cursor to the right" do
30
+ subject.run
31
+ expect_cursor(0, 1)
32
+ end
33
+
34
+ it "only moves one column past the end of the line" do
35
+ subject.run
36
+ expect_cursor(0, 1)
37
+
38
+ subject.run
39
+ expect_cursor(0, 2)
40
+
41
+ subject.run
42
+ expect_cursor(0, 2)
43
+ end
44
+ end
45
+
46
+ context "with a buffer containing multiple lines" do
47
+ before do
48
+ update_buffer("ab\ncd")
49
+ update_cursor(0, 2)
50
+ end
51
+
52
+ it "does not wrap to next line" do
53
+ subject.run
54
+ update_cursor(0, 2)
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,53 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe TextEditor::Command::CursorRight do
4
+ include CommandHelper
5
+
6
+ describe "#run" do
7
+ context "with an empty buffer" do
8
+ before do
9
+ expect_buffer("")
10
+ expect_cursor(0, 0)
11
+ end
12
+
13
+ it "does not move cursor" do
14
+ subject.run
15
+ expect_buffer("")
16
+ expect_cursor(0, 0)
17
+ end
18
+ end
19
+
20
+ context "with an non-empty buffer" do
21
+ before { update_buffer("ab") }
22
+ after { expect_buffer("ab") }
23
+
24
+ it "moves cursor to the right" do
25
+ subject.run
26
+ expect_cursor(0, 1)
27
+ end
28
+
29
+ it "only moves one column past the end of the line" do
30
+ subject.run
31
+ expect_cursor(0, 1)
32
+
33
+ subject.run
34
+ expect_cursor(0, 2)
35
+
36
+ subject.run
37
+ expect_cursor(0, 2)
38
+ end
39
+ end
40
+
41
+ context "with a buffer containing multiple lines" do
42
+ before do
43
+ update_buffer("ab\ncd")
44
+ update_cursor(0, 2)
45
+ end
46
+
47
+ it "does not wrap to next line" do
48
+ subject.run
49
+ update_cursor(0, 2)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,54 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe TextEditor::Command::CursorRightWithWrap do
4
+ include CommandHelper
5
+
6
+ describe "#run" do
7
+ context "with an empty buffer" do
8
+ before do
9
+ expect_buffer("")
10
+ expect_cursor(0, 0)
11
+ end
12
+
13
+ it "does not move cursor" do
14
+ subject.run
15
+ expect_buffer("")
16
+ expect_cursor(0, 0)
17
+ end
18
+ end
19
+
20
+ context "with an non-empty buffer" do
21
+ before { update_buffer("ab") }
22
+ after { expect_buffer("ab") }
23
+
24
+ it "moves cursor to the right" do
25
+ subject.run
26
+ expect_cursor(0, 1)
27
+ end
28
+
29
+ it "only moves one column past the end of the line" do
30
+ subject.run
31
+ expect_cursor(0, 1)
32
+
33
+ subject.run
34
+ expect_cursor(0, 2)
35
+
36
+ subject.run
37
+ expect_cursor(0, 2)
38
+ end
39
+ end
40
+
41
+ context "with a buffer containing multiple lines" do
42
+ before do
43
+ update_buffer("ab\ncd")
44
+ update_cursor(0, 2)
45
+ end
46
+
47
+ it "wraps to next line" do
48
+ subject.run
49
+ expect_buffer("ab\ncd")
50
+ expect_cursor(1, 0)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,33 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe TextEditor::Command::CursorStart do
4
+ include CommandHelper
5
+
6
+ describe "#run" do
7
+ context "with an empty buffer" do
8
+ before do
9
+ expect_buffer("")
10
+ expect_cursor(0, 0)
11
+ end
12
+
13
+ it "does not move cursor" do
14
+ subject.run
15
+ expect_buffer("")
16
+ expect_cursor(0, 0)
17
+ end
18
+ end
19
+
20
+ context "with an non-empty buffer" do
21
+ before do
22
+ update_buffer("ab cd")
23
+ update_cursor(0, 4)
24
+ end
25
+
26
+ it "moves cursor to the beginning of the line" do
27
+ subject.run
28
+ expect_buffer("ab cd")
29
+ expect_cursor(0, 0)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,46 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe TextEditor::Command::CursorStartWithoutWhitespace do
4
+ include CommandHelper
5
+
6
+ describe "#run" do
7
+ context "with an empty buffer" do
8
+ before do
9
+ expect_buffer("")
10
+ expect_cursor(0, 0)
11
+ end
12
+
13
+ it "does not move cursor" do
14
+ subject.run
15
+ expect_buffer("")
16
+ expect_cursor(0, 0)
17
+ end
18
+ end
19
+
20
+ context "with an non-empty buffer" do
21
+ before do
22
+ update_buffer("ab cd")
23
+ update_cursor(0, 4)
24
+ end
25
+
26
+ it "moves cursor to the beginning of the line" do
27
+ subject.run
28
+ expect_buffer("ab cd")
29
+ expect_cursor(0, 0)
30
+ end
31
+ end
32
+
33
+ context "with an non-empty buffer prefixed with whitespace" do
34
+ before do
35
+ update_buffer("\t ab cd")
36
+ update_cursor(0, 7)
37
+ end
38
+
39
+ it "moves cursor to the first non-whitespace character" do
40
+ subject.run
41
+ expect_buffer("\t ab cd")
42
+ expect_cursor(0, 3)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,56 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe TextEditor::Command::CursorUp do
4
+ include CommandHelper
5
+
6
+ describe "#run" do
7
+ context "with an empty buffer" do
8
+ before do
9
+ expect_buffer("")
10
+ expect_cursor(0, 0)
11
+ end
12
+
13
+ it "does not move cursor" do
14
+ subject.run
15
+ expect_buffer("")
16
+ expect_cursor(0, 0)
17
+ end
18
+ end
19
+
20
+ context "with a one line buffer" do
21
+ before { update_buffer("ab") }
22
+
23
+ it "does not move cursor" do
24
+ subject.run
25
+ expect_buffer("ab")
26
+ expect_cursor(0, 0)
27
+ end
28
+ end
29
+
30
+ context "with a buffer containing multiple lines" do
31
+ before do
32
+ update_buffer("ab\ncd")
33
+ update_cursor(1, 1)
34
+ end
35
+
36
+ it "moves up one line" do
37
+ subject.run
38
+ expect_buffer("ab\ncd")
39
+ expect_cursor(0, 1)
40
+ end
41
+ end
42
+
43
+ context "with a buffer containing different length lines" do
44
+ before do
45
+ update_buffer("a\nbcd")
46
+ update_cursor(1, 2)
47
+ end
48
+
49
+ it "moves up one line and clamps to the end" do
50
+ subject.run
51
+ expect_buffer("a\nbcd")
52
+ expect_cursor(0, 1)
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,8 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe TextEditor::Command::ForwardWord do
4
+ include CommandHelper
5
+
6
+ describe "#run" do
7
+ end
8
+ end
@@ -0,0 +1,24 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe TextEditor::Command::InsertChar do
4
+ include CommandHelper
5
+
6
+ describe "#run" do
7
+ before do
8
+ expect_buffer("")
9
+ expect_cursor(0, 0)
10
+ end
11
+
12
+ it "adds character and moves cursor" do
13
+ subject.run("a")
14
+ expect_buffer("a")
15
+ expect_cursor(0, 1)
16
+ end
17
+
18
+ it "adds multiple characters moves cursor" do
19
+ subject.run("abc")
20
+ expect_buffer("abc")
21
+ expect_cursor(0, 3)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,26 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe TextEditor::Command::InsertLine do
4
+ include CommandHelper
5
+
6
+ describe "#run" do
7
+ before do
8
+ expect_buffer("")
9
+ expect_cursor(0, 0)
10
+ end
11
+
12
+ it "adds newline and moves cursor" do
13
+ subject.run
14
+ expect_buffer("\n")
15
+ expect_cursor(1, 0)
16
+ end
17
+
18
+ it "adds newline in the middle of string" do
19
+ update_buffer("test")
20
+ update_cursor(0, 2)
21
+ subject.run
22
+ expect_buffer("te\nst")
23
+ expect_cursor(1, 0)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,27 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe TextEditor::Command::InsertTab do
4
+ include CommandHelper
5
+
6
+ describe "#run" do
7
+ before do
8
+ expect_buffer("")
9
+ expect_cursor(0, 0)
10
+ end
11
+
12
+ it "adds tab and moves cursor" do
13
+ subject.run
14
+ expect_buffer(" ")
15
+ expect_cursor(0, 2)
16
+ end
17
+
18
+ it "adds tab in the middle of string" do
19
+ update_buffer("test")
20
+ update_cursor(0, 2)
21
+ subject.run
22
+
23
+ expect_buffer("te st")
24
+ expect_cursor(0, 4)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,38 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe TextEditor::Command::PrependLineAndInsert do
4
+ include CommandHelper
5
+
6
+ let(:args) { %w(--flavor vim) }
7
+
8
+ before { expect_mode(:normal) }
9
+ after { expect_mode(:insert) }
10
+
11
+ describe "#run" do
12
+ context "with an empty buffer" do
13
+ before do
14
+ expect_buffer("")
15
+ expect_cursor(0, 0)
16
+ end
17
+
18
+ it "adds newline and moves cursor" do
19
+ subject.run
20
+ expect_buffer("\n")
21
+ expect_cursor(0, 0)
22
+ end
23
+ end
24
+
25
+ context "with a non-empty buffer" do
26
+ before do
27
+ update_buffer("123")
28
+ update_cursor(0, 2)
29
+ end
30
+
31
+ it "adds newline and moves cursor" do
32
+ subject.run
33
+ expect_buffer("\n123")
34
+ expect_cursor(0, 0)
35
+ end
36
+ end
37
+ end
38
+ end