text_editor 0.0.0 → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/.text_editor.rb +81 -2
- data/Gemfile.lock +1 -0
- data/README.org +103 -0
- data/bin/console +1 -1
- data/lib/core_ext/hash.rb +2 -0
- data/lib/core_ext/pathname.rb +2 -0
- data/lib/text_editor.rb +43 -13
- data/lib/text_editor/buffer.rb +3 -0
- data/lib/text_editor/clipboard.rb +1 -1
- data/lib/text_editor/clipboard/cygwin.rb +0 -4
- data/lib/text_editor/clipboard/linux.rb +0 -4
- data/lib/text_editor/clipboard/mac.rb +0 -4
- data/lib/text_editor/clipboard/unknown.rb +0 -4
- data/lib/text_editor/clipboard/windows.rb +0 -4
- data/lib/text_editor/command.rb +4 -4
- data/lib/text_editor/command/append_line_and_insert.rb +11 -0
- data/lib/text_editor/command/change_mode.rb +9 -0
- data/lib/text_editor/command/cursor_end_and_insert.rb +9 -0
- data/lib/text_editor/command/cursor_left.rb +1 -8
- data/lib/text_editor/command/cursor_left_with_wrap.rb +16 -0
- data/lib/text_editor/command/cursor_right.rb +2 -9
- data/lib/text_editor/command/cursor_right_and_insert.rb +9 -0
- data/lib/text_editor/command/cursor_right_with_wrap.rb +19 -0
- data/lib/text_editor/command/cursor_start_without_whitespace.rb +11 -0
- data/lib/text_editor/command/cursor_up.rb +1 -1
- data/lib/text_editor/command/forward_word.rb +16 -0
- data/lib/text_editor/command/insert_tab.rb +1 -1
- data/lib/text_editor/command/prepend_line_and_insert.rb +11 -0
- data/lib/text_editor/command/replace_line.rb +13 -0
- data/lib/text_editor/command/resolver.rb +3 -2
- data/lib/text_editor/component.rb +1 -1
- data/lib/text_editor/configuration.rb +11 -3
- data/lib/text_editor/configuration/bootstrap.rb +9 -3
- data/lib/text_editor/configuration/flavor.rb +20 -0
- data/lib/text_editor/configuration/log.rb +7 -0
- data/lib/text_editor/configuration/mode.rb +28 -0
- data/lib/text_editor/configuration/operating_system.rb +21 -0
- data/lib/text_editor/configuration/resolver.rb +1 -1
- data/lib/text_editor/configuration/tab.rb +11 -0
- data/lib/text_editor/cursor.rb +2 -0
- data/lib/text_editor/keyboard.rb +1 -1
- data/lib/text_editor/logger.rb +11 -0
- data/lib/text_editor/reactor.rb +5 -3
- data/lib/text_editor/terminal.rb +2 -19
- data/lib/text_editor/window.rb +34 -5
- data/spec/spec_helper.rb +3 -0
- data/spec/support/command_helper.rb +43 -0
- data/spec/text_editor/clipboard_spec.rb +0 -7
- data/spec/text_editor/command/append_line_and_insert_spec.rb +38 -0
- data/spec/text_editor/command/backspace_spec.rb +54 -0
- data/spec/text_editor/command/change_mode_spec.rb +17 -0
- data/spec/text_editor/command/cursor_down_spec.rb +57 -0
- data/spec/text_editor/command/cursor_end_and_insert_spec.rb +35 -0
- data/spec/text_editor/command/cursor_end_spec.rb +30 -0
- data/spec/text_editor/command/cursor_left_spec.rb +46 -0
- data/spec/text_editor/command/cursor_left_with_wrap_spec.rb +46 -0
- data/spec/text_editor/command/cursor_right_and_insert_spec.rb +58 -0
- data/spec/text_editor/command/cursor_right_spec.rb +53 -0
- data/spec/text_editor/command/cursor_right_with_wrap_spec.rb +54 -0
- data/spec/text_editor/command/cursor_start_spec.rb +33 -0
- data/spec/text_editor/command/cursor_start_without_whitespace_spec.rb +46 -0
- data/spec/text_editor/command/cursor_up_spec.rb +56 -0
- data/spec/text_editor/command/forward_word_spec.rb +8 -0
- data/spec/text_editor/command/insert_char_spec.rb +24 -0
- data/spec/text_editor/command/insert_line_spec.rb +26 -0
- data/spec/text_editor/command/insert_tab_spec.rb +27 -0
- data/spec/text_editor/command/prepend_line_and_insert_spec.rb +38 -0
- data/spec/text_editor/command/replace_line_spec.rb +34 -0
- data/spec/text_editor/command/resolver_spec.rb +2 -2
- data/spec/text_editor/command/write_file_spec.rb +25 -0
- data/spec/text_editor/command_spec.rb +8 -8
- data/spec/text_editor/component_spec.rb +7 -0
- data/spec/text_editor/configuration/log_spec.rb +26 -0
- data/spec/text_editor/{operating_system_spec.rb → configuration/operating_system_spec.rb} +1 -1
- data/spec/text_editor/configuration/tab_spec.rb +41 -0
- data/spec/text_editor/configuration_spec.rb +11 -4
- data/spec/text_editor/logger_spec.rb +17 -0
- data/text_editor.gemspec +2 -1
- metadata +84 -7
- data/lib/text_editor/mode.rb +0 -23
- data/lib/text_editor/mode/nano.rb +0 -30
- data/lib/text_editor/operating_system.rb +0 -19
data/lib/text_editor/command.rb
CHANGED
@@ -11,10 +11,6 @@ class TextEditor
|
|
11
11
|
new(editor).run(*args, &block)
|
12
12
|
end
|
13
13
|
|
14
|
-
def command
|
15
|
-
@command ||= Resolver.new(editor)
|
16
|
-
end
|
17
|
-
|
18
14
|
def content
|
19
15
|
buffer.line(line)
|
20
16
|
end
|
@@ -22,5 +18,9 @@ class TextEditor
|
|
22
18
|
def run(*)
|
23
19
|
raise NotImplementedError
|
24
20
|
end
|
21
|
+
|
22
|
+
def scan
|
23
|
+
@scan ||= StringScanner.new(content)
|
24
|
+
end
|
25
25
|
end
|
26
26
|
end
|
@@ -2,14 +2,7 @@ class TextEditor
|
|
2
2
|
class Command
|
3
3
|
class CursorLeft < Command
|
4
4
|
def run(*)
|
5
|
-
|
6
|
-
column = buffer.line(line.pred).size
|
7
|
-
new_cursor = cursor.move(line.pred, column)
|
8
|
-
else
|
9
|
-
new_cursor = cursor.left
|
10
|
-
end
|
11
|
-
|
12
|
-
window.update(buffer, new_cursor)
|
5
|
+
window.update(buffer, cursor.left)
|
13
6
|
end
|
14
7
|
end
|
15
8
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class TextEditor
|
2
|
+
class Command
|
3
|
+
class CursorLeftWithWrap < Command
|
4
|
+
def run(*)
|
5
|
+
if column.zero? && line.nonzero?
|
6
|
+
column = buffer.line(line.pred).size
|
7
|
+
new_cursor = cursor.move(line.pred, column)
|
8
|
+
else
|
9
|
+
new_cursor = cursor.left
|
10
|
+
end
|
11
|
+
|
12
|
+
window.update(buffer, new_cursor)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -2,16 +2,9 @@ class TextEditor
|
|
2
2
|
class Command
|
3
3
|
class CursorRight < Command
|
4
4
|
def run(*)
|
5
|
-
columns = content.size
|
6
5
|
lines = buffer.size.pred
|
7
|
-
|
8
|
-
new_cursor =
|
9
|
-
if column == columns && line != lines
|
10
|
-
cursor.move(line.next, 0)
|
11
|
-
else
|
12
|
-
cursor.right.clamp(lines, columns)
|
13
|
-
end
|
14
|
-
|
6
|
+
columns = buffer.line(line).size
|
7
|
+
new_cursor = cursor.right.clamp(lines, columns)
|
15
8
|
window.update(buffer, new_cursor)
|
16
9
|
end
|
17
10
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class TextEditor
|
2
|
+
class Command
|
3
|
+
class CursorRightWithWrap < Command
|
4
|
+
def run(*)
|
5
|
+
columns = content.size
|
6
|
+
lines = buffer.size.pred
|
7
|
+
|
8
|
+
new_cursor =
|
9
|
+
if column == columns && line != lines
|
10
|
+
cursor.move(line.next, 0)
|
11
|
+
else
|
12
|
+
cursor.right.clamp(lines, columns)
|
13
|
+
end
|
14
|
+
|
15
|
+
window.update(buffer, new_cursor)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class TextEditor
|
2
|
+
class Command
|
3
|
+
class ForwardWord < Command
|
4
|
+
def run(*)
|
5
|
+
lines = buffer.size.pred
|
6
|
+
index = content.index(/\W/, column.next)
|
7
|
+
index ||= column
|
8
|
+
|
9
|
+
new_cursor = cursor.move(line, index.next)
|
10
|
+
new_cursor = new_cursor.clamp(lines, content.size)
|
11
|
+
|
12
|
+
window.update(buffer, new_cursor)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -6,11 +6,12 @@ class TextEditor
|
|
6
6
|
def method_missing(method, *args, &block)
|
7
7
|
command = resolve(method)
|
8
8
|
return super unless command
|
9
|
-
command.
|
9
|
+
command.call(editor, *args, &block)
|
10
10
|
self
|
11
11
|
end
|
12
12
|
|
13
13
|
def resolve(method)
|
14
|
+
return method if method.respond_to?(:call)
|
14
15
|
parts = method.to_s.split("_")
|
15
16
|
name = parts.map(&:capitalize).join
|
16
17
|
command(name)
|
@@ -19,7 +20,7 @@ class TextEditor
|
|
19
20
|
private
|
20
21
|
|
21
22
|
def command(name)
|
22
|
-
namespace.const_get(name)
|
23
|
+
namespace.const_get(name)
|
23
24
|
rescue NameError
|
24
25
|
nil
|
25
26
|
end
|
@@ -2,14 +2,22 @@ class TextEditor
|
|
2
2
|
class Configuration
|
3
3
|
include Component
|
4
4
|
|
5
|
-
attr_accessor :files
|
5
|
+
attr_accessor :files, :default_flavor
|
6
|
+
|
7
|
+
def config
|
8
|
+
self
|
9
|
+
end
|
10
|
+
|
11
|
+
def flavor(name, &block)
|
12
|
+
editor.flavors[name] = Flavor.new(name, &block)
|
13
|
+
end
|
6
14
|
|
7
15
|
def log
|
8
|
-
@log ||=
|
16
|
+
@log ||= Log.new
|
9
17
|
end
|
10
18
|
|
11
19
|
def tab
|
12
|
-
@tab ||=
|
20
|
+
@tab ||= Tab.new
|
13
21
|
end
|
14
22
|
end
|
15
23
|
end
|
@@ -14,15 +14,21 @@ class TextEditor
|
|
14
14
|
end
|
15
15
|
|
16
16
|
resolver.files.each do |file|
|
17
|
-
|
17
|
+
load file
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
21
|
def options
|
22
22
|
@options ||= OptionParser.new do |opts|
|
23
|
-
opts.on("--log
|
23
|
+
opts.on("--log FILE", "-l") do |log|
|
24
|
+
config.log.file = log
|
25
|
+
end
|
26
|
+
|
27
|
+
opts.on("--flavor FLAVOR", "-f") do |flavor|
|
28
|
+
config.default_flavor = flavor.to_sym
|
29
|
+
end
|
24
30
|
|
25
|
-
opts.on("--version") do
|
31
|
+
opts.on("--version", "-v") do
|
26
32
|
puts editor.version
|
27
33
|
raise SystemExit
|
28
34
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class TextEditor
|
2
|
+
class Configuration
|
3
|
+
class Flavor
|
4
|
+
attr_reader :name
|
5
|
+
|
6
|
+
def initialize(name, &block)
|
7
|
+
@name = name.to_sym
|
8
|
+
instance_eval(&block) if block
|
9
|
+
end
|
10
|
+
|
11
|
+
def mode(name, &block)
|
12
|
+
modes[name.to_sym] = Mode.new(name, &block)
|
13
|
+
end
|
14
|
+
|
15
|
+
def modes
|
16
|
+
@modes ||= {}
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class TextEditor
|
2
|
+
class Configuration
|
3
|
+
class Mode
|
4
|
+
attr_reader :name
|
5
|
+
|
6
|
+
def initialize(name, &block)
|
7
|
+
@name = name.to_sym
|
8
|
+
instance_eval(&block) if block
|
9
|
+
end
|
10
|
+
|
11
|
+
def bind(key, command = nil, *args, &block)
|
12
|
+
keybindings[key] = [command || block, args]
|
13
|
+
end
|
14
|
+
|
15
|
+
def keybindings
|
16
|
+
@keybindings ||= {}
|
17
|
+
end
|
18
|
+
|
19
|
+
def resolve(key)
|
20
|
+
keybindings.each do |keybinding, command|
|
21
|
+
return command if keybinding === key
|
22
|
+
end
|
23
|
+
|
24
|
+
nil
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class TextEditor
|
2
|
+
class Configuration
|
3
|
+
class OperatingSystem
|
4
|
+
attr_reader :version
|
5
|
+
|
6
|
+
def initialize(version)
|
7
|
+
@version = version
|
8
|
+
end
|
9
|
+
|
10
|
+
def type
|
11
|
+
case version
|
12
|
+
when /cygwin/ then :Cygwin
|
13
|
+
when /bsd|linux/ then :Linux
|
14
|
+
when /darwin|mac/ then :Mac
|
15
|
+
when /mingw|mswin/ then :Windows
|
16
|
+
else :Unknown
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/text_editor/cursor.rb
CHANGED
data/lib/text_editor/keyboard.rb
CHANGED