mirror of
https://github.com/kennethreitz/clint.git
synced 2026-06-05 14:50:17 +00:00
Compare commits
78 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 53ec4c2e09 | |||
| 7b6138eda6 | |||
| d19650b93c | |||
| 98edec43e9 | |||
| ef48a47b8d | |||
| 3be47add4a | |||
| bf99084ece | |||
| 705cda4443 | |||
| 79cf7a023b | |||
| 7900853dbc | |||
| 96827698df | |||
| 23689f8b0a | |||
| 1a9e15dbba | |||
| a07602fdeb | |||
| 0855525fca | |||
| 872c78a5b9 | |||
| 5d7485c144 | |||
| 25b8d6a10e | |||
| 003411ebcd | |||
| f651e71093 | |||
| e500773da3 | |||
| 4b9e35c631 | |||
| e85fffdbb6 | |||
| b58f8dccdc | |||
| 3d63ed01f9 | |||
| 81165754a4 | |||
| 6f106e2f12 | |||
| 8adda14a27 | |||
| cc072a18f3 | |||
| c226b6b736 | |||
| 3406ab5329 | |||
| cf0c0bda70 | |||
| a4c2be8865 | |||
| fa2bbf9a0d | |||
| 9736b4cd71 | |||
| 2e68207d0a | |||
| 164d68ce37 | |||
| aa5008b2e0 | |||
| 8b7161b15b | |||
| 763ca94e7b | |||
| 2d200e3709 | |||
| d0f2375a7b | |||
| 2369bc387d | |||
| f12e47fa7c | |||
| 5d733d123d | |||
| 9e1ea1ef91 | |||
| 6d151a0794 | |||
| 13afb11dea | |||
| faeacbbfad | |||
| 401344ff1e | |||
| 598deda4d9 | |||
| bf7b5d0930 | |||
| 6404b0e49c | |||
| b92f3204c9 | |||
| 61225d90e6 | |||
| 3688a2a67b | |||
| dda561387c | |||
| 06af338874 | |||
| 9b91cf16b5 | |||
| ec8fe4a0b7 | |||
| dc6a9c63ea | |||
| 82d31e555f | |||
| 2f6e9cbfaa | |||
| b576ccbaae | |||
| 8e021f95e9 | |||
| 83bf2288dd | |||
| f2fc21755b | |||
| 52488ace36 | |||
| 8b6eb543b9 | |||
| 2cd9bd5263 | |||
| 76bee9c4ba | |||
| 14c4cdf904 | |||
| f6a20fc413 | |||
| fc3eeca54f | |||
| ebdb04848a | |||
| f0206b21fc | |||
| 5366c7e5b2 | |||
| 35ec821e37 |
+3
-1
@@ -1,2 +1,4 @@
|
||||
.idea
|
||||
MANIFEST
|
||||
MANIFEST
|
||||
*.pyc
|
||||
.tox
|
||||
|
||||
@@ -12,4 +12,7 @@ Patches and Suggestions
|
||||
|
||||
- Jeff Forcier
|
||||
- Morgan Goose
|
||||
- Travis Swicegood
|
||||
- Travis Swicegood
|
||||
- Will Thames
|
||||
- Greg Haskins
|
||||
- Miguel Araujo <maraujop>
|
||||
|
||||
+48
-4
@@ -1,10 +1,52 @@
|
||||
History
|
||||
-------
|
||||
|
||||
0.1.0 (2011-03-20)
|
||||
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)
|
||||
++++++++++++++++++
|
||||
|
||||
* Initial Release!
|
||||
* Python 2.5 Support
|
||||
* List of available colors
|
||||
|
||||
|
||||
0.2.0 (2011-03-23)
|
||||
++++++++++++++++++
|
||||
|
||||
* Column Printing!!!
|
||||
* (Auto/Manual) Disabling of Colors
|
||||
* Smarter Colors
|
||||
* max_width, min_width
|
||||
* Strip cli colors
|
||||
* bug fixes
|
||||
|
||||
|
||||
0.1.2 (2011-03-21)
|
||||
++++++++++++++++++
|
||||
|
||||
* Bugfixes
|
||||
|
||||
|
||||
0.1.1 (2011-03-20)
|
||||
@@ -16,7 +58,9 @@ History
|
||||
* Lots of Examples
|
||||
|
||||
|
||||
0.1.2 (2011-03-21)
|
||||
|
||||
0.1.0 (2011-03-20)
|
||||
++++++++++++++++++
|
||||
|
||||
* Bugfixes
|
||||
* Initial Release!
|
||||
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
include HISTORY.rst README.rst LICENSE AUTHORS NOTICE
|
||||
include HISTORY.rst README.rst LICENSE AUTHORS NOTICE
|
||||
@@ -1,4 +1,4 @@
|
||||
Clint includes some vendorized python libraries: appdirs, colorama, ordereddict.
|
||||
Clint includes some vendorized python libraries: appdirs, applib, colorama, ordereddict.
|
||||
|
||||
AppDirs License
|
||||
===============
|
||||
@@ -25,6 +25,33 @@ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
|
||||
AppLib License (console width detection functions are from this library)
|
||||
==============
|
||||
|
||||
# This is the MIT license
|
||||
|
||||
# Copyright (c) 2010 ActiveState Software Inc. All rights reserved.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included
|
||||
in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
|
||||
|
||||
Colorama License
|
||||
================
|
||||
|
||||
+30
-3
@@ -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
@@ -19,8 +19,8 @@ from .pipes import piped_in
|
||||
|
||||
|
||||
__title__ = 'clint'
|
||||
__version__ = '0.1.2'
|
||||
__build__ = 0x000102
|
||||
__version__ = '0.2.5'
|
||||
__build__ = 0x000205
|
||||
__author__ = 'Kenneth Reitz'
|
||||
__license__ = 'ISC'
|
||||
__copyright__ = 'Copyright 2011 Kenneth Reitz'
|
||||
|
||||
+42
-43
@@ -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."""
|
||||
|
||||
@@ -69,7 +49,7 @@ class Args(object):
|
||||
|
||||
|
||||
def __contains__(self, x):
|
||||
return bool(self.first(x))
|
||||
return self.first(x) is not None
|
||||
|
||||
|
||||
def get(self, x):
|
||||
@@ -90,7 +70,7 @@ class Args(object):
|
||||
|
||||
def _remove(x):
|
||||
found = self.first(x)
|
||||
if found:
|
||||
if found is not None:
|
||||
self._args.pop(found)
|
||||
|
||||
if is_collection(x):
|
||||
@@ -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))
|
||||
@@ -133,7 +113,7 @@ class Args(object):
|
||||
if is_collection(x):
|
||||
for item in x:
|
||||
found = _find(item)
|
||||
if found:
|
||||
if found is not None:
|
||||
return found
|
||||
return None
|
||||
else:
|
||||
@@ -181,6 +161,25 @@ class Args(object):
|
||||
else:
|
||||
return _find(x)
|
||||
|
||||
|
||||
def start_with(self, x):
|
||||
"""Returns all arguments beginning with given string (or list thereof)"""
|
||||
|
||||
_args = []
|
||||
|
||||
for arg in self.all:
|
||||
if is_collection(x):
|
||||
for _x in x:
|
||||
if arg.startswith(x):
|
||||
_args.append(arg)
|
||||
break
|
||||
else:
|
||||
if arg.startswith(x):
|
||||
_args.append(arg)
|
||||
|
||||
return Args(_args, no_argv=True)
|
||||
|
||||
|
||||
def contains_at(self, x, index):
|
||||
"""Tests if given [list of] string is at given index."""
|
||||
|
||||
@@ -193,7 +192,7 @@ class Args(object):
|
||||
return False
|
||||
else:
|
||||
return (x in self.all[index])
|
||||
|
||||
|
||||
except IndexError:
|
||||
return False
|
||||
|
||||
@@ -202,7 +201,7 @@ class Args(object):
|
||||
"""Returns true if argument exists at given index.
|
||||
Accepts: integer.
|
||||
"""
|
||||
|
||||
|
||||
try:
|
||||
self.all[x]
|
||||
return True
|
||||
@@ -212,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
|
||||
|
||||
@@ -247,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
|
||||
|
||||
|
||||
@@ -269,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:
|
||||
@@ -305,10 +304,10 @@ class Args(object):
|
||||
def flags(self):
|
||||
"""Returns Arg object including only flagged arguments."""
|
||||
|
||||
return self.all_with('-')
|
||||
return self.start_with('-')
|
||||
|
||||
|
||||
@property
|
||||
@property
|
||||
def not_flags(self):
|
||||
"""Returns Arg object excluding flagged arguments."""
|
||||
|
||||
@@ -322,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))
|
||||
@@ -339,11 +338,11 @@ 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)
|
||||
|
||||
return _args
|
||||
return Args(_args, no_argv=True)
|
||||
|
||||
@property
|
||||
def copy(self):
|
||||
|
||||
@@ -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')
|
||||
@@ -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'
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
@@ -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))
|
||||
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -9,6 +9,7 @@ This module contains the helper functions for dealing with unix pipes.
|
||||
"""
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import with_statement
|
||||
|
||||
import sys
|
||||
|
||||
|
||||
+9
-8
@@ -10,6 +10,7 @@ This module contains all the application resource features of clint.
|
||||
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import with_statement
|
||||
|
||||
import errno
|
||||
from os import remove, removedirs
|
||||
@@ -38,7 +39,7 @@ class AppDir(object):
|
||||
|
||||
def __repr__(self):
|
||||
return '<app-dir: %s>' % (self.path)
|
||||
|
||||
|
||||
|
||||
def __getattribute__(self, name):
|
||||
|
||||
@@ -52,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)
|
||||
@@ -65,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)
|
||||
|
||||
@@ -120,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)
|
||||
|
||||
@@ -160,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
|
||||
|
||||
@@ -8,8 +8,8 @@ This module provides the text output helper system.
|
||||
|
||||
"""
|
||||
|
||||
from __future__ import absolute_import
|
||||
|
||||
|
||||
from . import colored
|
||||
from .core import *
|
||||
from . import progress
|
||||
|
||||
from core import *
|
||||
|
||||
+50
-18
@@ -11,18 +11,25 @@ This module provides a simple and elegant wrapper for colorama.
|
||||
|
||||
from __future__ import absolute_import
|
||||
|
||||
import re
|
||||
import sys
|
||||
|
||||
from ..packages import colorama
|
||||
|
||||
|
||||
|
||||
colorama.init(autoreset=True)
|
||||
|
||||
|
||||
__all__ = (
|
||||
'red', 'green', 'yellow', 'blue',
|
||||
'black', 'magenta', 'cyan', 'white'
|
||||
'black', 'magenta', 'cyan', 'white',
|
||||
'clean', 'disable'
|
||||
)
|
||||
|
||||
COLORS = __all__[:-2]
|
||||
DISABLE_COLOR = False
|
||||
|
||||
if not sys.stdout.isatty():
|
||||
DISABLE_COLOR = True
|
||||
else:
|
||||
colorama.init(autoreset=True)
|
||||
|
||||
|
||||
class ColoredString(object):
|
||||
"""Enhanced string for __len__ operations on Colored output."""
|
||||
@@ -30,33 +37,52 @@ class ColoredString(object):
|
||||
super(ColoredString, self).__init__()
|
||||
self.s = s
|
||||
self.color = color
|
||||
self.color_str = '%s%s%s' % (
|
||||
getattr(colorama.Fore, self.color), self.s, colorama.Fore.RESET)
|
||||
|
||||
|
||||
@property
|
||||
def color_str(self):
|
||||
if sys.stdout.isatty() and not DISABLE_COLOR:
|
||||
return '%s%s%s' % (
|
||||
getattr(colorama.Fore, self.color), self.s, colorama.Fore.RESET)
|
||||
else:
|
||||
return self.s
|
||||
|
||||
|
||||
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 self.color_str.split(x)
|
||||
|
||||
def split(self, x=' '):
|
||||
return map(self._new, self.s.split(x))
|
||||
|
||||
def _new(self, s):
|
||||
return ColoredString(self.color, s)
|
||||
|
||||
|
||||
def clean(s):
|
||||
strip = re.compile("([^-_a-zA-Z0-9!@#%&=,/'\";:~`\$\^\*\(\)\+\[\]\.\{\}\|\?\<\>\\]+|[^\s]+)")
|
||||
txt = strip.sub('', str(s))
|
||||
|
||||
strip = re.compile(r'\[\d+m')
|
||||
txt = strip.sub('', txt)
|
||||
|
||||
return txt
|
||||
|
||||
|
||||
def black(string):
|
||||
@@ -82,3 +108,9 @@ def cyan(string):
|
||||
|
||||
def white(string):
|
||||
return ColoredString('WHITE', string)
|
||||
|
||||
def disable():
|
||||
"""Disables colors."""
|
||||
global DISABLE_COLOR
|
||||
|
||||
DISABLE_COLOR = True
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
clint.textui.columns
|
||||
~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Core TextUI functionality for column formatting.
|
||||
|
||||
"""
|
||||
|
||||
from __future__ import absolute_import
|
||||
|
||||
from .formatters import max_width, min_width
|
||||
from ..utils import tsplit
|
||||
|
||||
import sys
|
||||
|
||||
|
||||
NEWLINES = ('\n', '\r', '\r\n')
|
||||
|
||||
|
||||
|
||||
def _find_unix_console_width():
|
||||
import termios, fcntl, struct, sys
|
||||
|
||||
# fcntl.ioctl will fail if stdout is not a tty
|
||||
if not sys.stdout.isatty():
|
||||
return None
|
||||
|
||||
s = struct.pack("HHHH", 0, 0, 0, 0)
|
||||
fd_stdout = sys.stdout.fileno()
|
||||
size = fcntl.ioctl(fd_stdout, termios.TIOCGWINSZ, s)
|
||||
height, width = struct.unpack("HHHH", size)[:2]
|
||||
return width
|
||||
|
||||
|
||||
def _find_windows_console_width():
|
||||
# http://code.activestate.com/recipes/440694/
|
||||
from ctypes import windll, create_string_buffer
|
||||
STDIN, STDOUT, STDERR = -10, -11, -12
|
||||
|
||||
h = windll.kernel32.GetStdHandle(STDERR)
|
||||
csbi = create_string_buffer(22)
|
||||
res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
|
||||
|
||||
if res:
|
||||
import struct
|
||||
(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
|
||||
|
||||
|
||||
def console_width(kwargs):
|
||||
""""Determine console_width."""
|
||||
|
||||
if sys.platform.startswith('win'):
|
||||
console_width = _find_windows_console_width()
|
||||
else:
|
||||
console_width = _find_unix_console_width()
|
||||
|
||||
_width = kwargs.get('width', None)
|
||||
if _width:
|
||||
console_width = _width
|
||||
else:
|
||||
if not console_width:
|
||||
console_width = 80
|
||||
|
||||
return console_width
|
||||
|
||||
|
||||
|
||||
def columns(*cols, **kwargs):
|
||||
|
||||
columns = list(cols)
|
||||
|
||||
cwidth = console_width(kwargs)
|
||||
|
||||
_big_col = None
|
||||
_total_cols = 0
|
||||
|
||||
|
||||
for i, (string, width) in enumerate(cols):
|
||||
|
||||
if width is not None:
|
||||
_total_cols += (width + 1)
|
||||
cols[i][0] = max_width(string, width).split('\n')
|
||||
else:
|
||||
_big_col = i
|
||||
|
||||
if _big_col:
|
||||
cols[_big_col][1] = (cwidth - _total_cols) - len(cols)
|
||||
cols[_big_col][0] = max_width(cols[_big_col][0], cols[_big_col][1]).split('\n')
|
||||
|
||||
height = len(max([c[0] for c in cols], key=len))
|
||||
|
||||
for i, (strings, width) in enumerate(cols):
|
||||
|
||||
for _ in range(height - len(strings)):
|
||||
cols[i][0].append('')
|
||||
|
||||
for j, string in enumerate(strings):
|
||||
cols[i][0][j] = min_width(string, width)
|
||||
|
||||
stack = [c[0] for c in cols]
|
||||
_out = []
|
||||
|
||||
for i in range(height):
|
||||
_row = ''
|
||||
|
||||
for col in stack:
|
||||
_row += col[i]
|
||||
_row += ' '
|
||||
|
||||
_out.append(_row)
|
||||
# try:
|
||||
# pass
|
||||
# except:
|
||||
# pass
|
||||
|
||||
|
||||
|
||||
|
||||
return '\n'.join(_out)
|
||||
|
||||
|
||||
# string = max_width(string, width)
|
||||
# string = min_width(string, width)
|
||||
# pass
|
||||
# columns.append()
|
||||
|
||||
|
||||
|
||||
###########################
|
||||
|
||||
a = 'this is text that goes into a small column\n cool?'
|
||||
b = 'this is other text\nothertext\nothertext'
|
||||
|
||||
#columns((a, 10), (b, 20), (b, None))
|
||||
+16
-11
@@ -13,16 +13,20 @@ 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')
|
||||
__all__ = ('puts', 'puts_err', 'indent', 'columns', 'max_width', 'min_width')
|
||||
|
||||
|
||||
STDOUT = sys.stdout.write
|
||||
STDERR = sys.stderr.write
|
||||
|
||||
NEWLINES = ('\n', '\r', '\r\n')
|
||||
|
||||
|
||||
|
||||
class Writer(object):
|
||||
"""WriterUtilized by context managers."""
|
||||
@@ -58,13 +62,14 @@ class Writer(object):
|
||||
|
||||
|
||||
def __call__(self, s, newline=True, stream=STDOUT):
|
||||
|
||||
|
||||
if newline:
|
||||
s = tsplit(s, ('\n', '\r', '\r\n'))
|
||||
s = tsplit(s, NEWLINES)
|
||||
s = map(str, s)
|
||||
indent = ''.join(self.shared['indent_strings'])
|
||||
|
||||
s = ('\n' + indent).join(s)
|
||||
|
||||
|
||||
s = (str('\n' + indent)).join(s)
|
||||
|
||||
_str = ''.join((
|
||||
''.join(self.shared['indent_strings']),
|
||||
str(s),
|
||||
@@ -73,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=''):
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
clint.textui.formatters
|
||||
~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Core TextUI functionality for text formatting.
|
||||
|
||||
"""
|
||||
|
||||
from __future__ import absolute_import
|
||||
|
||||
from .colored import ColoredString, clean
|
||||
from ..utils import tsplit, schunk
|
||||
|
||||
|
||||
NEWLINES = ('\n', '\r', '\r\n')
|
||||
|
||||
|
||||
def min_width(string, cols, padding=' '):
|
||||
"""Returns given string with right padding."""
|
||||
|
||||
is_color = isinstance(string, ColoredString)
|
||||
|
||||
stack = tsplit(str(string), NEWLINES)
|
||||
|
||||
for i, substring in enumerate(stack):
|
||||
_sub = clean(substring).ljust((cols + 0), padding)
|
||||
if is_color:
|
||||
_sub = (_sub.replace(clean(substring), substring))
|
||||
stack[i] = _sub
|
||||
|
||||
return '\n'.join(stack)
|
||||
|
||||
|
||||
def max_width(string, cols, separator='\n'):
|
||||
"""Returns a freshly formatted """
|
||||
|
||||
is_color = isinstance(string, ColoredString)
|
||||
|
||||
if is_color:
|
||||
offset = 10
|
||||
string_copy = string._new('')
|
||||
else:
|
||||
offset = 0
|
||||
|
||||
stack = tsplit(string, NEWLINES)
|
||||
|
||||
for i, substring in enumerate(stack):
|
||||
stack[i] = substring.split()
|
||||
|
||||
_stack = []
|
||||
|
||||
for row in stack:
|
||||
_row = ['',]
|
||||
_row_i = 0
|
||||
|
||||
for word in row:
|
||||
if (len(_row[_row_i]) + len(word)) < (cols + offset):
|
||||
_row[_row_i] += word
|
||||
_row[_row_i] += ' '
|
||||
|
||||
elif len(word) > (cols - offset):
|
||||
|
||||
# ensure empty row
|
||||
if len(_row[_row_i]):
|
||||
_row.append('')
|
||||
_row_i += 1
|
||||
|
||||
chunks = schunk(word, (cols + offset))
|
||||
for i, chunk in enumerate(chunks):
|
||||
if not (i + 1) == len(chunks):
|
||||
_row[_row_i] += chunk
|
||||
_row.append('')
|
||||
_row_i += 1
|
||||
else:
|
||||
_row[_row_i] += chunk
|
||||
_row[_row_i] += ' '
|
||||
else:
|
||||
_row.append('')
|
||||
_row_i += 1
|
||||
_row[_row_i] += word
|
||||
_row[_row_i] += ' '
|
||||
|
||||
_row = map(str, _row)
|
||||
_stack.append(separator.join(_row))
|
||||
|
||||
_s = '\n'.join(_stack)
|
||||
if is_color:
|
||||
_s = string_copy._new(_s)
|
||||
return _s
|
||||
+44
-10
@@ -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()
|
||||
|
||||
|
||||
+47
-5
@@ -9,10 +9,31 @@ 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):
|
||||
@@ -28,7 +49,7 @@ def mkdir_p(path):
|
||||
"""Emulates `mkdir -p` behavior."""
|
||||
try:
|
||||
makedirs(path)
|
||||
except OSError as exc: # Python >2.5
|
||||
except OSError, exc: # Python >2.5
|
||||
if exc.errno == errno.EEXIST:
|
||||
pass
|
||||
else:
|
||||
@@ -36,16 +57,37 @@ 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):
|
||||
"""Splits string into n sized chunks."""
|
||||
|
||||
stack = []
|
||||
|
||||
substack = []
|
||||
current_count = 0
|
||||
|
||||
for char in string:
|
||||
if not current_count < size:
|
||||
stack.append(''.join(substack))
|
||||
substack = []
|
||||
current_count = 0
|
||||
|
||||
substack.append(char)
|
||||
current_count += 1
|
||||
|
||||
if len(substack):
|
||||
stack.append(''.join(substack))
|
||||
|
||||
return stack
|
||||
|
||||
@@ -12,5 +12,5 @@ text = 'THIS TEXT IS COLORED %s!'
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
for color in colored.__all__:
|
||||
for color in colored.COLORS:
|
||||
print getattr(colored, color)(text % color.upper())
|
||||
@@ -1,6 +1,9 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from __future__ import with_statement
|
||||
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
|
||||
@@ -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'))
|
||||
@@ -1,6 +1,8 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from __future__ import with_statement
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from __future__ import with_statement
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
|
||||
+3
-1
@@ -1,6 +1,8 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from __future__ import with_statement
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
@@ -21,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.')
|
||||
|
||||
@@ -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)
|
||||
|
||||
Executable
+23
@@ -0,0 +1,23 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
sys.path.insert(0, os.path.abspath('..'))
|
||||
|
||||
from clint.textui import puts, colored
|
||||
from clint.textui import columns
|
||||
|
||||
lorem = 'Lorem ipsum dolor sit amet, consehdfhdfhdfhdfhdfhctetur adi pisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
|
||||
|
||||
if __name__ == '__main__':
|
||||
# puts(min_width('test\nit', 20) + ' me')
|
||||
# puts(max_width(lorem, 20) + ' me')
|
||||
|
||||
# print max_width(lorem, 45)
|
||||
|
||||
col = 60
|
||||
|
||||
puts(columns([(colored.red('Column 1')), col], [(colored.green('Column Two')), None], [(colored.magenta('Column III')), col]))
|
||||
puts(columns(['hi there my name is kenneth and this is a columns', col], [lorem, None], ['kenneths', col]))
|
||||
@@ -6,6 +6,9 @@ import sys
|
||||
|
||||
from distutils.core import setup
|
||||
|
||||
import clint
|
||||
|
||||
|
||||
|
||||
def publish():
|
||||
"""Publish to PyPi"""
|
||||
@@ -19,7 +22,7 @@ required = []
|
||||
|
||||
setup(
|
||||
name='clint',
|
||||
version='0.1.2',
|
||||
version=clint.__version__,
|
||||
description='Python Command-line Application Tools',
|
||||
long_description=open('README.rst').read() + '\n\n' +
|
||||
open('HISTORY.rst').read(),
|
||||
@@ -29,7 +32,6 @@ setup(
|
||||
packages= [
|
||||
'clint',
|
||||
'clint.textui',
|
||||
|
||||
'clint.packages', 'clint.packages.colorama'
|
||||
],
|
||||
install_requires=required,
|
||||
@@ -40,7 +42,7 @@ setup(
|
||||
'Natural Language :: English',
|
||||
'License :: OSI Approved :: ISC License (ISCL)',
|
||||
'Programming Language :: Python',
|
||||
# 'Programming Language :: Python :: 2.5',
|
||||
'Programming Language :: Python :: 2.5',
|
||||
'Programming Language :: Python :: 2.6',
|
||||
'Programming Language :: Python :: 2.7',
|
||||
# 'Programming Language :: Python :: 3.0',
|
||||
|
||||
+1
-3
@@ -6,7 +6,7 @@
|
||||
import unittest
|
||||
|
||||
|
||||
class TablibTestCase(unittest.TestCas):
|
||||
class TablibTestCase(unittest.TestCase):
|
||||
"""Tablib test cases."""
|
||||
|
||||
def setUp(self):
|
||||
@@ -16,7 +16,5 @@ class TablibTestCase(unittest.TestCas):
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user