ansi-sys 0.4.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/ChangeLog CHANGED
@@ -1,3 +1,10 @@
1
+ * 2007-11-03 zunda <zunda at freeshell.org>
2
+ - (0.5.0)
3
+ - Formatted RDoc
4
+
5
+ * 2007-11-01 zunda <zunda at freeshell.org>
6
+ - Formatted RDoc through Screen class
7
+
1
8
  * 2007-10-27 zunda <zunda at freeshell.org>
2
9
  - (0.4.1)
3
10
  - Updated History.txt
@@ -1,3 +1,6 @@
1
+ == 0.5.0 2007-11-03
2
+ * Made Rdoc
3
+
1
4
  == 0.4.1 2007-10-27
2
5
 
3
6
  * All the codes on http://en.wikipedia.org/wiki/ANSI_escape_code
@@ -11,13 +11,14 @@ require 'webrick'
11
11
  module AnsiSys
12
12
  module VERSION #:nodoc:
13
13
  MAJOR = 0
14
- MINOR = 4
15
- TINY = 1
14
+ MINOR = 5
15
+ TINY = 0
16
16
 
17
17
  STRING = [MAJOR, MINOR, TINY].join('.')
18
18
  end
19
19
 
20
20
  module CSSFormatter
21
+ # make a CSS style-let from a Hash of CSS settings
21
22
  def hash_to_styles(hash, separator = '; ')
22
23
  unless hash.empty?
23
24
  return hash.map{|e| "#{e[0]}: #{e[1].join(' ')}"}.join(separator)
@@ -40,16 +41,19 @@ module AnsiSys
40
41
 
41
42
  attr_reader :buffer
42
43
 
44
+ # _csis_ is an Array of Code Sequence Introducers
45
+ # which can be \e[, \x9B, or both
43
46
  def initialize(csis = ["\x1b["]) # CSI can also be "\x9B"
44
47
  @code_start_re = Regexp.union(*(CODE_EQUIVALENT.keys + csis))
45
48
  @buffer = ''
46
49
  end
47
50
 
51
+ # add the String (clear text with some or no escape sequences) to buffer
48
52
  def push(string)
49
53
  @buffer += string
50
54
  end
51
55
 
52
- # returns array of tokens while deleting the tokenized part of the string
56
+ # returns array of tokens while deleting the tokenized part from buffer
53
57
  def lex!
54
58
  r = Array.new
55
59
  @buffer.gsub!(/(?:\r\n|\n\r)/, "\n")
@@ -79,19 +83,21 @@ module AnsiSys
79
83
  end
80
84
 
81
85
  class Characters
86
+ # widths of characters
82
87
  WIDTHS = {
83
88
  "\t" => 8,
84
89
  }
85
90
 
86
- attr_reader :string
87
- attr_reader :sgr
88
- attr_reader :initial_cursor
91
+ attr_reader :string # clear text
92
+ attr_reader :sgr # Select Graphic Rendition associated with the text
89
93
 
90
94
  def initialize(string, sgr)
91
95
  @string = string
92
96
  @sgr = sgr
93
97
  end
94
98
 
99
+ # echo the string onto the _screen_ with initial cursor as _cursor_
100
+ # _cursor_ position will be changed as the string is echoed
95
101
  def echo_on(screen, cursor)
96
102
  each_char do |c|
97
103
  w = width(c)
@@ -102,12 +108,14 @@ module AnsiSys
102
108
  end
103
109
 
104
110
  private
111
+ # iterator on each character
105
112
  def each_char(&block)
106
113
  @string.scan(/./).each do |c|
107
114
  yield(c)
108
115
  end
109
116
  end
110
117
 
118
+ # width of a character
111
119
  def width(char)
112
120
  if WIDTHS.has_key?(char)
113
121
  return WIDTHS[char]
@@ -122,12 +130,13 @@ module AnsiSys
122
130
  end
123
131
 
124
132
  class Cursor
133
+ # Escape sequence codes processed in this Class
125
134
  CODE_LETTERS = %w(A B C D E F G H f)
126
135
 
