Skip to content
Snippets Groups Projects
Unverified Commit cf293d0e authored by nandahkrishna's avatar nandahkrishna
Browse files

livecheck: progress bar for JSON output

parent e7ab97a8
No related branches found
No related tags found
No related merge requests found
Showing
with 432 additions and 29 deletions
require 'ruby-progressbar/output'
class ProgressBar
module Outputs
class NonTty < Output
DEFAULT_FORMAT_STRING = '%t: |%b|'.freeze
def clear
self.last_update_length = 0
stream.print "\n"
end
def last_update_length
@last_update_length ||= 0
end
def bar_update_string
formatted_string = bar.to_s
formatted_string = formatted_string[0...-1] unless bar.finished?
output_string = formatted_string[last_update_length..-1]
self.last_update_length = formatted_string.length
output_string.to_s
end
def default_format
DEFAULT_FORMAT_STRING
end
def resolve_format(*)
default_format
end
def refresh_with_format_change(*); end
def eol
bar.stopped? ? "\n" : ''
end
protected
attr_writer :last_update_length
end
end
end
require 'ruby-progressbar/output'
class ProgressBar
module Outputs
class Null < Output
alias refresh_with_format_change with_refresh
def clear; end
def log(_string); end
def refresh(*); end
def clear_string
''
end
def bar_update_string
''
end
def default_format
''
end
def resolve_format(_format)
''
end
def eol
''
end
end
end
end
require 'ruby-progressbar/output'
class ProgressBar
module Outputs
class Tty < Output
DEFAULT_FORMAT_STRING = '%t: |%B|'.freeze
alias refresh_with_format_change with_refresh
def clear
stream.print clear_string
stream.print "\r"
end
def bar_update_string
bar.to_s
end
def default_format
DEFAULT_FORMAT_STRING
end
def resolve_format(other_format)
other_format || default_format
end
def eol
bar.stopped? ? "\n" : "\r"
end
end
end
end
require 'ruby-progressbar/errors/invalid_progress_error'
class ProgressBar
class Progress
DEFAULT_TOTAL = 100
DEFAULT_BEGINNING_POSITION = 0
DEFAULT_SMOOTHING = 0.1
attr_reader :total,
:progress
attr_accessor :starting_position,
:running_average,
:smoothing
def initialize(options = {})
self.total = options.fetch(:total, DEFAULT_TOTAL)
self.smoothing = options[:smoothing] || DEFAULT_SMOOTHING
start :at => DEFAULT_BEGINNING_POSITION
end
def start(options = {})
self.running_average = 0
self.progress = \
self.starting_position = options[:at] || progress
end
def finish
self.progress = total unless unknown?
end
def finished?
@progress == @total
end
def increment
if progress == total
warn "WARNING: Your progress bar is currently at #{progress} out of #{total} " \
"and cannot be incremented. In v2.0.0 this will become a " \
"ProgressBar::InvalidProgressError."
end
self.progress += 1 unless progress == total
end
def decrement
if progress == 0
warn "WARNING: Your progress bar is currently at #{progress} out of #{total} " \
"and cannot be decremented. In v2.0.0 this will become a " \
"ProgressBar::InvalidProgressError."
end
self.progress -= 1 unless progress == 0
end
def reset
start :at => starting_position
end
def progress=(new_progress)
if total && new_progress > total
fail ProgressBar::InvalidProgressError,
"You can't set the item's current value to be greater than the total."
end
@progress = new_progress
self.running_average = Calculators::RunningAverage.calculate(running_average,
absolute,
smoothing)
end
def total=(new_total)
unless progress.nil? || new_total.nil? || new_total >= progress
fail ProgressBar::InvalidProgressError,
"You can't set the item's total value to less than the current progress."
end
@total = new_total
end
def percentage_completed
return 0 if total.nil?
return 100 if total == 0
# progress / total * 100
#
# Doing this way so we can avoid converting each
# number to a float and then back to an integer.
#
(progress * 100 / total).to_i
end
def none?
running_average.zero? || progress.zero?
end
def unknown?
progress.nil? || total.nil?
end
def total_with_unknown_indicator
total || '??'
end
def percentage_completed_with_precision
return 100.0 if total == 0
return 0.0 if total.nil?
'%5.2f' % [(progress * 100 / total.to_f * 100).floor / 100.0]
end
def absolute
progress - starting_position
end
end
end
require 'ruby-progressbar/refinements/enumerator'
class ProgressBar
module Refinements
module Enumerator
refine ::Enumerator do
def with_progressbar(options = {}, &block)
chain = ::Enumerator.new do |yielder|
progress_bar = ProgressBar.create(options.merge(:starting_at => 0, :total => size))
each do |*args|
yielder.yield(*args).tap do
progress_bar.increment
end
end
end
return chain unless block_given?
chain.each(&block)
end
end
end
end
end
class ProgressBar
class Throttle
attr_accessor :rate,
:started_at,
:stopped_at,
:timer
def initialize(options = {})
self.rate = options[:throttle_rate] || 0.01
self.started_at = nil
self.stopped_at = nil
self.timer = options.fetch(:throttle_timer, Timer.new)
end
def choke(options = {})
return unless !timer.started? ||
options.fetch(:force_update_if, false) ||
timer.elapsed_seconds >= rate
timer.restart
yield
end
end
end
# rubocop:disable Style/InlineComment
class ProgressBar
class Time
TIME_MOCKING_LIBRARY_METHODS = [
:__simple_stub__now, # ActiveSupport
:now_without_mock_time, # Timecop
:now_without_delorean, # Delorean
:now # Unmocked
].freeze
def initialize(time = ::Time)
self.time = time
end
def now
time.__send__(unmocked_time_method)
end
def unmocked_time_method
@unmocked_time_method ||= begin
TIME_MOCKING_LIBRARY_METHODS.find do |method|
time.respond_to? method
end
end
end
protected
attr_accessor :time
end
end
# rubocop:enable Style/InlineComment
require 'ruby-progressbar/time'
class ProgressBar
class Timer
attr_accessor :started_at,
:stopped_at
def initialize(options = {})
self.time = options[:time] || ::ProgressBar::Time.new
end
def start
self.started_at = stopped? ? time.now - (stopped_at - started_at) : time.now
self.stopped_at = nil
end
def stop
return unless started?
self.stopped_at = time.now
end
def pause
stop
end
def resume
start
end
def started?
started_at
end
def stopped?
stopped_at
end
def reset
self.started_at = nil
self.stopped_at = nil
end
def reset?
!started_at
end
def restart
reset
start
end
def elapsed_seconds
((stopped_at || time.now) - started_at)
end
def elapsed_whole_seconds
elapsed_seconds.floor
end
def divide_seconds(seconds)
hours, seconds = seconds.divmod(3600)
minutes, seconds = seconds.divmod(60)
[hours, minutes, seconds]
end
protected
attr_accessor :time
end
end
class ProgressBar
VERSION = '1.10.1'.freeze
end
......@@ -356,7 +356,18 @@ _brew_livecheck() {
local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in
-*)
__brewcomp "--verbose --quiet --debug --full-name --tap --installed --json --all --newer-only --help"
__brewcomp "
--full-name
--tap
--all
--installed
--newer-only
--json
--quiet
--debug
--verbose
--help
"
return
;;
esac
......
......@@ -448,16 +448,16 @@ __fish_brew_complete_arg 'list ls;
__fish_brew_complete_cmd 'livecheck' "Check for newer versions of formulae from upstream"
__fish_brew_complete_arg 'livecheck' -a '(__fish_brew_suggest_formulae_all)'
__fish_brew_complete_arg 'livecheck' -s v -l verbose -d "Make some output more verbose"
__fish_brew_complete_arg 'livecheck' -s q -l quiet -d "Suppress any warnings"
__fish_brew_complete_arg 'livecheck' -s d -l debug -d "Display any debugging information"
__fish_brew_complete_arg 'livecheck' -l full-name -d "Print formulae with fully-qualified name"
__fish_brew_complete_arg 'livecheck' -l tap -d "Check the formulae within the given tap, specified as user/repo"
__fish_brew_complete_arg 'livecheck' -l installed -d "Check formulae that are currently installed"
__fish_brew_complete_arg 'livecheck' -l json -d "Output information in JSON format"
__fish_brew_complete_arg 'livecheck' -l all -d "Check all available formulae"
__fish_brew_complete_arg 'livecheck' -l newer-only -d "Show the latest version only if it is newer than the formula"
__fish_brew_complete_arg 'livecheck' -s h -l help -d "Show the help message"
__fish_brew_complete_arg 'livecheck' -l full-name -d "Print formulae with fully-qualified name"
__fish_brew_complete_arg 'livecheck' -l tap -d "Check the formulae within the given tap, specified as user/repo"
__fish_brew_complete_arg 'livecheck' -l all -d "Check all available formulae"
__fish_brew_complete_arg 'livecheck' -l installed -d "Check formulae that are currently installed"
__fish_brew_complete_arg 'livecheck' -l newer-only -d "Show the latest version only if it's newer than the formula"
__fish_brew_complete_arg 'livecheck' -l json -d "Output information in JSON format"
__fish_brew_complete_arg 'livecheck' -s q -l quiet -d "Suppress warnings, don't print a progress bar for JSON output"
__fish_brew_complete_arg 'livecheck' -s d -l debug -d "Display any debugging information"
__fish_brew_complete_arg 'livecheck' -s v -l verbose -d "Make some output more verbose"
__fish_brew_complete_arg 'livecheck' -s h -l help -d "Show the help message"
__fish_brew_complete_cmd 'log' "Show git log for formula"
......
......@@ -508,15 +508,15 @@ _brew_list() {
# [--installed] [--json] [--all] [--newer-only] formulae
_brew_livecheck() {
_arguments \
'(--verbose,-v)'{--verbose,-v}'[Make some output more verbose]' \
'(--quiet,-q)'{--quiet,-q}'[Suppress any warnings]' \
'(--debug,-d)'{--debug,-d}'[Display any debugging information]' \
'--full-name[Print formulae with fully-qualified name]' \
'--tap[Check the formulae within the given tap, specified as user/repo]' \
'--installed[Check formulae that are currently installed]' \
'--json[Output information in JSON format]' \
'--all[Check all available formulae]' \
'--installed[Check formulae that are currently installed]' \
'--newer-only[Show the latest version only if it is newer than the formula]' \
'--json[Output information in JSON format]' \
'(--quiet,-q)'{--quiet,-q}'[Suppress warnings, do not print a progress bar for JSON output]' \
'(--debug,-d)'{--debug,-d}'[Display any debugging information]' \
'(--verbose,-v)'{--verbose,-v}'[Make some output more verbose]' \
'(--help,-h)'{--help,-h}'[Show the help message]' \
'*:: :__brew_formulae'
}
......@@ -590,7 +590,7 @@ _brew_pull() {
'--resolve[allow user to resolve patches that fail to apply]' \
'--branch-okay[do not warn on pulling branches other than master]' \
'--no-pbcopy[do not copy anything to the system]' \
'--no-publish[do no publish bottles to Bintray]' \
'--no-publish[do not publish bottles to Bintray]' \
'*:patch source: '
}
......
......@@ -1034,15 +1034,17 @@ or `~/.brew_livecheck_watchlist`.
* `--full-name`:
Print formulae with fully-qualified names.
* `--tap`:
Check the formulae within the given tap, specified as *`user`*`/`*`repo`*.
* `--installed`:
Check formulae that are currently installed.
* `--json`:
Output informations in JSON format.
Check formulae within the given tap, specified as *`user`*`/`*`repo`*.
* `--all`:
Check all available formulae.
* `--installed`:
Check formulae that are currently installed.
* `--newer-only`:
Show the latest version only if it's newer than the formula.
* `--json`:
Output information in JSON format.
* `-q`, `--quiet`:
Suppress warnings, don't print a progress bar for JSON output.
### `man` [*`options`*]
......
......@@ -1428,23 +1428,27 @@ Print formulae with fully\-qualified names\.
.
.TP
\fB\-\-tap\fR
Check the formulae within the given tap, specified as \fIuser\fR\fB/\fR\fIrepo\fR\.
Check formulae within the given tap, specified as \fIuser\fR\fB/\fR\fIrepo\fR\.
.
.TP
\fB\-\-all\fR
Check all available formulae\.
.
.TP
\fB\-\-installed\fR
Check formulae that are currently installed\.
.
.TP
\fB\-\-json\fR
Output informations in JSON format\.
\fB\-\-newer\-only\fR
Show the latest version only if it\'s newer than the formula\.
.
.TP
\fB\-\-all\fR
Check all available formulae\.
\fB\-\-json\fR
Output information in JSON format\.
.
.TP
\fB\-\-newer\-only\fR
Show the latest version only if it\'s newer than the formula\.
\fB\-q\fR, \fB\-\-quiet\fR
Suppress warnings, don\'t print a progress bar for JSON output\.
.
.SS "\fBman\fR [\fIoptions\fR]"
Generate Homebrew\'s manpages\.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment