Class: Fatty::Renderer::Truecolor

Inherits:
Fatty::Renderer show all
Includes:
Curses::WindowStyling
Defined in:
lib/fatty/renderer/truecolor.rb

Constant Summary

Constants included from Curses::WindowStyling

Curses::WindowStyling::ATTR_FLAGS

Constants inherited from Fatty::Renderer

CORNER_STYLES, FRAME_STYLES, POPUP_SELECTED_GUTTER, POPUP_UNSELECTED_GUTTER

Instance Attribute Summary

Attributes inherited from Fatty::Renderer

#context, #palette, #screen

Instance Method Summary collapse

Methods included from Curses::WindowStyling

#attr_flag, #pair_attr

Methods inherited from Fatty::Renderer

#apply_theme!, #flush_ansi_draws, #invalidate!, #queue_ansi_line, #queue_ansi_popup_line, #queue_ansi_rect, #queue_ansi_segments_line, #restore_output_cursor

Constructor Details

#initializeTruecolor

Returns a new instance of Truecolor.



8
9
10
11
12
# File 'lib/fatty/renderer/truecolor.rb', line 8

def initialize(...)
  super
  @ansi_renderer = Fatty::Ansi::Renderer.new
  @pending_ansi_draws = []
end

Instance Method Details

#begin_frameObject



195
196
197
# File 'lib/fatty/renderer/truecolor.rb', line 195

def begin_frame
  @pending_ansi_draws = []
end

#clear_physical_screen!Object



203
204
205
206
207
208
# File 'lib/fatty/renderer/truecolor.rb', line 203

def clear_physical_screen!
  $stdout.write("\e[2J\e[H")
  $stdout.flush
  invalidate!
  self
end

#finish_frameObject



199
200
201
# File 'lib/fatty/renderer/truecolor.rb', line 199

def finish_frame
  flush_ansi_draws unless @pending_ansi_draws.empty?
end

#render_alert(alert) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fatty/renderer/truecolor.rb', line 37

def render_alert(alert)
  state = alert_state(alert)
  return if state == @last_alert_state

  @last_alert_state = state
  text = alert ? alert.format : ""
  role = alert ? alert.role : :alert_info

  queue_ansi_line(
    row: screen.alert_rect.row,
    col: screen.alert_rect.col,
    width: screen.alert_rect.cols,
    text: text,
    role: role,
  )
end

#render_input_field(field) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fatty/renderer/truecolor.rb', line 69

def render_input_field(field)
  state = input_field_state(field)
  return if state == @last_input_state

  @last_input_state = state
  queue_ansi_segments_line(
    row: screen.input_rect.row,
    col: screen.input_rect.col,
    width: screen.input_rect.cols,
    segments: field_segments(
      field,
      base_role: :input,
      suggestion_role: :input_suggestion,
      region_role: :region,
    ),
    fill_role: :input,
  )
end

#render_output(output, viewport:, highlights: nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fatty/renderer/truecolor.rb', line 54

def render_output(output, viewport:, highlights: nil)
  lines = viewport.slice(output.lines)
  normalized = normalized_highlights(highlights)

  curr = output_state(
    viewport: viewport,
    lines: lines,
    highlights: normalized,
  )
  return if curr == @last_output_state

  draw_output_lines(lines, viewport: viewport, highlights: normalized)
  @last_output_state = curr
end

#render_pager_field(field, row:, role: :pager_status) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/fatty/renderer/truecolor.rb', line 88

def render_pager_field(field, row:, role: :pager_status)
  state = pager_field_state(field, row: row, role: role)
  return if state == @last_pager_field_state

  @last_pager_field_state = state

  win = context.output_win
  return unless win

  row0, col0 = win.origin
  return unless row0 && col0

  cols = win.respond_to?(:maxx) ? win.maxx : @screen.cols

  queue_ansi_segments_line(
    row: row0 + row,
    col: col0,
    width: cols,
    segments: field_segments(
      field,
      base_role: role,
      suggestion_role: :input_suggestion,
      region_role: :region,
    ),
    fill_role: role,
  )
