ruvim 0.1.0 → 0.2.0

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.
data/lib/ruvim/input.rb CHANGED
@@ -4,7 +4,7 @@ module RuVim
4
4
  @stdin = stdin
5
5
  end
6
6
 
7
- def read_key(timeout: nil, wakeup_ios: [])
7
+ def read_key(timeout: nil, wakeup_ios: [], esc_timeout: nil)
8
8
  ios = [@stdin, *wakeup_ios].compact
9
9
  readable = IO.select(ios, nil, nil, timeout)
10
10
  return nil unless readable
@@ -15,18 +15,25 @@ module RuVim
15
15
  return nil unless ready.include?(@stdin)
16
16
 
17
17
  ch = @stdin.getch
18
+ return :ctrl_b if ch == "\u0002"
18
19
  return :ctrl_c if ch == "\u0003"
20
+ return :ctrl_d if ch == "\u0004"
21
+ return :ctrl_e if ch == "\u0005"
22
+ return :ctrl_f if ch == "\u0006"
19
23
  return :ctrl_i if ch == "\u0009"
24
+ return :ctrl_l if ch == "\u000c"
20
25
  return :ctrl_n if ch == "\u000e"
21
26
  return :ctrl_o if ch == "\u000f"
22
27
  return :ctrl_p if ch == "\u0010"
23
28
  return :ctrl_r if ch == "\u0012"
29
+ return :ctrl_u if ch == "\u0015"
24
30
  return :ctrl_v if ch == "\u0016"
25
31
  return :ctrl_w if ch == "\u0017"
32
+ return :ctrl_y if ch == "\u0019"
26
33
  return :enter if ch == "\r" || ch == "\n"
27
34
  return :backspace if ch == "\u007f" || ch == "\b"
28
35
 
29
- return read_escape_sequence if ch == "\e"
36
+ return read_escape_sequence(timeout: esc_timeout) if ch == "\e"
30
37
 
31
38
  ch
32
39
  end
@@ -41,23 +48,28 @@ module RuVim
41
48
  nil
42
49
  end
43
50
 
44
- def read_escape_sequence
51
+ def read_escape_sequence(timeout: nil)
45
52
  extra = +""
53
+ recognized = {
54
+ "[A" => :up,
55
+ "[B" => :down,
56
+ "[C" => :right,
57
+ "[D" => :left,
58
+ "[5~" => :pageup,
59
+ "[6~" => :pagedown
60
+ }
61
+ wait = timeout.nil? ? 0.005 : [timeout.to_f, 0.0].max
46
62
  begin
47
- while IO.select([@stdin], nil, nil, 0.005)
63
+ while IO.select([@stdin], nil, nil, wait)
48
64
  extra << @stdin.read_nonblock(1)
65
+ key = recognized[extra]
66
+ return key if key
49
67
  end
50
68
  rescue IO::WaitReadable, EOFError
51
69
  end
52
70
 
53
71
  case extra
54
72
  when "" then :escape
55
- when "[A" then :up
56
- when "[B" then :down
57
- when "[C" then :right
58
- when "[D" then :left
59
- when "[5~" then :pageup
60
- when "[6~" then :pagedown
61
73
  else
62
74
  [:escape_sequence, extra]
63
75
  end
@@ -0,0 +1,46 @@
1
+ module RuVim
2
+ module KeywordChars
3
+ module_function
4
+
5
+ DEFAULT_CHAR_CLASS = "[:alnum:]_".freeze
6
+ DEFAULT_REGEX = /[[:alnum:]_]/.freeze
7
+
8
+ def char_class(raw)
9
+ spec = raw.to_s
10
+ return DEFAULT_CHAR_CLASS if spec.empty?
11
+
12
+ @char_class_cache ||= {}
13
+ return @char_class_cache[spec] if @char_class_cache.key?(spec)
14
+
15
+ extra = []
16
+ spec.split(",").each do |tok|
17
+ t = tok.strip
18
+ next if t.empty? || t == "@"
19
+
20
+ if t.length == 1
21
+ extra << Regexp.escape(t)
22
+ elsif t.match?(/\A\d+-\d+\z/)
23
+ a, b = t.split("-", 2).map(&:to_i)
24
+ lo, hi = [a, b].minmax
25
+ next if lo < 0 || hi > 255
26
+ extra << "#{Regexp.escape(lo.chr)}-#{Regexp.escape(hi.chr)}"
27
+ end
28
+ end
29
+
30
+ @char_class_cache[spec] = "#{DEFAULT_CHAR_CLASS}#{extra.join}".freeze
31
+ end
32
+
33
+ def regex(raw)
34
+ spec = raw.to_s
35
+ return DEFAULT_REGEX if spec.empty?
36
+
37
+ @regex_cache ||= {}
38
+ return @regex_cache[spec] if @regex_cache.key?(spec)
39
+
40
+ klass = char_class(spec)
41
+ @regex_cache[spec] = /[#{klass}]/
42
+ rescue RegexpError
43
+ DEFAULT_REGEX
44
+ end
45
+ end
46
+ end