Class: AnsiTerm::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/ansiterm/buffer.rb

Overview

# AnsiTerm::Buffer #

A terminal buffer that will eventually handle ANSI style strings fully. For now it will handle the sequences AnsiTerm::String handles but e.g. cursor movement etc. needs to be explicitly handled.

Extracted out of github.com/vidarh/re

FIXME: Provide method of setting default background color

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(w = 80, h = 25) ⇒ Buffer

Returns a new instance of Buffer.



17
18
19
20
21
22
23
24
# File 'lib/ansiterm/buffer.rb', line 17

def initialize(w=80, h=25)
  @lines = []
  @x = 0
  @y = 0
  @w = w
  @h = h
  @cache = []
end

Instance Attribute Details

#hObject (readonly)

Returns the value of attribute h.



15
16
17
# File 'lib/ansiterm/buffer.rb', line 15

def h
  @h
end

#linesObject (readonly)

Returns the value of attribute lines.



15
16
17
# File 'lib/ansiterm/buffer.rb', line 15

def lines
  @lines
end

#wObject (readonly)

Returns the value of attribute w.



15
16
17
# File 'lib/ansiterm/buffer.rb', line 15

def w
  @w
end

Instance Method Details

#clsObject



26
27
28
# File 'lib/ansiterm/buffer.rb', line 26

def cls
  @lines = (1..@h).map { nil }
end

#move_cursor(x, y) ⇒ Object



35
36
37
38
39
40
# File 'lib/ansiterm/buffer.rb', line 35

def move_cursor(x,y)
  @x = x
  @y = y
  @x = @w-1 if @x >= @w
  @y = @y-1 if @y >= @h
end


49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ansiterm/buffer.rb', line 49

def print *args
  args.each do |str|
    @lines[@y] ||= AnsiTerm::String.new
    l = @lines[@y]

    if l.length < @x
      l << (" "*(@x - l.length))
    end

    r=@x..@x+str.length-1
    #p [r, str]
    l[r] = str
  end
end

#resetObject



30
31
32
33
# File 'lib/ansiterm/buffer.rb', line 30

def reset
  cls
  @cache=[]
end

#resize(w, h) ⇒ Object



42
43
44
45
46
47
# File 'lib/ansiterm/buffer.rb', line 42

def resize(w,h)
  if @w != w || @h != h
    @w, @h = w,h
    @cache = []
  end
end

#scroll_up(num = 1, scroll_cache: false) ⇒ Object

This scrolls the buffer up If you want it to also scroll the cache pass ‘scroll_cache: true`. This will presume that you’ve scrolled the terminal yourself.



68
69
70
71
72
73
74
# File 'lib/ansiterm/buffer.rb', line 68

def scroll_up(num=1, scroll_cache: false)
  @lines.slice!(0)
  @lines << AnsiTerm::String.new
  if scroll_cache
    @cache.slice!(0)
  end
end

#to_sObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ansiterm/buffer.rb', line 76

def to_s
  out = ""
  cachehit=0
  cachemiss=0
  @lines.each_with_index do |line,y|
    line ||= ""
    line = line[0..@w]
    l = line.length
    s = line.to_str
    if @cache[y] != s
      # Move to start of line; output line; clear to end
      #if l > 0
        out << "\e[#{y+1};1H" << s
        if l < @w
          out << "\e[0m\e[0K"
        end
      #end
      cachemiss += s.length
      old = @cache[y]
      @cache[y] = s
    else
      cachehit += @cache[y].length
    end
  end
  out
end