jbe-termite 1.1.1

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.
Files changed (3) hide show
  1. data/README.textile +103 -0
  2. data/lib/termite.rb +159 -0
  3. metadata +54 -0
data/README.textile ADDED
@@ -0,0 +1,103 @@
1
+ h1. Termite
2
+
3
+ h2. ANSI and VT100 output escape wrapper in Ruby.
4
+
5
+ h3. Installation
6
+
7
+ @gem sources -a http://gems.github.com@
8
+
9
+ @sudo gem install jbe-termite@
10
+
11
+ h3. Features
12
+
13
+ * Colored terminal output
14
+ * Other ANSI decorations
15
+ * Redraw lines
16
+ * Simple output "animation"
17
+ * Progress bars
18
+ * Sensible syntax
19
+
20
+ h3. Requires
21
+
22
+ * 16 color terminal
23
+ * *nix. Windows is *not* supported.
24
+
25
+ h2. Examples
26
+
27
+ <pre>
28
+ <code>
29
+ require 'rubygems'
30
+ require 'termite'
31
+
32
+ include Termite::Palette
33
+
34
+ puts "Add some ' + green('color') + '!'
35
+
36
+ puts 'And also some ' + red('contrasting background!', :green)
37
+
38
+
39
+ puts red( 'The pentagram thy peace doth mar?' )
40
+
41
+ puts black(:yellow, 'To me, thou son of hell, explain,' )
42
+
43
+ puts blue(:underscore, cyan,
44
+ :blink, :bright, 'How earnest thou in, if this thine exit bar?' )
45
+
46
+ puts white(:bold, 'Could such a spirit aught ensnare?')
47
+ </code>
48
+ </pre>
49
+
50
+ Parameter order is irrelevant. Multiple strings will be joined.
51
+
52
+ <pre>
53
+ <code>
54
+
55
+ print 'Redrawing... '
56
+ redraw do |i|
57
+ if i < 11
58
+ sleep 0.05
59
+ i.to_s
60
+ elsif i == 11
61
+ 'OK'
62
+ else
63
+ false
64
+ end
65
+ end
66
+
67
+
68
+ print 'Animating progress bar..'
69
+ pb = Termite::Progress.new 'Loading', 35, :red
70
+ animated_redraw(50, 25) do |f|
71
+ pb.report f.to_f / 50
72
+ end
73
+
74
+ </code>
75
+ </pre>
76
+
77
+ See also the examples in './test'.
78
+
79
+
80
+ h3. LICENSE
81
+
82
+ (MIT License)
83
+
84
+ Copyright (c) 2009 Jostein Berre Eliassen
85
+
86
+ Permission is hereby granted, free of charge, to any person obtaining
87
+ a copy of this software and associated documentation files (the
88
+ 'Software'), to deal in the Software without restriction, including
89
+ without limitation the rights to use, copy, modify, merge, publish,
90
+ distribute, sublicense, and/or sell copies of the Software, and to
91
+ permit persons to whom the Software is furnished to do so, subject to
92
+ the following conditions:
93
+
94
+ The above copyright notice and this permission notice shall be
95
+ included in all copies or substantial portions of the Software.
96
+
97
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
98
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
99
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
100
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
101
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
102
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
103
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/lib/termite.rb ADDED
@@ -0,0 +1,159 @@
1
+ # ,_ _,
2
+ # '._.'
3
+ # '-, (_) ,-' TERMITE
4
+ # '._ .:. _.'
5
+ # _ '|Y|' _ A basic ANSI / VT100 library
6
+ # ,` `>\ /<` `,
7
+ # ` ,-` I `-, `
8
+ # | /=\ |
9
+ # ,-' |=| '-,
10
+ # )-( jostein.be/code
11
+ # \_/
12
+
13
+
14
+
15
+ module Termite
16
+
17
+ Colors = [ :black, :red, :green, :yellow,
18
+ :blue, :purple, :cyan, :white ]
19
+
20
+ Modifiers = {
21
+ :reset => 'c' ,
22
+
23
+ :enable_wrap => '[7h' ,
24
+ :disable_wrap => '[7l' ,
25
+
26
+ :home => '[H' ,
27
+ :save_cursor => '[s' ,
28
+ :restore_cursor => '[u' ,
29
+
30
+ :erase_front => '[K' ,
31
+ :erase_back => '[1K' ,
32
+ :erase_line => '[2K' ,
33
+ :erase_down => '[J' ,
34
+ :erase_up => '[1J' ,
35
+ :erase_screen => '[2J' ,
36
+
37
+ :text_reset => '[0m' ,
38
+ :bright => '[1m' ,
39
+ :dim => '[2m' ,
40
+ :underscore => '[4m' ,
41
+ :blink => '[5m' ,
42
+ :reverse => '[7m' ,
43
+ :hide => '[8m' ,
44
+ }
45
+ for n in 0..Colors.size
46
+ Modifiers[:"fg_#{Colors[n]}"] = "[#{n+30}m"
47
+ Modifiers[:"bg_#{Colors[n]}"] = "[#{n+40}m"
48
+ end
49
+
50
+ def self.escape( *args)
51
+ (args & Termite::Modifiers.keys).map do |m|
52
+ "\e" + Modifiers[m]
53
+ end.join ''
54
+ end
55
+
56
+ def self.decorate( *args )
57
+ text = args.select {|arg| arg.is_a? String }.join
58
+
59
+ self.escape( *args ) +
60
+ text +
61
+ self.escape( :text_reset )
62
+ end
63
+
64
+ def self.colorize( fg, *args )
65
+ args << :"fg_#{fg}"
66
+
67
+ unless (bg = args & Colors).empty?
68
+ args << :"bg_#{bg[0]}"
69
+ end
70
+ self.decorate( *args )
71
+ end
72
+
73
+ def self.rewrite( str )
74
+ self.escape(:erase_front) +
75
+ self.escape(:save_cursor) +
76
+ str + "\n" +
77
+ self.escape(:restore_cursor)
78
+ end
79
+ end
80
+
81
+ class Termite::Progress
82
+
83
+ @state = 0.0
84
+
85
+ attr_accessor :width, :state, :desc, :decorations
86
+
87
+ def initialize( desc, width = 35, color = false, *deco )
88
+ @desc = desc
89
+ @width = width
90
+ @decorations = deco
91
+ @decorations << :"fg_#{color}" if color
92
+ end
93
+
94
+ def reset
95
+ @state = 0.0
96
+ end
97
+
98
+ def report( s = false )
99
+ @state = s if s
100
+ bar
101
+ end
102
+
103
+ def bar
104
+ completed = (@state * @width).to_i
105
+ remaining = @width - completed
106
+ make_bar_string
107
+ Termite.decorate(@bs[0..(completed)],
108
+ :reverse, *@decorations) +
109
+ Termite.decorate(@bs[(completed+1)..@width],
110
+ *@decorations)
111
+ end
112
+
113
+ private
114
+
115
+ def make_bar_string
116
+ @bs = @desc
117
+ @bs = @bs[0..(@width-2)] + '..' if @bs.length > @width
118
+ @bs << (' ' * (width - @bs.length)) if @bs.length < @width
119
+ end
120
+ end
121
+
122
+
123
+
124
+ # Mixin:
125
+
126
+ module Termite::Palette
127
+ Termite::Colors.each do |color|
128
+ define_method color do |*args|
129
+ Termite.colorize( color, *args )
130
+ end
131
+ end
132
+
133
+ [ :bright, :dim, :underscore, :blink,
134
+ :reverse, :hide ].each do |mod|
135
+ define_method mod do |*args|
136
+ Termite.decorate(mod, *args)
137
+ end
138
+ end
139
+
140
+ def redraw
141
+ i = 0
142
+ while (x = yield(i))
143
+ print Termite.rewrite x
144
+ i = i + 1
145
+ end
146
+ puts
147
+ end
148
+
149
+ def animated_redraw(frames, fps)
150
+ spf = 1.0 / fps
151
+ redraw do |i|
152
+ if i < frames
153
+ sleep spf
154
+ yield(i)
155
+ end
156
+ end
157
+ end
158
+
159
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jbe-termite
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Jostein Berre Eliassen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-12 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Termite is a basic ANSI and VT100 Ruby library.
17
+ email: post@jostein.be
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README.textile
26
+ - lib/termite.rb
27
+ has_rdoc: false
28
+ homepage: http://github.com/jbe/termite
29
+ post_install_message:
30
+ rdoc_options: []
31
+
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: "0"
39
+ version:
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ requirements: []
47
+
48
+ rubyforge_project:
49
+ rubygems_version: 1.2.0
50
+ signing_key:
51
+ specification_version: 2
52
+ summary: Termite is a basic ANSI and VT100 Ruby library.
53
+ test_files: []
54
+