Class: BuildTool::Progressbar

Inherits:
Logging::Appender
  • Object
show all
Defined in:
lib/build-tool/command_actions.rb

Instance Method Summary collapse

Constructor Details

#initialize(title, &block) ⇒ Progressbar

Returns a new instance of Progressbar.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/build-tool/command_actions.rb', line 9

def initialize( title, &block )
    super( 'Progressbar', :level => :DEBUG )
    @pbar = nil
    @oldlogger = nil
    if Logging.appenders['stdout'].level >= ::Logging::level_num(:INFO)
        # We only do the progressbar thing if there is no verbose output active.
        begin
            # Remove the old stdout logger.
            @oldlogger = Logging.appenders[ 'stdout' ]
            Logging.logger[ 'root' ].remove_appenders( 'stdout' )
            Logging.logger[ 'root' ].add_appenders( self )
            # Add the progressbar logger
            @pbar = ANSI::Progressbar.new( title, 100 )
            yield
        ensure
            @pbar.finish
            # Reset the logger
            Logging.logger[ 'root' ].remove_appenders( 'Progressbar' )
            Logging.logger[ 'root' ].add_appenders( @oldlogger )
        end
    else
        # If there is verbose output just print the text
        logger.info( title )
        yield
    end

end

Instance Method Details

#write(event) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/build-tool/command_actions.rb', line 37

def write( event )
    message = event.data
    return if message.empty?

    case event.level
    when ::Logging::level_num( :ERROR )
    when ::Logging::level_num( :WARN )
        # This should never happen. In case of errors an exception is thrown which should
        # be catched in the initialize() method above. And this logger is removed there.
        raise NotImplementedError

    else
        match = /^\[ *(\d+)%\]/.match( message )
        if match
            # puts match[ 1 ]
            @pbar.set( match[ 1 ].to_i )
        end
    end
    self
end