# -*- coding: utf-8 -*-

import sys
import time


def get_terminal_size():
    if sys.platform in ('linux2', 'darwin'):
        import fcntl, termios, struct
        try:
            s = struct.unpack('hh', fcntl.ioctl(0, termios.TIOCGWINSZ, '1234'))
            return s
        except IOError:
            pass

    # Default to 24 by 80
    return 24, 80


def to_hms(delta):
    hours, remainder = divmod(int(delta), 3600)
    minutes, seconds = divmod(remainder, 60)

    return hours, minutes, seconds


def format_hms(delta):
    if delta is not None:
        return '{:02d}:{:02d}:{:02d}'.format(*to_hms(delta))
    else:
        return '--:--:--'


class ProgressBar(object):
    _dispfmt = '{:7.1%} [{}{}>{}] {:.{dps}f}/{:.{dps}f} ela: {} rem: {}'

    # Minimum time in seconds between updates
    _mindelta = 0.1

    def __init__(self, start, curr, end, dps=2):
        self.ststrt = start
        self.strtrt = curr
        self.stend = end
        self.dps = dps

        self._wstart = time.time()
        self._last_wallt = 0.0

        self._ncol = get_terminal_size()[1]
        self._nbarcol = self._ncol - 24 - 2*len('{:.{}f}'.format(end, dps))

        self.advance_to(curr)

    def advance_to(self, t):
        self.stcurr = min(t, self.stend)
        self.stelap = self.stcurr - self.strtrt

        self._render()

        if self.stcurr == self.stend:
            sys.stderr.write('\n')

    @property
    def walltime(self):
        return time.time() - self._wstart

    def _render(self):
        wallt = self.walltime
        delta = wallt - self._last_wallt

        # If we have rendered recently then do not do so again
        if delta < self._mindelta and self.stcurr != self.stend:
            return

        # Starting, current, elapsed and ending simulation times
        st, cu, el, en = self.ststrt, self.stcurr, self.stelap, self.stend

        # Relative times
        rcu, ren = cu - st,  en - st

        # Fraction of the simulation we've completed
        frac = float(rcu) / ren

        # Elapsed and estimated remaining wall time
        wela = format_hms(wallt)
        wrem = format_hms(wallt*(en - cu)/el if self.stelap > 0 else None)

        # Decide how many '+', '=' and ' ' to output for the progress bar
        n = self._nbarcol - len(wela) - len(wrem) - 1
        nps = int(n * (rcu - el)/ren)
        neq = int(n * el/ren)
        nsp = n - nps - neq

        # Render the progress bar
        s = self._dispfmt.format(frac, '+'*nps, '='*neq, ' '*nsp, cu, en,
                                 wela, wrem, dps=self.dps)

        # Write the progress bar and pad the remaining columns
        sys.stderr.write('\x1b[2K\x1b[G')
        sys.stderr.write(s)
        sys.stderr.flush()

        # Update the last render time
        self._last_wallt = wallt

@@ -26,16 +26,30 @@
 import s
+hutil%0Aimport s
 ys%0Aimpor
@@ -58,318 +58,8 @@
 time
-%0A%0A%0Adef get_terminal_size():%0A    if sys.platform in ('linux2', 'darwin'):%0A        import fcntl, termios, struct%0A        try:%0A            s = struct.unpack('hh', fcntl.ioctl(0, termios.TIOCGWINSZ, '1234'))%0A            return s%0A        except IOError:%0A            pass%0A%0A    # Default to 24 by 80%0A    return 24, 80
 %0A%0A%0Ad
@@ -764,16 +764,23 @@
 _ncol = 
+shutil.
 get_term
@@ -795,10 +795,16 @@
 e()%5B
-1%5D
+0%5D or 80
 %0A   

Switch to the get_terminal_size in shutil.