Class: TTY::Prompt::Slider
- Inherits:
-
Object
- Object
- TTY::Prompt::Slider
- Includes:
- Symbols
- Defined in:
- lib/tty/prompt/slider.rb
Overview
A class responsible for gathering numeric input from range
Constant Summary collapse
- HELP =
'(Use arrow keys, press Enter to select)'.freeze
Constants included from Symbols
TTY::Prompt::Symbols::KEYS, TTY::Prompt::Symbols::WIN_KEYS
Instance Method Summary collapse
-
#call(question, &block) ⇒ Object
Call the slider by passing question.
- #default(value) ⇒ Object
-
#initial ⇒ Integer
private
Setup initial active position.
-
#initialize(prompt, options = {}) ⇒ Slider
constructor
Initailize a Slider.
- #keyleft ⇒ Object (also: #keydown)
- #keyreturn ⇒ Object (also: #keyspace)
- #keyright ⇒ Object (also: #keyup)
- #max(value) ⇒ Object
- #min(value) ⇒ Object
-
#range ⇒ Array[Integer]
Range of numbers to render.
- #step(value) ⇒ Object
Methods included from Symbols
Constructor Details
#initialize(prompt, options = {}) ⇒ Slider
Initailize a Slider
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/tty/prompt/slider.rb', line 19 def initialize(prompt, = {}) @prompt = prompt @prefix = .fetch(:prefix) { @prompt.prefix } @min = .fetch(:min) { 0 } @max = .fetch(:max) { 10 } @step = .fetch(:step) { 1 } @default = [:default] @active_color = .fetch(:active_color) { @prompt.active_color } @help_color = .fetch(:help_color) { @prompt.help_color } @first_render = true @done = false @prompt.subscribe(self) end |
Instance Method Details
#call(question, &block) ⇒ Object
Call the slider by passing question
82 83 84 85 86 87 |
# File 'lib/tty/prompt/slider.rb', line 82 def call(question, &block) @question = question block.call(self) if block @active = initial render end |
#default(value) ⇒ Object
57 58 59 |
# File 'lib/tty/prompt/slider.rb', line 57 def default(value) @default = value end |
#initial ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Setup initial active position
39 40 41 42 43 44 45 |
# File 'lib/tty/prompt/slider.rb', line 39 def initial if @default.nil? range.size / 2 else range.index(@default) end end |
#keyleft ⇒ Object Also known as: keydown
89 90 91 |
# File 'lib/tty/prompt/slider.rb', line 89 def keyleft(*) @active -= 1 if @active > 0 end |
#keyreturn ⇒ Object Also known as: keyspace
99 100 101 |
# File 'lib/tty/prompt/slider.rb', line 99 def keyreturn(*) @done = true end |
#keyright ⇒ Object Also known as: keyup
94 95 96 |
# File 'lib/tty/prompt/slider.rb', line 94 def keyright(*) @active += 1 if (@active + @step) < range.size end |
#max(value) ⇒ Object
67 68 69 |
# File 'lib/tty/prompt/slider.rb', line 67 def max(value) @max = value end |
#min(value) ⇒ Object
62 63 64 |
# File 'lib/tty/prompt/slider.rb', line 62 def min(value) @min = value end |
#range ⇒ Array[Integer]
Range of numbers to render
52 53 54 |
# File 'lib/tty/prompt/slider.rb', line 52 def range (@min..@max).step(@step).to_a end |
#step(value) ⇒ Object
72 73 74 |
# File 'lib/tty/prompt/slider.rb', line 72 def step(value) @step = value end |