Sha256: 1cb61f3dfc16309c42c3b94b53cc676bb2ac65ccdab2a28851f56cbd7dd9db1b

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

module Ruco
  class CommandBar
    include Focusable

    attr_accessor :cursor_line

    SHORTCUTS = [
      '^W Exit',
      '^S Save',
      '^F Find',
      '^D Delete line'
    ]

    SEARCH_PREFIX = "Find: "

    def initialize(options)
      @options = options
      reset
    end

    def view
      if @find_mode
        SEARCH_PREFIX + @find_term
      else
        available_shortcuts
      end
    end

    def find
      @find_mode = true
    end

    def insert(text)
      @find_term += text
      if @find_term.include?("\n")
        @find_term.gsub!("\n",'')
        Command.new(:find, @find_term)
      end
    end

    def reset
      @find_mode = false
      @find_term = ''
    end

    def cursor_column
      view.size
    end

    private

    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

1 entries across 1 versions & 1 rubygems

Version Path
ruco-0.0.4 lib/ruco/command_bar.rb