bone 0.2.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.
@@ -0,0 +1,313 @@
1
+ #--
2
+ # TODO: Make it Drydock-like
3
+ # Adapted from: http://github.com/oneup/ruby-console/tree/tput
4
+ # See: http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x405.html
5
+ # See: man terminfo
6
+ #++
7
+
8
+
9
+ module Console #:nodoc:all
10
+ extend self
11
+ autoload :Timeout, 'timeout'
12
+ autoload :Thread, 'thread'
13
+
14
+ # ANSI escape sequence numbers for text attributes
15
+ ATTRIBUTES = {
16
+ :normal => 0,
17
+ :bright => 1,
18
+ :dim => 2,
19
+ :underline => 4,
20
+ :blink => 5,
21
+ :reverse => 7,
22
+ :hidden => 8,
23
+ :default => 0,
24
+ }.freeze unless defined? ATTRIBUTES
25
+
26
+ # ANSI escape sequence numbers for text colours
27
+ COLOURS = {
28
+ :black => 30,
29
+ :red => 31,
30
+ :green => 32,
31
+ :yellow => 33,
32
+ :blue => 34,
33
+ :magenta => 35,
34
+ :cyan => 36,
35
+ :white => 37,
36
+ :default => 39,
37
+ :random => 30 + rand(10).to_i
38
+ }.freeze unless defined? COLOURS
39
+
40
+ # ANSI escape sequence numbers for background colours
41
+ BGCOLOURS = {
42
+ :black => 40,
43
+ :red => 41,
44
+ :green => 42,
45
+ :yellow => 43,
46
+ :blue => 44,
47
+ :magenta => 45,
48
+ :cyan => 46,
49
+ :white => 47,
50
+ :default => 49,
51
+ :random => 40 + rand(10).to_i
52
+ }.freeze unless defined? BGCOLOURS
53
+
54
+ def valid_colour?(colour)
55
+ COLOURS.has_key? colour
56
+ end
57
+ alias :valid_color? :valid_colour?
58
+
59
+ def print_left(str, props={})
60
+ props[:x] ||= 0
61
+ props[:y] ||= Cursor.y
62
+ # print_at("x:#{props[:x]} y:#{props[:y]}", {:x => 0, :y => 10})
63
+ print_at(str, props)
64
+ end
65
+ def print_right(str, props={})
66
+ props[:x] ||= width
67
+ props[:y] ||= Cursor.y
68
+ props[:minus] = true unless props.has_key?(:minus)
69
+ print_at(str, props)
70
+ end
71
+ def print_spaced(*args)
72
+ props = (args.last.is_a? Hash) ? args.pop : {}
73
+ props[:y] = Cursor.y
74
+ chunk_width = (width / args.flatten.size).to_i
75
+ chunk_at = 0
76
+ args.each do |chunk|
77
+ props[:x] = chunk_at
78
+ print_at(chunk.to_s[0, chunk_width], props)
79
+ chunk_at += chunk_width
80
+ end
81
+ puts
82
+ end
83
+ def print_center(str, props={})
84
+ props[:x] = ((width - str.noatt.length) / 2).to_i-1
85
+ props[:y] ||= height
86
+ print_at(str, props)
87
+ end
88
+ def print_at(str, props={})
89
+ print_at_lamb = lambda {
90
+ props[:x] ||= 0
91
+ props[:y] ||= 0
92
+ props[:minus] = false unless props.has_key?(:minus)
93
+ props[:x] = props[:x]-str.noatt.size if props[:x] && props[:minus] # Subtract the str length from the position
94
+ Cursor.save
95
+ Cursor.move = [props[:x], props[:y]]
96
+ print str
97
+ Cursor.restore
98
+ }
99
+ RUBY_VERSION =~ /1.9/ ? Thread.exclusive(&print_at_lamb) : print_at_lamb.call
100
+ end
101
+
102
+ def self.style(col, bgcol=nil, att=nil)
103
+ valdor = []
104
+ valdor << COLOURS[col] if COLOURS.has_key?(col)
105
+ valdor << BGCOLOURS[bgcol] if BGCOLOURS.has_key?(bgcol)
106
+ valdor << ATTRIBUTES[att] if ATTRIBUTES.has_key?(att)
107
+ "\e[#{valdor.join(";")}m" # => \e[8;34;42m
108
+ end
109
+
110
+ def self.clear
111
+ tput :clear
112
+ end
113
+
114
+ def reset
115
+ tput :reset
116
+ end
117
+
118
+ def width
119
+ tput_val(:cols).to_i
120
+ end
121
+
122
+ def height
123
+ tput_val(:lines).to_i
124
+ end
125
+ end
126
+
127
+ module Cursor #:nodoc:all
128
+ extend self
129
+
130
+ # Returns [x,y] for the current cursor position.
131
+ def position
132
+ yx = [0,0]
133
+
134
+ position_lamb = lambda {
135
+ begin
136
+ # NOTE: Can we get cursor position from tput?
137
+ termsettings = `stty -g`
138
+
139
+ # DEBUGGING: The following code works in Ruby 1.9 but not 1.8.
140
+
141
+ system("stty raw -echo")
142
+ print "\e[6n" # Forces output of: \e[49;1R (\e is not printable)
143
+ c = ''
144
+ (pos ||= '') << c while (c = STDIN.getc) != 'R'# NOTE: There must be a better way!
145
+ yx = pos.scan(/(\d+);(\d+)/).flatten
146
+ yx[0] = yx[0].to_i - 1 # It returns 1 for the first column, but we want 0
147
+ yx[1] = yx[1].to_i - 1
148
+ ensure
149
+ system("stty #{termsettings}") # Get out of raw mode
150
+ end
151
+ }
152
+
153
+ RUBY_VERSION =~ /1.9/ ? Thread.exclusive(&position_lamb) : position_lamb.call
154
+ yx.reverse
155
+ end
156
+
157
+ def x; position[0]; end
158
+ def y; position[1]; end
159
+
160
+ def move=(*args)
161
+ x,y = *args.flatten
162
+ tput(:cup, y, x) # "tput cup" takes y before x
163
+ end
164
+
165
+ def up(n=1)
166
+ tput :cuu, n
167
+ end
168
+
169
+ def down(n=1)
170
+ tput :cud, n
171
+ end
172
+
173
+ def right(x=1)
174
+ tput :cuf, x
175
+ end
176
+
177
+ def left(x=1)
178
+ tput :cub, x
179
+ end
180
+
181
+ def line(n=1)
182
+ tput :il, n
183
+ end
184
+
185
+ def save
186
+ tput :sc
187
+ end
188
+
189
+ def restore
190
+ tput :rc
191
+ end
192
+
193
+ def clear_line
194
+ tput :el
195
+ end
196
+
197
+ # TODO: replace methods with this kinda thing
198
+ #@@capnames = {
199
+ # :restore => [:rc],
200
+ # :save => [:sc],
201
+ # :clear_line => [:el],
202
+ # :line => [:il, 1, 1],
203
+ #
204
+ # :up => [:cuu, 1, 1],
205
+ # :down => [:cud, 1, 1],
206
+ # :right => [:cuf, 1, 1],
207
+ # :left => [:cub, 1, 1],
208
+ #
209
+ # :move => [:cup, 2, 0, 0]
210
+ #}
211
+ #
212
+ #@@capnames.each_pair do |meth, cap|
213
+ # module_eval <<-RUBY
214
+ # def #{meth}(*args)
215
+ # tput '#{cap[0]}'
216
+ # end
217
+ # RUBY
218
+ #end
219
+
220
+ end
221
+
222
+ class Window #:nodoc:all
223
+ attr_accessor :row, :col, :width, :height, :text, :fg, :bg
224
+ attr_reader :threads
225
+
226
+ def initialize(*args)
227
+ @row = 1
228
+ @col = 1
229
+ @width = 10
230
+ @height = 5
231
+ @text = ""
232
+ @fg = :default
233
+ @bg = :default
234
+ @threads = []
235
+ end
236
+
237
+ def position=(x,y=nil)
238
+ @x = x
239
+ @y = y if y
240
+ end
241
+
242
+ def position
243
+ [@row, @col]
244
+ end
245
+
246
+ def self.bar(len, unit='=')
247
+ unit*len
248
+ end
249
+
250
+
251
+ # Execute the given block every +n+ seconds in a separate thread.
252
+ # The lower limit for +n+ is 1 second.
253
+ # Returns a Thread object.
254
+ def every_n_seconds(n)
255
+ #n = 1 if n < 1
256
+ thread = Thread.new do
257
+
258
+ begin
259
+ while true
260
+ before = Time.now
261
+ yield
262
+ interval = n - (Time.now - before)
263
+ sleep(interval) if interval > 0
264
+ end
265
+ rescue Interrupt
266
+ break
267
+ ensure
268
+ thread
269
+ end
270
+ end
271
+ end
272
+
273
+ # Print text to the screen via +type+ every +refresh+ seconds.
274
+ # Print the return value of the block to the screen using the
275
+ # print_+type+ method. +refresh+ is number of seconds to wait
276
+ # +props+ is the hash sent to print_+type+.
277
+ # Returns a Thread object.
278
+ #
279
+ # # Print the time in the upper right corner every second
280
+ # thread1 = Console.static(:right, 1, {:y => 0}) do
281
+ # Time.now.utc.strftime("%Y-%m-%d %H:%M:%S").colour(:blue, :white, :underline)
282
+ # end
283
+ #
284
+ def static(type, refresh=2, props={}, &b)
285
+ meth = "print_#{type}"
286
+ raise "#{meth} is not supported" unless Console.respond_to?(meth)
287
+
288
+ refresh ||= 0
289
+ refreh = refresh.to_s.to_i
290
+
291
+ thread = every_n_seconds(refresh) do
292
+ Console.send(meth, b.call, props.clone)
293
+ end
294
+
295
+ @threads << thread
296
+
297
+ thread
298
+ end
299
+
300
+ def join_threads
301
+ begin
302
+ @threads.each do |t|
303
+ t.join
304
+ end
305
+ rescue Interrupt
306
+ ensure
307
+ @threads.each do |t|
308
+ t.kill
309
+ end
310
+ end
311
+ end
312
+
313
+ end
@@ -0,0 +1,4 @@
1
+
2
+
3
+ require 'drydock/mixins/object'
4
+ require 'drydock/mixins/string'
@@ -0,0 +1,23 @@
1
+ class Object
2
+
3
+ # Executes tput +capnam+ with +args+. Returns true if tcap gives
4
+ # 0 exit status and false otherwise.
5
+ #
6
+ # tput :cup, 1, 4
7
+ # $ tput cup 1 4
8
+ #
9
+ def tput(capnam, *args)
10
+ system("tput #{capnam} #{args.flatten.join(' ')}")
11
+ end
12
+
13
+ # Executes tput +capnam+ with +args+. Returns the output of tput.
14
+ #
15
+ # tput_val :cols # => 16
16
+ # $ tput cols # => 16
17
+ #
18
+ def tput_val(capnam, *args)
19
+ `tput #{capnam} #{args.flatten.join(' ')}`.chomp
20
+ end
21
+ end
22
+
23
+
@@ -0,0 +1,66 @@
1
+ autoload :Console, 'drydock/console'
2
+ class String
3
+ @@print_with_attributes = true
4
+ def String.disable_colour; @@print_with_attributes = false; end
5
+ def String.disable_color; @@print_with_attributes = false; end
6
+ def String.enable_colour; @@print_with_attributes = true; end
7
+ def String.enable_color; @@print_with_attributes = true; end
8
+
9
+ # +col+, +bgcol+, and +attribute+ are symbols corresponding
10
+ # to Console::COLOURS, Console::BGCOLOURS, and Console::ATTRIBUTES.
11
+ # Returns the string in the format attributes + string + defaults.
12
+ #
13
+ # "MONKEY_JUNK".colour(:blue, :white, :blink) # => "\e[34;47;5mMONKEY_JUNK\e[39;49;0m"
14
+ #
15
+ def colour(col, bgcol = nil, attribute = nil)
16
+ return self unless @@print_with_attributes
17
+ Console.style(col, bgcol, attribute) +
18
+ self +
19
+ Console.style(:default, :default, :default)
20
+ end
21
+ alias :color :colour
22
+
23
+ # See colour
24
+ def bgcolour(bgcol = :default)
25
+ return self unless @@print_with_attributes
26
+ Console.style(nil, bgcol, nil) +
27
+ self +
28
+ Console.style(nil, :default, nil)
29
+ end
30
+ alias :bgcolor :bgcolour
31
+
32
+ # See colour
33
+ def att(a = :default)
34
+ return self unless @@print_with_attributes
35
+ Console.style(nil, nil, a) +
36
+ self +
37
+ Console.style(nil, nil, :default)
38
+ end
39
+
40
+ # Shortcut for att(:bright)
41
+ def bright; att(:bright); end
42
+
43
+ # Print the string at +x+ +y+. When +minus+ is any true value
44
+ # the length of the string is subtracted from the value of x
45
+ # before printing.
46
+ def print_at(x=nil, y=nil, minus=false)
47
+ args = {:minus=>minus}
48
+ args[:x] &&= x
49
+ args[:y] &&= y
50
+ Console.print_at(self, args)
51
+ end
52
+
53
+ # Returns the string with ANSI escape codes removed.
54
+ #
55
+ # NOTE: The non-printable attributes count towards the string size.
56
+ # You can use this method to get the "visible" size:
57
+ #
58
+ # "\e[34;47;5mMONKEY_JUNK\e[39;49;0m".noatt.size # => 11
59
+ # "\e[34;47;5mMONKEY_JUNK\e[39;49;0m".size # => 31
60
+ #
61
+ def noatt
62
+ gsub(/\e\[?[0-9;]*[mc]?/, '')
63
+ end
64
+ alias :noansi :noatt
65
+
66
+ end
@@ -0,0 +1,33 @@
1
+ require 'thread'
2
+
3
+ module Drydock
4
+ module Screen
5
+ extend self
6
+
7
+ @@mutex = Mutex.new
8
+ @@output = StringIO.new
9
+ @@offset = 0
10
+
11
+ def print(*msg)
12
+ @@mutex.synchronize do
13
+ @@output.print *msg
14
+ end
15
+ end
16
+
17
+ def puts(*msg)
18
+ @@mutex.synchronize do
19
+ @@output.puts *msg
20
+ end
21
+ end
22
+
23
+ def flush
24
+ @@mutex.synchronize do
25
+ #return if @@offset == @@output.tell
26
+ @@output.seek @@offset
27
+ STDOUT.puts @@output.read unless @@output.eof?
28
+ @@offset = @@output.tell
29
+ end
30
+ end
31
+
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bone
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Delano Mandelbaum
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-13 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: boned
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - "="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.2.0
24
+ version:
25
+ description: Get Bones
26
+ email: delano@solutious.com
27
+ executables:
28
+ - bone
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.md
33
+ - LICENSE.txt
34
+ - CHANGES.txt
35
+ files:
36
+ - CHANGES.txt
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - Rudyfile
41
+ - bin/bone
42
+ - bone.gemspec
43
+ - lib/bone.rb
44
+ - lib/bone/cli.rb
45
+ - try/bone.rb
46
+ - vendor/drydock-0.6.8/CHANGES.txt
47
+ - vendor/drydock-0.6.8/LICENSE.txt
48
+ - vendor/drydock-0.6.8/README.rdoc
49
+ - vendor/drydock-0.6.8/Rakefile
50
+ - vendor/drydock-0.6.8/bin/example
51
+ - vendor/drydock-0.6.8/drydock.gemspec
52
+ - vendor/drydock-0.6.8/lib/drydock.rb
53
+ - vendor/drydock-0.6.8/lib/drydock/console.rb
54
+ - vendor/drydock-0.6.8/lib/drydock/mixins.rb
55
+ - vendor/drydock-0.6.8/lib/drydock/mixins/object.rb
56
+ - vendor/drydock-0.6.8/lib/drydock/mixins/string.rb
57
+ - vendor/drydock-0.6.8/lib/drydock/screen.rb
58
+ has_rdoc: true
59
+ homepage: ""
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options:
64
+ - --line-numbers
65
+ - --title
66
+ - Get Bones
67
+ - --main
68
+ - README.rdoc
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: "0"
76
+ version:
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
82
+ version:
83
+ requirements: []
84
+
85
+ rubyforge_project: bone
86
+ rubygems_version: 1.3.5
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Get Bones
90
+ test_files: []
91
+