127
- attr_reader :cur_col
128
- attr_reader :cur_row
129
- attr_accessor :max_col
130
- attr_accessor :max_row
136
+ attr_reader :cur_col # current column number (1-)
137
+ attr_reader :cur_row # current row number (1-)
138
+ attr_accessor :max_col # maximum column number
139
+ attr_accessor :max_row # maximum row number
131
140
 
132
141
  def initialize(cur_col = 1, cur_row = 1, max_col = 80, max_row = 25)
133
142
  @cur_col = cur_col
@@ -136,6 +145,8 @@ module AnsiSys
136
145
  @max_row = max_row
137
146
  end
138
147
 
148
+ # applies self an escape sequence code that ends with _letter_ as String
149
+ # and with some _pars_ as Integers
139
150
  def apply_code!(letter, *pars)
140
151
  case letter
141
152
  when 'A'
@@ -178,6 +189,7 @@ module AnsiSys
178
189
  return self
179
190
  end
180
191
 
192
+ # changes current location for a character with _width_ to be echoed
181
193
  def advance!(width = 1)
182
194
  r = nil
183
195
  @cur_col += width
@@ -195,21 +207,38 @@ module AnsiSys
195
207
  class SGR
196
208
  extend CSSFormatter
197
209
 
210
+ # Escape sequence codes processed in this Class
198
211
  CODE_LETTERS = %w(m)
199
212
 
200
- attr_reader :intensity # :normal, :bold, or :faint
201
- attr_reader :italic # :off or :on
202
- attr_reader :underline # :none, :single, or :double
203
- attr_reader :blink # :off, :slow, or :rapid
204
- attr_reader :image # :positive or :negative
205
- attr_reader :conceal # :off or :on
206
- attr_reader :foreground # :black, :red, :green, :yellow, :blue, :magenta, :cyan, or :white
207
- attr_reader :background # :black, :red, :green, :yellow, :blue, :magenta, :cyan, or :white
213
+ # :normal, :bold, or :faint
214
+ attr_reader :intensity
215
+
216
+ # :off or :on
217
+ attr_reader :italic
218
+
219
+ # :none, :single, or :double
220
+ attr_reader :underline
221
+
222
+ # :off, :slow, or :rapid
223
+ attr_reader :blink
224
+
225
+ # :positive or :negative
226
+ attr_reader :image
227
+
228
+ # :off or :on
229
+ attr_reader :conceal
230
+
231
+ # :black, :red, :green, :yellow, :blue, :magenta, :cyan, or :white
232
+ attr_reader :foreground
233
+
234
+ # :black, :red, :green, :yellow, :blue, :magenta, :cyan, or :white
235
+ attr_reader :background
208
236
 
209
237
  def initialize
210
238
  reset!
211
239
  end
212
240
 
241
+ # true if all the attributes are same
213
242
  def ==(other)
214
243
  instance_variables.each do |ivar|
215
244
  return false unless instance_eval(ivar) == other.instance_eval(ivar)
@@ -217,10 +246,13 @@ module AnsiSys
217
246
  return true
218
247
  end
219
248
 
249
+ # resets attributes
220
250
  def reset!
221
251
  apply_code!('m', 0)
222
252
  end
223
253
 
254
+ # applies self an escape sequence code that ends with _letter_ as String
255
+ # and with some _pars_ as Integers
224
256
  def apply_code!(letter = 'm', *pars)
225
257
  raise AnsiSysError, "Invalid code for SGR: #{letter.inspect}" unless 'm' == letter
226
258
  pars = [0] unless pars
@@ -264,6 +296,8 @@ module AnsiSys
264
296
  return self
265
297
  end
266
298
 
299
+ # renders self as :html or :text _format_ - makes a <span> html scriptlet.
300
+ # _colors_ can be Screen.default_css_colors(_inverted_, _bright_).
267
301
  def render(format = :html, position = :prefix, colors = Screen.default_css_colors)
268
302
  case format
269
303
  when :html
@@ -288,10 +322,12 @@ module AnsiSys
288
322
  end
289
323
  end
290
324
 
325
+ # CSS stylelet
291
326
  def css_style(colors = Screen.default_css_colors)
