Class: RubyRich::CacheRender

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_rich/live.rb

Instance Method Summary collapse

Constructor Details

#initializeCacheRender

Returns a new instance of CacheRender.



8
9
10
# File 'lib/ruby_rich/live.rb', line 8

def initialize
  @cache = nil
end

Instance Method Details

#draw(buffer) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ruby_rich/live.rb', line 18

def draw(buffer)
  unless @cache
    system("clear")
    print_with_pos(0,0,buffer.map { |line| line.compact.join("") }.join("\n"))
    @cache = buffer
  else
    buffer.each_with_index do |arr, y|
      arr.each_with_index do |char, x|
        if @cache[y][x] != char
          print_with_pos(x + 1 , y + 1 , char)
          @cache[y][x] = char
        end
      end
    end
  end
end


12
13
14
15
16
# File 'lib/ruby_rich/live.rb', line 12

def print_with_pos(x,y,char)
  print "\e[?25l"   # 隐藏光标
  print "\e[#{y};#{x}H"   # 移动光标到左上角
  print char
end