mini_readline 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2485e678176d166843920e6f17956c7b839d90e3
4
- data.tar.gz: 16afd9de803f9b3d3aae4c8f32318edef88ffbd2
3
+ metadata.gz: fdfa436a71940734402798fef36869a990280c8a
4
+ data.tar.gz: 3d895f85d7c7f8a965362068aed8728fec2536c5
5
5
  SHA512:
6
- metadata.gz: 48fb1ac610d575f4b622f45531842a2ead3b493b7261b4f376181c763dea76942a8058e8594386b02bb1df4835fb8931c21d40e4fa10cddbca7818f915ca7f4c
7
- data.tar.gz: e7f170a4f2404366bb00db4d5482ada1e6fa927cd044707ed9e6d0a44b517fd62ac1f6c675e4791d3c5c8b1b2e9e3fa5b66fc6b636f32d279385d182976cc2b1
6
+ metadata.gz: 558703724da30c6794aa066ded902f72a1a41496b5fb88b2ac05cace0baa4021e06b4b2b03bf8a0f7db1edeabc87773a36f5e3595fe0fe11c583e56047bba3b1
7
+ data.tar.gz: e219431676db413815d454a5b4d9c6996ad71d20552f22c0df75130954413aa55ca419cf705aa6f96c1c0a960b760956e9b7af183e2dc905b835868ed64ed1b7
data/README.md CHANGED
@@ -8,7 +8,7 @@ that is part of Ruby. The mini readline will focus on the needs of Ruby programs
8
8
  as opposed to a UN*X shell program.
9
9
 
10
10
  The mini_readline gem is designed for use with MRI version 1.9.3 or later.
11
- The original readline which tries to support older versions of Ruby.
11
+ The original readline tried to support older versions of Ruby.
12
12
 
13
13
  ## Installation
14
14
  Add this line to your application's Gemfile:
@@ -206,6 +206,9 @@ special io needs exist.
206
206
 
207
207
  ### Auto-Complete
208
208
  The mini readline gem comes with two auto-complete engines. These are:
209
+ * MiniReadline::ArraySource - Make a selection from an array of choices. That
210
+ array is found in the option :array_src. This can either be an array of
211
+ strings or a proc (or lambda) that returns an array of strings.
209
212
  * MiniReadline::FileFolderSource - A simple, in-line auto-complete for files
210
213
  and folders that do **not** contain embedded spaces.
211
214
  * MiniReadline::QuotedFileFolderSource - A simple, in-line auto-complete for
@@ -3,6 +3,7 @@
3
3
  require_relative 'auto_complete/auto_manager'
4
4
  require_relative 'auto_complete/file_folder_source'
5
5
  require_relative 'auto_complete/quoted_file_folder_source'
6
+ require_relative 'auto_complete/array_source'
6
7
 
7
8
  #* read_line/window/edit/auto_complete.rb - Process :auto_complete
8
9
  module MiniReadline
@@ -0,0 +1,48 @@
1
+ # coding: utf-8
2
+
3
+ #* array_source.rb - An array as the source for auto-complete.
4
+ module MiniReadline
5
+
6
+ #* array_source.rb - An array as the source for auto-complete.
7
+ class ArraySource
8
+
9
+ #Create a new file/folder auto-data source. NOP
10
+ def initialize(options)
11
+ @options = options
12
+ end
13
+
14
+ #Construct a new data list for auto-complete
15
+ def rebuild(str)
16
+ extract_root_pivot(str)
17
+
18
+ list = (get_array.select {|str| str.start_with?(@pivot)}).sort
19
+
20
+ unless list.empty?
21
+ @cycler = list.cycle
22
+ else
23
+ @cycler = nil
24
+ end
25
+ end
26
+
27
+ #Parse the string into the two basic components.
28
+ def extract_root_pivot(str)
29
+ @root, @pivot = /\S+$/ =~ str ? [$PREMATCH, $MATCH] : [str, ""]
30
+ end
31
+
32
+ #Get the array of data from either an array or a block.
33
+ def get_array
34
+ if (src = @options[:array_src]).is_a?(Proc)
35
+ src.call
36
+ else
37
+ src || []
38
+ end
39
+ end
40
+
41
+ #Get the next string for auto-complete
42
+ def next
43
+ @root + @cycler.next
44
+ end
45
+
46
+ end
47
+
48
+ end
@@ -7,9 +7,7 @@ module MiniReadline
7
7
  class FileFolderSource
