bumblebee-status/bumblebee_status/modules/contrib/progress.py

112 lines
3.7 KiB
Python
Raw Normal View History

2020-04-10 16:58:19 +02:00
"""
Show progress for cp, mv, dd, ...
Parameters:
* progress.placeholder: Text to display while no process is running (defaults to 'n/a')
2020-04-10 16:58:19 +02:00
* progress.barwidth: Width of the progressbar if it is used (defaults to 8)
* progress.format: Format string (defaults to '{bar} {cmd} {arg}')
2020-05-06 12:57:38 +02:00
Available values are: {bar} {pid} {cmd} {arg} {percentage} {quantity} {speed} {time}
* progress.barfilledchar: Character used to draw the filled part of the bar (defaults to '#'), notice that it can be a string
* progress.baremptychar: Character used to draw the empty part of the bar (defaults to '-'), notice that it can be a string
2020-04-10 16:58:19 +02:00
Requires the following executable:
* progress
contributed by `remi-dupre <https://github.com/remi-dupre>`_ - many thanks!
2020-04-10 16:58:19 +02:00
"""
import core.module
import core.widget
2020-04-10 16:58:19 +02:00
import util.cli
import util.format
2020-04-10 16:58:19 +02:00
import re
2020-04-10 16:58:19 +02:00
class Module(core.module.Module):
def __init__(self, config, theme):
super().__init__(config, theme, core.widget.Widget(self.get_progress_text))
self.__active = False
2020-04-10 16:58:19 +02:00
2022-05-13 11:21:31 +02:00
def hidden(self):
return not self.__active
2020-04-10 16:58:19 +02:00
def get_progress_text(self, widget):
if self.update_progress_info(widget):
width = util.format.asint(self.parameter("barwidth", 8))
count = round((width * widget.get("per")) / 100)
filledchar = self.parameter("barfilledchar", "#")
emptychar = self.parameter("baremptychar", "-")
2020-04-10 16:58:19 +02:00
bar = "[{}{}]".format(filledchar * count, emptychar * (width - count))
str_format = self.parameter("format", "{bar} {cmd} {arg}")
2020-04-10 16:58:19 +02:00
return str_format.format(
bar=bar,
pid=widget.get("pid"),
cmd=widget.get("cmd"),
arg=widget.get("arg"),
percentage=widget.get("per"),
quantity=widget.get("qty"),
speed=widget.get("spd"),
time=widget.get("tim"),
2020-04-10 16:58:19 +02:00
)
else:
return self.parameter("placeholder", "n/a")
2020-04-10 16:58:19 +02:00
def update_progress_info(self, widget):
2022-07-21 08:07:33 +02:00
"""Update widget's information about the copy"""
if not self.__active:
return
2020-04-10 16:58:19 +02:00
# These regex extracts following groups:
# 1. pid
# 2. command
# 3. arguments
2022-07-21 08:07:33 +02:00
# 4. progress (xx.x formatted)
# 5. quantity (.. unit / .. unit formatted)
2020-04-10 16:58:19 +02:00
# 6. speed
# 7. time remaining
extract_nospeed = re.compile(
r"\[ *(\d*)\] ([a-zA-Z]*) (.*)\n\t(\d*\.*\d*)% \((.*)\)\n.*"
)
extract_wtspeed = re.compile(
r"\[ *(\d*)\] ([a-zA-Z]*) (.*)\n\t(\d*\.*\d*)% \((.*)\) (\d*\.\d .*) remaining (\d*:\d*:\d*)\n.*"
)
2020-04-10 16:58:19 +02:00
try:
raw = util.cli.execute("progress -qW 0.1")
2020-04-10 16:58:19 +02:00
result = extract_wtspeed.match(raw)
if not result:
2022-07-21 08:07:33 +02:00
# Abort speed measures
raw = util.cli.execute("progress -q")
2020-04-10 16:58:19 +02:00
result = extract_nospeed.match(raw)
widget.set("spd", "???.? B/s")
widget.set("tim", "??:??:??")
2020-04-10 16:58:19 +02:00
else:
widget.set("spd", result.group(6))
widget.set("tim", result.group(7))
widget.set("pid", int(result.group(1)))
widget.set("cmd", result.group(2))
widget.set("arg", result.group(3))
widget.set("per", float(result.group(4)))
widget.set("qty", result.group(5))
2020-04-10 16:58:19 +02:00
return True
except Exception:
return False
def update(self):
self.__active = bool(util.cli.execute("progress -q"))
2020-04-10 16:58:19 +02:00
def state(self, widget):
if self.__active:
return ["copying", "no-autohide"]
return "pending"
2020-04-10 16:58:19 +02:00
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4