vedeu 0.6.7 → 0.6.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/docs/dsl.md +4 -4
- data/lib/vedeu/all.rb +6 -18
- data/lib/vedeu/api.rb +14 -13
- data/lib/vedeu/bindings/system.rb +5 -4
- data/lib/vedeu/buffers/virtual_buffer.rb +13 -11
- data/lib/vedeu/configuration/api.rb +4 -3
- data/lib/vedeu/cursors/cursor.rb +23 -18
- data/lib/vedeu/distributed/client.rb +11 -6
- data/lib/vedeu/distributed/server.rb +4 -3
- data/lib/vedeu/dsl/keymap.rb +6 -6
- data/lib/vedeu/editor/all.rb +19 -0
- data/lib/vedeu/{input/editor → editor}/capture.rb +1 -1
- data/lib/vedeu/{input/editor → editor}/cropper.rb +0 -0
- data/lib/vedeu/{input/editor → editor}/cursor.rb +0 -0
- data/lib/vedeu/{input/editor → editor}/document.rb +0 -0
- data/lib/vedeu/{input/editor → editor}/documents.rb +0 -0
- data/lib/vedeu/{input/editor → editor}/editor.rb +0 -0
- data/lib/vedeu/{input/editor → editor}/insert.rb +0 -0
- data/lib/vedeu/{input/editor → editor}/line.rb +0 -0
- data/lib/vedeu/{input/editor → editor}/lines.rb +0 -0
- data/lib/vedeu/error.rb +21 -18
- data/lib/vedeu/esc/borders.rb +6 -4
- data/lib/vedeu/esc/colours.rb +13 -12
- data/lib/vedeu/esc/esc.rb +11 -10
- data/lib/vedeu/input/all.rb +15 -0
- data/lib/vedeu/input/input.rb +55 -52
- data/lib/vedeu/input/key.rb +36 -30
- data/lib/vedeu/input/keymap.rb +101 -92
- data/lib/vedeu/input/keymaps.rb +10 -6
- data/lib/vedeu/input/keys.rb +8 -4
- data/lib/vedeu/input/mapper.rb +125 -121
- data/lib/vedeu/input/translator.rb +159 -0
- data/lib/vedeu/internal_api.rb +16 -15
- data/lib/vedeu/logging/all.rb +13 -0
- data/lib/vedeu/logging/debug.rb +84 -0
- data/lib/vedeu/logging/lockless_log_device.rb +65 -0
- data/lib/vedeu/logging/log.rb +179 -0
- data/lib/vedeu/logging/mono_logger.rb +26 -0
- data/lib/vedeu/logging/timer.rb +68 -0
- data/lib/vedeu/menus/all.rb +12 -0
- data/lib/vedeu/{dsl/menu.rb → menus/dsl.rb} +19 -18
- data/lib/vedeu/menus/menu.rb +234 -0
- data/lib/vedeu/{null/menu.rb → menus/null.rb} +6 -6
- data/lib/vedeu/menus/repository.rb +18 -0
- data/lib/vedeu/models/cell.rb +62 -55
- data/lib/vedeu/models/escape.rb +55 -50
- data/lib/vedeu/models/page.rb +74 -69
- data/lib/vedeu/models/row.rb +55 -51
- data/lib/vedeu/models/views/all.rb +1 -0
- data/lib/vedeu/models/views/char.rb +1 -1
- data/lib/vedeu/models/views/html_char.rb +165 -0
- data/lib/vedeu/output/renderers/html.rb +5 -4
- data/lib/vedeu/repositories/model.rb +2 -0
- data/lib/vedeu/runtime/application.rb +110 -103
- data/lib/vedeu/runtime/bootstrap.rb +109 -103
- data/lib/vedeu/runtime/flags.rb +45 -41
- data/lib/vedeu/runtime/launcher.rb +1 -1
- data/lib/vedeu/runtime/main_loop.rb +51 -46
- data/lib/vedeu/runtime/traps.rb +20 -16
- data/lib/vedeu/version.rb +1 -1
- data/lib/vedeu.rb +2 -2
- data/test/lib/vedeu/buffers/virtual_buffer_test.rb +1 -1
- data/test/lib/vedeu/cursors/cursor_test.rb +11 -5
- data/test/lib/vedeu/dsl/interface_test.rb +1 -1
- data/test/lib/vedeu/dsl/keymap_test.rb +2 -2
- data/test/lib/vedeu/{input/editor → editor}/capture_test.rb +0 -0
- data/test/lib/vedeu/{input/editor → editor}/cropper_test.rb +0 -0
- data/test/lib/vedeu/{input/editor → editor}/cursor_test.rb +0 -0
- data/test/lib/vedeu/{input/editor → editor}/document_test.rb +0 -0
- data/test/lib/vedeu/{input/editor → editor}/documents_test.rb +0 -0
- data/test/lib/vedeu/{input/editor → editor}/editor_test.rb +0 -0
- data/test/lib/vedeu/{input/editor → editor}/insert_test.rb +0 -0
- data/test/lib/vedeu/{input/editor → editor}/line_test.rb +0 -0
- data/test/lib/vedeu/{input/editor → editor}/lines_test.rb +0 -0
- data/test/lib/vedeu/input/input_test.rb +58 -54
- data/test/lib/vedeu/input/key_test.rb +29 -25
- data/test/lib/vedeu/input/keymap_test.rb +74 -70
- data/test/lib/vedeu/input/keymaps_test.rb +8 -4
- data/test/lib/vedeu/input/keys_test.rb +12 -8
- data/test/lib/vedeu/input/mapper_test.rb +64 -56
- data/test/lib/vedeu/input/translator_test.rb +36 -0
- data/test/lib/vedeu/logging/debug_test.rb +39 -0
- data/test/lib/vedeu/logging/lockless_log_device_test.rb +42 -0
- data/test/lib/vedeu/logging/log_test.rb +52 -0
- data/test/lib/vedeu/logging/mono_logger_test.rb +43 -0
- data/test/lib/vedeu/logging/timer_test.rb +44 -0
- data/test/lib/vedeu/{dsl/menu_test.rb → menus/dsl_test.rb} +6 -6
- data/test/lib/vedeu/menus/menu_test.rb +305 -0
- data/test/lib/vedeu/{null/menu_test.rb → menus/null_test.rb} +5 -5
- data/test/lib/vedeu/menus/repository_test.rb +17 -0
- data/test/lib/vedeu/models/cell_test.rb +54 -50
- data/test/lib/vedeu/models/escape_test.rb +49 -45
- data/test/lib/vedeu/models/page_test.rb +167 -160
- data/test/lib/vedeu/models/row_test.rb +71 -67
- data/test/lib/vedeu/models/views/html_char_test.rb +110 -0
- data/test/lib/vedeu/output/compressor_test.rb +44 -22
- data/test/lib/vedeu/output/renderers_test.rb +4 -2
- data/test/lib/vedeu/runtime/application_test.rb +36 -32
- data/test/lib/vedeu/runtime/bootstrap_test.rb +31 -26
- data/test/lib/vedeu/runtime/flags_test.rb +33 -29
- data/test/lib/vedeu/runtime/launcher_test.rb +4 -2
- data/test/lib/vedeu/runtime/main_loop_test.rb +28 -24
- data/test/lib/vedeu/runtime/traps_test.rb +8 -4
- data/test/test_helper.rb +4 -2
- metadata +66 -63
- data/lib/vedeu/input/editor/all.rb +0 -19
- data/lib/vedeu/input/input_translator.rb +0 -155
- data/lib/vedeu/log/debug.rb +0 -79
- data/lib/vedeu/log/lockless_log_device.rb +0 -61
- data/lib/vedeu/log/log.rb +0 -172
- data/lib/vedeu/log/mono_logger.rb +0 -21
- data/lib/vedeu/log/timer.rb +0 -63
- data/lib/vedeu/models/menu.rb +0 -217
- data/lib/vedeu/models/menus.rb +0 -14
- data/lib/vedeu/output/html_char.rb +0 -161
- data/test/lib/vedeu/input/input_translator_test.rb +0 -32
- data/test/lib/vedeu/log/debug_test.rb +0 -35
- data/test/lib/vedeu/log/lockless_log_device_test.rb +0 -29
- data/test/lib/vedeu/log/log_test.rb +0 -48
- data/test/lib/vedeu/log/mono_logger_test.rb +0 -31
- data/test/lib/vedeu/log/timer_test.rb +0 -40
- data/test/lib/vedeu/models/menu_test.rb +0 -301
- data/test/lib/vedeu/models/menus_test.rb +0 -13
- data/test/lib/vedeu/output/html_char_test.rb +0 -106
data/lib/vedeu/esc/colours.rb
CHANGED
@@ -10,8 +10,8 @@ module Vedeu
|
|
10
10
|
|
11
11
|
extend self
|
12
12
|
|
13
|
-
# Produces the background named colour escape sequence hash from
|
14
|
-
# foreground escape sequence hash.
|
13
|
+
# Produces the background named colour escape sequence hash from
|
14
|
+
# the foreground escape sequence hash.
|
15
15
|
#
|
16
16
|
# @return [Hash<Symbol => Fixnum>]
|
17
17
|
def background_codes
|
@@ -22,10 +22,10 @@ module Vedeu
|
|
22
22
|
end
|
23
23
|
|
24
24
|
# Produces the foreground named colour escape sequence hash. The
|
25
|
-
# background escape sequences are also generated from this by
|
26
|
-
# the values.
|
27
|
-
# This hash gives rise to methods you can call directly on `Esc`
|
28
|
-
# produce the desired colours:
|
25
|
+
# background escape sequences are also generated from this by
|
26
|
+
# adding 10 to the values.
|
27
|
+
# This hash gives rise to methods you can call directly on `Esc`
|
28
|
+
# to produce the desired colours:
|
29
29
|
#
|
30
30
|
# @example
|
31
31
|
# Esc.red # => "\e[31m"
|
@@ -37,9 +37,10 @@ module Vedeu
|
|
37
37
|
# Esc.on_blue { 'some text' } # => "\e[44msome text\e[49m"
|
38
38
|
#
|
39
39
|
# # Valid names:
|
40
|
-
# :black, :red, :green, :yellow, :blue, :magenta, :cyan,
|
41
|
-
# :default, :dark_grey, :light_red, :light_green,
|
42
|
-
# :light_blue, :light_magenta, :light_cyan,
|
40
|
+
# :black, :red, :green, :yellow, :blue, :magenta, :cyan,
|
41
|
+
# :light_grey, :default, :dark_grey, :light_red, :light_green,
|
42
|
+
# :light_yellow, :light_blue, :light_magenta, :light_cyan,
|
43
|
+
# :white
|
43
44
|
#
|
44
45
|
# @return [Hash<Symbol => Fixnum>]
|
45
46
|
def foreground_codes
|
@@ -82,9 +83,9 @@ module Vedeu
|
|
82
83
|
end
|
83
84
|
end
|
84
85
|
|
85
|
-
# Dynamically creates methods for each terminal named colour.
|
86
|
-
# is given, then the colour is reset to 'default'
|
87
|
-
# called.
|
86
|
+
# Dynamically creates methods for each terminal named colour.
|
87
|
+
# When a block is given, then the colour is reset to 'default'
|
88
|
+
# once the block is called.
|
88
89
|
#
|
89
90
|
# @return [void]
|
90
91
|
def define_foregrounds!
|
data/lib/vedeu/esc/esc.rb
CHANGED
@@ -4,8 +4,8 @@ require 'vedeu/esc/colours'
|
|
4
4
|
|
5
5
|
module Vedeu
|
6
6
|
|
7
|
-
# Provides escape sequence strings for setting the cursor position
|
8
|
-
# display related functions.
|
7
|
+
# Provides escape sequence strings for setting the cursor position
|
8
|
+
# and various display related functions.
|
9
9
|
#
|
10
10
|
module Esc
|
11
11
|
|
@@ -14,10 +14,10 @@ module Vedeu
|
|
14
14
|
include Vedeu::EscapeSequences::Colours
|
15
15
|
extend self
|
16
16
|
|
17
|
-
# Return the stream with the escape sequences escaped so that they
|
18
|
-
# printed to the terminal instead of being interpreted by
|
19
|
-
# will render them. This way we can see what
|
20
|
-
# along with the content.
|
17
|
+
# Return the stream with the escape sequences escaped so that they
|
18
|
+
# can be printed to the terminal instead of being interpreted by
|
19
|
+
# the terminal which will render them. This way we can see what
|
20
|
+
# escape sequences are being sent along with the content.
|
21
21
|
#
|
22
22
|
# @param stream [String]
|
23
23
|
# @return [String]
|
@@ -27,8 +27,9 @@ module Vedeu
|
|
27
27
|
stream.gsub(/\e/, '\\e')
|
28
28
|
end
|
29
29
|
|
30
|
-
# Return the escape sequence string from the list of recognised
|
31
|
-
# 'commands', or an empty string when the 'command'
|
30
|
+
# Return the escape sequence string from the list of recognised
|
31
|
+
# sequence 'commands', or an empty string when the 'command'
|
32
|
+
# cannot be found.
|
32
33
|
#
|
33
34
|
# @param value [String|Symbol]
|
34
35
|
# @return [String]
|
@@ -43,8 +44,8 @@ module Vedeu
|
|
43
44
|
# Return the escape sequence to render a border character.
|
44
45
|
#
|
45
46
|
# @return [String]
|
46
|
-
# @yieldreturn [void] The border character to wrap with border on
|
47
|
-
# escape sequences.
|
47
|
+
# @yieldreturn [void] The border character to wrap with border on
|
48
|
+
# and off escape sequences.
|
48
49
|
def border
|
49
50
|
return '' unless block_given?
|
50
51
|
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Vedeu
|
2
|
+
|
3
|
+
module Input
|
4
|
+
|
5
|
+
end # Input
|
6
|
+
|
7
|
+
end # Vedeu
|
8
|
+
|
9
|
+
require 'vedeu/input/input'
|
10
|
+
require 'vedeu/input/key'
|
11
|
+
require 'vedeu/input/keymap'
|
12
|
+
require 'vedeu/input/keymaps'
|
13
|
+
require 'vedeu/input/keys'
|
14
|
+
require 'vedeu/input/mapper'
|
15
|
+
require 'vedeu/input/translator'
|
data/lib/vedeu/input/input.rb
CHANGED
@@ -1,70 +1,73 @@
|
|
1
1
|
module Vedeu
|
2
2
|
|
3
|
-
|
4
|
-
# special characters into symbols.
|
5
|
-
#
|
6
|
-
class Input
|
3
|
+
module Input
|
7
4
|
|
8
|
-
#
|
5
|
+
# Captures input from the user via {Vedeu::Terminal#input} and
|
6
|
+
# translates special characters into symbols.
|
9
7
|
#
|
10
|
-
|
11
|
-
# @return [String|Symbol]
|
12
|
-
def self.capture(reader)
|
13
|
-
new(reader).capture
|
14
|
-
end
|
8
|
+
class Input
|
15
9
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
10
|
+
# Instantiate Vedeu::Input::Input and capture keypress(es).
|
11
|
+
#
|
12
|
+
# @param (see #initialize)
|
13
|
+
# @return [String|Symbol]
|
14
|
+
def self.capture(reader)
|
15
|
+
new(reader).capture
|
16
|
+
end
|
17
|
+
|
18
|
+
# Returns a new instance of Vedeu::Input::Input.
|
19
|
+
#
|
20
|
+
# @param reader [IO] An object that responds to `#read`.
|
21
|
+
# Typically, this is Vedeu::Terminal.
|
22
|
+
# @return [Vedeu::Input::Input]
|
23
|
+
def initialize(reader)
|
24
|
+
@reader = reader
|
25
|
+
end
|
26
|
+
|
27
|
+
# Triggers either a ':_command_' event with the command when the
|
28
|
+
# reader is in cooked mode, or when in raw mode, the keypress
|
29
|
+
# event with the key(s) pressed.
|
30
|
+
#
|
31
|
+
# @return [Array|String|Symbol]
|
32
|
+
def capture
|
33
|
+
if reader.raw_mode?
|
34
|
+
Vedeu.trigger(:_keypress_, keypress)
|
33
35
|
|
34
|
-
|
35
|
-
|
36
|
+
elsif reader.fake_mode?
|
37
|
+
Vedeu.trigger(:_editor_, keypress)
|
36
38
|
|
37
|
-
|
38
|
-
|
39
|
+
else
|
40
|
+
Vedeu.trigger(:_command_, command)
|
39
41
|
|
42
|
+
end
|
40
43
|
end
|
41
|
-
end
|
42
44
|
|
43
|
-
|
45
|
+
protected
|
44
46
|
|
45
|
-
|
46
|
-
|
47
|
-
|
47
|
+
# @!attribute [r] reader
|
48
|
+
# @return [IO]
|
49
|
+
attr_reader :reader
|
48
50
|
|
49
|
-
|
51
|
+
private
|
50
52
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
key = input
|
53
|
+
# Returns the translated (when possible) keypress(es).
|
54
|
+
#
|
55
|
+
# @return [String|Symbol]
|
56
|
+
def keypress
|
57
|
+
key = input
|
57
58
|
|
58
|
-
|
59
|
-
|
59
|
+
Vedeu::Input::Translator.translate(key)
|
60
|
+
end
|
60
61
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
62
|
+
# Returns the input from the terminal.
|
63
|
+
#
|
64
|
+
# @return [String]
|
65
|
+
def input
|
66
|
+
@input ||= reader.read
|
67
|
+
end
|
68
|
+
alias_method :command, :input
|
69
|
+
|
70
|
+
end # Input
|
68
71
|
|
69
72
|
end # Input
|
70
73
|
|
data/lib/vedeu/input/key.rb
CHANGED
@@ -1,36 +1,42 @@
|
|
1
1
|
module Vedeu
|
2
2
|
|
3
|
-
|
4
|
-
#
|
5
|
-
class Key
|
3
|
+
module Input
|
6
4
|
|
7
|
-
#
|
8
|
-
#
|
9
|
-
attr_reader :input
|
10
|
-
alias_method :key, :input
|
11
|
-
|
12
|
-
# Returns a new instance of Vedeu::Key.
|
13
|
-
#
|
14
|
-
# @param input [String|Symbol]
|
15
|
-
# @param block [Proc]
|
16
|
-
# @raise [Vedeu::Error::InvalidSyntax] The required block was not given.
|
17
|
-
# @return [Vedeu::Key]
|
18
|
-
def initialize(input = nil, &block)
|
19
|
-
fail Vedeu::Error::InvalidSyntax, 'block not given' unless block_given?
|
20
|
-
|
21
|
-
@input = input
|
22
|
-
@output = block
|
23
|
-
end
|
24
|
-
|
25
|
-
# Pressing the key will call the procedure.
|
5
|
+
# A single keypress or combination of keypresses bound to a
|
6
|
+
# specific action.
|
26
7
|
#
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
8
|
+
class Key
|
9
|
+
|
10
|
+
# @!attribute [r] input
|
11
|
+
# @return [String|Symbol] Returns the key defined.
|
12
|
+
attr_reader :input
|
13
|
+
alias_method :key, :input
|
14
|
+
|
15
|
+
# Returns a new instance of Vedeu::Input::Key.
|
16
|
+
#
|
17
|
+
# @param input [String|Symbol]
|
18
|
+
# @param block [Proc]
|
19
|
+
# @raise [Vedeu::Error::InvalidSyntax] The required block was
|
20
|
+
# not given.
|
21
|
+
# @return [Vedeu::Input::Key]
|
22
|
+
def initialize(input = nil, &block)
|
23
|
+
fail Vedeu::Error::InvalidSyntax, 'block not given' unless block_given?
|
24
|
+
|
25
|
+
@input = input
|
26
|
+
@output = block
|
27
|
+
end
|
28
|
+
|
29
|
+
# Pressing the key will call the procedure.
|
30
|
+
#
|
31
|
+
# @return [|Symbol]
|
32
|
+
def output
|
33
|
+
@output.call
|
34
|
+
end
|
35
|
+
alias_method :action, :output
|
36
|
+
alias_method :press, :output
|
37
|
+
|
38
|
+
end # Key
|
39
|
+
|
40
|
+
end # Input
|
35
41
|
|
36
42
|
end # Vedeu
|
data/lib/vedeu/input/keymap.rb
CHANGED
@@ -1,98 +1,107 @@
|
|
1
1
|
module Vedeu
|
2
2
|
|
3
|
-
|
4
|
-
#
|
5
|
-
class Keymap
|
3
|
+
module Input
|
6
4
|
|
7
|
-
|
8
|
-
|
9
|
-
collection Vedeu::Keys
|
10
|
-
member Vedeu::Key
|
11
|
-
|
12
|
-
# @!attribute [rw] name
|
13
|
-
# @return [String]
|
14
|
-
attr_accessor :name
|
15
|
-
|
16
|
-
# Returns a new instance of Vedeu::Keymap.
|
17
|
-
#
|
18
|
-
# @param attributes [Hash]
|
19
|
-
# @option attributes name [String] The name of the keymap.
|
20
|
-
# @option attributes keys [Vedeu::Keys|Array] A collection of keys.
|
21
|
-
# @option attributes repository [Vedeu::Repository] This model's storage.
|
22
|
-
# @return [Vedeu::Keymap]
|
23
|
-
def initialize(attributes = {})
|
24
|
-
@attributes = defaults.merge!(attributes)
|
25
|
-
|
26
|
-
@attributes.each { |key, value| instance_variable_set("@#{key}", value) }
|
27
|
-
end
|
28
|
-
|
29
|
-
# Add a key to the keymap.
|
30
|
-
#
|
31
|
-
# @param key [Vedeu::Key]
|
32
|
-
# @return [void]
|
33
|
-
def add(key)
|
34
|
-
return false unless valid?(key)
|
35
|
-
|
36
|
-
@keys = keys.add(key)
|
37
|
-
end
|
38
|
-
|
39
|
-
# Returns the collection of keys defined for this keymap.
|
40
|
-
#
|
41
|
-
# @return [Vedeu::Keys]
|
42
|
-
def keys
|
43
|
-
collection.coerce(@keys, self)
|
44
|
-
end
|
45
|
-
|
46
|
-
# Check whether the key is already defined for this keymap.
|
47
|
-
#
|
48
|
-
# @param input [String|Symbol]
|
49
|
-
# @return [Boolean] A boolean indicating the input provided is already in
|
50
|
-
# use for this keymap.
|
51
|
-
def key_defined?(input)
|
52
|
-
keys.any? { |key| key.input == input }
|
53
|
-
end
|
54
|
-
|
55
|
-
# When the given input is registered with this keymap, this method triggers
|
56
|
-
# the action associated with the key.
|
5
|
+
# A container class for keys associated with a particular interface.
|
57
6
|
#
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
Vedeu
|
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
|
-
|
7
|
+
class Keymap
|
8
|
+
|
9
|
+
include Vedeu::Model
|
10
|
+
|
11
|
+
collection Vedeu::Input::Keys
|
12
|
+
member Vedeu::Input::Key
|
13
|
+
|
14
|
+
# @!attribute [rw] name
|
15
|
+
# @return [String]
|
16
|
+
attr_accessor :name
|
17
|
+
|
18
|
+
# Returns a new instance of Vedeu::Input::Keymap.
|
19
|
+
#
|
20
|
+
# @param attributes [Hash]
|
21
|
+
# @option attributes name [String] The name of the keymap.
|
22
|
+
# @option attributes keys [Vedeu::Input::Keys|Array]
|
23
|
+
# A collection of keys.
|
24
|
+
# @option attributes repository [Vedeu::Repository]
|
25
|
+
# This model's storage.
|
26
|
+
# @return [Vedeu::Input::Keymap]
|
27
|
+
def initialize(attributes = {})
|
28
|
+
@attributes = defaults.merge!(attributes)
|
29
|
+
|
30
|
+
@attributes.each do |key, value|
|
31
|
+
instance_variable_set("@#{key}", value)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Add a key to the keymap.
|
36
|
+
#
|
37
|
+
# @param key [Vedeu::Input::Key]
|
38
|
+
# @return [void]
|
39
|
+
def add(key)
|
40
|
+
return false unless valid?(key)
|
41
|
+
|
42
|
+
@keys = keys.add(key)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Returns the collection of keys defined for this keymap.
|
46
|
+
#
|
47
|
+
# @return [Vedeu::Input::Keys]
|
48
|
+
def keys
|
49
|
+
collection.coerce(@keys, self)
|
50
|
+
end
|
51
|
+
|
52
|
+
# Check whether the key is already defined for this keymap.
|
53
|
+
#
|
54
|
+
# @param input [String|Symbol]
|
55
|
+
# @return [Boolean] A boolean indicating the input provided is
|
56
|
+
# already in use for this keymap.
|
57
|
+
def key_defined?(input)
|
58
|
+
keys.any? { |key| key.input == input }
|
59
|
+
end
|
60
|
+
|
61
|
+
# When the given input is registered with this keymap, this method
|
62
|
+
# triggers the action associated with the key.
|
63
|
+
#
|
64
|
+
# @param input [String|Symbol]
|
65
|
+
# @return [Array|FalseClass]
|
66
|
+
def use(input)
|
67
|
+
return false unless key_defined?(input)
|
68
|
+
|
69
|
+
Vedeu.log(type: :input, message: "Key pressed: '#{input}'")
|
70
|
+
|
71
|
+
Vedeu.trigger(:key, input)
|
72
|
+
|
73
|
+
keys.select { |key| key.input == input }.map(&:press)
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
# Returns the default options/attributes for this class.
|
79
|
+
#
|
80
|
+
# @return [Hash]
|
81
|
+
def defaults
|
82
|
+
{
|
83
|
+
name: '',
|
84
|
+
keys: [],
|
85
|
+
repository: Vedeu::Input::Keymaps.keymaps,
|
86
|
+
}
|
87
|
+
end
|
88
|
+
|
89
|
+
# Checks that the provided key is not already registered with this
|
90
|
+
# keymap.
|
91
|
+
#
|
92
|
+
# @param key [Vedeu::Input::Key]
|
93
|
+
# @return [Boolean]
|
94
|
+
def valid?(key)
|
95
|
+
return true unless key_defined?(key.input)
|
96
|
+
|
97
|
+
Vedeu.log(type: :input,
|
98
|
+
message: "Keymap '#{name}' already defines '#{key.input}'.")
|
99
|
+
|
100
|
+
false
|
101
|
+
end
|
102
|
+
|
103
|
+
end # Keymap
|
104
|
+
|
105
|
+
end # Input
|
97
106
|
|
98
107
|
end # Vedeu
|
data/lib/vedeu/input/keymaps.rb
CHANGED
@@ -1,13 +1,17 @@
|
|
1
1
|
module Vedeu
|
2
2
|
|
3
|
-
|
4
|
-
#
|
5
|
-
class Keymaps < Vedeu::Repository
|
3
|
+
module Input
|
6
4
|
|
7
|
-
|
5
|
+
# Allows the storing of keymaps.
|
6
|
+
#
|
7
|
+
class Keymaps < Vedeu::Repository
|
8
8
|
|
9
|
-
|
9
|
+
singleton_class.send(:alias_method, :keymaps, :repository)
|
10
10
|
|
11
|
-
|
11
|
+
real Vedeu::Input::Keymap
|
12
|
+
|
13
|
+
end # Keymaps
|
14
|
+
|
15
|
+
end # Input
|
12
16
|
|
13
17
|
end # Vedeu
|
data/lib/vedeu/input/keys.rb
CHANGED
@@ -1,9 +1,13 @@
|
|
1
1
|
module Vedeu
|
2
2
|
|
3
|
-
|
4
|
-
#
|
5
|
-
class Keys < Vedeu::Collection
|
3
|
+
module Input
|
6
4
|
|
7
|
-
|
5
|
+
# A collection of {Vedeu::Input::Key} instances.
|
6
|
+
#
|
7
|
+
class Keys < Vedeu::Collection
|
8
|
+
|
9
|
+
end # Keys
|
10
|
+
|
11
|
+
end # Input
|
8
12
|
|
9
13
|
end # Vedeu
|