48 Commits

Author SHA1 Message Date
Kenneth Reitz 53ec4c2e09 Merge branch 'develop' 2011-09-24 14:38:05 -04:00
Kenneth Reitz 7b6138eda6 v0.2.5 2011-09-24 14:36:30 -04:00
Kenneth Reitz d19650b93c add userpath and environ var expansion to expand_path 2011-09-24 14:33:13 -04:00
Kenneth Reitz 98edec43e9 move expand_path to utils 2011-09-24 14:32:47 -04:00
Kenneth Reitz ef48a47b8d Merge branch 'master' into develop 2011-09-24 14:30:05 -04:00
Kenneth Reitz 3be47add4a rename arguments for bar 2011-08-12 09:48:24 -04:00
Kenneth Reitz bf99084ece Merge pull request #18 from maraujop/develop
Adding kwargs to progress bar, so that bar chars can be customized
2011-08-12 06:46:42 -07:00
Miguel Araujo Perez 705cda4443 Adding kwargs to progress bar, so that bar_empty_char and bar_filled_char can be easily customized. 2011-08-11 16:19:48 +02:00
Kenneth Reitz 79cf7a023b Merge branch 'develop' 2011-06-25 12:28:03 -04:00
Kenneth Reitz 7900853dbc v0.2.4 2011-06-25 12:27:44 -04:00
Kenneth Reitz 96827698df Update colorama to v0.2.3 2011-06-21 18:44:46 -04:00
Kenneth Reitz 23689f8b0a updated history 2011-06-15 12:40:14 -04:00
Kenneth Reitz 1a9e15dbba added Greg Haskins to AUTHORS 2011-06-15 12:39:34 -04:00
Greg Haskins a07602fdeb call colorama.init(..) at import time to ensure stdout/stderr get wrapped 2011-06-15 12:24:52 -04:00
Kenneth Reitz 0855525fca added eng.join example 2011-04-18 12:49:37 -04:00
Kenneth Reitz 872c78a5b9 no need to init colorama 2011-04-18 12:39:20 -04:00
Kenneth Reitz 5d7485c144 cleaner eng.join 2011-04-18 12:38:56 -04:00
Kenneth Reitz 25b8d6a10e oops 2011-04-18 12:25:06 -04:00
Kenneth Reitz 003411ebcd eng.plural! 2011-04-18 12:18:54 -04:00
star:Kenneth Reitz f651e71093 Merge branch 'develop' 2011-04-06 13:06:51 -04:00
star:Kenneth Reitz e500773da3 version bump (v0.2.3) 2011-04-06 13:06:14 -04:00
star:Kenneth Reitz 4b9e35c631 configurable progress interator streams 2011-04-06 13:02:40 -04:00
star:Kenneth Reitz e85fffdbb6 puts/puts_err fixes, defaults for string 2011-04-06 13:00:13 -04:00
star:Kenneth Reitz b58f8dccdc no colorama auto-init upon import 2011-04-06 10:08:25 -04:00
star:Kenneth Reitz 3d63ed01f9 whitespace cleanup 2011-04-03 08:11:23 -04:00
star:Kenneth Reitz 81165754a4 allow clint.textui.puts() to accept arbitrary file-like objects (like StringIO for <pre> generation) 2011-04-03 08:11:17 -04:00
star:Kenneth Reitz 6f106e2f12 toxic 2011-04-01 10:05:55 -04:00
star:Kenneth Reitz 8adda14a27 Merge branch 'feature/multiversion-testing' of https://github.com/offbyone/clint into develop 2011-04-01 10:04:28 -04:00
Chris Rose cc072a18f3 Add tox support 2011-04-01 07:58:48 -06:00
star:Kenneth Reitz c226b6b736 progress bar examples 2011-03-31 14:52:44 -04:00
Will Thames 3406ab5329 Added Will Thames to list of authors 2011-03-31 12:24:42 +01:00
Will Thames cf0c0bda70 Moved from __future__ line closer to the top of the file to prevent
File "piped.py", line 7
    from __future__ import with_statement
