Class: AnsiSys::SGR

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

Overview

Select Graphic Rendition

Constant Summary collapse

CODE_LETTERS =

Escape sequence codes processed in this Class

%w(m)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CSSFormatter

hash_to_styles

Constructor Details

#initializeSGR

Returns a new instance of SGR.



271
272
273
# File 'lib/ansisys.rb', line 271

def initialize
  reset!
end

Instance Attribute Details

#backgroundObject (readonly)

:black, :red, :green, :yellow, :blue, :magenta, :cyan, or :white



269
270
271
# File 'lib/ansisys.rb', line 269

def background
  @background
end

:off, :slow, or :rapid



257
258
259
# File 'lib/ansisys.rb', line 257

def blink
  @blink
end

#concealObject (readonly)

:off or :on



263
264
265
# File 'lib/ansisys.rb', line 263

def conceal
  @conceal
end

#foregroundObject (readonly)

:black, :red, :green, :yellow, :blue, :magenta, :cyan, or :white



266
267
268
# File 'lib/ansisys.rb', line 266

def foreground
  @foreground
end

#imageObject (readonly)

:positive or :negative



260
261
262
# File 'lib/ansisys.rb', line 260

def image
  @image
end

#intensityObject (readonly)

:normal, :bold, or :faint



248
249
250
# File 'lib/ansisys.rb', line 248

def intensity
  @intensity
end

#italicObject (readonly)

:off or :on



251
252
253
# File 'lib/ansisys.rb', line 251

def italic
  @italic
end

#underlineObject (readonly)

:none, :single, or :double



254
255
256
# File 'lib/ansisys.rb', line 254

def underline
  @underline
end

Instance Method Details

#==(other) ⇒ Object

true if all the attributes are same



276
277
278
279
280
281
# File 'lib/ansisys.rb', line 276

def ==(other)
  instance_variables.each do |ivar|
    return false unless instance_eval(ivar) == other.instance_eval(ivar)
  end
  return true
end

#apply_code!(letter = 'm', *pars) ⇒ Object

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

Raises:



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/ansisys.rb', line 290

def apply_code!(letter = 'm', *pars)
  raise AnsiSysError, "Invalid code for SGR: #{letter.inspect}" unless 'm' == letter
  pars = [0] unless pars
  pars.each do |code|
    case code
    when 0
      @intensity = :normal
      @italic = :off
      @underline = :none
      @blink = :off
      @image = :positive
      @conceal = :off
      @foreground = :white
      @background = :black
    when 1..28
      apply_code_table!(code)
    when 30..37
      @foreground = COLOR[code - 30]
      @intensity = :normal
    when 39
      reset!
    when 40..47
      @background = COLOR[code - 40]
      @intensity = :normal
    when 49
      reset!
    when 90..97
      @foreground = COLOR[code - 90]
      @intensity = :bold
    when 99
      reset!
    when 100..107
      @background = COLOR[code - 100]
      @intensity = :bold
    when 109
      reset!
    else
      raise AnsiSysError, "Invalid SGR code #{code.inspect}" unless CODE.has_key?(code)
    end
  end
  return self
end

#css_style(colors = Screen.default_css_colors) ⇒ Object

CSS stylelet



360
361
362
# File 'lib/ansisys.rb', line 360

def css_style(colors = Screen.default_css_colors)
  return CSSFormatter.hash_to_styles(css_styles(colors))
end

#css_styles(colors = Screen.default_css_colors) ⇒ Object

a Hash of CSS stylelet



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/ansisys.rb', line 365

def css_styles(colors = Screen.default_css_colors)
  r = Hash.new{|h, k| h[k] = Array.new}
  # intensity is not (yet) implemented
  r['font-style'] << 'italic' if @italic == :on
  r['text-decoration'] << 'underline' unless @underline == :none
  r['text-decoration'] << 'blink' unless @blink == :off
  case @image
  when :positive
    fg = @foreground
    bg = @background
  when :negative
    fg = @background
    bg = @foreground
  end
  fg = bg if @conceal == :on
  r['color'] << colors[@intensity][fg] unless fg == :white
  r['background-color'] << colors[@intensity][bg] unless bg == :black
  return r
end

#render(format = :html, position = :prefix, colors = Screen.default_css_colors) ⇒ Object

renders self as :html or :text format - makes a <span> html scriptlet. colors can be Screen.default_css_colors(inverted, bright).



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/ansisys.rb', line 335

def render(format = :html, position = :prefix, colors = Screen.default_css_colors)
  case format
  when :html
    case position
    when :prefix
      style_code = css_style(colors)
      if style_code
        return %Q|<span style="#{style_code}">|
      else
        return ''
      end
    when :postfix
      style_code = css_style(colors)
      if style_code
        return '</span>'
      else
        return ''
      end
    end
  when :text
    return ''
  end
end

#reset!Object

resets attributes



284
285
286
# File 'lib/ansisys.rb', line 284

def reset!
  apply_code!('m', 0)
end