class TTY::Prompt::Slider

A class responsible for gathering numeric input from range

@api public

Constants

FORMAT
HELP

Public Class Methods

new(prompt, **options) click to toggle source

Initailize a Slider

@param [Prompt] prompt

the prompt

@param [Hash] options

the options to configure this slider

@option options [Integer] :min The minimum value @option options [Integer] :max The maximum value @option options [Integer] :step The step value @option options [String] :format The display format

@api public

# File lib/tty/prompt/slider.rb, line 25
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

Public Instance Methods

call(question, possibilities = nil, &block) click to toggle source

Call the slider by passing question

@param [String] question

the question to ask

@apu public

# File lib/tty/prompt/slider.rb, line 165
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) click to toggle source

Add a single choice

@api public

# File lib/tty/prompt/slider.rb, line 125
def choice(*value, &block)
  if block
    @choices << (value << block)
  else
    @choices << value
  end
end
choices(values = (not_set = true)) click to toggle source

Add multiple choices

@param [Array] values

the values to add as choices

@api public

# File lib/tty/prompt/slider.rb, line 139
def choices(values = (not_set = true))
  if not_set
    @choices
  else
    values.each { |val| @choices << val }
  end
end
default(value) click to toggle source

@api public

# File lib/tty/prompt/slider.rb, line 103
def default(value)
  @default = value
end
default_help() click to toggle source

Default help text

@api public

# File lib/tty/prompt/slider.rb, line 77
def default_help
  arrows = @symbols[:arrow_left] + "/" + @symbols[:arrow_right]
  sprintf(HELP, arrows)
end
format(value) click to toggle source

@api public

# File lib/tty/prompt/slider.rb, line 148
def format(value)
  @format = value
end
help(text = (not_set = true)) click to toggle source

Set help text

@param [String] text

@api private

# File lib/tty/prompt/slider.rb, line 87
def help(text = (not_set = true))
  return @help if !@help.nil? && not_set

  @help = (@help.nil? && not_set) ? default_help : text
end
initial() click to toggle source

Setup initial active position

@return [Integer]

@api private

# File lib/tty/prompt/slider.rb, line 61
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
keydown(*)
Alias for: keyleft
keyenter(*)
Alias for: keyreturn
keyleft(*) click to toggle source
# File lib/tty/prompt/slider.rb, line 179
def keyleft(*)
  @active -= 1 if @active > 0
end
Also aliased as: keydown
keyreturn(*) click to toggle source
# File lib/tty/prompt/slider.rb, line 189
def keyreturn(*)
  @done = true
end
Also aliased as: keyspace, keyenter
keyright(*) click to toggle source
# File lib/tty/prompt/slider.rb, line 184
def keyright(*)
  @active += 1 if (@active + 1) < choices.size
end
Also aliased as: keyup
keyspace(*)
Alias for: keyreturn
keyup(*)
Alias for: keyright
max(value) click to toggle source

@api public

# File lib/tty/prompt/slider.rb, line 113
def max(value)
  @max = value
end
min(value) click to toggle source

@api public

# File lib/tty/prompt/slider.rb, line 108
def min(value)
  @min = value
end
quiet(value) click to toggle source

Set quiet mode.

@api public

# File lib/tty/prompt/slider.rb, line 155
def quiet(value)
  @quiet = value
end
show_help(value = (not_set = true)) click to toggle source

Change when help is displayed

@api public

# File lib/tty/prompt/slider.rb, line 96
def show_help(value = (not_set = true))
  return @show_ehlp if not_set

  @show_help = value
end
step(value) click to toggle source

@api public

# File lib/tty/prompt/slider.rb, line 118
def step(value)
  @step = value
end
symbols(new_symbols = (not_set = true)) click to toggle source

Change symbols used by this prompt

@param [Hash] new_symbols

the new symbols to use

@api public

# File lib/tty/prompt/slider.rb, line 50
def symbols(new_symbols = (not_set = true))
  return @symbols if not_set

  @symbols.merge!(new_symbols)
end

Private Instance Methods

answer() click to toggle source

@return [Integer, String]

@api private

# File lib/tty/prompt/slider.rb, line 241
def answer
  choices[@active].value
end
help_always?() click to toggle source

Check if help is always displayed

@api private

# File lib/tty/prompt/slider.rb, line 207
def help_always?
  @show_help =~ /always/i
end
help_start?() click to toggle source

Check if help is shown only on start

@api private

# File lib/tty/prompt/slider.rb, line 200
def help_start?
  @show_help =~ /start/i
end
refresh(lines) click to toggle source

Clear screen

@param [Integer] lines

the lines to clear

@api private

# File lib/tty/prompt/slider.rb, line 234
def refresh(lines)
  @prompt.print(@prompt.clear_lines(lines))
end
render() click to toggle source

Render an interactive range slider.

@api private

# File lib/tty/prompt/slider.rb, line 214
def render
  @prompt.print(@prompt.hide)
  until @done
    question = render_question
    @prompt.print(question)
    @prompt.read_keypress
    refresh(question.lines.count)
  end
  @prompt.print(render_question) unless @quiet
  answer
ensure
  @prompt.print(@prompt.show)
end
render_question() click to toggle source

Render question with the slider

@return [String]

@api private

# File lib/tty/prompt/slider.rb, line 250
def render_question
  header = ["#{@prefix}#{@question} "]
  if @done
    header << @prompt.decorate(choices[@active].to_s, @active_color)
    header << "\n"
  else
    header << render_slider
  end
  if @first_render && (help_start? || help_always?) ||
      (help_always? && !@done)
    header << "\n" + @prompt.decorate(help, @help_color)
    @first_render = false
  end
  header.join
end
render_slider() click to toggle source

Render slider representation

@return [String]

@api private

# File lib/tty/prompt/slider.rb, line 271
def render_slider
  slider = (@symbols[:line] * @active) +
           @prompt.decorate(@symbols[:bullet], @active_color) +
           (@symbols[:line] * (choices.size - @active - 1))
  value = choices[@active].name
  case @format
  when Proc
    @format.call(slider, value)
  else
    @format.gsub(":slider", slider) % [value]
  end
end