90 lines
2.8 KiB
Python
90 lines
2.8 KiB
Python
|
# -*- coding: utf-8 -*-
|
||
|
#
|
||
|
# Copyright Grégory Soutadé
|
||
|
|
||
|
# This file is part of SOAdvancedDissector
|
||
|
|
||
|
# SOAdvancedDissector is free software: you can redistribute it and/or modify
|
||
|
# it under the terms of the GNU General Public License as published by
|
||
|
# the Free Software Foundation, either version 3 of the License, or
|
||
|
# (at your option) any later version.
|
||
|
#
|
||
|
# SOAdvancedDissector is distributed in the hope that it will be useful,
|
||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
# GNU General Public License for more details.
|
||
|
#
|
||
|
# You should have received a copy of the GNU General Public License
|
||
|
# along with SOAdvancedDissector. If not, see <http://www.gnu.org/licenses/>.
|
||
|
#
|
||
|
|
||
|
import sys
|
||
|
|
||
|
class ProgressionDisplay():
|
||
|
"""Class that manage nice display
|
||
|
with progression continuous update in percent"""
|
||
|
|
||
|
def __init__(self):
|
||
|
self.reset(False)
|
||
|
|
||
|
def reset(self, printEmptyLine=False):
|
||
|
"""Reset display and optionaly print an empty line"""
|
||
|
self.message = ''
|
||
|
self.target_progression = 0
|
||
|
self.cur_progression = 0
|
||
|
self.watermark = 0
|
||
|
self.cur_watermark = 0
|
||
|
if printEmptyLine:
|
||
|
print('')
|
||
|
|
||
|
def setTarget(self, message, target=0, watermark=0):
|
||
|
"""New progression to display
|
||
|
|
||
|
Parameters
|
||
|
----------
|
||
|
message : str
|
||
|
Message to display
|
||
|
|
||
|
target (Optional) : int
|
||
|
Max progression value, if not set, no percent will be displayed
|
||
|
|
||
|
watermark (Optional) : int
|
||
|
Only display message when progression >= watermark
|
||
|
"""
|
||
|
self.reset()
|
||
|
self.message = message
|
||
|
self.target_progression = target
|
||
|
self.watermark = watermark
|
||
|
|
||
|
def progress(self, increment):
|
||
|
"""Increment progression and display new one"""
|
||
|
self.cur_progression += increment
|
||
|
self.cur_watermark += increment
|
||
|
|
||
|
if self.cur_progression > self.target_progression:
|
||
|
self.cur_progression = self.target_progression
|
||
|
|
||
|
if self.cur_watermark < self.watermark and\
|
||
|
self.cur_progression < self.target_progression:
|
||
|
return
|
||
|
|
||
|
display = ''
|
||
|
if self.target_progression:
|
||
|
cur_percent = int((self.cur_progression*100)/self.target_progression)
|
||
|
display = '{} [{}%]'.format(self.message, cur_percent)
|
||
|
else:
|
||
|
display = '{}'.format(self.message)
|
||
|
|
||
|
sys.stdout.write('\r{}\r'.format(' ' * (len(display)+5))) # First, clear line
|
||
|
sys.stdout.write(display)
|
||
|
sys.stdout.flush()
|
||
|
|
||
|
def finish(self):
|
||
|
"""End of progression, ensure we display "100%" value"""
|
||
|
if self.target_progression:
|
||
|
self.progress(self.target_progression) # Print 100%
|
||
|
print('')
|
||
|
else:
|
||
|
pass # Do nothing
|
||
|
|