vedeu 0.5.13 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +6 -0
  3. data/bin/vedeu_drb_server +1 -0
  4. data/config/rubocop_enabled.yml +2 -2
  5. data/docs/events.md +2 -0
  6. data/docs/getting_started.md +1 -1
  7. data/examples/drb_app.rb +1 -0
  8. data/examples/editor_app.rb +76 -0
  9. data/lib/vedeu/all.rb +13 -7
  10. data/lib/vedeu/{bindings.rb → bindings/bindings.rb} +2 -0
  11. data/lib/vedeu/bindings/focus.rb +66 -0
  12. data/lib/vedeu/bindings/refresh.rb +77 -0
  13. data/lib/vedeu/bindings/system.rb +19 -88
  14. data/lib/vedeu/buffers/buffer.rb +8 -8
  15. data/lib/vedeu/configuration/api.rb +32 -11
  16. data/lib/vedeu/configuration/cli.rb +8 -0
  17. data/lib/vedeu/cursor/move.rb +1 -0
  18. data/lib/vedeu/distributed/templates/default_configuration.vedeu +1 -0
  19. data/lib/vedeu/{dsl.rb → dsl/dsl.rb} +0 -0
  20. data/lib/vedeu/exceptions.rb +1 -1
  21. data/lib/vedeu/geometry/position.rb +28 -0
  22. data/lib/vedeu/input/capture.rb +76 -0
  23. data/lib/vedeu/input/editor/all.rb +19 -0
  24. data/lib/vedeu/input/editor/cropper.rb +74 -0
  25. data/lib/vedeu/input/editor/document.rb +254 -0
  26. data/lib/vedeu/input/editor/documents.rb +18 -0
  27. data/lib/vedeu/input/editor/editor.rb +100 -0
  28. data/lib/vedeu/input/editor/line.rb +143 -0
  29. data/lib/vedeu/input/editor/lines.rb +199 -0
  30. data/lib/vedeu/input/editor/virtual_cursor.rb +167 -0
  31. data/lib/vedeu/input/input.rb +9 -93
  32. data/lib/vedeu/input/input_translator.rb +155 -0
  33. data/lib/vedeu/internal_api.rb +9 -0
  34. data/lib/vedeu/log/lockless_log_device.rb +1 -1
  35. data/lib/vedeu/models/escape.rb +7 -8
  36. data/lib/vedeu/output/direct.rb +59 -0
  37. data/lib/vedeu/output/renderers/terminal.rb +8 -0
  38. data/lib/vedeu/output/viewport.rb +2 -3
  39. data/lib/vedeu/{plugins.rb → plugins/plugins.rb} +0 -0
  40. data/lib/vedeu/refresh/refresh_cursor.rb +1 -0
  41. data/lib/vedeu/{repositories.rb → repositories/repositories.rb} +0 -0
  42. data/lib/vedeu/terminal/content.rb +88 -0
  43. data/lib/vedeu/terminal/mode.rb +79 -0
  44. data/lib/vedeu/{terminal.rb → terminal/terminal.rb} +9 -25
  45. data/lib/vedeu/version.rb +1 -1
  46. data/test/lib/vedeu/{bindings_test.rb → bindings/bindings_test.rb} +0 -0
  47. data/test/lib/vedeu/bindings/focus_test.rb +19 -0
  48. data/test/lib/vedeu/bindings/refresh_test.rb +19 -0
  49. data/test/lib/vedeu/bindings/system_test.rb +1 -6
  50. data/test/lib/vedeu/borders/render_border_test.rb +43 -10
  51. data/test/lib/vedeu/buffers/buffer_test.rb +18 -0
  52. data/test/lib/vedeu/buffers/virtual_buffer_test.rb +15 -0
  53. data/test/lib/vedeu/configuration/api_test.rb +18 -2
  54. data/test/lib/vedeu/configuration/cli_test.rb +5 -0
  55. data/test/lib/vedeu/cursor/move_test.rb +1 -1
  56. data/test/lib/vedeu/dsl/dsl_test.rb +8 -0
  57. data/test/lib/vedeu/dsl/shared_test.rb +12 -0
  58. data/test/lib/vedeu/{output → esc}/esc_test.rb +0 -0
  59. data/test/lib/vedeu/geometry/position_test.rb +56 -0
  60. data/test/lib/vedeu/input/capture_test.rb +63 -0
  61. data/test/lib/vedeu/input/editor/cropper_test.rb +82 -0
  62. data/test/lib/vedeu/input/editor/document_test.rb +190 -0
  63. data/test/lib/vedeu/input/editor/documents_test.rb +17 -0
  64. data/test/lib/vedeu/input/editor/editor_test.rb +170 -0
  65. data/test/lib/vedeu/input/editor/line_test.rb +333 -0
  66. data/test/lib/vedeu/input/editor/lines_test.rb +561 -0
  67. data/test/lib/vedeu/input/editor/virtual_cursor_test.rb +184 -0
  68. data/test/lib/vedeu/input/input_test.rb +20 -4
  69. data/test/lib/vedeu/input/input_translator_test.rb +32 -0
  70. data/test/lib/vedeu/internal_api_test.rb +1 -0
  71. data/test/lib/vedeu/models/escape_test.rb +6 -6
  72. data/test/lib/vedeu/models/views/view_test.rb +15 -2
  73. data/test/lib/vedeu/null/null_test.rb +8 -0
  74. data/test/lib/vedeu/output/direct_test.rb +70 -0
  75. data/test/lib/vedeu/{plugins_test.rb → plugins/plugins_test.rb} +0 -0
  76. data/test/lib/vedeu/{repositories_test.rb → repositories/repositories_test.rb} +0 -0
  77. data/test/lib/vedeu/terminal/content_test.rb +100 -0
  78. data/test/lib/vedeu/terminal/mode_test.rb +95 -0
  79. data/test/lib/vedeu/{terminal_test.rb → terminal/terminal_test.rb} +23 -23
  80. data/test/test_helper.rb +1 -0
  81. metadata +67 -20
  82. data/lib/vedeu/terminal_mode.rb +0 -59
  83. data/test/lib/vedeu/terminal_mode_test.rb +0 -72
