mini_readline 0.0.1 → 0.0.2
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/README.md +25 -0
- data/lib/mini_readline/raw_term.rb +32 -6
- data/lib/mini_readline/raw_term/mapper.rb +43 -0
- data/lib/mini_readline/raw_term/other.rb +0 -9
- data/lib/mini_readline/raw_term/other/map.rb +4 -27
- data/lib/mini_readline/raw_term/windows.rb +4 -30
- data/lib/mini_readline/raw_term/windows/map.rb +0 -20
- data/lib/mini_readline/raw_term/windows/set_posn.rb +1 -22
- data/lib/mini_readline/read_line/history.rb +2 -4
- data/lib/mini_readline/version.rb +1 -1
- data/mini_readline.gemspec +5 -1
- data/sire.rb +7 -1
- data/tests/mini_readline_tests.rb +1 -0
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 18650c39506de94d12cb3f292413549722ec55fb
|
4
|
+
data.tar.gz: 7cba8a96b929d75870ee5eeb5b7f20d0b0eb04c8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b859c1130980260216186916fb5f23a3375ac1973fb3748cebe665890684a2a63dee2edabf771974ddb67049722902709583d93164f25d3bff328b10216b2ce1
|
7
|
+
data.tar.gz: de055b2fb3843cea35b0358d8f6f735d1c90591f714b69b26f1a2511985676b002e4bf6beddf028691059e35e7f4bce9e5ae409b5e24a4dfd84884db07cf0b16
|
data/README.md
CHANGED
@@ -18,6 +18,8 @@ that I will see the same bugs and fix them. Then perhaps it can be seen how
|
|
18
18
|
the original code can also be fixed. In the long run, this is perhaps the
|
19
19
|
most important goal.
|
20
20
|
|
21
|
+
<br>The mini_readline gem is designed for use with MRI version 1.9.3 or later.
|
22
|
+
|
21
23
|
## Installation
|
22
24
|
|
23
25
|
Add this line to your application's Gemfile:
|
@@ -34,6 +36,29 @@ Or install it yourself as:
|
|
34
36
|
|
35
37
|
$ gem install mini_readline
|
36
38
|
|
39
|
+
## Key Mappings
|
40
|
+
The mini_readline gem supports a simple set of editing commands. These vary
|
41
|
+
somewhat based on the system platform. The keyboard mappings (and alias
|
42
|
+
mappings) are listed below:
|
43
|
+
|
44
|
+
Editor Action | Windows Key | Other Key
|
45
|
+
-----------------|---------------------------|------------
|
46
|
+
Enter | Enter | Enter
|
47
|
+
Left | Left Arrow, Keypad Left | Left Arrow
|
48
|
+
Right | Right Arrow, Keypad Right | Right Arrow
|
49
|
+
Go to start | Home, Keypad Home | Home, Ctrl-A
|
50
|
+
Go to end | End, Keypad End | End, Ctrl-E
|
51
|
+
Previous History | Up Arrow, Keypad Up | Up Arrow, Ctrl-R
|
52
|
+
Next History | Down Arrow, Keypad Down | Down Arrow
|
53
|
+
Erase Left | Backspace | Backspace, Ctrl-H
|
54
|
+
Erase Right | Delete, Ctrl-Backspace | Delete, Ctrl-Backspace
|
55
|
+
Erase All | Escape | Ctrl-B
|
56
|
+
|
57
|
+
### Notes
|
58
|
+
* The label "Other" is an umbrella that bundles together the Linux, Mac,
|
59
|
+
and Cygwin platforms.
|
60
|
+
* References to Keypad keys under Windows assume that Num Lock is not engaged.
|
61
|
+
|
37
62
|
## Usage
|
38
63
|
|
39
64
|
The typical way of utilizing this gem is to place the following:
|
@@ -1,14 +1,40 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
|
3
|
-
|
4
|
-
if (RUBY_PLATFORM =~ /\bcygwin\b/i) || (RUBY_PLATFORM !~ /mswin|mingw/)
|
5
|
-
require_relative 'raw_term/other'
|
6
|
-
else
|
7
|
-
require_relative 'raw_term/windows'
|
8
|
-
end
|
3
|
+
require_relative 'raw_term/mapper'
|
9
4
|
|
10
5
|
#* raw_term.rb - Platform determination for raw terminal access.
|
11
6
|
module MiniReadline
|
7
|
+
|
8
|
+
#The class used to manipulate console i/o on a low level.
|
9
|
+
class RawTerm
|
10
|
+
|
11
|
+
#Create a mapper.
|
12
|
+
MAP = Mapper.new
|
13
|
+
|
14
|
+
#Map the printable characters.
|
15
|
+
(32..126).each do |code|
|
16
|
+
char = code.chr
|
17
|
+
MAP[char] = [:insert_text, char]
|
18
|
+
end
|
19
|
+
|
20
|
+
#Get a mapped sequence.
|
21
|
+
def get_mapped_keystroke
|
22
|
+
MAP.get_mapped_keystroke {get_raw_char}
|
23
|
+
end
|
24
|
+
|
25
|
+
#Start on a new line.
|
26
|
+
def put_new_line
|
27
|
+
print("\n")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
#Select the type of platform in use.
|
32
|
+
if (RUBY_PLATFORM =~ /\bcygwin\b/i) || (RUBY_PLATFORM !~ /mswin|mingw/)
|
33
|
+
require_relative 'raw_term/other'
|
34
|
+
else
|
35
|
+
require_relative 'raw_term/windows'
|
36
|
+
end
|
37
|
+
|
12
38
|
#Get an instance of a raw terminal controller object.
|
13
39
|
BASE_OPTIONS[:term] = RawTerm.new
|
14
40
|
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
#* raw_term/mapper.rb - Support for keystroke mapping.
|
4
|
+
module MiniReadline
|
5
|
+
|
6
|
+
#* raw_term/mapper.rb - Support for keystroke mapping.
|
7
|
+
class Mapper
|
8
|
+
|
9
|
+
#Set up the keystroke mapper.
|
10
|
+
def initialize
|
11
|
+
@map = Hash.new {|_hash, key| [:unmapped, key]}
|
12
|
+
end
|
13
|
+
|
14
|
+
#Add a map entry
|
15
|
+
def []=(index, value)
|
16
|
+
process_non_terminals(index)
|
17
|
+
@map[index] = value
|
18
|
+
end
|
19
|
+
|
20
|
+
#Handle the preamble characters in the command sequence.
|
21
|
+
def process_non_terminals(index)
|
22
|
+
seq = ""
|
23
|
+
|
24
|
+
index.chop.chars.each do |char|
|
25
|
+
seq << char
|
26
|
+
fail "Ambiguous entry #{index.inspect}" if @map.has_key?(seq) && @map[seq]
|
27
|
+
@map[seq] = false
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
#Get a mapped input sequence.
|
32
|
+
def get_mapped_keystroke
|
33
|
+
key_seq, key_cmd = "", nil
|
34
|
+
|
35
|
+
begin
|
36
|
+
key_seq << yield
|
37
|
+
key_cmd = @map[key_seq]
|
38
|
+
end until key_cmd
|
39
|
+
|
40
|
+
key_cmd
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -19,10 +19,6 @@ module MiniReadline
|
|
19
19
|
#Bell
|
20
20
|
BELL = "\x07"
|
21
21
|
|
22
|
-
#Set up the Other Raw Terminal.
|
23
|
-
def initialize
|
24
|
-
end
|
25
|
-
|
26
22
|
#Output a string
|
27
23
|
def put_string(str)
|
28
24
|
scan_string(str)
|
@@ -34,11 +30,6 @@ module MiniReadline
|
|
34
30
|
put_string CARRIAGE_RETURN
|
35
31
|
end
|
36
32
|
|
37
|
-
#Start on a new line.
|
38
|
-
def put_new_line
|
39
|
-
print("\n")
|
40
|
-
end
|
41
|
-
|
42
33
|
#Sound a beep
|
43
34
|
def beep
|
44
35
|
print BELL
|
@@ -6,20 +6,6 @@ module MiniReadline
|
|
6
6
|
#* other/map.rb - Character mapping for other systems.
|
7
7
|
class RawTerm
|
8
8
|
|
9
|
-
#Create a hash with a default value.
|
10
|
-
MAP = Hash.new {|_hash, key| [:unmapped, key]}
|
11
|
-
|
12
|
-
#Map the printable characters.
|
13
|
-
(32..126).each do |code|
|
14
|
-
char = code.chr
|
15
|
-
MAP[char] = [:insert_text, char]
|
16
|
-
end
|
17
|
-
|
18
|
-
#Map the non-terminal entries.
|
19
|
-
MAP["\e"] = false
|
20
|
-
MAP["\e["] = false
|
21
|
-
MAP["\eO"] = false
|
22
|
-
|
23
9
|
#Map the non-printing characters.
|
24
10
|
|
25
11
|
#Left Arrows
|
@@ -33,6 +19,7 @@ module MiniReadline
|
|
33
19
|
#Up Arrows
|
34
20
|
MAP["\e[A"] = [:previous_history]
|
35
21
|
MAP["\eOA"] = [:previous_history]
|
22
|
+
MAP["\x12"] = [:previous_history]
|
36
23
|
|
37
24
|
#Down Arrows
|
38
25
|
MAP["\e[B"] = [:next_history]
|
@@ -41,17 +28,19 @@ module MiniReadline
|
|
41
28
|
#The Home keys
|
42
29
|
MAP["\e[H"] = [:go_home]
|
43
30
|
MAP["\eOH"] = [:go_home]
|
31
|
+
MAP["\x01"] = [:go_home]
|
44
32
|
|
45
33
|
#The End keys
|
46
34
|
MAP["\e[F"] = [:go_end]
|
47
35
|
MAP["\eOF"] = [:go_end]
|
36
|
+
MAP["\x05"] = [:go_end]
|
48
37
|
|
49
38
|
#The Backspace key
|
50
39
|
MAP["\x7F"] = [:delete_left]
|
40
|
+
MAP["\x08"] = [:delete_left]
|
51
41
|
|
52
42
|
#The Delete keys
|
53
43
|
MAP["\x1F"] = [:delete_right]
|
54
|
-
MAP["\e[3"] = false
|
55
44
|
MAP["\e[3~"] = [:delete_right]
|
56
45
|
|
57
46
|
#The Enter key
|
@@ -59,17 +48,5 @@ module MiniReadline
|
|
59
48
|
|
60
49
|
#The Cancel key
|
61
50
|
MAP["\x02"] = [:cancel]
|
62
|
-
|
63
|
-
#Get a mapped sequence.
|
64
|
-
def get_mapped_keystroke
|
65
|
-
key_seq, key_cmd = "", nil
|
66
|
-
|
67
|
-
begin
|
68
|
-
key_seq << get_raw_char
|
69
|
-
key_cmd = MAP[key_seq]
|
70
|
-
end until key_cmd
|
71
|
-
|
72
|
-
key_cmd
|
73
|
-
end
|
74
51
|
end
|
75
52
|
end
|
@@ -27,21 +27,15 @@ module MiniReadline
|
|
27
27
|
@_getch = Win32API.new("msvcrt", "_getch", [])
|
28
28
|
@_kbhit = Win32API.new("msvcrt", "_kbhit", [])
|
29
29
|
@_beep = Win32API.new("user32", "MessageBeep", ['L'])
|
30
|
-
|
31
|
-
@_set_cursor_posn = Win32API.new("kernel32",
|
32
|
-
"SetConsoleCursorPosition",
|
30
|
+
@_set_cursor_posn = Win32API.new("kernel32", "SetConsoleCursorPosition",
|
33
31
|
['L','L'])
|
34
|
-
|
35
|
-
@_get_screen_info = Win32API.new("kernel32",
|
36
|
-
"GetConsoleScreenBufferInfo",
|
32
|
+
@_get_screen_info = Win32API.new("kernel32", "GetConsoleScreenBufferInfo",
|
37
33
|
['L','P'])
|
38
|
-
|
39
34
|
@_get_handle = Win32API.new("kernel32", "GetStdHandle", ['L'])
|
40
35
|
end
|
41
36
|
|
42
37
|
#Output a string
|
43
38
|
def put_string(str)
|
44
|
-
scan_string(str) unless @_out_handle
|
45
39
|
print(str)
|
46
40
|
end
|
47
41
|
|
@@ -51,38 +45,18 @@ module MiniReadline
|
|
51
45
|
put_string CARRIAGE_RETURN
|
52
46
|
end
|
53
47
|
|
54
|
-
#Start on a new line.
|
55
|
-
def put_new_line
|
56
|
-
print("\n")
|
57
|
-
end
|
58
|
-
|
59
48
|
#Sound a beep
|
60
49
|
def beep
|
61
50
|
@_beep.call(0)
|
62
51
|
end
|
63
52
|
|
64
|
-
#
|
65
|
-
def
|
53
|
+
#Get a uncooked character keystroke.
|
54
|
+
def get_raw_char
|
66
55
|
while (@_kbhit.call == 0)
|
67
56
|
sleep(WAIT_SLEEP)
|
68
57
|
end
|
69
|
-
end
|
70
58
|
|
71
|
-
#Get a uncooked character keystroke.
|
72
|
-
def get_raw_char
|
73
|
-
wait_for_key
|
74
59
|
@_getch.call.chr
|
75
60
|
end
|
76
|
-
|
77
|
-
#Determine the affect of a string on the cursor.
|
78
|
-
def scan_string(str)
|
79
|
-
str.chars.each do |char|
|
80
|
-
if char == CARRIAGE_RETURN
|
81
|
-
@cursor_posn = 0
|
82
|
-
else
|
83
|
-
@cursor_posn += 1
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
87
61
|
end
|
88
62
|
end
|
@@ -6,21 +6,8 @@ module MiniReadline
|
|
6
6
|
#* windows/map.rb - Character mapping for windows systems.
|
7
7
|
class RawTerm
|
8
8
|
|
9
|
-
#Create a hash with a default value.
|
10
|
-
MAP = Hash.new {|_hash, key| [:unmapped, key]}
|
11
|
-
|
12
|
-
#Map the printable characters.
|
13
|
-
(32..126).each do |code|
|
14
|
-
char = code.chr
|
15
|
-
MAP[char] = [:insert_text, char]
|
16
|
-
end
|
17
|
-
|
18
9
|
pfx = 0xE0.chr
|
19
10
|
|
20
|
-
#Map the non-terminal entries.
|
21
|
-
MAP["\x00"] = false
|
22
|
-
MAP[ pfx ] = false
|
23
|
-
|
24
11
|
#Map the non-printing characters.
|
25
12
|
|
26
13
|
#Left Arrows
|
@@ -60,12 +47,5 @@ module MiniReadline
|
|
60
47
|
|
61
48
|
#The Escape key
|
62
49
|
MAP["\x1B"] = [:cancel]
|
63
|
-
|
64
|
-
#Get a mapped sequence.
|
65
|
-
def get_mapped_keystroke
|
66
|
-
first_char = get_raw_char
|
67
|
-
|
68
|
-
MAP[first_char] || MAP[first_char + get_raw_char]
|
69
|
-
end
|
70
50
|
end
|
71
51
|
end
|
@@ -6,17 +6,8 @@ module MiniReadline
|
|
6
6
|
#* windows/set_posn.rb - Set the column of the cursor.
|
7
7
|
class RawTerm
|
8
8
|
|
9
|
-
#Set the position of the screen cursor within the line.
|
10
|
-
def set_posn(new_posn)
|
11
|
-
if @_out_handle
|
12
|
-
api_set_posn(new_posn)
|
13
|
-
else
|
14
|
-
term_set_posn(new_posn)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
9
|
#Move the cursor using windows voodoo API.
|
19
|
-
def
|
10
|
+
def set_posn(new_posn)
|
20
11
|
raw_buffer = 0.chr * 24
|
21
12
|
@_get_screen_info.call(@_out_handle, raw_buffer)
|
22
13
|
y_posn = (raw_buffer[6,2].unpack('S'))[0]
|
@@ -24,17 +15,5 @@ module MiniReadline
|
|
24
15
|
@_set_cursor_posn.call(@_out_handle, y_posn * 65536 + new_posn)
|
25
16
|
end
|
26
17
|
|
27
|
-
#Move the cursor using terminal primitives.
|
28
|
-
def term_set_posn(new_posn)
|
29
|
-
|
30
|
-
if new_posn > @cursor_posn
|
31
|
-
print("\e#{new_posn - @cursor_posn}C")
|
32
|
-
elsif new_posn < @cursor_posn
|
33
|
-
print("\e#{@cursor_posn - new_posn}D")
|
34
|
-
end
|
35
|
-
|
36
|
-
@cursor_posn = new_posn
|
37
|
-
end
|
38
|
-
|
39
18
|
end
|
40
19
|
end
|
@@ -45,10 +45,8 @@ module MiniReadline
|
|
45
45
|
|
46
46
|
#Append a string to the history buffer if enabled.
|
47
47
|
def append_history(str)
|
48
|
-
|
49
|
-
|
50
|
-
return if (str_strip == '') && @options[:no_blanks]
|
51
|
-
return if (history.include?(str_strip)) && @options[:no_dups]
|
48
|
+
return if @options[:no_blanks] && str.strip.empty?
|
49
|
+
history.delete(str) if @options[:no_dups]
|
52
50
|
|
53
51
|
history << str
|
54
52
|
end
|
data/mini_readline.gemspec
CHANGED
@@ -10,7 +10,11 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["peter.c.camilleri@gmail.com"]
|
11
11
|
|
12
12
|
spec.summary = "A simplified replacement for readline."
|
13
|
-
spec.description = "A gem for console command entry with line edit and
|
13
|
+
spec.description = "A gem for console command entry with line edit and " +
|
14
|
+
"history. This gem is like the standard readline " +
|
15
|
+
"gem except that it has been redesigned, simplified, " +
|
16
|
+
"and extensively cleaned up."
|
17
|
+
|
14
18
|
spec.homepage = "http://teuthida-technologies.com/"
|
15
19
|
spec.license = "MIT"
|
16
20
|
|
data/sire.rb
CHANGED
@@ -10,7 +10,7 @@ elsif ARGV[0] == 'local'
|
|
10
10
|
require './lib/mini_readline'
|
11
11
|
puts "\nOption(local). Loaded mini_readline from the local code folder."
|
12
12
|
elsif defined?(MiniReadline)
|
13
|
-
puts "
|
13
|
+
puts "\nThe mini_readline gem is already loaded."
|
14
14
|
else
|
15
15
|
begin
|
16
16
|
require 'mini_readline'
|
@@ -57,6 +57,12 @@ class SIRE
|
|
57
57
|
"Bye bye for now!"
|
58
58
|
end
|
59
59
|
|
60
|
+
#Get a mapped keystroke.
|
61
|
+
def g
|
62
|
+
print 'Press a key:'
|
63
|
+
MiniReadline::BASE_OPTIONS[:term].get_mapped_keystroke
|
64
|
+
end
|
65
|
+
|
60
66
|
#Test spawning a process. This breaks the regular readline gem.
|
61
67
|
def run(command)
|
62
68
|
IO.popen(command, "r+") do |io|
|
@@ -17,6 +17,7 @@ class MiniReadlineTester < Minitest::Test
|
|
17
17
|
assert_equal(Class, MiniReadline::Readline.class)
|
18
18
|
assert_equal(Class, MiniReadline::Edit.class)
|
19
19
|
assert_equal(Class, MiniReadline::EditWindow.class)
|
20
|
+
assert_equal(Class, MiniReadline::Mapper.class)
|
20
21
|
end
|
21
22
|
|
22
23
|
def test_platform_detection
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mini_readline
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Camilleri
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01-
|
11
|
+
date: 2016-01-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -38,7 +38,9 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
|
-
description: A gem for console command entry with line edit and history.
|
41
|
+
description: A gem for console command entry with line edit and history. This gem
|
42
|
+
is like the standard readline gem except that it has been redesigned, simplified,
|
43
|
+
and extensively cleaned up.
|
42
44
|
email:
|
43
45
|
- peter.c.camilleri@gmail.com
|
44
46
|
executables: []
|
@@ -53,6 +55,7 @@ files:
|
|
53
55
|
- lib/mini_readline.rb
|
54
56
|
- lib/mini_readline/options.rb
|
55
57
|
- lib/mini_readline/raw_term.rb
|
58
|
+
- lib/mini_readline/raw_term/mapper.rb
|
56
59
|
- lib/mini_readline/raw_term/other.rb
|
57
60
|
- lib/mini_readline/raw_term/other/map.rb
|
58
61
|
- lib/mini_readline/raw_term/other/set_posn.rb
|