Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import platform
- import shlex
- import struct
- import subprocess
- from threading import Lock
- def get_terminal_size():
- current_os = platform.system()
- tuple_xy = None
- if current_os == 'Windows':
- tuple_xy = _get_terminal_size_windows()
- if tuple_xy is None:
- tuple_xy = _get_terminal_size_tput()
- # needed for window's python in cygwin's xterm!
- if current_os in ['Linux', 'Darwin'] or current_os.startswith('CYGWIN'):
- tuple_xy = _get_terminal_size_linux()
- if tuple_xy is None:
- "default"
- tuple_xy = (80, 25) # default value
- return tuple_xy
- def _get_terminal_size_windows():
- try:
- from ctypes import windll, create_string_buffer
- # stdin handle is -10
- # stdout handle is -11
- # stderr handle is -12
- h = windll.kernel32.GetStdHandle(-12)
- csbi = create_string_buffer(22)
- res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
- if res:
- (bufx, bufy, curx, cury, wattr,
- left, top, right, bottom,
- maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
- sizex = right - left + 1
- sizey = bottom - top + 1
- return sizex, sizey
- except:
- pass
- def _get_terminal_size_tput():
- try:
- cols = int(subprocess.check_call(shlex.split('tput cols')))
- rows = int(subprocess.check_call(shlex.split('tput lines')))
- return cols, rows
- except:
- pass
- def _get_terminal_size_linux():
- def ioctl_GWINSZ(fd):
- try:
- import fcntl
- import termios
- cr = struct.unpack('hh',
- fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
- return cr
- except:
- pass
- cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
- if not cr:
- try:
- fd = os.open(os.ctermid(), os.O_RDONLY)
- cr = ioctl_GWINSZ(fd)
- os.close(fd)
- except:
- pass
- if not cr:
- try:
- cr = (os.environ['LINES'], os.environ['COLUMNS'])
- except:
- return None
- return int(cr[1]), int(cr[0])
- class Progressbar:
- WIDTH = 50
- TEMPLATE = "{bar} | {pos}/{max} - {msg}"
- CHARS = ("[", "▏▎▍▌▋▊▉█", " ", "]")
- def __init__(self, max):
- self.lock = Lock()
- self.max = max
- self.current = 0
- self.twidth = get_terminal_size()[0] - 1
- self.prev_msg = ""
- def log(self, msg):
- """ Safely print full-length message """
- with self.lock:
- if len(msg) < self.twidth:
- msg += " " * (self.twidth - len(msg))
- print(msg)
- self.dump(self.prev_msg)
- def inc(self, n=1, msg=""):
- with self.lock:
- self.current += n
- self.dump(msg or self.prev_msg)
- if msg:
- self.prev_msg = msg
- def reset(self, max=0):
- with self.lock:
- self.current = 0
- self.max = max
- def dump(self, msg=""):
- cnt = 0 if self.max == 0 else round(self.current / self.max * (self.WIDTH * len(self.CHARS[1])))
- r = cnt % len(self.CHARS[1])
- cnt //= len(self.CHARS[1])
- bar = self.CHARS[0] + self.CHARS[1][-1] * (cnt - 1) + self.CHARS[1][r]\
- + self.CHARS[2] * (self.WIDTH - cnt) + self.CHARS[3]
- if msg:
- self.prev_msg = msg
- else:
- msg = self.prev_msg
- t = self.TEMPLATE.format(bar=bar, pos=self.current, max=self.max,
- percent=self.current / self.max * 100,
- msg=msg)
- if len(t) >= self.twidth:
- t = t[:self.twidth][:-3] + "..."
- elif len(t) < self.twidth:
- t += " " * (self.twidth - len(t))
- print(t, end="\r")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement