class Doublespeak::Core

Attributes

data_source[R]
display[R]
format_data[R]
query[R]
saved_candidates[R]
selected_index[R]

Public Class Methods

new(data_source, options = {}) click to toggle source
# File lib/doublespeak/core.rb, line 6
def initialize(data_source, options = {})
  @data_source = data_source
  @format_data = options[:format_data] || ->(x) { "#{x}" }

  @display = options[:display] || Display.new(options)

  @query = options[:query] || ""
  @selected_index = options[:selected_index] || 0
  @saved_candidates = options[:saved_candidates] || []
end

Public Instance Methods

back_up() click to toggle source
# File lib/doublespeak/core.rb, line 46
def back_up
  query.chop!
end
entry(c) click to toggle source
# File lib/doublespeak/core.rb, line 55
def entry(c)
  if query.length < display.width
    query.concat(c)
  end
end
find_candidates() click to toggle source
# File lib/doublespeak/core.rb, line 50
def find_candidates
  @selected_index = 0
  @saved_candidates = query.strip.to_s.empty? ? [] : data_source.call(query)
end
finish_up() click to toggle source
# File lib/doublespeak/core.rb, line 26
def finish_up
  display.move_to_origin
  display.clear_to_end_of_line
  display.set_cursor_visible

  display.write display.format_selected_result.call(format_data.call(candidate)) unless candidate.nil?
  display.write "\n"
end
increment_selection(i) click to toggle source
# File lib/doublespeak/core.rb, line 35
def increment_selection(i)
  @selected_index = selected_index + i
  if saved_candidates.size == 0
    @selected_index = 0
  elsif selected_index < 0
    @selected_index = saved_candidates.size - 1
  elsif selected_index >= saved_candidates.size
    @selected_index = 0
  end
end
render() click to toggle source
# File lib/doublespeak/core.rb, line 17
def render
  display.move_to_origin
  display.clear_to_end_of_line

  display.write (query + status_line)

  display.move_to_origin
end

Private Instance Methods

candidate() click to toggle source
# File lib/doublespeak/core.rb, line 86
def candidate
  saved_candidates[selected_index]
end
candidate_text() click to toggle source
# File lib/doublespeak/core.rb, line 72
def candidate_text
  max_width = saved_candidates.map { |c| format_data.call(c).length }.max

  format_data.call(candidate)
    .ljust_noescape(max_width)
    .format_substring(query, display.format_result_textmatch, downcase: true)
end
selected_index_text() click to toggle source
# File lib/doublespeak/core.rb, line 80
def selected_index_text
  return "" if saved_candidates.length <= 1

  "(#{selected_index+1}/#{saved_candidates.length})".rjust(8, " ")
end
status_line() click to toggle source
# File lib/doublespeak/core.rb, line 63
def status_line
  if saved_candidates.empty?
    ""
  else
    width = display.width - query.length + 1
    display.format_result.call(candidate_text + selected_index_text).rjust_noescape(width)
  end
end