Class: TTY::Prompt::Slider

Inherits:
Object
  • Object
show all
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

Methods included from Symbols

symbols, windows?

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, 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

Instance Method Details

#call(question, &block) ⇒ Object

Call the slider by passing question

Parameters:

  • question (String)

    the question to ask



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

#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

Returns:

  • (Integer)


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

#keyleftObject Also known as: keydown



89
90
91
# File 'lib/tty/prompt/slider.rb', line 89

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

#keyreturnObject Also known as: keyspace



99
100
101
# File 'lib/tty/prompt/slider.rb', line 99

def keyreturn(*)
  @done = true
end

#keyrightObject 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

#rangeArray[Integer]

Range of numbers to render

Returns:

  • (Array[Integer])


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