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



488
489
490
491
492
493
# File 'lib/ansisys.rb', line 488

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



485
486
487
# File 'lib/ansisys.rb', line 485

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().



478
479
480
# File 'lib/ansisys.rb', line 478

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>



465
466
467
468
469
470
471
472
473
474
# File 'lib/ansisys.rb', line 465

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



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

def self.default_background; :black; end

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

a Hash of color names for each intensity



414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
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
# File 'lib/ansisys.rb', line 414

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:



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

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



561
562
563
# File 'lib/ansisys.rb', line 561

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

#css_styleObject

CSS stylelet to be used in <head>



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

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.



508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
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
# File 'lib/ansisys.rb', line 508

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



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

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