Module: Arachni::Mixins::ProgressBar

Extended by:
ProgressBar
Included in:
ProgressBar, UI::CLI::RPC::Instance, UI::CLI::Utilities
Defined in:
lib/arachni/mixins/progress_bar.rb

Overview

Progress bar and ETA methods

Instance Method Summary collapse

Instance Method Details

#eta(prog, start_time) ⇒ String

Calculates ETA (Estimated Time of Arrival) based on current progress and the start time of the operation.

Parameters:

  • prog (Float)

    current progress percentage

  • start_time (Time)

    start time of the operation

Returns:

  • (String)

    ETA: hour:min:sec



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/arachni/mixins/progress_bar.rb', line 46

def eta( prog, start_time )
    @last_prog ||= prog
    @last_eta  ||= "--:--:--"

    if @last_prog != prog
        elapsed = Time.now - start_time
        eta     = elapsed * 100 / prog - elapsed
        eta_str = format_time( eta )
    else
        eta_str = @last_eta
        prog = @last_prog
    end

    @last_prog = prog
    @last_eta  = eta_str
end

#format_time(t) ⇒ Object

Formats elapsed time to hour:min:sec



29
30
31
32
33
34
35
# File 'lib/arachni/mixins/progress_bar.rb', line 29

def format_time( t )
    t = t.to_i rescue 0
    sec = t % 60
    min = ( t / 60 ) % 60
    hour = t / 3600
    sprintf( "%02d:%02d:%02d", hour, min, sec )
end

#progress_bar(progress, width = 100) ⇒ String

Returns an ASCII progress bar based on the current progress percentage

Parameters:

  • progress (Float)

    progress percentage

  • width (Integer) (defaults to: 100)

    width of the progressbar in characters

Returns:

  • (String)

    70% [=======> ] 100%



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/arachni/mixins/progress_bar.rb', line 71

def progress_bar( progress, width = 100 )
    progress = 100.0 if progress > 100

    bar_prog = progress * width / 100

    bar  = '=' * ( bar_prog.ceil - 1 ).abs + '>'
    pad = ( width - bar_prog )
    bar += ' ' * pad if pad > 0

    "#{progress}% [#{bar}] 100% "
end