Class: Notice
- Inherits:
-
Object
- Object
- Notice
- Defined in:
- lib/notice.rb
Constant Summary collapse
- STATES =
{ :busy => "\x1B[1m[\x1B[0m\x1B[34mBUSY\x1B[0m\x1B[1m]\x1B[0m", :fail => "\x1B[1m[\x1B[0m\x1B[1;31mFAIL\x1B[0m\x1B[1m]\x1B[0m", :done => "\x1B[1m[\x1B[0m\x1B[1;32mDONE\x1B[0m\x1B[1m]\x1B[0m" }
- @@STACK =
{}
- @@width =
nil
Instance Method Summary collapse
- #create_line(name) ⇒ Object
- #get_width ⇒ Object
-
#initialize(name, &block) ⇒ Notice
constructor
A new instance of Notice.
Constructor Details
#initialize(name, &block) ⇒ Notice
Returns a new instance of Notice.
11 12 13 14 15 16 17 18 |
# File 'lib/notice.rb', line 11 def initialize(name, &block) @@width ||= get_width line = create_line(name.dup) @@STACK[name.to_s] ||= lambda { true } @@STACK[name.to_s] = block if block STDOUT.print line + STATES[:busy] STDOUT.print "\r#{line}#{(@@STACK[name.to_s].call ? STATES[:done] : STATES[:fail])}\n" end |
Instance Method Details
#create_line(name) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/notice.rb', line 20 def create_line(name) name = if name.size > (@@width - 10) name.gsub(/^(.{#{@@width - 10}}).*$/, '\1...') else name.ljust(@@width - 10, ' ') end " \x1B[1;34m::\x1B[0m \x1B[1m#{name}\x1B[0m" end |
#get_width ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/notice.rb', line 29 def get_width default = 80 begin tiocgwinsz = 0x5413 data = [0, 0, 0, 0].pack('SSSS') if STDOUT.ioctl(tiocgwinsz, data) >= 0 rows, cols, xpixels, ypixels = data.unpack('SSSS') (cols >= 0 ? cols : default) else default end rescue Exception => e default end end |