downup 0.4.3 → 0.5.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ae6bea2f18582ceaace3c1f7eb8f99508739823e
4
- data.tar.gz: 3caf86aa27195deab1ee03491a0e2c94dd239df5
3
+ metadata.gz: 5c807a7510191208226e0ec1f986945a8fdcbda7
4
+ data.tar.gz: fd88b648476543d72e5488aef01def58e593deaf
5
5
  SHA512:
6
- metadata.gz: 64ae718eda8e249c784a4991d9636f1752468330b9fe5220f0f4a78ad6754f6eaa898007f92e0cc8424d44b91f786fe36d9b8b0d218e687286956af06d773fb8
7
- data.tar.gz: b9d21db6b1899486fc41cf0d2b9d7121da8d7fddbc7a82e8bbb2d2c92202d7736a250989289133b2aff343f786c948897884109da32610c23fdb96de4efed610
6
+ metadata.gz: 393abc7587df882bbaa94d9a46d73307ff8a882b8cb22cee8a65b609bbcac72330c521b073e7454dfade1407265d57fff6b6c210988bb3fa900d7a283b129c36
7
+ data.tar.gz: a4db31d8bdc4a4f45ce71ca189a58aed78254e7d4f78bc627add5d4072cb66eabe96ada96ffd9bf74e69284cd68a0629bba7abf2bb15a65446c3761aa8ed06c8
data/downup-0.4.3.gem ADDED
Binary file
data/examples/basic.rb CHANGED
@@ -31,3 +31,13 @@ puts Downup::Base.new(
31
31
  default_color: :cyan,
32
32
  selected_color: :bg_magenta
33
33
  ).prompt
34
+
35
+ options = {
36
+ "a" => "Cat",
37
+ "b" => "Dog",
38
+ "c" => "Kangaroo",
39
+ "d" => "Snake",
40
+ "e" => "Eel"
41
+ }
42
+
43
+ puts Downup::Base.new(options: options).prompt
@@ -1,3 +1,3 @@
1
1
  module Downup
2
- VERSION = "0.4.3"
2
+ VERSION = "0.5.4"
3
3
  end
data/lib/downup.rb CHANGED
@@ -17,6 +17,8 @@ module Downup
17
17
  @default_color = default_color
18
18
  @selected_color = selected_color
19
19
  @header_proc = header_proc
20
+ @stdin = STDIN
21
+ @stdout = $stdout
20
22
  end
21
23
 
22
24
  def prompt(position = 0)
@@ -25,7 +27,7 @@ module Downup
25
27
  header_proc.call
26
28
  print_title
27
29
  print_options
28
- print "\n> "
30
+ stdout.print "\n> "
29
31
  process_input read_char
30
32
  end
31
33
 
@@ -36,7 +38,9 @@ module Downup
36
38
  :selected_position,
37
39
  :header_proc,
38
40
  :selected_color,
39
- :default_color
41
+ :default_color,
42
+ :stdin,
43
+ :stdout
40
44
 
41
45
  def process_input(input)
42
46
  case input
@@ -44,14 +48,27 @@ module Downup
44
48
  prompt(selected_position - 1)
45
49
  when "\e[B", "j"
46
50
  prompt(selected_position + 1)
47
- when "\u0003" then exit
51
+ when *option_keys
52
+ prompt(option_keys.index(input))
48
53
  when "\r"
54
+ execute_selection(input)
55
+ when "\u0003" then exit
56
+ else prompt(selected_position); end
57
+ end
58
+
59
+ def execute_selection(input)
60
+ case options
61
+ when Array
49
62
  options[selected_position]
50
- else
51
- prompt(selected_position)
63
+ when Hash
64
+ options.fetch(option_keys[selected_position])
52
65
  end
53
66
  end
54
67
 
68
+ def option_keys
69
+ options.is_a?(Array) ? [] : options.keys
70
+ end
71
+
55
72
  def position_selector(position)
56
73
  case position
57
74
  when -1 then options.count - 1
@@ -60,8 +77,15 @@ module Downup
60
77
  end
61
78
 
62
79
  def print_options
63
- options.each_with_index do |option, index|
64
- puts colorize_option(option, index)
80
+ case options
81
+ when Array
82
+ options.each_with_index do |option, index|
83
+ stdout.puts colorize_option(option, index)
84
+ end
85
+ when Hash
86
+ options.each_with_index do |option_array, index|
87
+ stdout.puts colorize_option(option_array.join(": "), index)
88
+ end
65
89
  end
66
90
  end
67
91
 
@@ -75,20 +99,20 @@ module Downup
75
99
 
76
100
  def print_title
77
101
  return if title.nil?
78
- puts "#{title}".red
102
+ stdout.puts "#{title}".red
79
103
  end
80
104
 
81
105
  def read_char
82
- STDIN.echo = false
83
- STDIN.raw!
84
- input = STDIN.getc.chr
106
+ stdin.echo = false
107
+ stdin.raw!
108
+ input = stdin.getc.chr
85
109
  if input == "\e" then
86
- input << STDIN.read_nonblock(3) rescue nil
87
- input << STDIN.read_nonblock(2) rescue nil
110
+ input << stdin.read_nonblock(3) rescue nil
111
+ input << stdin.read_nonblock(2) rescue nil
88
112
  end
89
113
  ensure
90
- STDIN.echo = true
91
- STDIN.cooked!
114
+ stdin.echo = true
115
+ stdin.cooked!
92
116
  return input
93
117
  end
94
118
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: downup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.5.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Begin
@@ -56,6 +56,7 @@ files:
56
56
  - bin/console
57
57
  - bin/setup
58
58
  - downup-0.3.3.gem
59
+ - downup-0.4.3.gem
59
60
  - downup.gemspec
60
61
  - examples/basic.rb
61
62
  - lib/downup.rb