class AnsiSys::Screen

Attributes

lines[R]

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

Public Class Methods

css_style(*args) click to toggle source

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

# File lib/ansisys.rb, line 496
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) click to toggle source

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

# File lib/ansisys.rb, line 483
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_background() click to toggle source
# File lib/ansisys.rb, line 429
def self.default_background; :black; end
default_css_colors(inverted = false, bright = false) click to toggle source

a Hash of color names for each intensity

# File lib/ansisys.rb, line 432
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_foreground() click to toggle source
# File lib/ansisys.rb, line 428
def self.default_foreground; :white; end
new(colors = Screen.default_css_colors, max_col = nil, max_row = nil) click to toggle source

a Screen

# File lib/ansisys.rb, line 506
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

Public Instance Methods

apply_code!(letter, *pars) click to toggle source

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

# File lib/ansisys.rb, line 579
def apply_code!(letter, *pars)
        return self
end
css_style() click to toggle source

CSS stylelet to be used in <head>

# File lib/ansisys.rb, line 514
def css_style
        self.class.css_style(@colors, @max_col, @max_row)
end
render(format = :html, css_class = 'screen', css_style = nil) click to toggle source

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.

# File lib/ansisys.rb, line 526
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) click to toggle source

register the char at a specific location on Screen

# File lib/ansisys.rb, line 519
def write(char, char_width, col, row, sgr)
        @lines[Integer(row)][Integer(col)] = [char, char_width, sgr.dup]
end