Class: Keisanjaku::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/keisanjaku/renderer.rb

Constant Summary collapse

MIN_WIDTH =
60
DEFAULT_WIDTH =
100
LABEL_WIDTH =
6

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(color: true) ⇒ Renderer

Returns a new instance of Renderer.



10
11
12
# File 'lib/keisanjaku/renderer.rb', line 10

def initialize(color: true)
  @color = color
end

Class Method Details

.render(state, width: DEFAULT_WIDTH, color: true, message: nil, highlight_scale: nil) ⇒ Object



35
36
37
# File 'lib/keisanjaku/renderer.rb', line 35

def self.render(state, width: DEFAULT_WIDTH, color: true, message: nil, highlight_scale: nil)
  new(color: color).render(state, width: width, message: message, highlight_scale: highlight_scale)
end

Instance Method Details

#render(state, width: DEFAULT_WIDTH, message: nil, highlight_scale: nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/keisanjaku/renderer.rb', line 14

def render(state, width: DEFAULT_WIDTH, message: nil, highlight_scale: nil)
  width = [width.to_i, MIN_WIDTH].max
  rail_width = width - LABEL_WIDTH - 2
  cursor_col = column_for(state.cursor, rail_width)
  highlight_name = highlight_scale&.to_s&.upcase
  lines = []
  lines << border_line(rail_width, :top)
  lines.concat(render_scale("K", state, rail_width, cursor_col, highlight_name))
  lines.concat(render_scale("A", state, rail_width, cursor_col, highlight_name))
  lines << border_line(rail_width, :divider)
  slide_names = state.face == :front ? %w[B CI C] : %w[S T]
  slide_names.each { |name| lines.concat(render_scale(name, state, rail_width, cursor_col, highlight_name)) }
  lines << border_line(rail_width, :divider)
  lines.concat(render_scale("D", state, rail_width, cursor_col, highlight_name))
  lines.concat(render_scale("L", state, rail_width, cursor_col, highlight_name))
  lines << border_line(rail_width, :bottom)
  lines << status_line(state, width, highlight_name)
  lines << ANSI.fit(message.to_s, width) if message
  lines.map { |line| ANSI.fit(line, width) }
end