1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
# frozen_string_literal: true
module TTY
# A class responsible for shell prompt interactions.
class Prompt
# A class responsible for gathering numeric input from range
#
# @api public
class Slider
HELP = "(Use %s arrow keys, press Enter to select)"
FORMAT = ":slider %s"
# 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
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
# Change symbols used by this prompt
#
# @param [Hash] new_symbols
# the new symbols to use
#
# @api public
def symbols(new_symbols = (not_set = true))
return @symbols if not_set
@symbols.merge!(new_symbols)
end
# Setup initial active position
#
# @return [Integer]
#
# @api private
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
# Default help text
#
# @api public
def default_help
arrows = @symbols[:arrow_left] + "/" + @symbols[:arrow_right]
sprintf(HELP, arrows)
end
# Set help text
#
# @param [String] text
#
# @api private
def help(text = (not_set = true))
return @help if !@help.nil? && not_set
@help = (@help.nil? && not_set) ? default_help : text
end
# Change when help is displayed
#
# @api public
def show_help(value = (not_set = true))
return @show_ehlp if not_set
@show_help = value
end
# @api public
def default(value)
@default = value
end
# @api public
def min(value)
@min = value
end
# @api public
def max(value)
@max = value
end
# @api public
def step(value)
@step = value
end
# Add a single choice
#
# @api public
def choice(*value, &block)
if block
@choices << (value << block)
else
@choices << value
end
end
# Add multiple choices
#
# @param [Array[Object]] values
# the values to add as choices
#
# @api public
def choices(values = (not_set = true))
if not_set
@choices
else
values.each { |val| @choices << val }
end
end
# @api public
def format(value)
@format = value
end
# Set quiet mode.
#
# @api public
def quiet(value)
@quiet = value
end
# Call the slider by passing question
#
# @param [String] question
# the question to ask
#
# @apu public
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
def keyleft(*)
@active -= 1 if @active > 0
end
alias keydown keyleft
def keyright(*)
@active += 1 if (@active + 1) < choices.size
end
alias keyup keyright
def keyreturn(*)
@done = true
end
alias keyspace keyreturn
alias keyenter keyreturn
private
# Check if help is shown only on start
#
# @api private
def help_start?
@show_help =~ /start/i
end
# Check if help is always displayed
#
# @api private
def help_always?
@show_help =~ /always/i
end
# Render an interactive range slider.
#
# @api private
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
# Clear screen
#
# @param [Integer] lines
# the lines to clear
#
# @api private
def refresh(lines)
@prompt.print(@prompt.clear_lines(lines))
end
# @return [Integer, String]
#
# @api private
def answer
choices[@active].value
end
# Render question with the slider
#
# @return [String]
#
# @api private
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 representation
#
# @return [String]
#
# @api private
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
end # Slider
end # Prompt
end # TTY
|