Class: Yatte::Screen

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

Constant Summary collapse

THROTTLE_MS =

~60fps

16

Instance Method Summary collapse

Constructor Details

#initialize(terminal) ⇒ Screen

Returns a new instance of Screen.



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

def initialize(terminal)
  @terminal = terminal
  @last_render_time = 0.0
  @previous_lines = []
  @previous_size = nil
  @force_redraw = true
end

Instance Method Details

#force_redraw!Object



76
77
78
# File 'lib/yatte/screen.rb', line 76

def force_redraw!
  @force_redraw = true
end

#refresh(buffer:, cursor:, filename:, dirty:, message: "", message_time: nil, selection: nil, bracket_match: nil, tabs: nil, active_tab_index: 0, language: :plain, git_status: nil, force: false) ⇒ Object



15
16
17
18
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
# File 'lib/yatte/screen.rb', line 15

def refresh(buffer:, cursor:, filename:, dirty:,
  message: "", message_time: nil, selection: nil, bracket_match: nil,
  tabs: nil, active_tab_index: 0, language: :plain, git_status: nil,
  force: false)
  now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  elapsed_ms = (now - @last_render_time) * 1000
  unless force || @force_redraw || elapsed_ms >= THROTTLE_MS
    return
  end

  rows, cols = @terminal.size
  current_size = [rows, cols]
  if @previous_size != current_size
    @force_redraw = true
    @previous_size = current_size
  end

  tab_bar_height = (tabs && tabs.length > 1) ? 1 : 0
  text_rows = rows - 2 - tab_bar_height
  git_gutter = git_status ? 1 : 0
  gutter_width = gutter_width_for(buffer.line_count) + git_gutter
  text_cols = cols - gutter_width

  cursor.scroll(text_rows, text_cols)

  # Build all lines
  new_lines = []
  if tab_bar_height > 0
    new_lines << build_tab_bar(tabs, active_tab_index, cols)
  end
  text_rows.times do |i|
    file_row = i + cursor.row_offset
    new_lines << if file_row >= buffer.line_count
      build_empty_row(buffer, cursor, i, text_rows, cols)
    else
      build_text_row(buffer, file_row, cursor, gutter_width,
        text_cols, cols, selection, bracket_match, language, git_status)
    end
  end
  new_lines << build_status_bar(filename, dirty, buffer, cursor, cols)
  new_lines << build_message_bar(message, message_time, cols)

  # Diff and write only changed lines
  buf = +""
  buf << Yatte::HIDE_CURSOR

  new_lines.each_with_index do |line, i|
    if @force_redraw || i >= @previous_lines.length || @previous_lines[i] != line
      buf << "\x1b[#{i + 1};1H" << line << Yatte::CLEAR
    end
  end

  position_cursor(buf, cursor, gutter_width, tab_bar_height)
  buf << Yatte::SHOW_CURSOR

  @terminal.write(buf)
  @previous_lines = new_lines
  @force_redraw = false
  @last_render_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

#text_area_size(tab_count: 1) ⇒ Object



80
81
82
83
84
85
# File 'lib/yatte/screen.rb', line 80

def text_area_size(tab_count: 1)
  rows, cols = @terminal.size
  gutter_width = 5
  tab_bar_height = (tab_count > 1) ? 1 : 0
  [rows - 2 - tab_bar_height, cols - gutter_width]
end