Class: Shellfie::AnsiLineBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/shellfie/ansi_line_buffer.rb

Instance Method Summary collapse

Constructor Details

#initializeAnsiLineBuffer

Returns a new instance of AnsiLineBuffer.



5
6
7
8
9
# File 'lib/shellfie/ansi_line_buffer.rb', line 5

def initialize
  @cells = []
  @column = 0
  @pending_escape = +""
end

Instance Method Details

#clear(command, params) ⇒ Object



49
50
51
52
# File 'lib/shellfie/ansi_line_buffer.rb', line 49

def clear(command, params)
  mode = first_param(params, default: 0)
  command == "K" ? clear_line(mode) : clear_screen(mode)
end

#move(command, params) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/shellfie/ansi_line_buffer.rb', line 31

def move(command, params)
  amount = first_param(params, default: 1)
  case command
  when "C"
    @column += amount
  when "D"
    @column = [@column - amount, 0].max
  when "G"
    @column = [amount - 1, 0].max
  end
end

#position(params) ⇒ Object



43
44
45
46
47
# File 'lib/shellfie/ansi_line_buffer.rb', line 43

def position(params)
  values = params.to_s.split(";")
  column = values.length >= 2 ? Integer(values[1], exception: false) : 1
  @column = [[column || 1, 1].max - 1, 0].max
end

#to_sObject



54
55
56
57
58
59
# File 'lib/shellfie/ansi_line_buffer.rb', line 54

def to_s
  last = @cells.rindex { |cell| !cell.nil? }
  return @pending_escape unless last

  @cells[0..last].map { |cell| cell || " " }.join + @pending_escape
end

#write_character(char) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/shellfie/ansi_line_buffer.rb', line 15

def write_character(char)
  case char
  when "\r"
    @column = 0
  when "\b"
    @column = [@column - 1, 0].max
    @cells[@column] = nil
  when "\a"
    nil
  else
    @cells[@column] = "#{@pending_escape}#{char}"
    @pending_escape = +""
    @column += 1
  end
end

#write_escape(sequence) ⇒ Object



11
12
13
# File 'lib/shellfie/ansi_line_buffer.rb', line 11

def write_escape(sequence)
  @pending_escape << sequence
end