Class: AnsiSys::Screen

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

Constant Summary collapse

CODE_LETTERS =

Escape sequence codes processed in this Class

%w()

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(colors = Screen.default_css_colors, max_col = nil, max_row = nil) ⇒ Screen

a Screen



505
506
507
508
509
510
# File 'lib/ansisys.rb', line 505

def initialize(colors = Screen.default_css_colors, max_col = nil, max_row = nil)
  @colors = colors
  @max_col = max_col
  @max_row = max_row
  @lines = Hash.new{|hash, key| hash[key] = Hash.new}
end

Instance Attribute Details

#linesObject (readonly)

a Hash of keys as rows, which each value a Hash of keys columns and each value as an Array of character, its width, and associated SGR



502
503
504
# File 'lib/ansisys.rb', line 502

def lines
  @lines
end

Class Method Details

.css_style(*args) ⇒ Object

CSS stylelet to be used in <head>. Takes the same arguments as Screen::css_styles().



495
496
497
# File 'lib/ansisys.rb', line 495

def self.css_style(*args)
  return "pre.screen {\n\t" + CSSFormatter.hash_to_styles(self.css_styles(*args), ";\n\t") + ";\n}\n"
end

.css_styles(colors = Screen.default_css_colors, max_col = nil, max_row = nil) ⇒ Object

a Hash of CSS stylelet to be used in <head>



482
483
484
485
486
487
488
489
490
491
# File 'lib/ansisys.rb', line 482

def self.css_styles(colors = Screen.default_css_colors, max_col = nil, max_row = nil)
  h = {
    'color' => [colors[:normal][:white]],
    'background-color' => [colors[:normal][:black]],
    'padding' => ['0.5em'],
  }
  h['width'] = ["#{Float(max_col)/2}em"] if max_col
  #h['height'] = ["#{max_row}em"] if max_row # could not find appropriate unit
  return h
end

.default_backgroundObject



428
# File 'lib/ansisys.rb', line 428

def self.default_background; :black; end

.default_css_colors(inverted = false, bright = false) ⇒ Object

a Hash of color names for each intensity



431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# File 'lib/ansisys.rb', line 431

def self.default_css_colors(inverted = false, bright = false)
  r = {
    :normal => {
      :black => 'black',
      :red => 'maroon',
      :green => 'green',
      :yellow => 'olive',
      :blue => 'navy',
      :magenta => 'purple',
      :cyan => 'teal',
      :white => 'silver',
    },
    :bold => {
      :black => 'gray',
      :red => 'red',
      :green => 'lime',
      :yellow => 'yellow',
      :blue => 'blue',
      :magenta => 'fuchsia',
      :cyan => 'cyan',
      :white => 'white'
    },
    :faint => {
      :black => 'black',
      :red => 'maroon',
      :green => 'green',
      :yellow => 'olive',
      :blue => 'navy',
      :magenta => 'purple',
      :cyan => 'teal',
      :white => 'silver',
    },
  }

  if bright
    r[:bold][:black] = 'black'
    [:normal, :faint].each do |i|
      r[i] = r[:bold]
    end
  end

  if inverted
    r.each_key do |i|
      r[i][:black], r[i][:white] = r[i][:white], r[i][:black]
    end
  end

  return r
end

.default_foregroundObject

:nodoc:



427
# File 'lib/ansisys.rb', line 427

def self.default_foreground; :white; 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



578
579
580
# File 'lib/ansisys.rb', line 578

def apply_code!(letter, *pars)
  return self
end

#css_styleObject

CSS stylelet to be used in <head>



513
514
515
# File 'lib/ansisys.rb', line 513

def css_style
  self.class.css_style(@colors, @max_col, @max_row)
end

#render(format = :html, css_class = 'screen', css_style = nil) ⇒ Object

render the characters into :html or :text Class name in CSS can be specified as css_class. Additional stylelet can be specified as css_style.



525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
# File 'lib/ansisys.rb', line 525

def render(format = :html, css_class = 'screen', css_style = nil)
  result = case format
  when :text
    ''
  when :html
    %Q|<pre#{css_class ? %Q[ class="#{css_class}"] : ''}#{css_style ? %Q| style="#{css_style}"| : ''}>\n|
  else
    raise AnsiSysError, "Invalid format option to render: #{format.inspect}"
  end

  unless @lines.keys.empty?
    prev_sgr = nil
    max_row = @lines.keys.max
    (1..max_row).each do |row|
      if @lines.has_key?(row) and not @lines[row].keys.empty?
        col = 1
        while col <= @lines[row].keys.max
          if @lines[row].has_key?(col) and @lines[row][col]
            char, width, sgr = @lines[row][col]
            if prev_sgr != sgr
              result += prev_sgr.render(format, :postfix, @colors) if prev_sgr
              result += sgr.render(format, :prefix, @colors)
              prev_sgr = sgr
            end
            case format
            when :text
              result += char
            when :html
              result += WEBrick::HTMLUtils.escape(char)
            end
            col += width
          else
            result += ' '
            col += 1
          end
        end
      end
      result += "\n" if row < max_row
    end
    result += prev_sgr.render(format, :postfix, @colors) if prev_sgr
  end

  result += case format
  when :text
    ''
  when :html
    '</pre>'
  end
  return result
end

#write(char, char_width, col, row, sgr) ⇒ Object

register the char at a specific location on Screen



518
519
520
# File 'lib/ansisys.rb', line 518

def write(char, char_width, col, row, sgr)
  @lines[Integer(row)][Integer(col)] = [char, char_width, sgr.dup]
end