Class: RuVim::Screen

Inherits:
Object
  • Object
show all
Defined in:
lib/ruvim/screen.rb

Constant Summary collapse

DEFAULT_TABSTOP =
2
SYNTAX_CACHE_LIMIT =
2048
WRAP_SEGMENTS_CACHE_LIMIT =
1024

Instance Method Summary collapse

Constructor Details

#initialize(terminal:) ⇒ Screen

Returns a new instance of Screen.



8
9
10
11
12
13
# File 'lib/ruvim/screen.rb', line 8

def initialize(terminal:)
  @terminal = terminal
  @last_frame = nil
  @syntax_color_cache = {}
  @wrapped_segments_cache = {}
end

Instance Method Details

#current_window_view_height(editor) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ruvim/screen.rb', line 82

def current_window_view_height(editor)
  rows, cols = @terminal.winsize
  text_rows, text_cols = editor.text_viewport_size(rows:, cols:)
  text_rows = [text_rows, 1].max
  text_cols = [text_cols, 1].max
  rect = window_rects(editor, text_rows:, text_cols:)[editor.current_window_id]
  height = [rect ? rect[:height] : text_rows, 1].max
  editor.current_window_view_height_hint = height if editor.respond_to?(:current_window_view_height_hint=)
  height
rescue StandardError
  1
end

#invalidate_cache!Object



15
16
17
# File 'lib/ruvim/screen.rb', line 15

def invalidate_cache!
  @last_frame = nil
end

#render(editor) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ruvim/screen.rb', line 19

def render(editor)
  @rich_render_info = nil

  rows, cols = @terminal.winsize
  text_rows, text_cols = editor.text_viewport_size(rows:, cols:)
  text_rows = [text_rows, 1].max
  text_cols = [text_cols, 1].max

  rects = window_rects(editor, text_rows:, text_cols:)
  if (current_rect = rects[editor.current_window_id])
    editor.current_window_view_height_hint = [current_rect[:height], 1].max
  end
  editor.window_order.each do |win_id|
    win = editor.windows.fetch(win_id)
    buf = editor.buffers.fetch(win.buffer_id)
    rect = rects[win_id]
    next unless rect
    content_width = [rect[:width] - number_column_width(editor, win, buf), 1].max
    if RuVim::RichView.active?(editor)
      # Vertical scrolling only — keep raw col_offset untouched
      win.ensure_visible(
        buf,
        height: [rect[:height], 1].max,
        width: content_width,
        tabstop: tabstop_for(editor, win, buf),
        scrolloff: editor.effective_option("scrolloff", window: win, buffer: buf),
        sidescrolloff: editor.effective_option("sidescrolloff", window: win, buffer: buf)
      )
      ensure_visible_rich(editor, win, buf, rect, content_width)
    else
      win.col_offset = 0 if wrap_enabled?(editor, win, buf)
      win.ensure_visible(
        buf,
        height: [rect[:height], 1].max,
        width: content_width,
        tabstop: tabstop_for(editor, win, buf),
        scrolloff: editor.effective_option("scrolloff", window: win, buffer: buf),
        sidescrolloff: editor.effective_option("sidescrolloff", window: win, buffer: buf)
      )
      if wrap_enabled?(editor, win, buf)
        ensure_visible_under_wrap(editor, win, buf, height: [rect[:height], 1].max, content_w: content_width)
      end
    end
  end

  frame = build_frame(editor, rows:, cols:, text_rows:, text_cols:, rects:)
  out = if can_diff_render?(frame)
          render_diff(frame)
        else
          render_full(frame)
        end
  cursor_row, cursor_col = cursor_screen_position(editor, text_rows, rects)
  out << "\e[#{cursor_row};#{cursor_col}H"
  if cursor_use_terminal?(editor)
    out << "\e[6 q"
    out << "\e[?25h"
  else
    out << "\e[?25l"
  end
  @last_frame = frame.merge(cursor_row:, cursor_col:)
  @terminal.write(out)
end