Class: AnsiSys::Terminal

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

Constant Summary collapse

CODE_LETTERS =

Escape sequence codes processed in this Class

%w(J K S T n s u)

Instance Method Summary collapse

Constructor Details

#initialize(csis = ["\x1b["]) ⇒ Terminal

csis is an Array of Code Sequence Introducers which can be e[, x9B, or both



572
573
574
575
# File 'lib/ansisys.rb', line 572

def initialize(csis = ["\x1b["])
  @lexer = Lexer.new(csis)
  @stream = Array.new
end

Instance Method Details

#apply_code!(letter, *pars) ⇒ Object

applies self an escape sequence code that ends with letter as String and with some pars as Integers



611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
# File 'lib/ansisys.rb', line 611

def apply_code!(letter, *pars)
  case letter
  when 'J'
    cur_col = @cursor.cur_col
    cur_row = @cursor.cur_row
    lines = @screens[-1].lines
    if pars.empty? or 0 == pars[0]
      rs = lines.keys.select{|r| r > cur_row}
      cs = lines[cur_row].keys.select{|c| c >= cur_col}
    elsif 1 == pars[0]
      rs = lines.keys.select{|r| r < cur_row}
      cs = lines[cur_row].keys.select{|c| c <= cur_col}
    elsif 2 == pars[0]
      rs = lines.keys
      cs = []
      @cursor.apply_code!('H', 1, 1)
    end
    rs.each do |r|
      lines.delete(r)
    end
    cs.each do |c|
      lines[cur_row].delete(c)
    end
  when 'K'
    cur_col = @cursor.cur_col
    cur_row = @cursor.cur_row
    line = @screens[-1].lines[cur_row]
    if pars.empty? or 0 == pars[0]
      cs = line.keys.select{|c| c >= cur_col}
    elsif 1 == pars[0]
      cs = line.keys.select{|c| c <= cur_col}
    elsif 2 == pars[0]
      cs = line.keys
    end
    cs.each do |c|
      line.delete(c)
    end
  when 'S'
    lines = @screens[-1].lines
    n = pars.empty? ? 1 : pars[0]
    n.times do |l|
      lines.delete(l)
    end
    rs = lines.keys.sort
    rs.each do |r|
      lines[r-n] = lines[r]
      lines.delete(r)
    end
    @cursor.apply_code!('H', rs[-1] - n + 1, 1)
  when 'T'
    lines = @screens[-1].lines
    n = pars.empty? ? 1 : pars[0]
    rs = lines.keys.sort_by{|a| -a} # sort.reverse
    rs.each do |r|
      lines[r+n] = lines[r]
      lines.delete(r)
    end
    @cursor.apply_code!('H', rs[-1] - n + 1, 1)
  when 's'
    @stored_cursor = @cursor.dup
  when 'u'
    @cursor = @stored_cursor.dup if @stored_cursor
  end

  return self
end

#css_style(format = :html, max_col = 80, max_row = nil, colors = Screen.default_css_colors) ⇒ Object

CSS stylelet to be used in <head>



586
587
588
589
590
591
592
593
# File 'lib/ansisys.rb', line 586

def css_style(format = :html, max_col = 80, max_row = nil, colors = Screen.default_css_colors)
  case format
  when :html
    Screen.css_style(colors, max_col, max_row)
  when :text
    ''
  end
end

#echo(data) ⇒ Object

echoes data, a String of characters or escape sequences to the Terminal. This method actually just buffers the echoed data.



580
581
582
583
# File 'lib/ansisys.rb', line 580

def echo(data)
  @lexer.push(data)
  return self
end

#render(format = :html, max_col = 80, max_row = nil, colors = Screen.default_css_colors, css_class = 'screen', css_style = nil) ⇒ Object

renders the echoed data as format of :html or :text. max_col, max_row can be specified as Integer. colors can be Screen.default_css_colors(inverted, bright).



598
599
600
601
602
603
604
605
606
607
# File 'lib/ansisys.rb', line 598

def render(format = :html, max_col = 80, max_row = nil, colors = Screen.default_css_colors, css_class = 'screen', css_style = nil)
  screens = populate(format, max_col, max_row, colors)
  separator = case format
  when :html
    "\n"
  when :text
    "\n---\n"
  end
  return screens.map{|screen| screen.render(format, css_class, css_style)}.join(separator)
end