292
327
  return CSSFormatter.hash_to_styles(css_styles(colors))
293
328
  end
294
329
 
330
+ # a Hash of CSS stylelet
295
331
  def css_styles(colors = Screen.default_css_colors)
296
332
  r = Hash.new{|h, k| h[k] = Array.new}
297
333
  # intensity is not (yet) implemented
@@ -335,7 +371,7 @@ module AnsiSys
335
371
  25 => [:blink, :off],
336
372
  27 => [:image, :positive],
337
373
  28 => [:conceal, :off],
338
- }
374
+ } # :nodoc:
339
375
 
340
376
  COLOR = {
341
377
  0 => :black,
@@ -346,17 +382,18 @@ module AnsiSys
346
382
  5 => :magenta,
347
383
  6 => :cyan,
348
384
  7 => :white,
349
- }
385
+ } # :nodoc:
350
386
 
351
387
  end
352
388
 
353
389
  class Screen
354
- CODE_LETTERS = %w()
390
+ # Escape sequence codes processed in this Class
391
+ CODE_LETTERS = %w() # :nodoc:
355
392
 
356
393
  def self.default_foreground; :white; end
357
394
  def self.default_background; :black; end
358
395
 
359
- # intensity -> color
396
+ # a Hash of color names for each intensity
360
397
  def self.default_css_colors(inverted = false, bright = false)
