def right_bar(percent, width): ''' See left_bar(); inverse and in red instead. ''' blocks = int(percent * width) remain = (percent * width) - blocks part = int(remain * 8) rpart = unichr(0x258f - part) # for smooth bar if part == 0: rpart = ' ' return colors.red('+' * (blocks), bg='red') + (colors.red(rpart)) + (' ' * (width - blocks))
def draw_bars(self): up = min(1.0, self._bandwidth[-1][0] / self._max) dn = min(1.0, self._bandwidth[-1][1] / self._max) kbup = self._bandwidth[-1][1] / 1024.0 kbdn = self._bandwidth[-1][0] / 1024.0 status = ' ' + colors.green('%.2f' % kbdn) status += '/' status += colors.red('%.2f' % kbup) # + ' KiB write' status += ' KiB/s' status += ' (%d streams, %d circuits)' % (self.streams(), self.circuits()) # include the paths of any currently-active streams streams = '' for stream in self._state.streams.values(): # ...there's a window during which it may not be attached yet if stream.circuit: circpath = '>'.join( map(lambda r: r.location.countrycode, stream.circuit.path)) streams += ' ' + circpath if len(streams) > 24: streams = streams[:21] + '...' print( left_bar(up, 20) + unichr(0x21f5) + right_bar(dn, 20) + status + streams)
def string_for_circuit(state, circuit): # path = '->'.join(map(lambda x: x.location.countrycode or '??', circuit.path)) path = '->'.join(map(lambda x: x.name, circuit.path)) state = circuit.state if state.lower() == 'failed': state = colors.red(state) return 'Circuit %d (%s) is %s for purpose "%s"' % (circuit.id, path, state, circuit.purpose)
def draw_bars(self): up = min(1.0, self._bandwidth[-1][0] / self._max) dn = min(1.0, self._bandwidth[-1][1] / self._max) kbup = self._bandwidth[-1][1] / 1024.0 kbdn = self._bandwidth[-1][0] / 1024.0 status = ' ' + colors.green('%.2f' % kbdn) + '/' # + ' KiB read' status += ', ' + colors.red('%.2f' % kbup) # + ' KiB write' status += ' (%d streams, %d circuits)' % (self.streams(), self.circuits()) # include the paths of any currently-active streams streams = '' for stream in self._state.streams.values(): # ...there's a window during which it may not be attached yet if stream.circuit: circpath = '>'.join(map(lambda r: r.location.countrycode, stream.circuit.path)) streams += ' ' + circpath if len(streams) > 24: streams = streams[:21] + '...' print(left_bar(up, 20) + unichr(0x21f5) + right_bar(dn, 20) + status + streams)
def stream_failed(self, stream, remote_reason='', **kw): print('Stream %d %s because "%s"' % (stream.id, colors.red('failed'), colors.red(remote_reason)))
def error(fail): print(colors.red('Error:'), fail.getErrorMessage())
def circuit_closed(self, circuit, **kw): print('Circuit %d %s lasted %s (%s).' % (circuit.id, colors.red('closed'), humanize.time.naturaldelta(circuit.age()), flags(kw)))