class Omnibar::View

Constants

NON_ASCII_REGEX

Public Class Methods

new(state) click to toggle source
# File lib/omnibar/view.rb, line 5
def initialize(state)
  @state = state
end

Public Instance Methods

[](line) click to toggle source
# File lib/omnibar/view.rb, line 9
def [](line)
  render[line]
end
cursor_position() click to toggle source
# File lib/omnibar/view.rb, line 30
def cursor_position
  "\e[0;#{max_label_length + @state.input.length + 3 - @state.cursor_offset}H"
end
highlight(text) click to toggle source
# File lib/omnibar/view.rb, line 49
def highlight(text)
  ANSI.color(text,
             fg: Omnibar.config.render.highlight.fg,
             bg: Omnibar.config.render.highlight.bg)
end
input_line() click to toggle source
# File lib/omnibar/view.rb, line 24
def input_line
  prompt = Omnibar.config.render.prompt
  prompt = prompt.call(max_label_length) if prompt.respond_to?(:call)
  "#{prompt} #{@state.input}"
end
length() click to toggle source
# File lib/omnibar/view.rb, line 13
def length
  render.length
end
lpad(text, length = ANSI.size[:width]) click to toggle source
# File lib/omnibar/view.rb, line 60
def lpad(text, length = ANSI.size[:width])
  textlength = ANSI.strip(text).length + non_ascii_chars(text).length
  (' ' * [0, (length - textlength)].max) + text
end
max_label_length() click to toggle source
# File lib/omnibar/view.rb, line 65
def max_label_length
  @mll ||= (@state.results.map(&:first).map { |s| ANSI.strip(s) }.map(&:length).max || 0) + 1
end
non_ascii_chars(string) click to toggle source
# File lib/omnibar/view.rb, line 69
def non_ascii_chars(string)
  string.chars.select { |c| c.match?(NON_ASCII_REGEX) }
end
render() click to toggle source
# File lib/omnibar/view.rb, line 17
def render
  @render ||= [
    input_line,
    *rendered_queries
  ]
end
rendered_queries() click to toggle source
# File lib/omnibar/view.rb, line 34
def rendered_queries
  @state.results.map.with_index do |result, i|
    text = [
      lpad(result.first, max_label_length),
      rpad(result.last, ANSI.size[:width] - max_label_length - 2)
    ]

    if i == @state.selection
      text.map { |t| highlight(t) }.join(highlight(': '))
    else
      text.join(': ')
    end
  end
end
rpad(text, length = ANSI.size[:width]) click to toggle source
# File lib/omnibar/view.rb, line 55
def rpad(text, length = ANSI.size[:width])
  textlength = ANSI.strip(text).length + non_ascii_chars(text).length
  text + (' ' * [0, (length - textlength)].max)
end