@@ -0,0 +1,167 @@
1
+ module Vedeu
2
+
3
+ module Editor
4
+
5
+ # Maintains a cursor position within the Vedeu::Editor::Document class.
6
+ #
7
+ class VirtualCursor
8
+
9
+ # @!attribute [rw] bx
10
+ # @return [Fixnum]
11
+ attr_accessor :bx
12
+
13
+ # @!attribute [rw] by
14
+ # @return [Fixnum]
15
+ attr_accessor :by
16
+
17
+ # @!attribute [rw] bxn
18
+ # @return [Fixnum]
19
+ attr_accessor :bxn
20
+
21
+ # @!attribute [rw] byn
22
+ # @return [Fixnum]
23
+ attr_accessor :byn
24
+
25
+ # @!attribute [rw] ox
26
+ # @return [Fixnum]
27
+ attr_accessor :ox
28
+
29
+ # @!attribute [rw] oy
30
+ # @return [Fixnum]
31
+ attr_accessor :oy
32
+
33
+ # @!attribute [rw] x
34
+ # @return [Fixnum]
35
+ attr_accessor :x
36
+
37
+ # @!attribute [rw] y
38
+ # @return [Fixnum]
39
+ attr_accessor :y
40
+
41
+ # Returns a new instance of Vedeu::Editor::VirtualCursor.
42
+ #
43
+ # @param y [Fixnum] The current line.
44
+ # @param x [Fixnum] The current character with the line.
45
+ # @param by [Fixnum]
46
+ # @param bx [Fixnum]
47
+ # @param byn [Fixnum]
48
+ # @param bxn [Fixnum]
49
+ # @param oy [Fixnum]
50
+ # @param ox [Fixnum]
51
+ # @return [Vedeu::Editor::VirtualCursor]
52
+ def initialize(y: 0, x: 0, by:, bx:, byn:, bxn:, oy: 0, ox: 0)
53
+ @y = (y.nil? || y < 0) ? 0 : y
54
+ @x = (x.nil? || x < 0) ? 0 : x
55
+ @by = by
56
+ @bx = bx
57
+ @byn = byn
58
+ @bxn = bxn
59
+ @ox = ox
60
+ @oy = oy
61
+ end
62
+
63
+ # Move the virtual cursor to the beginning of the line.
64
+ #
65
+ # @return [Vedeu::Editor::VirtualCursor]
66
+ def bol
67
+ @ox = 0
68
+ @x = 0
69
+
70
+ self
71
+ end
72
+
73
+ # Move the virtual cursor down by one line.
74
+ #
75
+ # @return [Vedeu::Editor::VirtualCursor]
76
+ def down
77
+ @y += 1
78
+
79
+ self
80
+ end
81
+
82
+ # Move the virtual cursor to the left.
83
+ #
84
+ # @return [Vedeu::Editor::VirtualCursor]
85
+ def left
86
+ @ox -= 1 unless @ox == 0
87
+ @x -= 1
88
+
89
+ self
90
+ end
91
+
92
+ # Move the virtual cursor to the origin (0, 0).
93
+ #
94
+ # @return [Vedeu::Editor::VirtualCursor]
95
+ def reset!
96
+ @x = 0
97
+ @y = 0
98
+ @ox = 0
99
+ @oy = 0
100
+
101
+ self
102
+ end
103
+
104
+ # Move the virtual cursor to the right.
105
+ #
106
+ # @return [Vedeu::Editor::VirtualCursor]
107
+ def right
108
+ @x += 1
109
+
110
+ self
111
+ end
112
+
113
+ # Return the escape sequence for setting the cursor position and show the
114
+ # cursor.
115
+ #
116
+ # @return [String]
117
+ def to_s
118
+ "\e[#{real_y};#{real_x}H\e[?25h"
119
+ end
120
+
121
+ # Move the virtual cursor up by one line.
122
+ #
123
+ # @return [Vedeu::Editor::VirtualCursor]
124
+ def up
125
+ @oy -= 1 unless @oy == 0
126
+ @y -= 1
127
+
128
+ self
129
+ end
130
+
131
+ # @return [Fixnum] The column/character coordinate.
132
+ def x
133
+ @x = 0 if @x <= 0
134
+ @ox = @x - (bxn - bx) if @x > bxn - bx
135
+
136
+ @x
137
+ end
138
+
139
+ # @return [Fixnum] The row/line coordinate.
140
+ def y
141
+ @y = 0 if @y <= 0
142
+ @oy = @y - (byn - by) if @y > byn - by
143
+
144
+ @y
145
+ end
146
+
147
+ private
148
+
149
+ # Return the real y coordinate.
150
+ #
151
+ # @return [Fixnum]
152
+ def real_y
153
+ (by + y) - oy
154
+ end
155
+
156
+ # Return the real x coordinate.
157
+ #
158
+ # @return [Fixnum]
159
+ def real_x
160
+ (bx + x) - ox
161
+ end
162
+
163
+ end # VirtualCursor
164
+
165
+ end # Editor
166
+
167
+ end # Vedeu
@@ -31,6 +31,9 @@ module Vedeu
31
31
  if reader.raw_mode?
