Class: Foruiman::TUI::Renderer

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

Constant Summary collapse

HELP =
[
  ["↔ NAVIGATE", "Tab / Shift-Tab", "Change tab", "← →  or  h l"],
  [nil, "1–9 / 0", "Select a process / all logs", nil],
  ["≡ READ LOGS", "↑ ↓  or  k j", "Scroll a line", nil],
  [nil, "PgUp / PgDn", "Scroll a page", "Ctrl-U / Ctrl-D"],
  [nil, "g / Home", "Oldest retained line", nil],
  [nil, "f / G / End", "Follow newest output", nil],
  [nil, "Space", "Pause / resume following", nil],
  ["⚙ PROCESSES", "r / R", "Restart selected / all", "r on all restarts all"],
  [nil, "s / S", "Start/stop selected / stop all", "s on all stops all"],
  ["⌨ INPUT", "i", "Edit a command; Enter sends", "Ctrl-X returns to Foruiman"],
  ["◇ SESSION", "? / Escape", "Close help", nil],
  [nil, "q / Ctrl-C", "Stop processes and quit", nil]
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(theme: Theme.new) ⇒ Renderer

Returns a new instance of Renderer.



25
26
27
28
# File 'lib/foruiman/tui/renderer.rb', line 25

def initialize(theme: Theme.new)
  @theme = theme
  @first_tab = 0
end

Class Method Details

.expanded?(rows:, columns:) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/foruiman/tui/renderer.rb', line 30

def self.expanded?(rows:, columns:)
  rows >= 14 && columns >= 60
end

.log_height(rows:, columns:, input: false) ⇒ Object



34
35
36
37
38
# File 'lib/foruiman/tui/renderer.rb', line 34

def self.log_height(rows:, columns:, input: false)
  return 0 if rows < 6 || columns < 24

  rows - (expanded?(rows: rows, columns: columns) ? 7 : 5) - (input && rows >= 8 ? 2 : 0)
end

.truncate(text, columns) ⇒ Object



67
68
69
# File 'lib/foruiman/tui/renderer.rb', line 67

def self.truncate(text, columns)
  Text.clip(text, columns) + Foruiman::ANSI::RESET
end

.width(text) ⇒ Object



63
64
65
# File 'lib/foruiman/tui/renderer.rb', line 63

def self.width(text)
  Text.width(text)
end

Instance Method Details

#render(state, engine, rows:, columns:) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/foruiman/tui/renderer.rb', line 40

def render(state, engine, rows:, columns:)
  @width = [columns - 1, 1].max
  @inside = [@width - 4, 0].max
  @height = self.class.log_height(rows: rows, columns: columns, input: state.input?)
  return tiny_frame(state, engine, rows) if @height.zero?

  lines = [header(engine)]
  if self.class.expanded?(rows: rows, columns: columns)
    hint = @inside >= 70 ? " Tab switch · 1–9 / 0 select " : ""
    lines << border(@theme.paint(" processes ", :muted, bold: true), @theme.paint(hint, :faint))
    lines << panel(tab_row(state, engine, @inside - 2))
    lines << border("", "", bottom: true)
  else
    lines << " #{@theme.paint('│', :border)} #{tab_row(state, engine, @inside - 2)}"
  end
  lines << log_header(state, engine)
  lines.concat(state.help ? help_rows : log_rows(state, engine))
  lines << log_footer(state, engine)
  lines.concat(input_rows(state)) if state.input? && rows >= 8
  lines << controls(state, engine)
  frame(lines, rows)
end