Class: Fir::Renderer

Inherits:
Object
  • Object
show all
Includes:
ScreenHelper
Defined in:
lib/fir/renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ScreenHelper

#clear, #cursor_back, #horizontal_absolute, #line_prompt, #next_line, #previous_line

Constructor Details

#initialize(output) ⇒ Renderer

Returns a new instance of Renderer.



11
12
13
14
# File 'lib/fir/renderer.rb', line 11

def initialize(output)
  @output = output
  output.syswrite(line_prompt)
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



9
10
11
# File 'lib/fir/renderer.rb', line 9

def output
  @output
end

Instance Method Details

#lines_to_render(state) ⇒ Object



49
50
51
52
53
54
55
# 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) ⇒ Object



42
43
44
45
46
47
# 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) ⇒ Object



16
17
18
19
20
# 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) ⇒ Object



27
28
29
30
31
32
33
# 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) ⇒ Object



35
36
37
38
39
40
# 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) ⇒ Object



22
23
24
25
# 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) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/fir/renderer.rb', line 57

def result_prompt(state)
  if state.executable?
    "\n=> "
  else
    ''
  end
end