32
32
  Vedeu.trigger(:_keypress_, keypress)
33
33
 
34
+ elsif reader.fake_mode?
35
+ Vedeu.trigger(:_editor_, keypress)
36
+
34
37
  else
35
38
  Vedeu.trigger(:_command_, command)
36
39
 
@@ -45,14 +48,6 @@ module Vedeu
45
48
 
46
49
  private
47
50
 
48
- # Returns the input from the terminal.
49
- #
50
- # @return [String]
51
- def input
52
- @input ||= reader.read
53
- end
54
- alias_method :command, :input
55
-
56
51
  # Returns the translated (when possible) keypress(es) as either a String or
57
52
  # a Symbol.
58
53
  #
@@ -60,95 +55,16 @@ module Vedeu
60
55
  def keypress
61
56
  key = input
62
57
 
63
- specials.fetch(key, key)
58
+ Vedeu::InputTranslator.translate(key)
64
59
  end
65
60
 
66
- # Translates (when possible) entered escape sequences into symbols
67
- # representing the key which was pressed.
61
+ # Returns the input from the terminal.
68
62
  #
69
- # @return [Hash]
70
- def specials
71
- {
72
- "\r" => :enter,
73
- "\n" => :enter,
74
- "\t" => :tab,
75
- "\e" => :escape,
76
- "\e[A" => :up,
77
- "\e[B" => :down,
78
- "\e[C" => :right,
79
- "\e[D" => :left,
80
- "\e[5~" => :page_up,
81
- "\e[6~" => :page_down,
82
- "\e[H" => :home,
83
- "\e[3~" => :delete,
84
- "\e[F" => :end,
85
- "\e[Z" => :shift_tab,
86
- "\e[1;2P" => :print_screen,
87
- "\e[1;2Q" => :scroll_lock,
88
- "\e[1;2R" => :pause_break,
89
- "\u007F" => :backspace,
90
- "\u0001" => :ctrl_a,
91
- "\u0002" => :ctrl_b,
92
- "\u0003" => :ctrl_c,
93
- "\u0004" => :ctrl_d,
94
- "\u0005" => :ctrl_e,
95
- "\u0006" => :ctrl_f,
96
- "\u0007" => :ctrl_g,
97
- "\u0008" => :ctrl_h,
98
- # "\u0009" => :ctrl_i, # duplicates tab
99
- "\u0010" => :ctrl_j, # produces "\n"
100
- "\u0011" => :ctrl_k,
101
- "\u0012" => :ctrl_l,
102
- "\u0013" => :ctrl_m,
103
- "\u0014" => :ctrl_n,
104
- "\u0015" => :ctrl_o,
105
- "\u0016" => :ctrl_p,
106
- "\u0017" => :ctrl_q,
107
- "\u0018" => :ctrl_r,
108
- "\u0019" => :ctrl_s,
109
- # "\u0020" => :ctrl_t, # duplicates spacebar
110
- "\u0021" => :ctrl_u,
111
- "\u0022" => :ctrl_v,
112
- "\u0023" => :ctrl_w,
113
- "\u0024" => :ctrl_x,
114
- "\u0025" => :ctrl_y,
115
- "\u0026" => :ctrl_z,
116
- }.merge!(f_keys)
117
- end
118
-
119
- # @return [Hash<String => Symbol]
120
- def f_keys
121
- {
122
- "\eOP" => :f1,
123
- "\eOQ" => :f2,
124
- "\eOR" => :f3,
125
- "\eOS" => :f4,
126
- "\e[15~" => :f5,
127
- "\e[15;2~" => :shift_f5,
128
- "\e[15;5~" => :ctrl_f5,
129
- "\e[17~" => :f6,
130
- "\e[17;2~" => :shift_f6,
131
- "\e[17;5~" => :ctrl_f6,
132
- "\e[18~" => :f7,
133
- "\e[18;2~" => :shift_f7,
134
- "\e[18;5~" => :ctrl_f7,
135
- "\e[19~" => :f8,
136
- "\e[19;2~" => :shift_f8,
137
- "\e[19;5~" => :ctrl_f8,
138
- "\e[20~" => :f9,
139
- "\e[20;2~" => :shift_f9,
140
- "\e[20;5~" => :ctrl_f9,
141
- "\e[21~" => :f10,
142
- "\e[21;2~" => :shift_f10,
143
- "\e[21;5~" => :ctrl_f10,
144
- "\e[23~" => :f11,
145
- "\e[23;2~" => :shift_f11,
146
- "\e[23;5~" => :ctrl_f11,
147
- "\e[24~" => :f12,
148
- "\e[24;2~" => :shift_f12,
149
- "\e[24;5~" => :ctrl_f12,
150
- }
63
+ # @return [String]
64
+ def input
65
+ @input ||= reader.read
151
66
  end