SyntaxError: from __future__ imports must occur at the beginning of the file
2011-03-31 12:21:57 +01:00
star:Kenneth Reitz a4c2be8865 pre me right 2011-03-31 06:16:09 -04:00
star:Kenneth Reitz fa2bbf9a0d link to examples directory 2011-03-31 01:07:54 -04:00
star:Kenneth Reitz 9736b4cd71 oh yeah, columns. 2011-03-31 01:05:07 -04:00
star:Kenneth Reitz 2e68207d0a updated readme for newcomers 2011-03-31 00:48:02 -04:00
star:Kenneth Reitz 164d68ce37 loggin' 2011-03-30 23:08:33 -04:00
star:Kenneth Reitz aa5008b2e0 namespace fix 2011-03-30 23:03:39 -04:00
star:Kenneth Reitz 8b7161b15b Configurable progress bars :)
Argument changes.
2011-03-30 23:03:34 -04:00
star:Kenneth Reitz 763ca94e7b newline support for textui.puts 2011-03-30 23:03:13 -04:00
star:Kenneth Reitz 2d200e3709 progress.bar(width=), not size. 2011-03-30 22:53:36 -04:00
star:Kenneth Reitz d0f2375a7b No more '>' in progress bar 2011-03-30 22:51:49 -04:00
star:Kenneth Reitz 2369bc387d progressbar => bar 2011-03-30 22:47:33 -04:00
star:Kenneth Reitz f12e47fa7c add dots progress iterator 2011-03-30 22:47:18 -04:00
star:Kenneth Reitz 5d733d123d put progress uis in separate namespace (for now) 2011-03-30 22:46:57 -04:00
star:Kenneth Reitz 9e1ea1ef91 Automatically disable color 2011-03-30 22:41:40 -04:00
star:Kenneth Reitz 6d151a0794 mention examples. 2011-03-24 07:10:40 -04:00
star:Kenneth Reitz 13afb11dea Elaborate on what Clint IS. 2011-03-24 07:08:00 -04:00
22 changed files with 385 additions and 115 deletions
+3 -1
View File
@@ -1,2 +1,4 @@
.idea
MANIFEST
MANIFEST
*.pyc
.tox
+4 -1
View File
@@ -12,4 +12,7 @@ Patches and Suggestions
- Jeff Forcier
- Morgan Goose
- Travis Swicegood
- Travis Swicegood
- Will Thames
- Greg Haskins
- Miguel Araujo <maraujop>
+24
View File
@@ -1,6 +1,30 @@
History
-------
0.2.4
+++++
* New eng module
* Win32 Bugfix
0.2.3
+++++
* Only init colors if they are used (iPython compatability)
* New progress module
* Various bugfixes
0.2.2
+++++
* Auto Color Disabling
* Progress Namespace Change
* New Progress Bars
* textui.puts newline fix
0.2.1 (2011-03-24)
++++++++++++++++++
+30 -3
View File
@@ -12,16 +12,43 @@ commandline applications.
**T** ools
.
Features:
---------
Clint is awesome. Crazy awesome. It supports colors, but detects if the session is a TTY, so doesn't render the colors if you're piping stuff around. Automagically.
Awesome nest-able indentation context manager. Example: (``with indent(4): puts('indented text')``). It supports custom email-style quotes. Of course, it supports color too, if and when needed.
It has an awesome Column printer with optional auto-expanding columns. It detects how wide your current console is and adjusts accordingly. It wraps your words properly to fit the column size. With or without colors mixed in. All with a single function call.
The world's easiest to use implicit argument system w/ chaining methods for filtering. Seriously.
Run the various executables in examples_ to get a good feel for what Clint offers.
.. _examples: https://github.com/kennethreitz/clint/tree/develop/examples
You'll never want to not use it.
Current Features:
-----------------
- Little Documentation (bear with me for now)
- CLI Colors and Indents
- Extremely Simple + Powerful Column Printer
- Iterator-based Progress Bar
- Implicit Argument Handling
- Simple Support for Unix Pipes
- Simple Support for Incoming Unix Pipes
- Application Directory management
Future Features:
----------------
- Documentation!
- Simple choice system ``Are you sure? [Yn]``
- Default query system ``Installation Path [/usr/local/bin/]``
- Suggestions welcome.
Example
-------
+2 -2
View File
@@ -19,8 +19,8 @@ from .pipes import piped_in
__title__ = 'clint'
__version__ = '0.2.1'
__build__ = 0x000201
__version__ = '0.2.5'
__build__ = 0x000205
__author__ = 'Kenneth Reitz'
__license__ = 'ISC'
__copyright__ = 'Copyright 2011 Kenneth Reitz'
+18 -38
View File
@@ -13,33 +13,13 @@ from __future__ import absolute_import
import os
from sys import argv
from glob import glob
from .packages.ordereddict import OrderedDict
from .utils import is_collection
from .utils import expand_path, is_collection
__all__ = ('Args', )
def _expand_path(path):
"""Expands directories and globs in given path."""
paths = []
if os.path.isdir(path):
for (dir, dirs, files) in os.walk(path):
for file in files:
paths.append(os.path.join(dir, file))
else:
paths.extend(glob(path))
return paths
class Args(object):
"""CLI Argument management."""
@@ -110,20 +90,20 @@ class Args(object):
def any_contain(self, x):
"""Tests if given string is contained in any stored argument."""
return bool(self.first_with(x))
def contains(self, x):
"""Tests if given object is in arguments list.
"""Tests if given object is in arguments list.
Accepts strings and lists of strings."""
return self.__contains__(x)
def first(self, x):
"""Returns first found index of given value (or list of values)"""
def _find( x):
try:
return self.all.index(str(x))
@@ -212,7 +192,7 @@ class Args(object):
return False
else:
return (x in self.all[index])
except IndexError:
return False
@@ -221,7 +201,7 @@ class Args(object):
"""Returns true if argument exists at given index.
Accepts: integer.
"""
try:
self.all[x]
return True
@@ -231,15 +211,15 @@ class Args(object):
def value_after(self, x):
"""Returns value of argument after given found argument (or list thereof)."""
try:
try:
i = self.all.index(x)
except ValueError:
return None
return self.all[i + 1]
except IndexError:
return None
@@ -266,21 +246,21 @@ class Args(object):
return collection
@property
def last(self):
"""Returns last argument."""
try:
return self.all[-1]
except IndexError:
return None
@property
def all(self):
"""Returns all arguments."""
return self._args
@@ -288,7 +268,7 @@ class Args(object):
"""Returns all arguments containing given string (or list thereof)"""
_args = []
for arg in self.all:
if is_collection(x):
for _x in x:
@@ -327,7 +307,7 @@ class Args(object):
return self.start_with('-')
@property
@property
def not_flags(self):
"""Returns Arg object excluding flagged arguments."""
@@ -341,7 +321,7 @@ class Args(object):
_paths = []
for arg in self.all:
for path in _expand_path(arg):
for path in expand_path(arg):
if os.path.exists(path):
if absolute:
_paths.append(os.path.abspath(path))
@@ -358,7 +338,7 @@ class Args(object):
_args = []
for arg in self.all:
if not len(_expand_path(arg)):
if not len(expand_path(arg)):
if not os.path.exists(arg):
_args.append(arg)
+49
View File
@@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-
"""
clint.eng
~~~~~~~~~
This module provides English language string helpers.
"""
MORON_MODE = False
COMMA = ','
CONJUNCTION = 'and'
SPACE = ' '
def join(l, conj=CONJUNCTION, im_a_moron=MORON_MODE, seperator=COMMA):
"""Joins lists of words. Oxford comma and all."""
collector = []
left = len(l)
seperator = seperator + SPACE
conj = conj + SPACE
for _l in l[:]:
left += -1
collector.append(_l)
if left == 1:
if len(l) == 2 or im_a_moron:
collector.append(SPACE)
else:
collector.append(seperator)
collector.append(conj)
elif left is not 0:
collector.append(seperator)
return unicode(str().join(collector))
if __name__ == '__main__':
print join(['blue', 'red', 'yellow'], conj='or', im_a_moron=True)
print join(['blue', 'red', 'yellow'], conj='or')
print join(['blue', 'red'], conj='or')
print join(['blue', 'red'], conj='and')
print join(['blue'], conj='and')
print join(['blue', 'red', 'yellow', 'green', 'ello'], conj='and')
+2 -2
View File
@@ -1,6 +1,6 @@
from .initialise import init
from .initialise import init, deinit, reinit
from .ansi import Fore, Back, Style
from .ansitowin32 import AnsiToWin32
VERSION = '0.1.18'
VERSION = '0.2.3'
+8 -2
View File
@@ -118,12 +118,12 @@ class AnsiToWin32(object):
self.wrapped.flush()
if self.autoreset:
self.reset_all()
def reset_all(self):
if self.convert:
self.call_win32('m', (0,))
else:
elif is_a_tty(self.wrapped):
self.wrapped.write(Style.RESET_ALL)
@@ -173,4 +173,10 @@ class AnsiToWin32(object):
args = func_args[1:]
kwargs = dict(on_stderr=self.on_stderr)
func(*args, **kwargs)
elif command in ('H', 'f'): # set cursor position
func = winterm.set_cursor_position
func(params, on_stderr=self.on_stderr)
elif command in ('J'):
func = winterm.erase_data
func(params, on_stderr=self.on_stderr)
+20 -3
View File
@@ -7,6 +7,9 @@ from .ansitowin32 import AnsiToWin32
orig_stdout = sys.stdout
orig_stderr = sys.stderr
wrapped_stdout = sys.stdout
wrapped_stderr = sys.stderr
atexit_done = False
@@ -16,11 +19,14 @@ def reset_all():
def init(autoreset=False, convert=None, strip=None, wrap=True):
if wrap==False and (autoreset==True or convert==True or strip==True):
if not wrap and any([autoreset, convert, strip]):
raise ValueError('wrap=False conflicts with any other arg=True')
sys.stdout = wrap_stream(orig_stdout, convert, strip, autoreset, wrap)
sys.stderr = wrap_stream(orig_stderr, convert, strip, autoreset, wrap)
global wrapped_stdout, wrapped_stderr
sys.stdout = wrapped_stdout = \
wrap_stream(orig_stdout, convert, strip, autoreset, wrap)
sys.stderr = wrapped_stderr = \
wrap_stream(orig_stderr, convert, strip, autoreset, wrap)
global atexit_done
if not atexit_done:
@@ -28,6 +34,16 @@ def init(autoreset=False, convert=None, strip=None, wrap=True):
atexit_done = True
def deinit():
sys.stdout = orig_stdout
sys.stderr = orig_stderr
def reinit():
sys.stdout = wrapped_stdout
sys.stderr = wrapped_stdout
def wrap_stream(stream, convert, strip, autoreset, wrap):
if wrap:
wrapper = AnsiToWin32(stream,
@@ -36,3 +52,4 @@ def wrap_stream(stream, convert, strip, autoreset, wrap):
stream = wrapper.stream
return stream
+44 -16
View File
@@ -48,8 +48,16 @@ else:
("srWindow", SMALL_RECT),
("dwMaximumWindowSize", COORD),
]
def __str__(self):
return '(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)' % (
self.dwSize.Y, self.dwSize.X
, self.dwCursorPosition.Y, self.dwCursorPosition.X
, self.wAttributes
, self.srWindow.Top, self.srWindow.Left, self.srWindow.Bottom, self.srWindow.Right
, self.dwMaximumWindowSize.Y, self.dwMaximumWindowSize.X
)
def GetConsoleScreenBufferInfo(stream_id):
def GetConsoleScreenBufferInfo(stream_id=STDOUT):
handle = handles[stream_id]
csbi = CONSOLE_SCREEN_BUFFER_INFO()
success = windll.kernel32.GetConsoleScreenBufferInfo(
@@ -62,34 +70,54 @@ else:
def SetConsoleTextAttribute(stream_id, attrs):
handle = handles[stream_id]
success = windll.kernel32.SetConsoleTextAttribute(handle, attrs)
assert success
return windll.kernel32.SetConsoleTextAttribute(handle, attrs)
def SetConsoleCursorPosition(stream_id, position):
handle = handles[stream_id]
position = COORD(*position)
success = windll.kernel32.SetConsoleCursorPosition(handle, position)
assert success
# If the position is out of range, do nothing.
if position.Y <= 0 or position.X <= 0:
return
# Adjust for Windows' SetConsoleCursorPosition:
# 1. being 0-based, while ANSI is 1-based.
# 2. expecting (x,y), while ANSI uses (y,x).
adjusted_position = COORD(position.Y - 1, position.X - 1)
# Adjust for viewport's scroll position
sr = GetConsoleScreenBufferInfo(STDOUT).srWindow
adjusted_position.Y += sr.Top
adjusted_position.X += sr.Left
# Resume normal processing
handle = handles[stream_id]
success = windll.kernel32.SetConsoleCursorPosition(handle, adjusted_position)
return success
def FillConsoleOutputCharacter(stream_id, char, length, start):
handle = handles[stream_id]
char = TCHAR(char)
length = DWORD(length)
start = COORD(*start)
num_written = DWORD(0)
# AttributeError: function 'FillConsoleOutputCharacter' not found
# could it just be that my types are wrong?
success = windll.kernel32.FillConsoleOutputCharacter(
# Note that this is hard-coded for ANSI (vs wide) bytes.
success = windll.kernel32.FillConsoleOutputCharacterA(
handle, char, length, start, byref(num_written))
assert success
return num_written.value
def FillConsoleOutputAttribute(stream_id, attr, length, start):
''' FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten )'''
handle = handles[stream_id]
attribute = WORD(attr)
length = DWORD(length)
num_written = DWORD(0)
# Note that this is hard-coded for ANSI (vs wide) bytes.
success = windll.kernel32.FillConsoleOutputAttribute(
handle, attribute, length, start, byref(num_written))
return success
if __name__=='__main__':
x = GetConsoleScreenBufferInfo(STDOUT)
print(x.dwSize)
print(x.dwCursorPosition)
print(x.wAttributes)
print(x.srWindow)
print(x.dwMaximumWindowSize)
print(x)
print('dwSize(height,width) = (%d,%d)' % (x.dwSize.Y, x.dwSize.X))
print('dwCursorPosition(y,x) = (%d,%d)' % (x.dwCursorPosition.Y, x.dwCursorPosition.X))
print('wAttributes(color) = %d = 0x%02x' % (x.wAttributes, x.wAttributes))
print('srWindow(Top,Left)-(Bottom,Right) = (%d,%d)-(%d,%d)' % (x.srWindow.Top, x.srWindow.Left, x.srWindow.Bottom, x.srWindow.Right))
print('dwMaximumWindowSize(maxHeight,maxWidth) = (%d,%d)' % (x.dwMaximumWindowSize.Y, x.dwMaximumWindowSize.X))
+35 -2
View File
@@ -22,8 +22,7 @@ class WinStyle(object):
class WinTerm(object):
def __init__(self):
self._default = \
win32.GetConsoleScreenBufferInfo(win32.STDOUT).wAttributes
self._default = win32.GetConsoleScreenBufferInfo(win32.STDOUT).wAttributes
self.set_attrs(self._default)
self._default_fore = self._fore
self._default_back = self._back
@@ -67,3 +66,37 @@ class WinTerm(object):
handle = win32.STDERR
win32.SetConsoleTextAttribute(handle, attrs)
def set_cursor_position(self, position=None, on_stderr=False):
if position is None:
#I'm not currently tracking the position, so there is no default.
#position = self.get_position()
return
handle = win32.STDOUT
if on_stderr:
handle = win32.STDERR
win32.SetConsoleCursorPosition(handle, position)
def erase_data(self, mode=0, on_stderr=False):
# 0 (or None) should clear from the cursor to the end of the screen.
# 1 should clear from the cursor to the beginning of the screen.
# 2 should clear the entire screen. (And maybe move cursor to (1,1)?)
#
# At the moment, I only support mode 2. From looking at the API, it
# should be possible to calculate a different number of bytes to clear,
# and to do so relative to the cursor position.
if mode[0] not in (2,):
return
handle = win32.STDOUT
if on_stderr:
handle = win32.STDERR
# here's where we'll home the cursor
coord_screen = win32.COORD(0,0)
csbi = win32.GetConsoleScreenBufferInfo(handle)
# get the number of character cells in the current buffer
dw_con_size = csbi.dwSize.X * csbi.dwSize.Y
# fill the entire screen with blanks
win32.FillConsoleOutputCharacter(handle, ord(' '), dw_con_size, coord_screen)
# now set the buffer's attributes accordingly
win32.FillConsoleOutputAttribute(handle, self.get_attrs(), dw_con_size, coord_screen );
# put the cursor at (0, 0)
win32.SetConsoleCursorPosition(handle, (coord_screen.X, coord_screen.Y))
+8 -8
View File
@@ -39,7 +39,7 @@ class AppDir(object):
def __repr__(self):
return '<app-dir: %s>' % (self.path)
def __getattribute__(self, name):
@@ -53,11 +53,11 @@ class AppDir(object):
"""Raises if operations are carried out on an unconfigured AppDir."""
if not self.path:
raise NotConfigured()
def _create(self):
"""Creates current AppDir at AppDir.path."""
self._raise_if_none()
if not self._exists:
mkdir_p(self.path)
@@ -66,7 +66,7 @@ class AppDir(object):
def open(self, filename, mode='r'):
"""Returns file object from given filename."""
self._raise_if_none()
fn = path_join(self.path, filename)
@@ -121,11 +121,11 @@ class AppDir(object):
else:
raise why
def read(self, filename, binary=False):
"""Returns contents of given file with AppDir.
If file doesn't exist, returns None."""
self._raise_if_none()
fn = path_join(self.path, filename)
@@ -161,11 +161,11 @@ log = AppDir()
def init(vendor, name):
global user, site, cache, log
ad = AppDirs(name, vendor)
user.path = ad.user_data_dir
site.path = ad.site_data_dir
cache.path = ad.user_cache_dir
log.path = ad.user_log_dir
+2
View File
@@ -10,4 +10,6 @@ This module provides the text output helper system.
from . import colored
from . import progress
from core import *
+10 -8
View File
@@ -25,7 +25,9 @@ __all__ = (
COLORS = __all__[:-2]
DISABLE_COLOR = False
if sys.stdout.isatty():
if not sys.stdout.isatty():
DISABLE_COLOR = True
else:
colorama.init(autoreset=True)
@@ -47,25 +49,25 @@ class ColoredString(object):
def __len__(self):
return len(self.s)
def __repr__(self):
return "<%s-string: '%s'>" % (self.color, self.s)
def __str__(self):
return self.__unicode__().encode('utf8')
def __unicode__(self):
return self.color_str
def __add__(self, other):
return str(self.color_str) + str(other)
def __radd__(self, other):
return str(other) + str(self.color_str)
def __mul__(self, other):
return (self.color_str * other)
def split(self, x=' '):
return map(self._new, self.s.split(x))
+8 -9
View File
@@ -13,13 +13,12 @@ from __future__ import absolute_import
import sys
from .progress import progressbar
from .formatters import max_width, min_width
from .cols import columns
from ..utils import tsplit
__all__ = ('puts', 'puts_err', 'indent', 'progressbar', 'columns', 'max_width', 'min_width')
__all__ = ('puts', 'puts_err', 'indent', 'columns', 'max_width', 'min_width')
STDOUT = sys.stdout.write
@@ -63,14 +62,14 @@ class Writer(object):
def __call__(self, s, newline=True, stream=STDOUT):
if newline:
s = tsplit(s, NEWLINES)
s = map(str, s)
indent = ''.join(self.shared['indent_strings'])
s = (str('\n' + indent)).join(s)
_str = ''.join((
''.join(self.shared['indent_strings']),
str(s),
@@ -79,14 +78,14 @@ class Writer(object):
stream(_str)
def puts(s, newline=True):
def puts(s='', newline=True, stream=STDOUT):
"""Prints given string to stdout via Writer interface."""
Writer()(s, stream=STDOUT)
Writer()(s, newline, stream=stream)
def puts_err(s, newline=True):
def puts_err(s='', newline=True, stream=STDERR):
"""Prints given string to stderr via Writer interface."""
Writer()(s, stream=STDERR)
Writer()(s, newline, stream=stream)
def indent(indent=4, quote=''):
+44 -10
View File
@@ -12,21 +12,55 @@ from __future__ import absolute_import
import sys
STREAM = sys.stderr
def progressbar(it, prefix='', size=32, hide=False):
BAR_TEMPLATE = '%s[%s%s] %i/%i\r'
DOTS_CHAR = '.'
def bar(it, label='', width=32, hide=False, empty_char='-', filled_char='='):
"""Progress iterator. Wrap your iterables with it."""
count = len(it)
if count:
def _show(_i):
x = int(size*_i/count)
if not hide:
sys.stdout.write("%s[%s>%s] %i/%i\r" % (prefix, "="*x, "-"*(size-x), _i, count))
sys.stdout.flush()
def _show(_i):
x = int(width*_i/count)
if not hide:
STREAM.write(BAR_TEMPLATE % (
label, bar_filled_char*x, bar_empty_char*(width-x), _i, count))
STREAM.flush()
count = len(it)
if count:
_show(0)
for i, item in enumerate(it):
yield item
_show(i+1)
if not hide:
sys.stdout.write("\n")
sys.stdout.flush()
STREAM.write('\n')
STREAM.flush()
def dots(it, label='', hide=False):
"""Progress iterator. Prints a dot for each item being iterated"""
count = 0
if not hide:
STREAM.write(label)
for item in it:
if not hide:
STREAM.write(DOTS_CHAR)
sys.stderr.flush()
count += 1
yield item
STREAM.write('\n')
STREAM.flush()
+24 -4
View File
@@ -11,9 +11,29 @@ Various Python helpers used within clint.
from __future__ import absolute_import
from __future__ import with_statement
import sys
import errno
import os.path
from os import makedirs
from glob import glob
def expand_path(path):
"""Expands directories and globs in given path."""
paths = []
path = os.path.expanduser(path)
path = os.path.expandvars(path)
if os.path.isdir(path):
for (dir, dirs, files) in os.walk(path):
for file in files:
paths.append(os.path.join(dir, file))
else:
paths.extend(glob(path))
return paths
def is_collection(obj):
@@ -37,17 +57,17 @@ def mkdir_p(path):
def tsplit(string, delimiters):
"""Behaves str.split but supports tuples of delimiters."""
delimiters = tuple(delimiters)
stack = [string,]
for delimiter in delimiters:
for i, substring in enumerate(stack):
substack = substring.split(delimiter)
stack.pop(i)
for j, _substring in enumerate(substack):
stack.insert(i+j, _substring)
return stack
def schunk(string, size):
+31
View File
@@ -0,0 +1,31 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
sys.path.insert(0, os.path.abspath('..'))
from clint.eng import join
from clint.textui import colored, indent, puts
colors = [
colored.blue('blue'),
colored.red('red'),
colored.yellow('yellow'),
colored.green('green'),
colored.magenta('magenta')
]
colors = map(str, colors)
puts('Smart:')
with indent(4):
for i in range(len(colors)):
puts(join(colors[:i+1]))
puts('\n')
puts('Stupid:')
with indent(4):
for i in range(len(colors)):
puts(join(colors[:i+1], im_a_moron=True, conj='\'n'))
+3 -3
View File
@@ -1,11 +1,11 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import with_statement
import sys
import os
from __future__ import with_statement
sys.path.insert(0, os.path.abspath('..'))
from clint import piped_in
@@ -23,4 +23,4 @@ if __name__ == '__main__':
with indent(5, quote=colored.red(' |')):
puts(in_data)
else:
puts(colored.red('Warning: ') + 'No data was piped in.')
puts(colored.red('Warning: ') + 'No data was piped in.')
+4 -3
View File
@@ -8,12 +8,13 @@ sys.path.insert(0, os.path.abspath('..'))
from time import sleep
from random import random
from clint.textui import progressbar
from clint.textui import progress
if __name__ == '__main__':
for i in progressbar(range(100)):
for i in progress.bar(range(100)):
sleep(random() * 0.2)
for i in progress.dots(range(100)):
sleep(random() * 0.2)
+12
View File
@@ -0,0 +1,12 @@
[tox]
envlist = py25,py26,py27,py3
[testenv]
commands=py.test --junitxml=junit-{envname}.xml
deps = pytest
[testenv:pypy]
basepython=/usr/bin/pypy-c
[testenv:py3]
basepython=/usr/bin/python3