Class: AnsiSys::SGR
Overview
Select Graphic Rendition
Constant Summary collapse
- CODE_LETTERS =
Escape sequence codes processed in this Class
%w(m)
Instance Attribute Summary collapse
-
#background ⇒ Object
readonly
:black, :red, :green, :yellow, :blue, :magenta, :cyan, or :white.
-
#blink ⇒ Object
readonly
:off, :slow, or :rapid.
-
#conceal ⇒ Object
readonly
:off or :on.
-
#foreground ⇒ Object
readonly
:black, :red, :green, :yellow, :blue, :magenta, :cyan, or :white.
-
#image ⇒ Object
readonly
:positive or :negative.
-
#intensity ⇒ Object
readonly
:normal, :bold, or :faint.
-
#italic ⇒ Object
readonly
:off or :on.
-
#underline ⇒ Object
readonly
:none, :single, or :double.
Instance Method Summary collapse
-
#==(other) ⇒ Object
true if all the attributes are same.
-
#apply_code!(letter = 'm', *pars) ⇒ Object
applies self an escape sequence code that ends with letter as String and with some pars as Integers.
-
#css_style(colors = Screen.default_css_colors) ⇒ Object
CSS stylelet.
-
#css_styles(colors = Screen.default_css_colors) ⇒ Object
a Hash of CSS stylelet.
-
#initialize ⇒ SGR
constructor
A new instance of SGR.
-
#render(format = :html, position = :prefix, colors = Screen.default_css_colors) ⇒ Object
renders self as :html or :text format - makes a <span> html scriptlet.
-
#reset! ⇒ Object
resets attributes.
Methods included from CSSFormatter
Constructor Details
#initialize ⇒ SGR
Returns a new instance of SGR.
271 272 273 |
# File 'lib/ansisys.rb', line 271 def initialize reset! end |
Instance Attribute Details
#background ⇒ Object (readonly)
:black, :red, :green, :yellow, :blue, :magenta, :cyan, or :white
269 270 271 |
# File 'lib/ansisys.rb', line 269 def background @background end |
#blink ⇒ Object (readonly)
:off, :slow, or :rapid
257 258 259 |
# File 'lib/ansisys.rb', line 257 def blink @blink end |
#conceal ⇒ Object (readonly)
:off or :on
263 264 265 |
# File 'lib/ansisys.rb', line 263 def conceal @conceal end |
#foreground ⇒ Object (readonly)
:black, :red, :green, :yellow, :blue, :magenta, :cyan, or :white
266 267 268 |
# File 'lib/ansisys.rb', line 266 def foreground @foreground end |
#image ⇒ Object (readonly)
:positive or :negative
260 261 262 |
# File 'lib/ansisys.rb', line 260 def image @image end |
#intensity ⇒ Object (readonly)
:normal, :bold, or :faint
248 249 250 |
# File 'lib/ansisys.rb', line 248 def intensity @intensity end |
#italic ⇒ Object (readonly)
:off or :on
251 252 253 |
# File 'lib/ansisys.rb', line 251 def italic @italic end |
#underline ⇒ Object (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
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 |