67
+ alias_method :command, :input
152
68
 
153
69
  end # Input
154
70
 
@@ -0,0 +1,155 @@
1
+ module Vedeu
2
+
3
+ # Translates escape sequences provided by the terminal into symbols which
4
+ # Vedeu can use for various behaviours.
5
+ #
6
+ class InputTranslator
7
+
8
+ # @param code [String]
9
+ # @return [Symbol]
10
+ def self.translate(code)
11
+ new(code).translate
12
+ end
13
+
14
+ # Returns a new instance of Vedeu::InputTranslator.
15
+ #
16
+ # @param code [String]
17
+ # @return [Vedeu::InputTranslator]
18
+ def initialize(code)
19
+ @code = code
20
+ end
21
+
22
+ # @return [Symbol]
23
+ def translate
24
+ symbols.fetch(code, code)
25
+ end
26
+
27
+ protected
28
+
29
+ # @!attribute [r] code
30
+ # @return [String]
31
+ attr_reader :code
32
+
33
+ private
34
+
35
+ # @return [Hash<String => Symbol>]
36
+ def symbols
37
+ @symbols ||= f_keys.merge!(ctrl_letters).merge!(specials)
38
+ end
39
+
40
+ # @return [Hash<String => Symbol]
41
+ def ctrl_letters
42
+ {
43
+ "\u0001" => :ctrl_a,
44
+ "\u0002" => :ctrl_b,
45
+ "\u0003" => :ctrl_c,
46
+ "\u2404" => :ctrl_c,
47
+ "\u0004" => :ctrl_d,
48
+ "\u2403" => :ctrl_d,
49
+ "\u0005" => :ctrl_e,
50
+ "\u0006" => :ctrl_f,
51
+ "\u0007" => :ctrl_g,
52
+ "\u0008" => :ctrl_h,
53
+ # "\u0009" => :ctrl_i, # duplicates tab
54
+ "\u0010" => :ctrl_j, # produces "\n"
55
+ "\u0011" => :ctrl_k,
56
+ "\u0012" => :ctrl_l,
57
+ "\u0013" => :ctrl_m,
58
+ "\u0014" => :ctrl_n,
59
+ "\u0015" => :ctrl_o,
60
+ "\u0016" => :ctrl_p,
61
+ "\u0017" => :ctrl_q,
62
+ "\u0018" => :ctrl_r,
63
+ "\u2412" => :ctrl_r,
64
+ "\u0019" => :ctrl_s,
65
+ # "\u0020" => :ctrl_t, # duplicates spacebar
66
+ "\u0021" => :ctrl_u,
67
+ "\u0022" => :ctrl_v,
68
+ "\u0023" => :ctrl_w,
69
+ "\u0024" => :ctrl_x,
70
+ "\u0025" => :ctrl_y,
71
+ "\u0026" => :ctrl_z,
72
+ }
73
+ end
74
+
75
+ # @return [Hash<String => Symbol]
76
+ def f_keys
77
+ {
78
+ "\eOP" => :f1,
79
+ "\eOQ" => :f2,
80
+ "\eOR" => :f3,
81
+ "\eOS" => :f4,
82
+
83
+ "\e[15~" => :f5,
84
+ "\e[15;2~" => :shift_f5,
85
+ "\e[15;5~" => :ctrl_f5,
86
+
87
+ "\e[17~" => :f6,
88
+ "\e[17;2~" => :shift_f6,
89
+ "\e[17;5~" => :ctrl_f6,
90
+
91
+ "\e[18~" => :f7,
92
+ "\e[18;2~" => :shift_f7,
93
+ "\e[18;5~" => :ctrl_f7,
94
+
95
+ "\e[19~" => :f8,
96
+ "\e[19;2~" => :shift_f8,
97
+ "\e[19;5~" => :ctrl_f8,
98
+
99
+ "\e[20~" => :f9,
100
+ "\e[20;2~" => :shift_f9,
101
+ "\e[20;5~" => :ctrl_f9,
102
+
103
+ "\e[21~" => :f10,
104
+ "\e[21;2~" => :shift_f10,
105
+ "\e[21;5~" => :ctrl_f10,
106
+
107
+ "\e[23~" => :f11,
108
+ "\e[23;2~" => :shift_f11,
109
+ "\e[23;5~" => :ctrl_f11,
110
+
111
+ "\e[24~" => :f12,
112
+ "\e[24;2~" => :shift_f12,
113
+ "\e[24;5~" => :ctrl_f12,
114
+ }
115
+ end
116
+
117
+ # @return [Hash<String => Symbol>]
118
+ def specials
119
+ {
120
+ "\u007F" => :backspace,
121
+ "\u2408" => :backspace,
122
+ "\u23CE" => :carriage_return,
123
+ "\e[3~" => :delete,
124
+ "\u232B" => :delete,
125
+ "\e[B" => :down,
126
+ "\u2193" => :down,
127
+ "\e[F" => :end,
128
+ "\r" => :enter,
129
+ "\n" => :enter,
130
+ "\e" => :escape,
131
+ "\u238B" => :escape,
132
+ "\e[H" => :home,
133
+ "\eOH" => :home,
134
+ "\e[2~" => :insert,
135
+ "\e[D" => :left,
136
+ "\u2190" => :left,
137
+ "\u240A" => :line_feed,
138
+ "\e[5~" => :page_up,
139
+ "\e[6~" => :page_down,
140
+ "\e[1;2R" => :pause_break,
141
+ "\e[1;2P" => :print_screen,
142
+ "\e[C" => :right,
143
+ "\u2192" => :right,
144
+ "\e[1;2Q" => :scroll_lock,
145
+ "\e[Z" => :shift_tab,
146
+ "\t" => :tab,
147
+ "\u21B9" => :tab,
148
+ "\e[A" => :up,
149
+ "\u2191" => :up,
150
+ }
151
+ end
152
+
153
+ end # InputTranslator
154
+
155
+ end # Vedeu
@@ -55,6 +55,15 @@ module Vedeu
55
55
  # @return [Vedeu::Debug]
56
56
  def_delegators Vedeu::Debug, :debug
57
57
 
58
+ # Manipulate the repository of documents.
59
+ #
60
+ # @example
61
+ # Vedeu.documents
62
+ #
63
+ # @!method documents
64
+ # @return [Vedeu::Editor::Documents]
65
+ def_delegators Vedeu::Editor::Documents, :documents
66
+
58
67
  # Manipulate the repository of events.
59
68
  #
60
69
  # @example