lib/tty/prompt/slider.rb in tty-prompt-0.13.2 vs lib/tty/prompt/slider.rb in tty-prompt-0.14.0

- old
+ new

@@ -11,22 +11,34 @@ class Slider include Symbols HELP = '(Use arrow keys, press Enter to select)'.freeze + FORMAT = ':slider %d'.freeze + # 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 } @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 } @first_render = true @done = false @prompt.subscribe(self) end @@ -71,10 +83,14 @@ # @api public def step(value) @step = value end + def format(value) + @format = value + end + # Call the slider by passing question # # @param [String] question # the question to ask # @@ -87,21 +103,22 @@ end def keyleft(*) @active -= 1 if @active > 0 end - alias_method :keydown, :keyleft + alias keydown keyleft def keyright(*) - @active += 1 if (@active + @step) < range.size + @active += 1 if (@active + 1) < range.size end - alias_method :keyup, :keyright + alias keyup keyright def keyreturn(*) @done = true end - alias_method :keyspace, :keyreturn + alias keyspace keyreturn + alias keyenter keyreturn private # Render an interactive range slider. # @@ -141,42 +158,34 @@ # # @return [String] # # @api private def render_question - header = "#{@prefix}#{@question} #{render_header}\n" - @first_render = false - header << render_slider unless @done - header - end - - # Render actual answer or help - # - # @return [String] - # - # @api private - def render_header + header = "#{@prefix}#{@question} " if @done - @prompt.decorate(answer.to_s, @active_color) - elsif @first_render - @prompt.decorate(HELP, @help_color) + header << @prompt.decorate(answer.to_s, @active_color) + header << "\n" + else + header << render_slider end + if @first_render + header << "\n" + @prompt.decorate(HELP, @help_color) + @first_render = false + end + header end # Render slider representation # # @return [String] # # @api private 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 + slider = (symbols[:line] * @active) + + @prompt.decorate(symbols[:handle], @active_color) + + (symbols[:line] * (range.size - @active - 1)) + value = " #{range[@active]}" + @format.gsub(':slider', slider) % [value] end end # Slider end # Prompt end # TTY