Class: Fatty::Renderer::Curses
- Inherits:
-
Fatty::Renderer
- Object
- Fatty::Renderer
- Fatty::Renderer::Curses
- Includes:
- Curses::WindowStyling
- Defined in:
- lib/fatty/renderer/curses.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
Instance Method Summary collapse
- #begin_frame ⇒ Object
- #finish_frame ⇒ Object
-
#initialize ⇒ Curses
constructor
A new instance of Curses.
- #render_alert(alert) ⇒ Object
- #render_input_field(field, role: :input) ⇒ Object
- #render_output(output, viewport:, highlights: nil) ⇒ Object
-
#render_pager_field(field, row:, role: :pager_status) ⇒ Object
Render an InputField-like status line.
- #render_popup(session:) ⇒ Object
- #render_prompt_popup(session:) ⇒ Object
- #render_status(text, role: :status_info) ⇒ Object
- #restore_cursor(field) ⇒ Object
-
#restore_output_cursor(field, row:) ⇒ Object
Restore cursor into the output window at a specific output-win row.
Methods included from Curses::WindowStyling
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
Constructor Details
#initialize ⇒ Curses
Returns a new instance of Curses.
10 11 12 |
# File 'lib/fatty/renderer/curses.rb', line 10 def initialize(...) super end |
Instance Method Details
#begin_frame ⇒ Object
352 353 |
# File 'lib/fatty/renderer/curses.rb', line 352 def begin_frame end |
#finish_frame ⇒ Object
355 356 357 |
# File 'lib/fatty/renderer/curses.rb', line 355 def finish_frame ::Curses.doupdate end |
#render_alert(alert) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/fatty/renderer/curses.rb', line 94 def render_alert(alert) state = alert_state(alert) return if state == @last_alert_state @last_alert_state = state win = context.alert_win return unless win text = alert ? alert.format : "" text = Fatty::Ansi.strip(text) role = alert ? alert.role : :alert attr = pair_attr(role, fallback: pair_attr(:alert, fallback: ::Curses::A_REVERSE)) cols = win.respond_to?(:maxx) ? win.maxx : screen.alert_rect.cols win.bkgdset(attr) if win.respond_to?(:bkgdset) win.erase win.attrset(attr) win.setpos(0, 0) win.addstr(text.ljust(cols)[0, cols]) stage_window(win) end |
#render_input_field(field, role: :input) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/fatty/renderer/curses.rb', line 61 def render_input_field(field, role: :input) state = input_field_state(field) return if state == @last_input_state @last_input_state = state win = context.input_win return unless win width = win.respond_to?(:maxx) ? win.maxx : screen.cols base_attr = pair_attr(role, fallback: ::Curses::A_NORMAL) region_attr = pair_attr(:region, fallback: ::Curses::A_REVERSE) suggestion_attr = pair_attr(:input_suggestion, fallback: base_attr) win.bkgdset(base_attr) if win.respond_to?(:bkgdset) win.erase win.attrset(base_attr) render_field_into( win: win, field: field, row: 0, width: width, base_role: role, base_attr: base_attr, region_attr: region_attr, suggestion_attr: suggestion_attr, ) cursor_x = field.cursor_x.to_i.clamp(0, [width - 1, 0].max) win.setpos(0, cursor_x) stage_window(win) end |
#render_output(output, viewport:, highlights: nil) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/fatty/renderer/curses.rb', line 40 def render_output(output, viewport:, highlights: nil) lines = .slice(output.lines) normalized = normalized_highlights(highlights) curr = output_state( viewport: , lines: lines, highlights: normalized, ) prev = @last_output_state if prev && can_incrementally_scroll_output?(prev, curr) scroll_output_window_delta!(prev: prev, curr: curr) else draw_output_lines(lines, viewport: , highlights: normalized) end @last_output_state = curr end |
#render_pager_field(field, row:, role: :pager_status) ⇒ Object
Render an InputField-like status line.
Used for pager mode ("--More--" etc.). It intentionally does not move the cursor; ShellSession decides whether to show a cursor in paging vs input mode.
Curses uses a derived one-line window to isolate the pager/search field from output_win scrolling/background effects.
Truecolor renders the same field as an ANSI overlay at absolute coordinates, so no derived window is needed there.
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 |
# File 'lib/fatty/renderer/curses.rb', line 129 def render_pager_field(field, row:, role: :pager_status) win = context.output_win return unless win cols = win.respond_to?(:maxx) ? win.maxx : @screen.cols attr = pair_attr(role, fallback: ::Curses::A_REVERSE) field_win = win.derwin(1, cols, row, 0) field_win.bkgdset(attr) if field_win.respond_to?(:bkgdset) field_win.erase field_win.attrset(attr) render_field_into( win: field_win, field: field, row: 0, width: cols, base_attr: attr, region_attr: pair_attr(:region, fallback: ::Curses::A_REVERSE), suggestion_attr: pair_attr(:input_suggestion, fallback: attr), base_role: role, ) field_win.noutrefresh stage_window(win) ensure field_win&.close if field_win&.respond_to?(:close) end |
#render_popup(session:) ⇒ Object
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 183 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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/fatty/renderer/curses.rb', line 158 def render_popup(session:) state = popup_state(session) return if state == @last_popup_state @last_popup_state = state win = session.win return unless win width = win.maxx height = win.maxy return if width < 2 || height < 2 win.erase frame_attr = pair_attr(:popup_frame, fallback: ::Curses::A_NORMAL) popup_attr = pair_attr(:popup, fallback: ::Curses::A_NORMAL) input_attr = pair_attr(:popup_input, fallback: ::Curses::A_REVERSE) selected_attr = pair_attr(:popup_selection, fallback: ::Curses::A_REVERSE) counts_attr = pair_attr(:popup_counts, fallback: popup_attr) win.attrset(frame_attr) draw_popup_frame(win, width: width, height: height) if session.title && !session.title.empty? title = Fatty::Ansi.strip(session.title.to_s).tr("\r\n", " ") title = Fatty::Ansi.truncate_visible(" #{title} ", [width - 4, 0].max) unless title.empty? win.setpos(0, 2) win.addstr(title) end end inner_h = height - 2 inner_w = width - 2 return if inner_h <= 0 || inner_w <= 0 inner = win.derwin(inner_h, inner_w, 1, 1) inner.bkgdset(popup_attr) if inner.respond_to?(:bkgdset) inner.erase row = 0 if session. && !session..empty? = Fatty::Ansi.strip(session..to_s).tr("\r\n", " ") = Fatty::Ansi.truncate_visible(, inner_w) inner.attrset(popup_attr) inner.setpos(row, 0) inner.addstr(.ljust(inner_w)) row += 1 end counts_present = !!session.counts input_row = inner_h - 1 counts_row = counts_present ? input_row - 1 : nil list_row = row list_h = input_row - list_row list_h -= 1 if counts_present list_h = [list_h, 0].max items = session.displayed selected = session.selected start = session.scroll_start(list_h: list_h) (0...list_h).each do |offset| item_index = start + offset item_selected = item_index == selected attr = item_selected ? selected_attr : popup_attr line = "" if item_index < items.length item = items[item_index] gutter = session.gutter_for(item: item, selected: item_selected) text = Fatty::Ansi.strip(item.to_s).tr("\r\n", " ") available = [inner_w - Fatty::Ansi.visible_length(gutter), 0].max text = Fatty::Ansi.truncate_visible(text, available) line = Fatty::Ansi.truncate_visible(gutter + text, inner_w) end inner.attrset(attr) inner.setpos(list_row + offset, 0) inner.addstr(line.ljust(inner_w)) end if counts_present && counts_row && counts_row >= 0 counts_text = Fatty::Ansi.truncate_visible(popup_counts_text(session), inner_w) inner.attrset(counts_attr) inner.setpos(counts_row, 0) inner.addstr(counts_text.ljust(inner_w)) end render_field_into( win: inner, field: session.field, row: input_row, width: inner_w, base_role: :popup_input, base_attr: input_attr, region_attr: pair_attr(:region, fallback: ::Curses::A_REVERSE), suggestion_attr: pair_attr(:input_suggestion, fallback: input_attr), ) cursor_x = session.field.cursor_x.to_i.clamp(0, [inner_w - 1, 0].max) win.setpos(1 + input_row, 1 + cursor_x) stage_window(win) rescue RuntimeError => e raise unless e..include?("closed window") || e..include?("already closed window") nil ensure inner&.close if inner&.respond_to?(:close) end |
#render_prompt_popup(session:) ⇒ Object
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/fatty/renderer/curses.rb', line 277 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 width = win.maxx height = win.maxy win.erase frame_attr = pair_attr(:popup_frame, fallback: ::Curses::A_NORMAL) popup_attr = pair_attr(:popup, fallback: ::Curses::A_NORMAL) input_attr = pair_attr(:popup_input, fallback: ::Curses::A_REVERSE) win.attrset(frame_attr) draw_popup_frame(win, width: width, height: height) if session.title && !session.title.empty? win.setpos(0, 2) win.addstr(" #{Fatty::Ansi.strip(session.title)} ") end inner_h = height - 2 inner_w = width - 2 return if inner_h <= 0 || inner_w <= 0 inner = win.derwin(inner_h, inner_w, 1, 1) inner.bkgdset(popup_attr) if inner.respond_to?(:bkgdset) inner.erase inner.attrset(popup_attr) input_row = inner_h - 1 if session. && !session..empty? = Fatty::Ansi.strip(session..to_s).tr("\r\n", " ") = Fatty::Ansi.truncate_visible(, inner_w) inner.setpos(0, 0) inner.addstr(.ljust(inner_w)) end render_field_into( win: inner, field: session.field, row: input_row, width: inner_w, base_role: :popup_input, base_attr: input_attr, region_attr: pair_attr(:region, fallback: ::Curses::A_REVERSE), suggestion_attr: pair_attr(:input_suggestion, fallback: input_attr), ) cursor_x = session.field.cursor_x.to_i.clamp(0, [inner_w - 1, 0].max) win.setpos(1 + input_row, 1 + cursor_x) stage_window(inner) stage_window(win) ensure inner&.close if inner&.respond_to?(:close) 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 36 37 38 |
# File 'lib/fatty/renderer/curses.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 win = context.status_win return unless win rows = screen.status_rect.rows cols = screen.status_rect.cols base_attr = pair_attr(role, fallback: ::Curses::A_REVERSE) win.bkgdset(base_attr) if win.respond_to?(:bkgdset) win.erase win.attrset(base_attr) status_render_lines(text, width: cols, max_rows: rows).each_with_index do |line, row| win.setpos(row, 0) rendered = Fatty::Ansi.truncate_visible(line, cols) padding = [cols - Fatty::Ansi.visible_length(rendered), 0].max win.addstr(rendered + (" " * padding)) end stage_window(win) end |
#restore_cursor(field) ⇒ Object
342 343 344 345 346 347 348 349 350 |
# File 'lib/fatty/renderer/curses.rb', line 342 def restore_cursor(field) win = context.input_win return unless win cols = win.respond_to?(:maxx) ? win.maxx : @screen.input_rect.cols x = field.cursor_x.to_i.clamp(0, [cols - 1, 0].max) win.setpos(0, x) stage_window(win) 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.
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/fatty/renderer/curses.rb', line 361 def restore_output_cursor(field, row:) win = context.output_win cols = @screen.cols x = field.cursor_x.to_i x = x.clamp(0, [cols - 1, 0].max) if context.truecolor 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), } return end @frame_touched = true win.setpos(row, x) stage_window(win) end |