mini_readline 0.5.2 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +32 -9
- data/lib/mini_readline/read_line.rb +15 -0
- data/lib/mini_readline/read_line/edit/auto_complete.rb +1 -0
- data/lib/mini_readline/read_line/edit/auto_complete/auto_file_source.rb +79 -0
- data/lib/mini_readline/version.rb +1 -1
- data/tests/mini_readline_tests.rb +11 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 677e16d732f1de6aaab8fe7a37c41e1abb18364c
|
4
|
+
data.tar.gz: f42cd241844d9810a030317fcd9f9ae4efaeeb58
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e2f9de63c86714f2e338c361477c020d0d38ad9f9f1082eee28a358b0391f9eac9f0dff12b51df75c194294c7d721051518984f2d3d3148831e3f6cd08f06f7d
|
7
|
+
data.tar.gz: fc33371d82f378538db0ef0df28e40615b977729a5fbb6526ca43b3f1db6477d6d08758b0c22e4c5e058389a78bc85bc15f0cdabbfdb66496e8904faffdee7b1
|
data/README.md
CHANGED
@@ -282,15 +282,31 @@ the environment and plugs in the needed object. This can be overridden where
|
|
282
282
|
special io needs exist.
|
283
283
|
|
284
284
|
### Auto-Complete
|
285
|
-
The mini readline gem comes with
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
285
|
+
The mini readline gem comes with four auto-complete engines. These are:
|
286
|
+
|
287
|
+
###### MiniReadline::ArraySource
|
288
|
+
Make a selection from an array of choices. That array is found in the
|
289
|
+
option :array_src. This can either be an array of strings or a proc (or lambda)
|
290
|
+
that returns an array of strings. This is an excellent choice for choosing
|
291
|
+
from a list or program generated selection of choices.
|
292
|
+
|
293
|
+
###### MiniReadline::FileFolderSource
|
294
|
+
A simple, in-line auto-complete for files and folders. This is an excellent
|
295
|
+
choice for cases where file names are to be used by a ruby program or passed
|
296
|
+
to a Linux/Other command line shell.
|
297
|
+
|
298
|
+
###### MiniReadline::QuotedFileFolderSource
|
299
|
+
A simple, in-line auto-complete for files and folders embedded in quotes.
|
300
|
+
This is a good choice where the returned string is to be evaluated as ruby
|
301
|
+
code. The enclosing quotes will ensure that file names are evaluated as
|
302
|
+
strings. NOTE: This is the default auto-complete data source.
|
303
|
+
|
304
|
+
###### MiniReadline::AutoFileSource
|
305
|
+
This auto-complete for files and folders is designed to automatically select
|
306
|
+
the appropriate folder separator character and use quotes when files contain
|
307
|
+
embedded spaces. This is a good choice when building commands with files that
|
308
|
+
will be passed to the command line processor in multi-platform, portable
|
309
|
+
environments.
|
294
310
|
|
295
311
|
### Adding Custom Auto-Completers
|
296
312
|
It is possible, and fairly straightforward to add application specific
|
@@ -344,6 +360,13 @@ options.
|
|
344
360
|
statement to permit the use of clearer, easier to read access to regular
|
345
361
|
expression results.
|
346
362
|
|
363
|
+
### Important Security Note
|
364
|
+
|
365
|
+
It must be remembered that any time strings are passed to the command line
|
366
|
+
processor, there are serious security concerns. Passing such strings should
|
367
|
+
only be done in cases where the user would be trusted with access to the
|
368
|
+
command line itself. Untrusted users should **never** be given such access!
|
369
|
+
|
347
370
|
## Demo
|
348
371
|
A simple demo of mini_readline in action is available. To access this demo use
|
349
372
|
the following from the mini_readline root folder:
|
@@ -37,10 +37,12 @@ module MiniReadline
|
|
37
37
|
#* prompt - A string used to prompt the user. '>' is popular.
|
38
38
|
#* options - A hash of options; Typically symbol: value
|
39
39
|
def readline(options = {})
|
40
|
+
suppress_warnings
|
40
41
|
initialize_parms(options)
|
41
42
|
@edit.edit_process
|
42
43
|
ensure
|
43
44
|
@term.conclude
|
45
|
+
restore_warnings
|
44
46
|
end
|
45
47
|
|
46
48
|
#Initialize the read line process. This basically process the arguments
|
@@ -78,5 +80,18 @@ module MiniReadline
|
|
78
80
|
fail "Too long: #{prompt.inspect}"
|
79
81
|
end
|
80
82
|
end
|
83
|
+
|
84
|
+
#No warnings please!
|
85
|
+
def suppress_warnings
|
86
|
+
@old_stderr = $stderr
|
87
|
+
$stderr = File.open(File::NULL, 'w')
|
88
|
+
end
|
89
|
+
|
90
|
+
#Restore warnings to their typical ugliness.
|
91
|
+
def restore_warnings
|
92
|
+
$stderr.close
|
93
|
+
$stderr = @old_stderr
|
94
|
+
end
|
95
|
+
|
81
96
|
end
|
82
97
|
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
#* auto_file.rb - The data source for mysh file name auto-complete.
|
4
|
+
module MiniReadline
|
5
|
+
|
6
|
+
#* auto_file_source.rb - A flexible file source for shell emulation.
|
7
|
+
class AutoFileSource
|
8
|
+
|
9
|
+
#Create a new file/folder auto-data source. NOP
|
10
|
+
def initialize(_options)
|
11
|
+
#Do nothing here!
|
12
|
+
end
|
13
|
+
|
14
|
+
#Construct a new data list for auto-complete
|
15
|
+
def rebuild(str)
|
16
|
+
extract_root_pivot(str)
|
17
|
+
|
18
|
+
list = Dir.glob(dress_down(@pivot) + '*')
|
19
|
+
|
20
|
+
@cycler = list.empty? ? nil : list.cycle
|
21
|
+
end
|
22
|
+
|
23
|
+
#The regex for extraction of the root and pivot.
|
24
|
+
EXTRACT = /("[^"\s][^"]*"?$)|(\S+$)/
|
25
|
+
|
26
|
+
#Parse the string into the two basic components.
|
27
|
+
def extract_root_pivot(str)
|
28
|
+
@root, @pivot = EXTRACT =~ str ? [$PREMATCH, $MATCH] : [str, ""]
|
29
|
+
end
|
30
|
+
|
31
|
+
#Get the next string for auto-complete
|
32
|
+
def next
|
33
|
+
@root + dress_up(@cycler.next)
|
34
|
+
end
|
35
|
+
|
36
|
+
#Prepare the file name for internal use.
|
37
|
+
#<br>Endemic Code Smells
|
38
|
+
#* :reek:UtilityFunction
|
39
|
+
def dress_down(name)
|
40
|
+
name.gsub("\\", "/").gsub('"', '')
|
41
|
+
end
|
42
|
+
|
43
|
+
#Prepare the file name for external use.
|
44
|
+
#<br>Endemic Code Smells
|
45
|
+
#* :reek:UtilityFunction
|
46
|
+
def dress_up(name)
|
47
|
+
dress_up_quotes(dress_up_slashes(name))
|
48
|
+
end
|
49
|
+
|
50
|
+
#Dress up slashes and backslashes.
|
51
|
+
def dress_up_slashes(name)
|
52
|
+
backslash? ? name.gsub("/", "\\") : name
|
53
|
+
end
|
54
|
+
|
55
|
+
#Dress up in quotes if needed.
|
56
|
+
#<br>Endemic Code Smells
|
57
|
+
#* :reek:UtilityFunction
|
58
|
+
def dress_up_quotes(name)
|
59
|
+
name[' '] ? "\"#{name}\"" : name
|
60
|
+
end
|
61
|
+
|
62
|
+
#Does this file name use backslashes?
|
63
|
+
def backslash?
|
64
|
+
if @pivot.end_with?("\\")
|
65
|
+
true
|
66
|
+
elsif @pivot.end_with?("/")
|
67
|
+
false
|
68
|
+
elsif @pivot["\\"]
|
69
|
+
true
|
70
|
+
elsif @pivot["/"]
|
71
|
+
false
|
72
|
+
else
|
73
|
+
MiniReadline::PLATFORM == :windows
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
@@ -119,7 +119,7 @@ class MiniReadlineTester < Minitest::Test
|
|
119
119
|
assert(result.is_a?(String))
|
120
120
|
end
|
121
121
|
|
122
|
-
def
|
122
|
+
def test_file_quoted
|
123
123
|
puts "\nPlease select an quoted file."
|
124
124
|
|
125
125
|
edit = MiniReadline::Readline.new(auto_complete: true,
|
@@ -129,6 +129,16 @@ class MiniReadlineTester < Minitest::Test
|
|
129
129
|
assert(result.is_a?(String))
|
130
130
|
end
|
131
131
|
|
132
|
+
def test_file_shell
|
133
|
+
puts "\nPlease select an auto file."
|
134
|
+
|
135
|
+
edit = MiniReadline::Readline.new(auto_complete: true,
|
136
|
+
auto_source: MiniReadline::AutoFileSource)
|
137
|
+
|
138
|
+
result = edit.readline(prompt: "File: ")
|
139
|
+
assert(result.is_a?(String))
|
140
|
+
end
|
141
|
+
|
132
142
|
def test_end_of_input_detection
|
133
143
|
edit = MiniReadline::Readline.new()
|
134
144
|
puts "Exit by signaling end of input"
|
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.
|
4
|
+
version: 0.6.0
|
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-
|
11
|
+
date: 2016-08-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|
@@ -98,6 +98,7 @@ files:
|
|
98
98
|
- lib/mini_readline/read_line/edit.rb
|
99
99
|
- lib/mini_readline/read_line/edit/auto_complete.rb
|
100
100
|
- lib/mini_readline/read_line/edit/auto_complete/array_source.rb
|
101
|
+
- lib/mini_readline/read_line/edit/auto_complete/auto_file_source.rb
|
101
102
|
- lib/mini_readline/read_line/edit/auto_complete/auto_manager.rb
|
102
103
|
- lib/mini_readline/read_line/edit/auto_complete/file_folder_source.rb
|
103
104
|
- lib/mini_readline/read_line/edit/auto_complete/quoted_file_folder_source.rb
|