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, #theme_version

Instance Method Summary collapse

Methods included from Curses::WindowStyling

#attr_flag, #pair_attr

Methods inherited from Fatty::Renderer

#apply_theme!, #invalidate!

Constructor Details

#initializeTruecolor

Returns a new instance of Truecolor.



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

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

Instance Method Details

#begin_frameObject



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

def begin_frame
  @pending_ansi_draws = []
end

#clear_input_fieldObject



82
83
84
85
86
87
88
89
90
91
# File 'lib/fatty/renderer/truecolor.rb', line 82

def clear_input_field
  @last_input_state = nil
  queue_ansi_line(
    row: screen.input_rect.row,
    col: screen.input_rect.col,
    width: screen.input_rect.cols,
    text: "",
    role: :input,
  )
end

#clear_physical_screen!Object



219
220
221
222
223
224
# File 'lib/fatty/renderer/truecolor.rb', line 219

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

#finish_frameObject



215
216
217
# File 'lib/fatty/renderer/truecolor.rb', line 215

def finish_frame
  flush_ansi_draws unless @pending_ansi_draws.empty?
end

#hide_cursorObject



241
242
243
244
245
246
# File 'lib/fatty/renderer/truecolor.rb', line 241

def hide_cursor
  @cursor_visible = false
  ::Curses.curs_set(0)
  @ansi_renderer.write_ansi(@ansi_renderer.hide_cursor)
  self
end

#render_alert(alert_session) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fatty/renderer/truecolor.rb', line 24

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

  alert = alert_session.current
  @last_alert_state = state
  text = alert ? alert.format : ""
  role = alert ? alert_role(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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/fatty/renderer/truecolor.rb', line 63

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_session, viewport: output_session.viewport) ⇒ Object



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

def render_output(output_session, viewport: output_session.viewport)
  lines = viewport.slice(output_session.visible_lines)
  normalized = normalized_highlights(output_session.highlights)
  line_number_width =
    if output_session.line_numbers?
      output_session.output.lines.length.to_s.length
    end

  curr = output_state(output_session, viewport: viewport)
  return if curr == @last_output_state

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

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



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

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

Modal overlays, render_popup and render_prompt_popu are deliberately not state-cached.

They must be restaged on every frame because they are drawn on top of the focused session. Even when the overlay's own state has not changed, the underlying session may have redrawn the area beneath it. Skipping the overlay render would let the underlying frame erase or visually punch through it.



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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/fatty/renderer/truecolor.rb', line 126

def render_popup(session:)
  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)

  # Draw the displayed items inside the window with a gutter to have an
  # indicator of what is selected, if any.
  filter_present = session.filter_present?
  counts_present = session.counts_present?
  input_row = filter_present ? inner_h - 1 : nil
  counts_row =
    if counts_present
      filter_present ? input_row - 1 : inner_h - 1
    end

  list_end =
    if counts_present
      counts_row
    elsif filter_present
      input_row
    else
      inner_h
    end
  list_h = [list_end - row, 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 session.counts_present?

  if filter_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
end

#render_prompt_popup(session:) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/fatty/renderer/truecolor.rb', line 184

def render_prompt_popup(session:)
  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)
  show_cursor
  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(status_session) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/fatty/renderer/truecolor.rb', line 15

def render_status(status_session)
  state = status_state(status_session)
  return if state == @last_status_state

  @last_status_state = state

  draw_status_lines(status_session)
end

#restore_cursor(field) ⇒ Object



226
227
228
229
230
231
232
# File 'lib/fatty/renderer/truecolor.rb', line 226

def restore_cursor(field)
  return self unless @cursor_visible

  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

#restore_output_cursor(field, row:) ⇒ Object

Restore cursor into the output window at a specific output-win row. row: is 0..(screen.output_rect.rows-1), NOT an absolute screen row.



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/fatty/renderer/truecolor.rb', line 266

def restore_output_cursor(field, row:)
  return self unless @cursor_visible

  cols = @screen.cols
  x = field.cursor_x.to_i
  x = x.clamp(0, [cols - 1, 0].max)
  row0 = @screen.output_rect.row
  col0 = @screen.output_rect.col
  cols = @screen.output_rect.cols
  @pending_ansi_draws << {
    type: :cursor,
    row: row0 + row,
    col: col0 + x.clamp(0, [cols - 1, 0].max),
  }
end

#show_cursorObject



234
235
236
237
238
239
# File 'lib/fatty/renderer/truecolor.rb', line 234

def show_cursor
  @cursor_visible = true
  ::Curses.curs_set(1)
  @ansi_renderer.write_ansi(@ansi_renderer.hide_cursor)
  self
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.



251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/fatty/renderer/truecolor.rb', line 251

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