2014-12-23 17:32:29 +01:00
|
|
|
require 'dbus'
|
|
|
|
|
2014-12-23 18:19:55 +01:00
|
|
|
module ProgressBar
|
2018-12-28 12:28:41 +01:00
|
|
|
@klasses ||= []
|
|
|
|
class <<self
|
|
|
|
def register klass
|
|
|
|
if klass.possible?
|
|
|
|
@klasses.push klass
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def new *a, **o
|
|
|
|
klass = @klasses.find &:possible?
|
|
|
|
klass.new *a, **o
|
2014-12-23 18:19:55 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ProgressBar::Base
|
2014-12-23 17:32:29 +01:00
|
|
|
attr_reader :max, :i, :text, :error
|
|
|
|
attr_accessor :start
|
2018-12-28 12:28:41 +01:00
|
|
|
def initialize max = nil, text = nil, **_options
|
2014-12-23 17:32:29 +01:00
|
|
|
@start, @max, @i, @text, @error = Time.now, max || 100, 0, text || '', nil
|
2014-12-24 11:46:00 +01:00
|
|
|
at_exit {finish}
|
2014-12-23 17:32:29 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def i= x
|
|
|
|
@i = x.to_i
|
|
|
|
change_progress
|
|
|
|
end
|
|
|
|
|
2014-12-23 19:45:59 +01:00
|
|
|
def max= val
|
|
|
|
@max = val
|
|
|
|
change_progress
|
|
|
|
end
|
|
|
|
|
2014-12-23 17:32:29 +01:00
|
|
|
def increment!( x = nil) self.i += (x || 1) end
|
|
|
|
alias to_i i
|
|
|
|
alias inc! increment!
|
|
|
|
def done_rel() 100.0*i/max end
|
2014-12-23 20:04:26 +01:00
|
|
|
def done_dur() Time.now-@start end
|
2014-12-23 17:32:29 +01:00
|
|
|
|
|
|
|
def total_dur
|
|
|
|
done_dur * max / i
|
|
|
|
end
|
|
|
|
|
|
|
|
def text= x
|
|
|
|
@text = x
|
|
|
|
change_text
|
|
|
|
end
|
|
|
|
|
|
|
|
def error= x
|
|
|
|
@error = x
|
|
|
|
change_error
|
|
|
|
end
|
|
|
|
|
|
|
|
def change_progress() end
|
|
|
|
def change_text() end
|
|
|
|
def change_error() end
|
|
|
|
def finish() end
|
2018-12-28 12:28:41 +01:00
|
|
|
def self.possible?() false end
|
2014-12-23 17:32:29 +01:00
|
|
|
end
|
|
|
|
|
2014-12-23 18:19:55 +01:00
|
|
|
class ProgressBar::Console < ProgressBar::Base
|
2018-12-28 12:28:41 +01:00
|
|
|
def self.possible?
|
|
|
|
STDERR.tty?
|
|
|
|
end
|
|
|
|
|
2014-12-23 17:32:29 +01:00
|
|
|
def initialize *a
|
|
|
|
super *a
|
|
|
|
change_text
|
|
|
|
end
|
|
|
|
|
|
|
|
def format_time t
|
|
|
|
if t.finite?
|
2014-12-23 20:04:26 +01:00
|
|
|
sprintf "%02d:%02d:%02d", t/3600, t/60 % 3600, t % 60
|
2014-12-23 17:32:29 +01:00
|
|
|
else
|
|
|
|
"--:--:--"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def change_text
|
2014-12-23 20:32:06 +01:00
|
|
|
l = [[100.0*i/max, 0].max, 100].min.to_i
|
2014-12-23 17:32:29 +01:00
|
|
|
dd, td = done_dur, total_dur
|
2014-12-23 20:32:06 +01:00
|
|
|
b = ?= * l
|
|
|
|
b[-1] = ?> unless 0 == l or 100 == l
|
|
|
|
STDOUT.printf "\r\e[J%s / %s [%-*s] %s", format_time(dd), format_time(td), 100, b, text
|
2014-12-23 17:32:29 +01:00
|
|
|
end
|
|
|
|
alias change_progress change_text
|
|
|
|
|
|
|
|
def change_error
|
|
|
|
STDERR.printf "\r\e[J%s\n", error
|
|
|
|
change_text
|
|
|
|
end
|
2014-12-23 20:35:56 +01:00
|
|
|
|
|
|
|
def finish
|
|
|
|
STDERR.puts
|
|
|
|
end
|
2014-12-23 17:32:29 +01:00
|
|
|
end
|
|
|
|
|
2018-12-28 12:28:41 +01:00
|
|
|
class ProgressBar::KDE < ProgressBar::Base
|
|
|
|
def self.possible?
|
|
|
|
'KDE' == ENV['XDG_CURRENT_DESKTOP']
|
|
|
|
end
|
|
|
|
attr_reader :bus, :service, :view
|
|
|
|
|
|
|
|
def initialize max, text = nil, title = nil, app_name: nil, app_icon_name: nil, unit: nil, is_cancable: nil, is_suspendable: nil, unit: nil, **_options
|
|
|
|
super max, text
|
|
|
|
text = @text
|
|
|
|
text = nil if text.nil? or text.empty?
|
|
|
|
title = nil if title.nil? or title.empty?
|
|
|
|
@unit = '' if unit.nil? or unit.empty?
|
|
|
|
@errors = []
|
|
|
|
@bus = DBus::SessionBus.instance
|
|
|
|
@service = @bus['org.kde.JobViewServer']
|
|
|
|
jvs = @service['/JobViewServer']['org.kde.JobViewServer']
|
|
|
|
flags = (is_cancable ? 0x1 : 0) | (is_suspendable ? 0x2 : 0)
|
|
|
|
view_path = jvs.requestView app_name || $0, app_icon_name || '', flags
|
|
|
|
@view = @service[view_path]['org.kde.JobViewV2']
|
|
|
|
@view.setTotalAmount max, unit
|
|
|
|
end
|
|
|
|
|
|
|
|
def change_text
|
|
|
|
@view.setInfoMessage text
|
|
|
|
end
|
|
|
|
|
|
|
|
def change_progress
|
|
|
|
@view.setProcessedAmount i, unit
|
|
|
|
@view.setPercentage done_rel
|
|
|
|
end
|
|
|
|
|
|
|
|
def change_error
|
|
|
|
@view.setError error
|
|
|
|
end
|
|
|
|
|
|
|
|
def finish
|
|
|
|
@view.terminate
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-23 18:19:55 +01:00
|
|
|
class ProgressBar::KDialog < ProgressBar::Base
|
2018-12-28 12:28:41 +01:00
|
|
|
def self.possible?
|
|
|
|
path = Pathname.new `which kdialog`.chomp
|
|
|
|
$?.exitstatus and path.executable?
|
|
|
|
end
|
|
|
|
|
2014-12-23 17:32:29 +01:00
|
|
|
attr_reader :dialog_service_path, :dialog_object_path, :errors, :dialog_object
|
2018-12-28 12:28:41 +01:00
|
|
|
|
|
|
|
def initialize max, text = nil, title = nil, **_options
|
2014-12-23 19:45:59 +01:00
|
|
|
super max, text
|
2014-12-23 23:02:07 +01:00
|
|
|
text = @text
|
2018-12-28 12:28:41 +01:00
|
|
|
text = nil if text.nil? or text.empty?
|
2014-12-24 11:46:00 +01:00
|
|
|
title = nil if title.nil? or title.empty?
|
2014-12-23 17:32:29 +01:00
|
|
|
@errors = []
|
2018-12-28 12:28:41 +01:00
|
|
|
args = ['kdialog', title ? ['--title', title] : [], '--progressbar', text || '.', max].flatten.compact.map &:to_s
|
2014-12-23 17:32:29 +01:00
|
|
|
@dialog_service_path, @dialog_object_path = IO.popen( args, 'r', &:readlines).join("\n").split ' '
|
|
|
|
@dialog_bus = DBus.session_bus
|
|
|
|
@dialog_service = @dialog_bus[@dialog_service_path]
|
|
|
|
@dialog_object = @dialog_service.object @dialog_object_path
|
|
|
|
@dialog_object.introspect
|
|
|
|
@dialog_object.showCancelButton true
|
2014-12-23 23:02:07 +01:00
|
|
|
change_text if text.nil?
|
2014-12-23 17:32:29 +01:00
|
|
|
change_progress
|
|
|
|
rescue DBus::Error
|
|
|
|
raise Interrupt if $!.name == 'org.freedesktop.DBus.Error.ServiceUnknown'
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.kdialog *a
|
|
|
|
windowid = ENV['WINDOWID']
|
|
|
|
windowid = (windowid.is_a?(String) && !windowid.empty?) ? ['--attach', windowid] : []
|
|
|
|
system 'kdialog', *windowid, *a
|
|
|
|
end
|
|
|
|
def kdialog(*a) self.class.kdialog *a end
|
|
|
|
|
|
|
|
def change_progress()
|
|
|
|
@dialog_object.Set '', 'value', i
|
|
|
|
raise Interrupt if @dialog_object.wasCancelled.first
|
|
|
|
rescue DBus::Error
|
|
|
|
raise Interrupt if $!.name == 'org.freedesktop.DBus.Error.ServiceUnknown'
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
|
|
|
|
def change_text()
|
|
|
|
@dialog_object.setLabelText text
|
|
|
|
raise Interrupt if @dialog_object.wasCancelled.first
|
|
|
|
rescue DBus::Error
|
|
|
|
raise Interrupt if $!.name == 'org.freedesktop.DBus.Error.ServiceUnknown'
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
|
|
|
|
def change_error() @errors.push error end
|
|
|
|
def finish()
|
|
|
|
@dialog_object.close rescue DBus::Error
|
|
|
|
kdialog '--detailederror', "Some errors occured:", errors.join( "<br/>\n") unless errors.empty?
|
|
|
|
end
|
2014-12-23 19:45:59 +01:00
|
|
|
|
|
|
|
def max= val
|
|
|
|
@dialog_object.Set '', 'maximum', val
|
|
|
|
end
|
|
|
|
|
|
|
|
def max
|
|
|
|
@dialog_object.Get '', 'maximum'
|
|
|
|
end
|
2014-12-23 17:32:29 +01:00
|
|
|
end
|
2018-12-28 12:28:41 +01:00
|
|
|
|
|
|
|
ProgressBar.register ProgressBar::Console
|
|
|
|
ProgressBar.register ProgressBar::KDE
|
|
|
|
ProgressBar.register ProgressBar::KDialog
|