Class: Rvim::Screen

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

Constant Summary collapse

SMCUP =
"\e[?1049h"
RMCUP =
"\e[?1049l"
HIDE_CURSOR =
"\e[?25l"
SHOW_CURSOR =
"\e[?25h"
CLEAR =
"\e[2J"
HOME =
"\e[H"
REVERSE_ON =
"\e[7m"
REVERSE_OFF =
"\e[27m"
DIM_ON =
"\e[2m"
DIM_OFF =
"\e[22m"
DIFF_BG_ON =
"\e[48;5;52m"
DIFF_BG_OFF =
"\e[49m"
UNDERLINE_ON =
"\e[4m"
UNDERLINE_OFF =
"\e[24m"
ERASE_LINE =
"\e[2K"
CURSOR_BLOCK =

DECSCUSR cursor shapes — match NeoVim’s default ‘guicursor’ for n: block, i/c: vertical bar, r: underline.

"\e[2 q"
CURSOR_BAR =
"\e[6 q"
CURSOR_UNDERLINE =
"\e[4 q"
MOUSE_ENABLE =
"\e[?1000h\e[?1006h"
MOUSE_DISABLE =
"\e[?1006l\e[?1000l"

Instance Method Summary collapse

Constructor Details

#initialize(editor) ⇒ Screen

Returns a new instance of Screen.



27
28
29
30
31
# File 'lib/rvim/screen.rb', line 27

def initialize(editor)
  @editor = editor
  @rows = 24
  @cols = 80
end

Instance Method Details

#content_width_for(win) ⇒ Object



146
147
148
149
150
151
# File 'lib/rvim/screen.rb', line 146

def content_width_for(win)
  return 0 unless win

  gw = gutter_width(win.buffer)
  [win.width - gw, 0].max
end

#cursor_shape_for_modeObject



136
137
138
139
140
141
142
143
144
# File 'lib/rvim/screen.rb', line 136

def cursor_shape_for_mode
  if @editor.respond_to?(:replace_mode) && @editor.replace_mode
    CURSOR_UNDERLINE
  elsif @editor.prompt_mode || @editor.editing_mode_label == :vi_insert
    CURSOR_BAR
  else
    CURSOR_BLOCK
  end
end

#disable_mouseObject



68
69
70
# File 'lib/rvim/screen.rb', line 68

def disable_mouse
  $stdout.write(MOUSE_DISABLE)
end

#enable_mouseObject



64
65
66
# File 'lib/rvim/screen.rb', line 64

def enable_mouse
  $stdout.write(MOUSE_ENABLE)
end

#gutter_width_for(buffer) ⇒ Object



72
73
74
# File 'lib/rvim/screen.rb', line 72

def gutter_width_for(buffer)
  gutter_width(buffer)
end

#mouse_enabled?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/rvim/screen.rb', line 60

def mouse_enabled?
  !@editor.settings.get(:mouse).to_s.empty?
end

#renderObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rvim/screen.rb', line 95

def render
  @rows, @cols = Reline::IOGate.get_screen_size
  reserved = @editor.prompt_mode == :listing ? list_overlay_rows + 1 : 1
  reserved_top = tabline_height
  layout_windows(@rows - reserved - reserved_top, @cols)
  if reserved_top.positive?
    @editor.windows.each { |w| w.row += reserved_top }
  end

  out = +HIDE_CURSOR
  out << render_title if @editor.settings.get(:title)
  out << render_tabline if reserved_top.positive?
  @editor.windows.each { |win| out << render_window(win) }
  out << render_vertical_dividers
  if @editor.prompt_mode == :listing
    out << render_listing_overlay
  end
  if @editor.cmdline_popup && !@editor.cmdline_popup.empty? && @editor.settings.get(:wildmenu)
    out << render_cmdline_popup
  end
  out << move_to(@rows, 1) << ERASE_LINE << bottom_line

  cw = @editor.current_window
  if @editor.prompt_mode
    out << move_to(@rows, @editor.prompt_buffer.length + 2)
  elsif cw
    gw = gutter_width(cw.buffer)
    content_width = cw.width - gw
    wrap_on = @editor.settings.get(:wrap)
    display_row_offset, display_col = cursor_display_position(cw, content_width, wrap_on)
    cursor_row = cw.row + display_row_offset + 1
    cursor_col = cw.col + gw + display_col + 1
    out << move_to(cursor_row, cursor_col)
  end
  out << cursor_shape_for_mode
  out << SHOW_CURSOR

  $stdout.write(out)
  $stdout.flush
end

#render_titleObject



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

def render_title
  str = @editor.settings.get(:titlestring).to_s
  title = if str.empty?
            name = @editor.filepath ? File.basename(@editor.filepath) : '[No Name]'
            "#{name} - rvim"
          else
            str
          end
  "\e]0;#{title}\a"
end

#scroll_topObject



33
34
35
# File 'lib/rvim/screen.rb', line 33

def scroll_top
  @editor.current_window&.scroll_top || 0
end

#scroll_top=(value) ⇒ Object



37
38
39
# File 'lib/rvim/screen.rb', line 37

def scroll_top=(value)
  @editor.current_window.scroll_top = value if @editor.current_window
end

#setupObject



44
45
46
47
48
49
50
# File 'lib/rvim/screen.rb', line 44

def setup
  $stdout.write(SMCUP)
  $stdout.write(CLEAR)
  $stdout.write(HOME)
  enable_mouse if mouse_enabled?
  $stdout.flush
end

#split_segments_public(line, max_width) ⇒ Object



153
154
155
# File 'lib/rvim/screen.rb', line 153

def split_segments_public(line, max_width)
  split_line_segments(line, max_width)
end

#tabline_heightObject



76
77
78
79
80
81
82
# File 'lib/rvim/screen.rb', line 76

def tabline_height
  stal = @editor.settings.get(:showtabline).to_i
  return 0 if stal <= 0
  return 1 if stal >= 2
  # stal == 1: only when more than one tab
  @editor.tabs.size > 1 ? 1 : 0
end

#teardownObject



52
53
54
55
56
57
58
# File 'lib/rvim/screen.rb', line 52

def teardown
  disable_mouse
  $stdout.write(CURSOR_BLOCK)
  $stdout.write(SHOW_CURSOR)
  $stdout.write(RMCUP)
  $stdout.flush
end