Class: TTY2::Prompt::Slider

Inherits:
Object
  • Object
show all
Defined in:
lib/tty2/prompt/slider.rb

Overview

A class responsible for gathering numeric input from range

Constant Summary collapse

HELP =
"(Use %s arrow keys, press Enter to select)"
FORMAT =
":slider %s"

Instance Method Summary collapse

Constructor Details

#initialize(prompt, **options) ⇒ Slider

Initailize a Slider

Options Hash (**options):

  • :min (Integer)

    The minimum value

  • :max (Integer)

    The maximum value

  • :step (Integer)

    The step value

  • :format (String)

    The display format



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tty2/prompt/slider.rb', line 26

def initialize(prompt, **options)
  @prompt       = prompt
  @prefix       = options.fetch(:prefix) { @prompt.prefix }
  @choices      = Choices.new
  @min          = options.fetch(:min, 0)
  @max          = options.fetch(:max, 10)
  @step         = options.fetch(:step, 1)
  @default      = options[:default]
  @active_color = options.fetch(:active_color) { @prompt.active_color }
  @help_color   = options.fetch(:help_color) { @prompt.help_color }
  @format       = options.fetch(:format) { FORMAT }
  @quiet        = options.fetch(:quiet) { @prompt.quiet }
  @help         = options[:help]
  @show_help    = options.fetch(:show_help) { :start }
  @symbols      = @prompt.symbols.merge(options.fetch(:symbols, {}))
  @first_render = true
  @done         = false
end

Instance Method Details

#call(question, possibilities = nil, &block) ⇒ Object

Call the slider by passing question



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/tty2/prompt/slider.rb', line 166

def call(question, possibilities = nil, &block)
  @question = question
  choices(possibilities) if possibilities
  block.call(self) if block
  # set up a Choices collection for min, max, step
  # if no possibilities were supplied
  choices((@min..@max).step(@step).to_a) if @choices.empty?

  @active = initial
  @prompt.subscribe(self) do
    render
  end
end

#choice(*value, &block) ⇒ Object

Add a single choice



126
127
128
129
130
131
132
# File 'lib/tty2/prompt/slider.rb', line 126

def choice(*value, &block)
  if block
    @choices << (value << block)
  else
    @choices << value
  end
end

#choices(values = (not_set = true)) ⇒ Object

Add multiple choices



140
141
142
143
144
145
146
# File 'lib/tty2/prompt/slider.rb', line 140

def choices(values = (not_set = true))
  if not_set
    @choices
  else
    values.each { |val| @choices << val }
  end
end

#default(value) ⇒ Object



104
105
106
# File 'lib/tty2/prompt/slider.rb', line 104

def default(value)
  @default = value
end

#default_helpObject

Default help text



78
79
80
81
# File 'lib/tty2/prompt/slider.rb', line 78

def default_help
  arrows = @symbols[:arrow_left] + "/" + @symbols[:arrow_right]
  sprintf(HELP, arrows)
end

#format(value) ⇒ Object



149
150
151
# File 'lib/tty2/prompt/slider.rb', line 149

def format(value)
  @format = value
end

#help(text = (not_set = true)) ⇒ Object

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.

Set help text



88
89
90
91
92
# File 'lib/tty2/prompt/slider.rb', line 88

def help(text = (not_set = true))
  return @help if !@help.nil? && not_set

  @help = (@help.nil? && not_set) ? default_help : text
end

#initialInteger

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



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tty2/prompt/slider.rb', line 62

def initial
  if @default.nil?
    # no default - choose the middle option
    choices.size / 2
  elsif default_choice = choices.find_by(:name, @default)
    # found a Choice by name - use it
    choices.index(default_choice)
  else
    # default is the index number
    @default - 1
  end
end

#keyleftObject Also known as: keydown



180
181
182
# File 'lib/tty2/prompt/slider.rb', line 180

def keyleft(*)
  @active -= 1 if @active > 0
end

#keyreturnObject Also known as: keyspace, keyenter



190
191
192
# File 'lib/tty2/prompt/slider.rb', line 190

def keyreturn(*)
  @done = true
end

#keyrightObject Also known as: keyup



185
186
187
# File 'lib/tty2/prompt/slider.rb', line 185

def keyright(*)
  @active += 1 if (@active + 1) < choices.size
end

#max(value) ⇒ Object



114
115
116
# File 'lib/tty2/prompt/slider.rb', line 114

def max(value)
  @max = value
end

#min(value) ⇒ Object



109
110
111
# File 'lib/tty2/prompt/slider.rb', line 109

def min(value)
  @min = value
end

#quiet(value) ⇒ Object

Set quiet mode.



156
157
158
# File 'lib/tty2/prompt/slider.rb', line 156

def quiet(value)
  @quiet = value
end

#show_help(value = (not_set = true)) ⇒ Object

Change when help is displayed



97
98
99
100
101
# File 'lib/tty2/prompt/slider.rb', line 97

def show_help(value = (not_set = true))
  return @show_ehlp if not_set

  @show_help = value
end

#step(value) ⇒ Object



119
120
121
# File 'lib/tty2/prompt/slider.rb', line 119

def step(value)
  @step = value
end

#symbols(new_symbols = (not_set = true)) ⇒ Object

Change symbols used by this prompt



51
52
53
54
55
# File 'lib/tty2/prompt/slider.rb', line 51

def symbols(new_symbols = (not_set = true))
  return @symbols if not_set

  @symbols.merge!(new_symbols)
end