class Fir::Renderer

Attributes

output[R]

Public Class Methods

new(output) click to toggle source
# File lib/fir/renderer.rb, line 11
def initialize(output)
  @output = output
  output.syswrite(line_prompt)
end

Public Instance Methods

lines_to_render(state) click to toggle source
# File lib/fir/renderer.rb, line 49
def lines_to_render(state)
  if state.executable?
    state.lines[0...-1]
  else
    state.lines
  end
end
lines_with_prompt(state) click to toggle source
# File lib/fir/renderer.rb, line 42
def lines_with_prompt(state)
  lines_to_render(state).map.with_index do |line, i|
    prompt = state.indents[i].zero? ? '>' : '*'
    [line_prompt(prompt), ('  ' * state.indents[i]), line.join]
  end
end
perform(state) click to toggle source
# File lib/fir/renderer.rb, line 16
def perform(state)
  output.syswrite(rendered_lines(state))
  output.syswrite(rendered_cursor(state))
  output.syswrite(rendered_suggestion(state)) if state.suggestion
end
rendered_cursor(state) click to toggle source
# File lib/fir/renderer.rb, line 27
def rendered_cursor(state)
  cursor = ''
  if state.cursor.x != state.current_line.length
    cursor = "#{cursor}#{cursor_back(state.current_line.length - state.cursor.x)}"
  end
  cursor
end
rendered_lines(state) click to toggle source
# File lib/fir/renderer.rb, line 35
def rendered_lines(state)
  lines_with_prompt(state)
    .map(&:join)
    .join("\n#{horizontal_absolute(1)}")
    .concat(result_prompt(state))
end
rendered_suggestion(state) click to toggle source
# File lib/fir/renderer.rb, line 22
def rendered_suggestion(state)
  return unless state.suggestion.length.positive?
  "#{state.suggestion}#{cursor_back(state.suggestion.length)}"
end
result_prompt(state) click to toggle source
# File lib/fir/renderer.rb, line 57
def result_prompt(state)
  if state.executable?
    "\n=> "
  else
    ''
  end
end