Class: TTY::Prompt::Slider

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/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 %d"

Instance Method Summary collapse

Constructor Details

#initialize(prompt, **options) ⇒ Slider

Initailize a Slider

Parameters:

  • prompt (Prompt)

    the prompt

  • options (Hash)

    the options to configure this 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
# File 'lib/tty/prompt/slider.rb', line 26

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 }
  @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, &block) ⇒ Object

Call the slider by passing question

Parameters:

  • question (String)

    the question to ask


143
144
145
146
147
148
149
150
# File 'lib/tty/prompt/slider.rb', line 143

def call(question, &block)
  @question = question
  block.call(self) if block
  @active = initial
  @prompt.subscribe(self) do
    render
  end
end

#default(value) ⇒ Object


107
108
109
# File 'lib/tty/prompt/slider.rb', line 107

def default(value)
  @default = value
end

#default_helpObject

Default help text


72
73
74
75
# File 'lib/tty/prompt/slider.rb', line 72

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

#format(value) ⇒ Object


126
127
128
# File 'lib/tty/prompt/slider.rb', line 126

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

Parameters:

  • text (String) (defaults to: (not_set = true))

82
83
84
85
86
# File 'lib/tty/prompt/slider.rb', line 82

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

Returns:

  • (Integer)

61
62
63
64
65
66
67
# File 'lib/tty/prompt/slider.rb', line 61

def initial
  if @default.nil?
    range.size / 2
  else
    range.index(@default)
  end
end

#keyleftObject Also known as: keydown


152
153
154
# File 'lib/tty/prompt/slider.rb', line 152

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

#keyreturnObject Also known as: keyspace, keyenter


162
163
164
# File 'lib/tty/prompt/slider.rb', line 162

def keyreturn(*)
  @done = true
end

#keyrightObject Also known as: keyup


157
158
159
# File 'lib/tty/prompt/slider.rb', line 157

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

#max(value) ⇒ Object


117
118
119
# File 'lib/tty/prompt/slider.rb', line 117

def max(value)
  @max = value
end

#min(value) ⇒ Object


112
113
114
# File 'lib/tty/prompt/slider.rb', line 112

def min(value)
  @min = value
end

#quiet(value) ⇒ Object

Set quiet mode.


133
134
135
# File 'lib/tty/prompt/slider.rb', line 133

def quiet(value)
  @quiet = value
end

#rangeArray[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.

Range of numbers to render

Returns:

  • (Array[Integer])

102
103
104
# File 'lib/tty/prompt/slider.rb', line 102

def range
  (@min..@max).step(@step).to_a
end

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

Change when help is displayed


91
92
93
94
95
# File 'lib/tty/prompt/slider.rb', line 91

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

  @show_help = value
end

#step(value) ⇒ Object


122
123
124
# File 'lib/tty/prompt/slider.rb', line 122

def step(value)
  @step = value
end

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

Change symbols used by this prompt

Parameters:

  • new_symbols (Hash) (defaults to: (not_set = true))

    the new symbols to use


50
51
52
53
54
# 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