class TTY::Prompt::Slider

A class responsible for gathering numeric input from range

@api public

Constants

HELP

Public Class Methods

new(prompt, options = {}) click to toggle source

Initailize a Slider

@api public

# File lib/tty/prompt/slider.rb, line 19
def initialize(prompt, options = {})
  @prompt       = prompt
  @prefix       = options.fetch(:prefix) { @prompt.prefix }
  @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 }
  @first_render = true
  @done         = false

  @prompt.subscribe(self)
end

Public Instance Methods

call(question, &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 82
def call(question, &block)
  @question = question
  block.call(self) if block
  @active = initial
  render
end
default(value) click to toggle source

@api public

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

Setup initial active position

@return [Integer]

@api private

# File lib/tty/prompt/slider.rb, line 39
def initial
  if @default.nil?
    range.size / 2
  else
    range.index(@default)
  end
end
keydown(*)
Alias for: keyleft
keyleft(*) click to toggle source
# File lib/tty/prompt/slider.rb, line 89
def keyleft(*)
  @active -= 1 if @active > 0
end
Also aliased as: keydown
keyreturn(*) click to toggle source
# File lib/tty/prompt/slider.rb, line 99
def keyreturn(*)
  @done = true
end
Also aliased as: keyspace
keyright(*) click to toggle source
# File lib/tty/prompt/slider.rb, line 94
def keyright(*)
  @active += 1 if (@active + @step) < range.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 67
def max(value)
  @max = value
end
min(value) click to toggle source

@api public

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

Range of numbers to render

@return [Array]

@apip private

# File lib/tty/prompt/slider.rb, line 52
def range
  (@min..@max).step(@step).to_a
end
step(value) click to toggle source

@api public

# File lib/tty/prompt/slider.rb, line 72
def step(value)
  @step = value
end

Private Instance Methods

answer() click to toggle source

@return [Integer]

@api private

# File lib/tty/prompt/slider.rb, line 136
def answer
  range[@active]
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 129
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 109
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)
  answer
ensure
  @prompt.print(@prompt.show)
end
render_header() click to toggle source

Render actual answer or help

@return [String]

@api private

# File lib/tty/prompt/slider.rb, line 157
def render_header
  if @done
    @prompt.decorate(answer.to_s, @active_color)
  elsif @first_render
    @prompt.decorate(HELP, @help_color)
  end
end
render_question() click to toggle source

Render question with the slider

@return [String]

@api private

# File lib/tty/prompt/slider.rb, line 145
def render_question
  header = "#{@prefix}#{@question} #{render_header}\n"
  @first_render = false
  header << render_slider unless @done
  header
end
render_slider() click to toggle source

Render slider representation

@return [String]

@api private

# File lib/tty/prompt/slider.rb, line 170
def render_slider
  output = ''
  output << symbols[:pipe]
  output << symbols[:line] * @active
  output << @prompt.decorate(symbols[:handle], @active_color)
  output << symbols[:line] * (range.size - @active - 1)
  output << symbols[:pipe]
  output << " #{range[@active]}"
  output
end