Sha256: 644e3f8beb4eae1ac5768c087600923d020d214bd54b40b16f602c255f4093af

Contents?: true

Size: 1.92 KB

Versions: 16

Compression:

Stored size: 1.92 KB

Contents

module Ruco
  class CommandBar
    attr_accessor :cursor_line, :form
    delegate :move, :delete, :insert, :selecting, :selection, :text_in_selection, :to => :form

    SHORTCUTS = [
      '^W Exit',
      '^S Save',
      '^F Find',
      '^R Replace',
      '^D Delete line',
      '^G Go to line',
      '^B Select mode'
    ]

    def initialize(options)
      @options = options
      @forms_cache = {}
      reset
    end

    def view
      if @form
        @form.view
      else
        available_shortcuts
      end
    end

    def style_map
      if @form
        map = @form.style_map
        map.invert!
        map.prepend(:reverse, 0, 0..@options[:columns])
        map
      else
        StyleMap.single_line_reversed(@options[:columns])
      end
    end

    def ask(question, options={}, &block)
      @form = cached_form_if(options[:cache], question) do
        Form.new(question, :columns => @options[:columns]) do |result|
          @form = nil
          block.call(result)
        end
      end
    end

    def reset
      @forms_cache[@forms_cache.key(@form)] = nil
      @form = nil
    end

    def cursor
      if @form
        Position.new cursor_line, @form.cursor.column
      else
        Position.new cursor_line, 0
      end
    end

    private

    def cached_form_if(cache, question)
      if cache
        new_form = yield
        if @forms_cache[question]
          new_form.insert(@forms_cache[question].value)
          new_form.move(:to, 0,0)
          new_form.selecting{ move(:to_eol) }
        end
        @forms_cache[question] = new_form
      else
        yield
      end
    end

    def available_shortcuts
      used_columns = 0
      spacer = '    '
      shortcuts_that_fit = SHORTCUTS.select do |shortcut|
        used_columns += shortcut.size
        it_fits = (used_columns <= @options[:columns])
        used_columns += spacer.size
        it_fits
      end
      shortcuts_that_fit * spacer
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
ruco-0.1.12 lib/ruco/command_bar.rb
ruco-0.1.11 lib/ruco/command_bar.rb
ruco-0.1.10 lib/ruco/command_bar.rb
ruco-0.1.9 lib/ruco/command_bar.rb
ruco-0.1.8 lib/ruco/command_bar.rb
ruco-0.1.7 lib/ruco/command_bar.rb
ruco-0.1.6 lib/ruco/command_bar.rb
ruco-0.1.5 lib/ruco/command_bar.rb
ruco-0.1.4 lib/ruco/command_bar.rb
ruco-0.1.3 lib/ruco/command_bar.rb
ruco-0.1.2 lib/ruco/command_bar.rb
ruco-0.1.1 lib/ruco/command_bar.rb
ruco-0.1.0 lib/ruco/command_bar.rb
ruco-0.0.56 lib/ruco/command_bar.rb
ruco-0.0.55 lib/ruco/command_bar.rb
ruco-0.0.54 lib/ruco/command_bar.rb