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.



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

def initialize
	reset!
end

Instance Attribute Details

#backgroundObject (readonly)

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



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

def background
  @background
end

:off, :slow, or :rapid



240
241
242
# File 'lib/ansisys.rb', line 240

def blink
  @blink
end

#concealObject (readonly)

:off or :on



246
247
248
# File 'lib/ansisys.rb', line 246

def conceal
  @conceal
end

#foregroundObject (readonly)

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



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

def foreground
  @foreground
end

#imageObject (readonly)

:positive or :negative



243
244
245
# File 'lib/ansisys.rb', line 243

def image
  @image
end

#intensityObject (readonly)

:normal, :bold, or :faint



231
232
233
# File 'lib/ansisys.rb', line 231

def intensity
  @intensity
end

#italicObject (readonly)

:off or :on



234
235
236
# File 'lib/ansisys.rb', line 234

def italic
  @italic
end

#underlineObject (readonly)

:none, :single, or :double



237
238
239
# File 'lib/ansisys.rb', line 237

def underline
  @underline
end

Instance Method Details

#==(other) ⇒ Object

true if all the attributes are same



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

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:



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
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
# File 'lib/ansisys.rb', line 273

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



343
344
345
# File 'lib/ansisys.rb', line 343

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



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/ansisys.rb', line 348

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



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/ansisys.rb', line 318

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



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

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