end

#render_popup(session:) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/fatty/renderer/truecolor.rb', line 116

def render_popup(session:)
  state = popup_state(session)
  return if state == @last_popup_state

  @last_popup_state = state

  win = session.win
  return unless win

  row0, col0 = win.origin
  return unless row0 && col0

  width = win.maxx
  height = win.maxy
  inner_w = [width - 2, 0].max
  inner_h = [height - 2, 0].max

  # Draw the window with the border
  render_popup_frame(session: session)

  # Add any title
  render_popup_title(session: session) if session.title

  # Draw the message if any inside the window
  layout = PopupLayout.new(row: 0, width: inner_w)
  row = render_popup_message(session: session, layout: layout)

  counts_present = !!session.counts
  input_row = inner_h - 1
  counts_row = counts_present ? input_row - 1 : nil

  # Draw the displayed items inside the window with a gutter to have an
  # indicator of what is selected, if any.
  list_h = [inner_h - row - 1 - (counts_present ? 1 : 0), 0].max
  layout = PopupLayout.new(row: row, width: inner_w, height: list_h)
  render_popup_items(session: session, layout: layout)

  # Draw the "counts" line to indicate the total number of items, items
  # selected, and items displayed.
  layout = PopupLayout.new(row: counts_row, width: inner_w)
  render_popup_counts(session: session, layout: layout) if counts_present

  # Draw the input field for the user to type narrowing selection
  # queries.
  layout = PopupLayout.new(row: input_row, width: inner_w)
  render_popup_input_field(session: session, layout: layout)
end

#render_prompt_popup(session:) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/fatty/renderer/truecolor.rb', line 164

def render_prompt_popup(session:)
  state = prompt_popup_state(session)
  return if state == @last_prompt_popup_state

  @last_prompt_popup_state = state

  win = session.win
  return unless win

  row0, col0 = win.origin
  return unless row0 && col0

  inner_w = [win.maxx - 2, 0].max
  inner_h = [win.maxy - 2, 0].max
  return if inner_w <= 0 || inner_h <= 0

  render_popup_frame(session: session)
  render_popup_title(session: session) if session.title

  layout = PopupLayout.new(row: 0, width: inner_w)
  row = render_popup_message(session: session, layout: layout)

  layout = PopupLayout.new(row: row, width: inner_w)
  render_popup_input_field(session: session, layout: layout)
rescue RuntimeError => e
  raise unless e.message.include?("closed window") ||
               e.message.include?("already closed window")

  nil
end

#render_status(text, role: :status_info) ⇒ Object



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

def render_status(text, role: :status_info)
  state = status_state(text, role)
  return if state == @last_status_state

  @last_status_state = state

  lines = status_render_lines(
    text,
    width: screen.status_rect.cols,
    max_rows: screen.status_rect.rows,
  )

  screen.status_rect.rows.times do |idx|
    queue_ansi_line(
      row: screen.status_rect.row + idx,
      col: screen.status_rect.col,
      width: screen.status_rect.cols,
      text: lines[idx].to_s,
      role: role,
    )
  end
end

#restore_cursor(field) ⇒ Object



210
211
212
213
214
# File 'lib/fatty/renderer/truecolor.rb', line 210

def restore_cursor(field)
  rect = screen.input_rect
  x = field.cursor_x.to_i.clamp(0, [rect.cols - 1, 0].max)
  queue_ansi_cursor(row: rect.row, col: rect.col + x)
end

#sync_backgrounds!Object

In truecolor mode, curses windows must still have themed backgrounds. ncurses may repaint or expose its backing store during getch/doupdate, so we must keep the backing store visually consistent with ANSI output.



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/fatty/renderer/truecolor.rb', line 219

def sync_backgrounds!
  return self unless context.truecolor

  sync_window_background(context.output_win, :output)
  sync_window_background(context.input_win, :input)
  sync_window_background(context.alert_win, :info)

  if @screen.status_rect.rows.positive?
    sync_window_background(context.status_win, :status)
  end
  self
end