361
398
  r = {
362
399
  :normal => {
@@ -407,6 +444,7 @@ module AnsiSys
407
444
  return r
408
445
  end
409
446
 
447
+ # a Hash of CSS stylelet to be used in <head>
410
448
  def self.css_styles(colors = Screen.default_css_colors, max_col = nil, max_row = nil)
411
449
  h = {
412
450
  'color' => [colors[:normal][:white]],
@@ -418,12 +456,17 @@ module AnsiSys
418
456
  return h
419
457
  end
420
458
 
459
+ # CSS stylelet to be used in <head>
421
460
  def self.css_style(*args)
422
461
  return "pre.screen {\n\t" + CSSFormatter.hash_to_styles(self.css_styles(*args), ";\n\t") + ";\n}\n"
423
462
  end
424
463
 
464
+ # a Hash of keys as rows,
465
+ # which each value a Hash of keys columns and each value as
466
+ # an Array of character, its width, and associated SGR
425
467
  attr_reader :lines
426
468
 
469
+ # a Screen
427
470
  def initialize(colors = Screen.default_css_colors, max_col = nil, max_row = nil)
428
471
  @colors = colors
429
472
  @max_col = max_col
@@ -431,14 +474,19 @@ module AnsiSys
431
474
  @lines = Hash.new{|hash, key| hash[key] = Hash.new}
432
475
  end
433
476
 
477
+ # CSS stylelet to be used in <head>
434
478
  def css_style
435
479
  self.class.css_style(@colors, @max_col, @max_row)
436
480
  end
437
481
 
482
+ # register the _char_ at a specific location on Screen
438
483
  def write(char, char_width, col, row, sgr)
439
484
  @lines[Integer(row)][Integer(col)] = [char, char_width, sgr.dup]
440
485
  end
441
486
 
487
+ # render the characters into :html or :text
488
+ # Class name in CSS can be specified as _css_class_.
489
+ # Additional stylelet can be specified as _css_style_.
442
490
  def render(format = :html, css_class = 'screen', css_style = nil)
443
491
  result = case format
444
492
  when :text
@@ -490,24 +538,33 @@ module AnsiSys
490
538
  return result
491
539
  end
492
540
 
493
- def apply_code!(letter, *pars) # TODO - some codes
541
+ # applies self an escape sequence code that ends with _letter_ as String
542
+ # and with some _pars_ as Integers
543
+ def apply_code!(letter, *pars)
494
544
  return self
495
- end
545
+ end # :nodoc:
496
546
  end
497
547
 
498
548
  class Terminal
549
+ # Escape sequence codes processed in this Class
499
550
  CODE_LETTERS = %w(J K S T n s u)
500
551
 
552
+ # _csis_ is an Array of Code Sequence Introducers
553
+ # which can be \e[, \x9B, or both
501
554
  def initialize(csis = ["\x1b["])
502
555
  @lexer = Lexer.new(csis)
503
556
  @stream = Array.new
504
557
  end
505
558
 
559
+ # echoes _data_, a String of characters or escape sequences
560
+ # to the Terminal.
561
+ # This method actually just buffers the echoed data.
506
562
  def echo(data)
507
563
  @lexer.push(data)
508
564
  return self
509
565
  end
510
566
 
567
+ # CSS stylelet to be used in <head>
511
568
  def css_style(format = :html, max_col = 80, max_row = nil, colors = Screen.default_css_colors)
512
569
  case format
513
570
  when :html
@@ -517,6 +574,9 @@ module AnsiSys
517
574
  end
518
575
  end
519
576
 
577
+ # renders the echoed data as _format_ of :html or :text.
578
+ # _max_col_, _max_row_ can be specified as Integer.
579
+ # _colors_ can be Screen.default_css_colors(_inverted_, _bright_).
520
580
  def render(format = :html, max_col = 80, max_row = nil, colors = Screen.default_css_colors, css_class = 'screen', css_style = nil)
521
581
  screens = populate(format, max_col, max_row, colors)
522
582
  separator = case format
@@ -528,6 +588,8 @@ module AnsiSys
528
588
  return screens.map{|screen| screen.render(format, css_class, css_style)}.join(separator)
529
589
  end
530
590
 
591
+ # applies self an escape sequence code that ends with _letter_ as String
592
+ # and with some _pars_ as Integers
531
593
  def apply_code!(letter, *pars)
532
594
  case letter
533
595
  when 'J'
@@ -628,6 +690,11 @@ module AnsiSys
628
690
  end
629
691
 
630
692
  if defined?(Hiki) and Hiki::Plugin == self.class
693
+ # a Hiki plugin method to render a file of text with ANSI escape sequences.
694
+ # Attached file name should be specified as _file_name_.
695
+ # _max_row_ can be specified.
696
+ # _invert_ and _bright_ can be specified to change colors.
697
+ # _page_ can be specified to show a file attached to another page.
631
698
  def ansi_screen(file_name, max_row = 80, invert = false, bright = true, page = @page)
632
699
  return '' unless file_name =~ /\.(txt|rd|rb|c|pl|py|sh|java|html|htm|css|xml|xsl|sql|yaml)\z/i
633
700
  page_file_name = "#{page.untaint.escape}/#{file_name.untaint.escape}"
@@ -35,10 +35,7 @@
35
35
  <p>Get Version</p>
36
36
  <a href="http://rubyforge.org/projects/ansi-sys" class="numbers">0.4.1</a>
37
37
  </div>
38
- <h1>&#x2192; &#8216;ansi-sys&#8217;</h1>
39
-
40
-
41
- <h2>What</h2>
38
+ <h2>What</h2>
42
39
 
43
40
 
44
41
  <p>MS-DOS console (with <code>DEVICE=ANSI.SYS</code>), Linux
@@ -85,9 +82,12 @@ misc/plugin directory and configure Hiki to enable the plugin.</p>
85
82
  <p>As a Ruby library:</p>
86
83
 
87
84
 
88
- <pre>terminal = Terminal.new
85
+ <pre>
86
+ require 'ansisys'
87
+ terminal = AnsiSys::Terminal.new
89
88
  terminal.echo("ansi-escaped-text")
90
- terminal.render #=&gt; HTML fragment</pre>
89
+ terminal.render #=&gt; HTML fragment
90
+ </pre>
91
91
 
92
92
  <p>As a Hiki plugin:</p>
93
93
 
@@ -110,7 +110,7 @@ terminal.render #=&gt; HTML fragment</pre>
110
110
  <p>Please refer
111
111
  <a href="rdoc/files/License_txt.html">License.txt</a></p>
112
112
  <p class="coda">
113
- zunda, 21st October 2007<br>
113
+ zunda, 3rd November 2007<br>
114
114
  Theme extended from <a href="http://rb2js.rubyforge.org/">Paul Battley</a>
115
115
  </p>
116
116
  </div>
@@ -1,8 +1,5 @@
1
1
  h1. Ruby-ANSI.SYS
2
2
 
3
- h1. &#x2192; 'ansi-sys'
4
-
5
-
6
3
  h2. What
7
4
 
8
5
  MS-DOS console (with <code>DEVICE=ANSI.SYS</code>), Linux
@@ -41,9 +38,12 @@ h2. Usage
41
38
 
42
39
  As a Ruby library:
43
40
 
44
- <pre>terminal = Terminal.new
41
+ <pre>
42
+ require 'ansisys'
43
+ terminal = AnsiSys::Terminal.new
45
44
  terminal.echo("ansi-escaped-text")
46
- terminal.render #=> HTML fragment</pre>
45
+ terminal.render #=> HTML fragment
46
+ </pre>
47
47
 
48
48
  As a Hiki plugin:
49
49
 
@@ -1,12 +1,12 @@
1
1
  body {
2
- background-color: #E1D1F1;
2
+ background-color: #FFB;
3
3
  font-family: "Georgia", sans-serif;
4
4
  line-height: 1.6em;
5
5
  padding: 1.6em 0 0 0;
6
- color: #333;
6
+ color: #000;
7
7
  }
8
8
  h1, h2, h3, h4, h5, h6 {
9
- color: #444;
9
+ color: #008;
10
10
  }
11
11
  h1 {
12
12
  font-family: sans-serif;
@@ -22,7 +22,7 @@ li {
22
22
  list-style-type: square;
23
23
  }
24
24
  a {
25
- color: #5E5AFF;
25
+ color: #008;
26
26
  font-weight: normal;
27
27
  text-decoration: underline;
28
28
  }
@@ -45,14 +45,14 @@ blockquote {
45
45
 
46
46
  table {
47
47
  line-height: 1.4em;
48
- color: #ff8;
48
+ color: #008;
49
49
  background-color: #111;
50
50
  padding: 2px 10px 2px 10px;
51
51
  border-style: dashed;
52
52
  }
53
53
 
54
54
  th {
55
- color: #fff;
55
+ color: #008;
56
56
  }
57
57
 
58
58
  td {
@@ -75,8 +75,8 @@ pre, code {
75
75
  line-height: 1.4em;
76
76
  }
77
77
  pre {
78
- color: #ff8;
79
- background-color: #111;
78
+ color: #0F0;
79
+ background-color: #010;
80
80
  padding: 2px 10px 2px 10px;
81
81
  }
82
82
  .comment { color: #aaa; font-style: italic; }
@@ -95,12 +95,12 @@ pre {
95
95
  text-align: right;
96
96
  font-family: sans-serif;
97
97
  font-weight: normal;
98
- background-color: #B3ABFF;
99
- color: #141331;
98
+ background-color: #FFF;
99
+ color: #008;
100
100
  padding: 15px 20px 10px 20px;
101
101
  margin: 0 auto;
102
102
  margin-top: 15px;
103
- border: 3px solid #141331;
103
+ border: 3px solid #000;
104
104
  }
105
105
 
106
106
  #version .numbers {
@@ -113,16 +113,16 @@ pre {
113
113
 
114
114
  #version p {
115
115
  text-decoration: none;
116
- color: #141331;
117
- background-color: #B3ABFF;
116
+ color: #008;
117
+ background-color: #FFF;
118
118
  margin: 0;
119
119
  padding: 0;
120
120
  }
121
121
 
122
122
  #version a {
123
123
  text-decoration: none;
124
- color: #141331;
125
- background-color: #B3ABFF;
124
+ color: #008;
125
+ background-color: #FFF;
126
126
  }
127
127
 
128
128
  .clickable {
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.4
2
+ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: ansi-sys
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.4.1
7
- date: 2007-10-27 00:00:00 -10:00
6
+ version: 0.5.0
7
+ date: 2007-11-03 00:00:00 -10:00
8
8
  summary: Ruby-ANSI.SYS is a library to render texts with ANSI escape sequences.
9
9
  require_paths:
10
10
  - lib