Class: Screen

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

Instance Method Summary collapse

Constructor Details

#initializeScreen

Returns a new instance of Screen.



2
3
4
5
6
# File 'lib/log_query/screen.rb', line 2

def initialize
  @old_lines = []
  @new_lines = []
  @current_line = 0
end

Instance Method Details

#delete_current_lineObject



26
27
28
# File 'lib/log_query/screen.rb', line 26

def delete_current_line
  @buffer += "\e[2K"
end

#drawObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/log_query/screen.rb', line 35

def draw
  @buffer = ""
  # pad line buffers
  line_count = [@old_lines.size, @new_lines.size].max
  @old_lines.fill(nil, @old_lines.size, line_count) if @old_lines.size < line_count
  @new_lines.fill(nil, @new_lines.size, line_count) if @new_lines.size < line_count

  @old_lines.zip(@new_lines).each_with_index do |(old, new), index|
    if old != new
      move_to(index)
      delete_current_line
      print_line(new) 
    end
  end
  @old_lines = @new_lines
  @new_lines = []
  print @buffer
end

#line(value) ⇒ Object



8
9
10
# File 'lib/log_query/screen.rb', line 8

def line(value)
  @new_lines << value
end

#move_to(row) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/log_query/screen.rb', line 12

def move_to(row)
  #puts "moving to #{row}"
  #puts "current line #{@current_line}"
  diff = row - @current_line
  if diff > 0
    #puts "moving down #{diff.abs}"
    @buffer += "\e[#{diff.abs}B"
  elsif diff < 0
    #puts "moving up #{diff.abs}"
    @buffer += "\e[#{diff.abs}A"
  end
  @current_line += diff
end


30
31
32
33
# File 'lib/log_query/screen.rb', line 30

def print_line(line)
  @buffer += "#{line}\n"
  @current_line += 1
end