8
8
 
9
9
  #Create a new file/folder auto-data source. NOP
10
- def initialize(options)
11
- @options = options
12
- end
10
+ def initialize(_options); end
13
11
 
14
12
  #Construct a new data list for auto-complete
15
13
  def rebuild(str)
@@ -7,9 +7,7 @@ module MiniReadline
7
7
  class QuotedFileFolderSource
8
8
 
9
9
  #Create a new file/folder auto-data source. NOP
10
- def initialize(options)
11
- @options = options
12
- end
10
+ def initialize(_options); end
13
11
 
14
12
  #Construct a new data list for auto-complete
15
13
  def rebuild(str)
@@ -24,9 +22,12 @@ module MiniReadline
24
22
  end
25
23
  end
26
24
 
25
+ #The parsing regular expression.
26
+ REGEX = /(?<=\")([^\"\s][^\"]*)?(?=\"?$)/
27
+
27
28
  #Parse the string into the two basic components.
28
29
  def extract_root_pivot(str)
29
- @root, @pivot = /(?<=\")([^\"\s][^\"]*)?(?=\"?$)/ =~ str ? [$PREMATCH, $MATCH] : [str + '"', ""]
30
+ @root, @pivot = REGEX =~ str ? [$PREMATCH, $MATCH] : [str + '"', ""]
30
31
  end
31
32
 
32
33
  #Get the next string for auto-complete
@@ -1,4 +1,4 @@
1
1
  module MiniReadline
2
2
  #The current version of the mini_readline gem.
3
- VERSION = "0.3.0"
3
+ VERSION = "0.3.1"
4
4
  end
@@ -71,6 +71,43 @@ class MiniReadlineTester < Minitest::Test
71
71
  assert_equal("quit", result)
72
72
  end
73
73
 
74
+ def test_array_complete
75
+ puts "\nPlease select an approved fruit."
76
+
77
+ fruit = ["apple", "blackberry", "blueberry", "cantaloupe", "cherry",
78
+ "clementine fruit", "coconut", "cranberry", "date", "elderberry",
79
+ "fig", "gooseberry", "grape", "loganberry", "lychee",
80
+ "mango", "olive", "orange", "papaya", "passion fruit", "peach",
81
+ "pear", "pineapple", "plum", "raspberry", "redcurrant",
82
+ "star fruit", "strawberry", "tomato"]
83
+
84
+ edit = MiniReadline::Readline.new(auto_complete: true,
85
+ auto_source: MiniReadline::ArraySource,
86
+ array_src: fruit)
87
+
88
+ result = edit.readline(prompt: "Fruit: ")
89
+ assert(fruit.include?(result))
90
+ end
91
+
92
+ def test_block_complete
93
+ puts "\nPlease select an approved fruit."
94
+
95
+ fruit = ["apple", "blackberry", "blueberry", "cantaloupe", "cherry",
96
+ "clementine fruit", "coconut", "cranberry", "date", "elderberry",
97
+ "fig", "gooseberry", "grape", "loganberry", "lychee",
98
+ "mango", "olive", "orange", "papaya", "passion fruit", "peach",
99
+ "pear", "pineapple", "plum", "raspberry", "redcurrant",
100
+ "star fruit", "strawberry", "tomato"]
101
+
102
+ edit = MiniReadline::Readline.new(auto_complete: true,
103
+ auto_source: MiniReadline::ArraySource,
104
+ array_src: lambda { fruit })
105
+
106
+ result = edit.readline(prompt: "Fruit: ")
107
+ assert(fruit.include?(result))
108
+ end
109
+
110
+
74
111
  def test_end_of_input_detection
75
112
  edit = MiniReadline::Readline.new()
76
113
  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.3.0
4
+ version: 0.3.1
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-26 00:00:00.000000000 Z
11
+ date: 2016-01-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -95,6 +95,7 @@ files:
95
95
  - lib/mini_readline/read_line.rb
96
96
  - lib/mini_readline/read_line/edit.rb
97
97
  - lib/mini_readline/read_line/edit/auto_complete.rb
98
+ - lib/mini_readline/read_line/edit/auto_complete/array_source.rb
98
99
  - lib/mini_readline/read_line/edit/auto_complete/auto_manager.rb
99
100
  - lib/mini_readline/read_line/edit/auto_complete/file_folder_source.rb
100
101
  - lib/mini_readline/read_line/edit/auto_complete/quoted_file_folder_source.rb