Commit 55dab825 authored by sumpfralle's avatar sumpfralle

improved progress indicator: none, bar, dot, text


git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@601 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent f8942e03
...@@ -316,6 +316,8 @@ if __name__ == "__main__": ...@@ -316,6 +316,8 @@ if __name__ == "__main__":
if opts.quiet: if opts.quiet:
log.setLevel(logging.WARNING) log.setLevel(logging.WARNING)
# disable the progress bar
opts.progress = "none"
# show version and exit # show version and exit
if opts.show_version: if opts.show_version:
...@@ -343,6 +345,15 @@ if __name__ == "__main__": ...@@ -343,6 +345,15 @@ if __name__ == "__main__":
else: else:
log.info("Psyco was disabled via the commandline") log.info("Psyco was disabled via the commandline")
# initialize the progress bar
progress_styles = {"none": pycam.Gui.Console.ConsoleProgressBar.STYLE_NONE,
"text": pycam.Gui.Console.ConsoleProgressBar.STYLE_TEXT,
"bar": pycam.Gui.Console.ConsoleProgressBar.STYLE_BAR,
"dot": pycam.Gui.Console.ConsoleProgressBar.STYLE_DOT,
}
progress_bar = pycam.Gui.Console.ConsoleProgressBar(sys.stdout,
progress_styles[opts.progress])
if not opts.export_gcode and not opts.export_task_config: if not opts.export_gcode and not opts.export_task_config:
show_gui(inputfile, opts.config_file) show_gui(inputfile, opts.config_file)
else: else:
...@@ -453,7 +464,6 @@ if __name__ == "__main__": ...@@ -453,7 +464,6 @@ if __name__ == "__main__":
if opts.export_gcode: if opts.export_gcode:
# generate the toolpath # generate the toolpath
start_time = time.time() start_time = time.time()
progress_bar = pycam.Gui.Console.ConsoleProgressBar(sys.stdout)
toolpath = pycam.Toolpath.Generator.generate_toolpath_from_settings( toolpath = pycam.Toolpath.Generator.generate_toolpath_from_settings(
model, tps, callback=progress_bar.update) model, tps, callback=progress_bar.update)
progress_bar.finish() progress_bar.finish()
......
...@@ -26,13 +26,52 @@ import os ...@@ -26,13 +26,52 @@ import os
class ConsoleProgressBar(object): class ConsoleProgressBar(object):
def __init__(self, output): STYLE_NONE = 0
STYLE_TEXT = 1
STYLE_BAR = 2
STYLE_DOT = 3
PROGRESS_BAR_LENGTH = 70
def __init__(self, output, style=None):
if style is None:
style = ConsoleProgressBar.STYLE_TEXT
self.output = output self.output = output
self.style = style
self.last_length = 0 self.last_length = 0
self.text = "" self.text = ""
self.percent = 0 self.percent = 0
def _output_current_state(self, progress_happened=True):
if self.style == ConsoleProgressBar.STYLE_TEXT:
text = "%d%% %s" % (self.percent, self.text)
self.last_length = len(text)
elif self.style == ConsoleProgressBar.STYLE_BAR:
bar_length = ConsoleProgressBar.PROGRESS_BAR_LENGTH
hashes = int(bar_length * self.percent / 100.0)
empty = bar_length - hashes
text = "[%s%s]" % ("#" * hashes, "." * empty)
# include a text like " 10% " in the middle
percent_text = " %d%% " % self.percent
start_text = text[:(len(text) - len(percent_text)) / 2]
end_text = text[-(len(text) - len(start_text) - len(percent_text)):]
text = start_text + percent_text + end_text
self.last_length = len(text)
elif self.style == ConsoleProgressBar.STYLE_DOT:
if progress_happened:
text = "."
else:
text = ""
# don't remove any previous characters
self.last_length = 0
else:
raise ValueError("ConsoleProgressBar: invalid style (%d)" \
% self.style)
self.output.write(text)
self.output.flush()
def update(self, text=None, percent=None, **kwargs): def update(self, text=None, percent=None, **kwargs):
if self.style == ConsoleProgressBar.STYLE_NONE:
return
if not text is None: if not text is None:
self.text = text self.text = text
if not percent is None: if not percent is None:
...@@ -40,11 +79,10 @@ class ConsoleProgressBar(object): ...@@ -40,11 +79,10 @@ class ConsoleProgressBar(object):
if self.last_length > 0: if self.last_length > 0:
# delete the previous line # delete the previous line
self.output.write("\x08" * self.last_length) self.output.write("\x08" * self.last_length)
result = "%d%% %s" % (self.percent, self.text) self._output_current_state(progress_happened = (not percent is None))
self.last_length = len(result)
self.output.write(result)
self.output.flush()
def finish(self): def finish(self):
if self.style == ConsoleProgressBar.STYLE_NONE:
return
self.output.write(os.linesep) self.output.write(os.linesep)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment