mirror of
https://github.com/kennethreitz/clint.git
synced 2026-06-05 23:00:18 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4a06f19e4f | |||
| a841043b56 |
+1
-7
@@ -1,11 +1,5 @@
|
||||
.idea
|
||||
.project
|
||||
.settings
|
||||
.pydevproject
|
||||
MANIFEST
|
||||
*.pyc
|
||||
.tox
|
||||
build/
|
||||
clint.egg-info/
|
||||
dist/
|
||||
|
||||
*~
|
||||
|
||||
@@ -6,10 +6,6 @@ Development Lead
|
||||
|
||||
- Kenneth Reitz <me@kennethreitz.com>
|
||||
|
||||
Maintainers
|
||||
```````````
|
||||
|
||||
- Jason Piper <j.piper@me.com>
|
||||
|
||||
Patches and Suggestions
|
||||
```````````````````````
|
||||
@@ -18,15 +14,3 @@ Patches and Suggestions
|
||||
- Morgan Goose
|
||||
- Travis Swicegood
|
||||
- Will Thames
|
||||
- Greg Haskins
|
||||
- Miguel Araujo <maraujop>
|
||||
- takluyver
|
||||
- kracekumar
|
||||
- Alejandro Gómez <alejandrogomez>
|
||||
- Jason Piper <jpiper>
|
||||
- Gianluca Brindisi <gbrindisi>
|
||||
- Don Spaulding <donspauldingii@gmail.com>
|
||||
- Justin Barber <barber.justin (at) gmail>
|
||||
- Dmitry Medvinsky
|
||||
- Eric Anderson
|
||||
- Joshua Richardson
|
||||
|
||||
@@ -11,4 +11,4 @@ All functionality should be available in pure Python. Optional C (via Cython)
|
||||
implementations may be written for performance reasons, but should never
|
||||
replace the Python implementation.
|
||||
|
||||
Lastly, don't take yourself too seriously :)
|
||||
Lastly, don't take yourself too seriously :)
|
||||
-36
@@ -1,42 +1,6 @@
|
||||
History
|
||||
-------
|
||||
|
||||
0.3.5
|
||||
+++++
|
||||
* progress.bar is now a context manager - doesn't require an iterable anymore (thanks to @jric)
|
||||
* Bug fixes
|
||||
|
||||
0.3.4
|
||||
+++++
|
||||
* Fixed Python 3 basestring deprecation
|
||||
* Fixed examples
|
||||
|
||||
0.3.3
|
||||
+++++
|
||||
* Fixed Python 3 build issues
|
||||
* Fixed README and HISTORY being installed to /usr
|
||||
* Support added for bold text
|
||||
|
||||
0.3.2
|
||||
+++++
|
||||
* Unknown
|
||||
|
||||
0.3.1
|
||||
+++++
|
||||
* Unknown
|
||||
|
||||
0.3.0
|
||||
+++++
|
||||
|
||||
* Python 3 support!
|
||||
|
||||
0.2.4
|
||||
+++++
|
||||
|
||||
* New eng module
|
||||
* Win32 Bugfix
|
||||
|
||||
|
||||
0.2.3
|
||||
+++++
|
||||
|
||||
|
||||
+7
-8
@@ -4,7 +4,7 @@ Clint: Python Command-line Application Tools
|
||||
**Clint** is a module filled with a set of awesome tools for developing
|
||||
commandline applications.
|
||||
|
||||
.. image:: https://raw.github.com/kennethreitz/clint/master/misc/clint.jpeg
|
||||
.. image:: https://github.com/kennethreitz/clint/raw/master/misc/clint.jpeg
|
||||
|
||||
**C** ommand
|
||||
**L** ine
|
||||
@@ -24,7 +24,7 @@ The world's easiest to use implicit argument system w/ chaining methods for filt
|
||||
|
||||
Run the various executables in examples_ to get a good feel for what Clint offers.
|
||||
|
||||
.. _examples: https://github.com/kennethreitz/clint/tree/master/examples
|
||||
.. _examples: https://github.com/kennethreitz/clint/tree/develop/examples
|
||||
|
||||
You'll never want to not use it.
|
||||
|
||||
@@ -70,7 +70,7 @@ I want to quote my console text (like email). ::
|
||||
>>> puts('pretty cool, eh?')
|
||||
|
||||
not indented text
|
||||
> quoted text
|
||||
> indented text
|
||||
> pretty cool, eh?
|
||||
|
||||
I want to color my console text. ::
|
||||
@@ -91,9 +91,7 @@ I want to get data piped to stdin. ::
|
||||
|
||||
I want to get the first commandline argument passed in. ::
|
||||
|
||||
>>> from clint import arguments
|
||||
>>> args = arguments.Args()
|
||||
>>> args.get(0)
|
||||
>>> clint.args.get(0)
|
||||
|
||||
# if no argument was passed, get returns None
|
||||
|
||||
@@ -149,7 +147,7 @@ Contribute
|
||||
----------
|
||||
|
||||
If you'd like to contribute, simply fork `the repository`_, commit your changes
|
||||
to the **master** branch (or branch off of it), and send a pull request. Make
|
||||
to the **develop** branch (or branch off of it), and send a pull request. Make
|
||||
sure you add yourself to AUTHORS_.
|
||||
|
||||
|
||||
@@ -157,8 +155,9 @@ Roadmap
|
||||
-------
|
||||
- Unittests
|
||||
- Sphinx Documentation
|
||||
- Python 2.5, 3.1, 3.2 Support
|
||||
|
||||
|
||||
|
||||
.. _`the repository`: http://github.com/kennethreitz/clint
|
||||
.. _AUTHORS: http://github.com/kennethreitz/clint/blob/develop/AUTHORS
|
||||
.. _AUTHORS: http://github.com/kennethreitz/clint/blob/master/AUTHORS
|
||||
|
||||
+7
-11
@@ -11,14 +11,7 @@ This module sets up the main interface for all of clint.
|
||||
|
||||
from __future__ import absolute_import
|
||||
|
||||
try:
|
||||
from collections import OrderedDict
|
||||
except ImportError:
|
||||
from .packages.ordereddict import OrderedDict
|
||||
import collections
|
||||
collections.OrderedDict = OrderedDict
|
||||
|
||||
from .arguments import *
|
||||
from . import arguments
|
||||
from . import textui
|
||||
from . import utils
|
||||
from .pipes import piped_in
|
||||
@@ -26,9 +19,12 @@ from .pipes import piped_in
|
||||
|
||||
|
||||
__title__ = 'clint'
|
||||
__version__ = '0.3.5'
|
||||
__build__ = 0x000305
|
||||
__version__ = '0.2.3'
|
||||
__build__ = 0x000203
|
||||
__author__ = 'Kenneth Reitz'
|
||||
__license__ = 'ISC'
|
||||
__copyright__ = 'Copyright 2012 Kenneth Reitz'
|
||||
__copyright__ = 'Copyright 2011 Kenneth Reitz'
|
||||
__docformat__ = 'restructuredtext'
|
||||
|
||||
|
||||
args = arguments.Args()
|
||||
|
||||
+39
-23
@@ -13,17 +13,33 @@ 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
|
||||
|
||||
try:
|
||||
from collections import OrderedDict
|
||||
except ImportError:
|
||||
from .packages.ordereddict import OrderedDict
|
||||
|
||||
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."""
|
||||
|
||||
@@ -94,20 +110,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))
|
||||
@@ -196,7 +212,7 @@ class Args(object):
|
||||
return False
|
||||
else:
|
||||
return (x in self.all[index])
|
||||
|
||||
|
||||
except IndexError:
|
||||
return False
|
||||
|
||||
@@ -205,7 +221,7 @@ class Args(object):
|
||||
"""Returns true if argument exists at given index.
|
||||
Accepts: integer.
|
||||
"""
|
||||
|
||||
|
||||
try:
|
||||
self.all[x]
|
||||
return True
|
||||
@@ -215,15 +231,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
|
||||
|
||||
@@ -241,7 +257,7 @@ class Args(object):
|
||||
for arg in self.all:
|
||||
if arg.startswith('-'):
|
||||
_current_group = arg
|
||||
collection.setdefault(arg, Args(no_argv=True))
|
||||
collection[arg] = Args(no_argv=True)
|
||||
else:
|
||||
if _current_group:
|
||||
collection[_current_group]._args.append(arg)
|
||||
@@ -250,21 +266,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
|
||||
|
||||
|
||||
@@ -272,7 +288,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:
|
||||
@@ -311,7 +327,7 @@ class Args(object):
|
||||
return self.start_with('-')
|
||||
|
||||
|
||||
@property
|
||||
@property
|
||||
def not_flags(self):
|
||||
"""Returns Arg object excluding flagged arguments."""
|
||||
|
||||
@@ -325,7 +341,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))
|
||||
@@ -342,7 +358,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)
|
||||
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
clint.eng
|
||||
~~~~~~~~~
|
||||
|
||||
This module provides English language string helpers.
|
||||
|
||||
"""
|
||||
from __future__ import print_function
|
||||
|
||||
MORON_MODE = False
|
||||
COMMA = ','
|
||||
CONJUNCTION = 'and'
|
||||
SPACE = ' '
|
||||
|
||||
try:
|
||||
unicode
|
||||
except NameError:
|
||||
unicode = str
|
||||
|
||||
|
||||
def join(l, conj=CONJUNCTION, im_a_moron=MORON_MODE, separator=COMMA):
|
||||
"""Joins lists of words. Oxford comma and all."""
|
||||
|
||||
collector = []
|
||||
left = len(l)
|
||||
separator = separator + 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(separator)
|
||||
|
||||
collector.append(conj)
|
||||
|
||||
elif left is not 0:
|
||||
collector.append(separator)
|
||||
|
||||
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, deinit, reinit
|
||||
from .initialise import init
|
||||
from .ansi import Fore, Back, Style
|
||||
from .ansitowin32 import AnsiToWin32
|
||||
|
||||
VERSION = '0.2.3'
|
||||
VERSION = '0.1.18'
|
||||
|
||||
|
||||
@@ -2,11 +2,13 @@
|
||||
This module generates ANSI character codes to printing colors to terminals.
|
||||
See: http://en.wikipedia.org/wiki/ANSI_escape_code
|
||||
'''
|
||||
from . import caps
|
||||
|
||||
CSI = '\033['
|
||||
|
||||
def code_to_chars(code):
|
||||
return CSI + str(code) + 'm'
|
||||
def code_to_chars(codes):
|
||||
codes = [str(code) for code in codes]
|
||||
return CSI + (";".join(codes)) + 'm'
|
||||
|
||||
class AnsiCodes(object):
|
||||
def __init__(self, codes):
|
||||
@@ -14,34 +16,44 @@ class AnsiCodes(object):
|
||||
if not name.startswith('_'):
|
||||
value = getattr(codes, name)
|
||||
setattr(self, name, code_to_chars(value))
|
||||
self._method = codes._caps_method
|
||||
|
||||
def __call__(self, code):
|
||||
if isinstance(code, basestring):
|
||||
return getattr(self, code)
|
||||
else: # extended (pass to capability)
|
||||
return code_to_chars(getattr(caps.capability, self._method)(code))
|
||||
|
||||
class AnsiFore:
|
||||
BLACK = 30
|
||||
RED = 31
|
||||
GREEN = 32
|
||||
YELLOW = 33
|
||||
BLUE = 34
|
||||
MAGENTA = 35
|
||||
CYAN = 36
|
||||
WHITE = 37
|
||||
RESET = 39
|
||||
_caps_method = 'fg'
|
||||
BLACK = (30,)
|
||||
RED = (31,)
|
||||
GREEN = (32,)
|
||||
YELLOW = (33,)
|
||||
BLUE = (34,)
|
||||
MAGENTA = (35,)
|
||||
CYAN = (36,)
|
||||
WHITE = (37,)
|
||||
RESET = (39,)
|
||||
|
||||
class AnsiBack:
|
||||
BLACK = 40
|
||||
RED = 41
|
||||
GREEN = 42
|
||||
YELLOW = 43
|
||||
BLUE = 44
|
||||
MAGENTA = 45
|
||||
CYAN = 46
|
||||
WHITE = 47
|
||||
RESET = 49
|
||||
_caps_method = 'bg'
|
||||
BLACK = (40,)
|
||||
RED = (41,)
|
||||
GREEN = (42,)
|
||||
YELLOW = (43,)
|
||||
BLUE = (44,)
|
||||
MAGENTA = (45,)
|
||||
CYAN = (46,)
|
||||
WHITE = (47,)
|
||||
RESET = (49,)
|
||||
|
||||
class AnsiStyle:
|
||||
BRIGHT = 1
|
||||
DIM = 2
|
||||
NORMAL = 22
|
||||
RESET_ALL = 0
|
||||
_caps_method = 'style'
|
||||
BRIGHT = (1,)
|
||||
DIM = (2,)
|
||||
NORMAL = (22,)
|
||||
RESET_ALL = (0,)
|
||||
|
||||
Fore = AnsiCodes( AnsiFore )
|
||||
Back = AnsiCodes( AnsiBack )
|
||||
|
||||
@@ -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,))
|
||||
elif is_a_tty(self.wrapped):
|
||||
else:
|
||||
self.wrapped.write(Style.RESET_ALL)
|
||||
|
||||
|
||||
@@ -173,10 +173,4 @@ 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)
|
||||
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
from .conv import xterm256
|
||||
from .conv import ansi
|
||||
|
||||
class _Base(object):
|
||||
"""
|
||||
Base 16-color ANSI.
|
||||
All ANSI terminals as well as Windows support this.
|
||||
"""
|
||||
def fg(self, rgb):
|
||||
(colid, bright) = ansi.from_rgb(rgb)
|
||||
if bright:
|
||||
return (1, 30 + colid)
|
||||
else:
|
||||
return (30 + colid,)
|
||||
|
||||
def bg(self, rgb):
|
||||
(colid, bright) = ansi.from_rgb(rgb)
|
||||
# Ignore brightness for background?
|
||||
return (40 + colid)
|
||||
|
||||
def __str__():
|
||||
return "BASE"
|
||||
|
||||
class _XTerm256(object):
|
||||
"""
|
||||
256-color terminal. Most modern terminal emulators support this.
|
||||
Putty, Gnome Terminal, and even oldies like (m)rxvt, xterm.
|
||||
"""
|
||||
def fg(self, rgb):
|
||||
return (38, 5, xterm256.from_rgb(rgb))
|
||||
|
||||
def bg(self, rgb):
|
||||
return (48, 5, xterm256.from_rgb(rgb))
|
||||
|
||||
def __str__():
|
||||
return "XTERM256"
|
||||
|
||||
class _RGB(object):
|
||||
"""
|
||||
Full 24-bit RGB support.
|
||||
As far as I know, only Konsole can do this.
|
||||
"""
|
||||
def fg(self, rgb):
|
||||
return (38, 2) + rgb
|
||||
|
||||
def bg(self, rgb):
|
||||
return (48, 2) + rgb
|
||||
|
||||
def __str__():
|
||||
return "RGB"
|
||||
|
||||
class ColorCapability:
|
||||
ANSI = _Base() # Base 16-color ANSI support
|
||||
XTERM256 = _XTerm256() # xterm256 (GNOME terminal / Putty)
|
||||
RGB = _RGB() # Full RGB (Konsole)
|
||||
|
||||
# Current terminal caps, default to ANSI
|
||||
capability = ColorCapability.ANSI
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
"""
|
||||
ANSI 16-color conversion utilities.
|
||||
"""
|
||||
# Wladimir van der Laan, 2011
|
||||
#
|
||||
# The original ansi colors are defined as follows
|
||||
#
|
||||
# 0 black
|
||||
# 1 red
|
||||
# 2 green
|
||||
# 3 brown (green+red)
|
||||
# 4 blue
|
||||
# 5 magenta (blue+red)
|
||||
# 6 cyan (blue+green)
|
||||
# 7 white
|
||||
#
|
||||
# However, the exact RGB mappings depend on the terminal that
|
||||
# is used.
|
||||
# In addition to these colors, nearly all terminals support 8
|
||||
# colors that are simply brighter versions of the defined colors.
|
||||
|
||||
_colors = [
|
||||
(0,0,0),
|
||||
(128,0,0),
|
||||
(0,128,0),
|
||||
(128,128,0),
|
||||
(0,0,128),
|
||||
(128,0,128),
|
||||
(0,128,128),
|
||||
(192,192,192),
|
||||
(128,128,128),
|
||||
(255,0,0),
|
||||
(0,255,0),
|
||||
(255,255,0),
|
||||
(0,0,255),
|
||||
(255,0,255),
|
||||
(0,255,255),
|
||||
(255,255,255)
|
||||
]
|
||||
|
||||
def to_rgb(color):
|
||||
"""
|
||||
Convert ANSI color tuple (color_id, bright) to RGB tuple.
|
||||
Raise `ValueError` in case of invalid color spec.
|
||||
"""
|
||||
color = (color[1]<<3)|color[0]
|
||||
try:
|
||||
return _colors[color]
|
||||
except IndexError:
|
||||
raise ValueError("Color %i out of range" % color)
|
||||
|
||||
def from_rgb(rgb):
|
||||
"""
|
||||
Convert RGB tuple to ANSI color tuple (color_id, bright).
|
||||
Raise `ValueError` in case of invalid color spec.
|
||||
"""
|
||||
min_d = 1000000
|
||||
closest = None
|
||||
for idx,col in enumerate(_colors):
|
||||
dr = rgb[0]-col[0]
|
||||
dg = rgb[1]-col[1]
|
||||
db = rgb[2]-col[2]
|
||||
d = dr*dr + dg*dg + db*db
|
||||
if d < min_d:
|
||||
min_d = d
|
||||
closest = idx
|
||||
return (closest&7, closest>>3)
|
||||
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
"""
|
||||
HTML color conversion utilities.
|
||||
"""
|
||||
# Wladimir van der Laan, 2011
|
||||
|
||||
colors = {
|
||||
'aliceblue': '#F0F8FF',
|
||||
'antiquewhite': '#FAEBD7',
|
||||
'aqua': '#00FFFF',
|
||||
'aquamarine': '#7FFFD4',
|
||||
'azure': '#F0FFFF',
|
||||
'beige': '#F5F5DC',
|
||||
'bisque': '#FFE4C4',
|
||||
'black': '#000000',
|
||||
'blanchedalmond': '#FFEBCD',
|
||||
'blue': '#0000FF',
|
||||
'blueviolet': '#8A2BE2',
|
||||
'brown': '#A52A2A',
|
||||
'burlywood': '#DEB887',
|
||||
'cadetblue': '#5F9EA0',
|
||||
'chartreuse': '#7FFF00',
|
||||
'chocolate': '#D2691E',
|
||||
'coral': '#FF7F50',
|
||||
'cornflowerblue': '#6495ED',
|
||||
'cornsilk': '#FFF8DC',
|
||||
'crimson': '#DC143C',
|
||||
'cyan': '#00FFFF',
|
||||
'darkblue': '#00008B',
|
||||
'darkcyan': '#008B8B',
|
||||
'darkgoldenrod': '#B8860B',
|
||||
'darkgray': '#A9A9A9',
|
||||
'darkgreen': '#006400',
|
||||
'darkgrey': '#A9A9A9',
|
||||
'darkkhaki': '#BDB76B',
|
||||
'darkmagenta': '#8B008B',
|
||||
'darkolivegreen': '#556B2F',
|
||||
'darkorange': '#FF8C00',
|
||||
'darkorchid': '#9932CC',
|
||||
'darkred': '#8B0000',
|
||||
'darksalmon': '#E9967A',
|
||||
'darkseagreen': '#8FBC8F',
|
||||
'darkslateblue': '#483D8B',
|
||||
'darkslategray': '#2F4F4F',
|
||||
'darkslategrey': '#2F4F4F',
|
||||
'darkturquoise': '#00CED1',
|
||||
'darkviolet': '#9400D3',
|
||||
'deeppink': '#FF1493',
|
||||
'deepskyblue': '#00BFFF',
|
||||
'dimgray': '#696969',
|
||||
'dimgrey': '#696969',
|
||||
'dodgerblue': '#1E90FF',
|
||||
'firebrick': '#B22222',
|
||||
'floralwhite': '#FFFAF0',
|
||||
'forestgreen': '#228B22',
|
||||
'fuchsia': '#FF00FF',
|
||||
'gainsboro': '#DCDCDC',
|
||||
'ghostwhite': '#F8F8FF',
|
||||
'gold': '#FFD700',
|
||||
'goldenrod': '#DAA520',
|
||||
'gray': '#808080',
|
||||
'green': '#008000',
|
||||
'greenyellow': '#ADFF2F',
|
||||
'grey': '#808080',
|
||||
'honeydew': '#F0FFF0',
|
||||
'hotpink': '#FF69B4',
|
||||
'indianred ': '#CD5C5C',
|
||||
'indigo ': '#4B0082',
|
||||
'ivory': '#FFFFF0',
|
||||
'khaki': '#F0E68C',
|
||||
'lavender': '#E6E6FA',
|
||||
'lavenderblush': '#FFF0F5',
|
||||
'lawngreen': '#7CFC00',
|
||||
'lemonchiffon': '#FFFACD',
|
||||
'lightblue': '#ADD8E6',
|
||||
'lightcoral': '#F08080',
|
||||
'lightcyan': '#E0FFFF',
|
||||
'lightgoldenrodyellow': '#FAFAD2',
|
||||
'lightgray': '#D3D3D3',
|
||||
'lightgreen': '#90EE90',
|
||||
'lightgrey': '#D3D3D3',
|
||||
'lightpink': '#FFB6C1',
|
||||
'lightsalmon': '#FFA07A',
|
||||
'lightseagreen': '#20B2AA',
|
||||
'lightskyblue': '#87CEFA',
|
||||
'lightslategray': '#778899',
|
||||
'lightslategrey': '#778899',
|
||||
'lightsteelblue': '#B0C4DE',
|
||||
'lightyellow': '#FFFFE0',
|
||||
'lime': '#00FF00',
|
||||
'limegreen': '#32CD32',
|
||||
'linen': '#FAF0E6',
|
||||
'magenta': '#FF00FF',
|
||||
'maroon': '#800000',
|
||||
'mediumaquamarine': '#66CDAA',
|
||||
'mediumblue': '#0000CD',
|
||||
'mediumorchid': '#BA55D3',
|
||||
'mediumpurple': '#9370D8',
|
||||
'mediumseagreen': '#3CB371',
|
||||
'mediumslateblue': '#7B68EE',
|
||||
'mediumspringgreen': '#00FA9A',
|
||||
'mediumturquoise': '#48D1CC',
|
||||
'mediumvioletred': '#C71585',
|
||||
'midnightblue': '#191970',
|
||||
'mintcream': '#F5FFFA',
|
||||
'mistyrose': '#FFE4E1',
|
||||
'moccasin': '#FFE4B5',
|
||||
'navajowhite': '#FFDEAD',
|
||||
'navy': '#000080',
|
||||
'oldlace': '#FDF5E6',
|
||||
'olive': '#808000',
|
||||
'olivedrab': '#6B8E23',
|
||||
'orange': '#FFA500',
|
||||
'orangered': '#FF4500',
|
||||
'orchid': '#DA70D6',
|
||||
'palegoldenrod': '#EEE8AA',
|
||||
'palegreen': '#98FB98',
|
||||
'paleturquoise': '#AFEEEE',
|
||||
'palevioletred': '#D87093',
|
||||
'papayawhip': '#FFEFD5',
|
||||
'peachpuff': '#FFDAB9',
|
||||
'peru': '#CD853F',
|
||||
'pink': '#FFC0CB',
|
||||
'plum': '#DDA0DD',
|
||||
'powderblue': '#B0E0E6',
|
||||
'purple': '#800080',
|
||||
'red': '#FF0000',
|
||||
'rosybrown': '#BC8F8F',
|
||||
'royalblue': '#4169E1',
|
||||
'saddlebrown': '#8B4513',
|
||||
'salmon': '#FA8072',
|
||||
'sandybrown': '#F4A460',
|
||||
'seagreen': '#2E8B57',
|
||||
'seashell': '#FFF5EE',
|
||||
'sienna': '#A0522D',
|
||||
'silver': '#C0C0C0',
|
||||
'skyblue': '#87CEEB',
|
||||
'slateblue': '#6A5ACD',
|
||||
'slategray': '#708090',
|
||||
'slategrey': '#708090',
|
||||
'snow': '#FFFAFA',
|
||||
'springgreen': '#00FF7F',
|
||||
'steelblue': '#4682B4',
|
||||
'tan': '#D2B48C',
|
||||
'teal': '#008080',
|
||||
'thistle': '#D8BFD8',
|
||||
'tomato': '#FF6347',
|
||||
'turquoise': '#40E0D0',
|
||||
'violet': '#EE82EE',
|
||||
'wheat': '#F5DEB3',
|
||||
'white': '#FFFFFF',
|
||||
'whitesmoke': '#F5F5F5',
|
||||
'yellow': '#FFFF00',
|
||||
'yellowgreen': '#9ACD32'}
|
||||
|
||||
def to_rgb(color):
|
||||
"""
|
||||
Parse HTML color specification to RGB tuple.
|
||||
Raise `ValueError` in case of invalid color spec.
|
||||
"""
|
||||
if color in colors:
|
||||
color = colors[color]
|
||||
|
||||
if color.startswith("#"):
|
||||
color = color[1:]
|
||||
|
||||
if len(color) == 6: #RRGGBB
|
||||
rgb = (int(color[0:2],16),
|
||||
int(color[2:4],16),
|
||||
int(color[4:6],16))
|
||||
elif len(color) == 3: #RGB
|
||||
rgb = (int(color[0],16),
|
||||
int(color[1],16),
|
||||
int(color[2],16))
|
||||
rgb = tuple([(x<<4)|x for x in rgb])
|
||||
else:
|
||||
raise ValueError("Invalid color %s" % color)
|
||||
|
||||
return rgb
|
||||
|
||||
def from_rgb(rgb):
|
||||
"""
|
||||
Convert RGB tuple to HTML color specification.
|
||||
"""
|
||||
return "#%02x%02x%02x" % rgb
|
||||
@@ -0,0 +1,115 @@
|
||||
"""
|
||||
256-color xterm conversion utilities.
|
||||
"""
|
||||
# Wladimir van der Laan, 2011
|
||||
|
||||
# whole colortable, filled in later
|
||||
colortable = None
|
||||
|
||||
# the 6 value iterations in the xterm color cube
|
||||
valuerange = [ 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF ]
|
||||
|
||||
# 16 basic ANSI colors + lighter variants
|
||||
# these can be different depending on the terminal settings,
|
||||
# so do not use them when converting from RGB.
|
||||
basic16 = [
|
||||
[ 0x00, 0x00, 0x00 ], # 0
|
||||
[ 0xCD, 0x00, 0x00 ], # 1
|
||||
[ 0x00, 0xCD, 0x00 ], # 2
|
||||
[ 0xCD, 0xCD, 0x00 ], # 3
|
||||
[ 0x00, 0x00, 0xEE ], # 4
|
||||
[ 0xCD, 0x00, 0xCD ], # 5
|
||||
[ 0x00, 0xCD, 0xCD ], # 6
|
||||
[ 0xE5, 0xE5, 0xE5 ], # 7
|
||||
[ 0x7F, 0x7F, 0x7F ], # 8
|
||||
[ 0xFF, 0x00, 0x00 ], # 9
|
||||
[ 0x00, 0xFF, 0x00 ], # 10
|
||||
[ 0xFF, 0xFF, 0x00 ], # 11
|
||||
[ 0x5C, 0x5C, 0xFF ], # 12
|
||||
[ 0xFF, 0x00, 0xFF ], # 13
|
||||
[ 0x00, 0xFF, 0xFF ], # 14
|
||||
[ 0xFF, 0xFF, 0xFF ] # 15
|
||||
]
|
||||
|
||||
# Closest color on RGB color cube (from valuerange)
|
||||
closest6 = [
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
||||
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
||||
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3,
|
||||
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
|
||||
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
|
||||
3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
|
||||
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
|
||||
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5,
|
||||
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
|
||||
|
||||
def _color_dist(c, rgb):
|
||||
"""
|
||||
Return squared Euclidian distance of rgb to color `c`
|
||||
in the color table.
|
||||
"""
|
||||
d0 = colortable[c][0] - rgb[0]
|
||||
d1 = colortable[c][1] - rgb[1]
|
||||
d2 = colortable[c][2] - rgb[2]
|
||||
return d0*d0 + d1*d1 + d2*d2
|
||||
|
||||
def to_rgb(color):
|
||||
"""
|
||||
Convert a xterm256 color value (0-255) to 3 unsigned chars RGB
|
||||
tuple.
|
||||
"""
|
||||
rgb = [0,0,0]
|
||||
if color < 16:
|
||||
# 16 basic colors
|
||||
rgb[0] = basic16[color][0]
|
||||
rgb[1] = basic16[color][1]
|
||||
rgb[2] = basic16[color][2]
|
||||
elif color >= 16 and color <= 231:
|
||||
# color cube color
|
||||
color -= 16
|
||||
rgb[0] = valuerange[(color/36)%6]
|
||||
rgb[1] = valuerange[(color/6)%6]
|
||||
rgb[2] = valuerange[color%6]
|
||||
elif color >= 232 and color <= 255:
|
||||
# gray tone
|
||||
rgb[0] = rgb[1] = rgb[2] = 8+(color-232)*0x0a
|
||||
|
||||
return rgb
|
||||
|
||||
def from_rgb(rgb):
|
||||
"""
|
||||
Convert RGB tuple to xterm256 color value (0-255).
|
||||
"""
|
||||
# Optimized algorithm to find the xterm256 color with Euclidian closest distance to a
|
||||
# provided RGB value: as the palette consists of an independent 6x6x6 RGB cube
|
||||
# and a 24 grey tone scale, determine the closest color in both palettes, then
|
||||
# from these two return the on that's closest to the requested RGB color.
|
||||
|
||||
# Compute closest color on 6x6x6 RGB cube
|
||||
r = [closest6[x] for x in rgb]
|
||||
b = 16 + r[0]*36 + r[1]*6 + r[2]
|
||||
|
||||
# Compute closest point on greyscale line
|
||||
greyscale = sum(rgb)
|
||||
r = (greyscale-9) / 30
|
||||
c = 232 + max(min(r,23),0)
|
||||
|
||||
# Return RGB cube or greyscale color depending
|
||||
# on which is closest.
|
||||
distb = _color_dist(b, rgb)
|
||||
distc = _color_dist(c, rgb)
|
||||
if distb <= distc:
|
||||
color = b
|
||||
else:
|
||||
color = c
|
||||
|
||||
return color
|
||||
|
||||
colortable = [to_rgb(c) for c in xrange(0, 256)]
|
||||
@@ -7,9 +7,6 @@ from .ansitowin32 import AnsiToWin32
|
||||
orig_stdout = sys.stdout
|
||||
orig_stderr = sys.stderr
|
||||
|
||||
wrapped_stdout = sys.stdout
|
||||
wrapped_stderr = sys.stderr
|
||||
|
||||
atexit_done = False
|
||||
|
||||
|
||||
@@ -19,14 +16,11 @@ def reset_all():
|
||||
|
||||
def init(autoreset=False, convert=None, strip=None, wrap=True):
|
||||
|
||||
if not wrap and any([autoreset, convert, strip]):
|
||||
if wrap==False and (autoreset==True or convert==True or strip==True):
|
||||
raise ValueError('wrap=False conflicts with any other arg=True')
|
||||
|
||||
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)
|
||||
sys.stdout = wrap_stream(orig_stdout, convert, strip, autoreset, wrap)
|
||||
sys.stderr = wrap_stream(orig_stderr, convert, strip, autoreset, wrap)
|
||||
|
||||
global atexit_done
|
||||
if not atexit_done:
|
||||
@@ -34,16 +28,6 @@ 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,
|
||||
@@ -52,4 +36,3 @@ def wrap_stream(stream, convert, strip, autoreset, wrap):
|
||||
stream = wrapper.stream
|
||||
return stream
|
||||
|
||||
|
||||
|
||||
@@ -48,16 +48,8 @@ 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=STDOUT):
|
||||
def GetConsoleScreenBufferInfo(stream_id):
|
||||
handle = handles[stream_id]
|
||||
csbi = CONSOLE_SCREEN_BUFFER_INFO()
|
||||
success = windll.kernel32.GetConsoleScreenBufferInfo(
|
||||
@@ -70,54 +62,34 @@ else:
|
||||
|
||||
def SetConsoleTextAttribute(stream_id, attrs):
|
||||
handle = handles[stream_id]
|
||||
return windll.kernel32.SetConsoleTextAttribute(handle, attrs)
|
||||
success = windll.kernel32.SetConsoleTextAttribute(handle, attrs)
|
||||
assert success
|
||||
|
||||
def SetConsoleCursorPosition(stream_id, position):
|
||||
position = COORD(*position)
|
||||
# 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
|
||||
position = COORD(*position)
|
||||
success = windll.kernel32.SetConsoleCursorPosition(handle, position)
|
||||
assert 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)
|
||||
# Note that this is hard-coded for ANSI (vs wide) bytes.
|
||||
success = windll.kernel32.FillConsoleOutputCharacterA(
|
||||
# AttributeError: function 'FillConsoleOutputCharacter' not found
|
||||
# could it just be that my types are wrong?
|
||||
success = windll.kernel32.FillConsoleOutputCharacter(
|
||||
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)
|
||||
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))
|
||||
print(x.dwSize)
|
||||
print(x.dwCursorPosition)
|
||||
print(x.wAttributes)
|
||||
print(x.srWindow)
|
||||
print(x.dwMaximumWindowSize)
|
||||
|
||||
|
||||
@@ -22,7 +22,8 @@ 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
|
||||
@@ -66,37 +67,3 @@ 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))
|
||||
|
||||
+1
-1
@@ -115,7 +115,7 @@ class AppDir(object):
|
||||
remove(fn)
|
||||
else:
|
||||
removedirs(fn)
|
||||
except OSError as why:
|
||||
except OSError, why:
|
||||
if why.errno == errno.ENOENT:
|
||||
pass
|
||||
else:
|
||||
|
||||
@@ -7,13 +7,9 @@ clint.textui
|
||||
This module provides the text output helper system.
|
||||
|
||||
"""
|
||||
import sys
|
||||
if sys.platform.startswith('win'):
|
||||
from ..packages import colorama
|
||||
colorama.init()
|
||||
|
||||
|
||||
from . import colored
|
||||
from . import progress
|
||||
from . import prompt
|
||||
|
||||
from .core import *
|
||||
from core import *
|
||||
|
||||
+50
-69
@@ -14,9 +14,8 @@ from __future__ import absolute_import
|
||||
import re
|
||||
import sys
|
||||
|
||||
PY3 = sys.version_info[0] >= 3
|
||||
|
||||
from ..packages import colorama
|
||||
from ..packages.colorama.conv import html
|
||||
|
||||
__all__ = (
|
||||
'red', 'green', 'yellow', 'blue',
|
||||
@@ -25,52 +24,25 @@ __all__ = (
|
||||
)
|
||||
|
||||
COLORS = __all__[:-2]
|
||||
DISABLE_COLOR = False
|
||||
|
||||
if 'get_ipython' in dir():
|
||||
"""
|
||||
when ipython is fired lot of variables like _oh, etc are used.
|
||||
There are so many ways to find current python interpreter is ipython.
|
||||
get_ipython is easiest is most appealing for readers to understand.
|
||||
"""
|
||||
if not sys.stdout.isatty():
|
||||
DISABLE_COLOR = True
|
||||
else:
|
||||
DISABLE_COLOR = False
|
||||
|
||||
|
||||
|
||||
class ColoredString(object):
|
||||
"""Enhanced string for __len__ operations on Colored output."""
|
||||
def __init__(self, color, s, always_color=False, bold=False):
|
||||
def __init__(self, color, s):
|
||||
super(ColoredString, self).__init__()
|
||||
self.s = s
|
||||
self.color = color
|
||||
self.always_color = always_color
|
||||
self.bold = bold
|
||||
|
||||
def __getattr__(self, att):
|
||||
def func_help(*args, **kwargs):
|
||||
result = getattr(self.s, att)(*args, **kwargs)
|
||||
try:
|
||||
is_result_string = isinstance(result, basestring)
|
||||
except NameError:
|
||||
is_result_string = isinstance(result, str)
|
||||
if is_result_string:
|
||||
return self._new(result)
|
||||
elif isinstance(result, list):
|
||||
return [self._new(x) for x in result]
|
||||
else:
|
||||
return result
|
||||
return func_help
|
||||
|
||||
@property
|
||||
def color_str(self):
|
||||
style = 'BRIGHT' if self.bold else 'NORMAL'
|
||||
c = '%s%s%s%s' % (getattr(colorama.Fore, self.color), getattr(colorama.Style, style), self.s, colorama.Fore.RESET)
|
||||
|
||||
if self.always_color:
|
||||
return c
|
||||
elif sys.stdout.isatty() and not DISABLE_COLOR:
|
||||
return c
|
||||
if sys.stdout.isatty() and not DISABLE_COLOR:
|
||||
colorama.init(autoreset=True)
|
||||
return '%s%s%s' % (
|
||||
colorama.Fore(self.color), self.s, colorama.Fore.RESET)
|
||||
else:
|
||||
return self.s
|
||||
|
||||
@@ -81,23 +53,11 @@ class ColoredString(object):
|
||||
def __repr__(self):
|
||||
return "<%s-string: '%s'>" % (self.color, self.s)
|
||||
|
||||
def __str__(self):
|
||||
return self.__unicode__().encode('utf8')
|
||||
|
||||
def __unicode__(self):
|
||||
value = self.color_str
|
||||
if isinstance(value, bytes):
|
||||
return value.decode('utf8')
|
||||
return value
|
||||
|
||||
if PY3:
|
||||
__str__ = __unicode__
|
||||
else:
|
||||
def __str__(self):
|
||||
value = self.color_str
|
||||
if isinstance(value, bytes):
|
||||
return value
|
||||
return value.encode('utf8')
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self.color_str)
|
||||
return self.color_str
|
||||
|
||||
def __add__(self, other):
|
||||
return str(self.color_str) + str(other)
|
||||
@@ -108,6 +68,9 @@ class ColoredString(object):
|
||||
def __mul__(self, other):
|
||||
return (self.color_str * other)
|
||||
|
||||
def split(self, x=' '):
|
||||
return map(self._new, self.s.split(x))
|
||||
|
||||
def _new(self, s):
|
||||
return ColoredString(self.color, s)
|
||||
|
||||
@@ -116,35 +79,53 @@ def clean(s):
|
||||
strip = re.compile("([^-_a-zA-Z0-9!@#%&=,/'\";:~`\$\^\*\(\)\+\[\]\.\{\}\|\?\<\>\\]+|[^\s]+)")
|
||||
txt = strip.sub('', str(s))
|
||||
|
||||
strip = re.compile(r'\[\d+m')
|
||||
strip = re.compile(r'\[[\d;]+m')
|
||||
txt = strip.sub('', txt)
|
||||
|
||||
return txt
|
||||
|
||||
|
||||
def black(string, always=False, bold=False):
|
||||
return ColoredString('BLACK', string, always_color=always, bold=bold)
|
||||
def black(string):
|
||||
return ColoredString('BLACK', string)
|
||||
|
||||
def red(string, always=False, bold=False):
|
||||
return ColoredString('RED', string, always_color=always, bold=bold)
|
||||
def red(string):
|
||||
return ColoredString('RED', string)
|
||||
|
||||
def green(string, always=False, bold=False):
|
||||
return ColoredString('GREEN', string, always_color=always, bold=bold)
|
||||
def green(string):
|
||||
return ColoredString('GREEN', string)
|
||||
|
||||
def yellow(string, always=False, bold=False):
|
||||
return ColoredString('YELLOW', string, always_color=always, bold=bold)
|
||||
def yellow(string):
|
||||
return ColoredString('YELLOW', string)
|
||||
|
||||
def blue(string, always=False, bold=False):
|
||||
return ColoredString('BLUE', string, always_color=always, bold=bold)
|
||||
def blue(string):
|
||||
return ColoredString('BLUE', string)
|
||||
|
||||
def magenta(string, always=False, bold=False):
|
||||
return ColoredString('MAGENTA', string, always_color=always, bold=bold)
|
||||
def magenta(string):
|
||||
return ColoredString('MAGENTA', string)
|
||||
|
||||
def cyan(string, always=False, bold=False):
|
||||
return ColoredString('CYAN', string, always_color=always, bold=bold)
|
||||
def cyan(string):
|
||||
return ColoredString('CYAN', string)
|
||||
|
||||
def white(string, always=False, bold=False):
|
||||
return ColoredString('WHITE', string, always_color=always, bold=bold)
|
||||
def white(string):
|
||||
return ColoredString('WHITE', string)
|
||||
|
||||
def rgb(color, string):
|
||||
"""
|
||||
RGB-colored text.
|
||||
|
||||
The `color` argument can have the following formats:
|
||||
- #3f3f3f
|
||||
- 3f3f3f
|
||||
- ('3f', '3f', '3f')
|
||||
- (100, 24, 244)
|
||||
"""
|
||||
if isinstance(color, basestring):
|
||||
color = html.to_rgb(color)
|
||||
else:
|
||||
if isinstance(color[0], basestring):
|
||||
color = (int(color[0],16), int(color[1],16), int(color[2],16))
|
||||
|
||||
return ColoredString(color, string)
|
||||
|
||||
def disable():
|
||||
"""Disables colors."""
|
||||
|
||||
@@ -94,6 +94,7 @@ def columns(*cols, **kwargs):
|
||||
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):
|
||||
|
||||
+54
-49
@@ -13,15 +13,12 @@ from __future__ import absolute_import
|
||||
|
||||
import sys
|
||||
|
||||
from contextlib import contextmanager
|
||||
|
||||
from .formatters import max_width, min_width
|
||||
from .cols import columns
|
||||
from ..utils import tsplit
|
||||
|
||||
|
||||
__all__ = ('puts', 'puts_err', 'indent', 'dedent', 'columns', 'max_width',
|
||||
'min_width', 'STDOUT', 'STDERR')
|
||||
__all__ = ('puts', 'puts_err', 'indent', 'columns', 'max_width', 'min_width')
|
||||
|
||||
|
||||
STDOUT = sys.stdout.write
|
||||
@@ -29,60 +26,68 @@ STDERR = sys.stderr.write
|
||||
|
||||
NEWLINES = ('\n', '\r', '\r\n')
|
||||
|
||||
INDENT_STRINGS = []
|
||||
|
||||
# Private
|
||||
|
||||
def _indent(indent=0, quote='', indent_char=' '):
|
||||
"""Indent util function, compute new indent_string"""
|
||||
if indent > 0:
|
||||
indent_string = ''.join((
|
||||
str(quote),
|
||||
(indent_char * (indent - len(quote)))
|
||||
class Writer(object):
|
||||
"""WriterUtilized by context managers."""
|
||||
|
||||
shared = dict(indent_level=0, indent_strings=[])
|
||||
|
||||
|
||||
def __init__(self, indent=0, quote='', indent_char=' '):
|
||||
self.indent = indent
|
||||
self.indent_char = indent_char
|
||||
self.indent_quote = quote
|
||||
if self.indent > 0:
|
||||
self.indent_string = ''.join((
|
||||
str(quote),
|
||||
(self.indent_char * (indent - len(self.indent_quote)))
|
||||
))
|
||||
else:
|
||||
self.indent_string = ''.join((
|
||||
('\x08' * (-1 * (indent - len(self.indent_quote)))),
|
||||
str(quote))
|
||||
)
|
||||
|
||||
if len(self.indent_string):
|
||||
self.shared['indent_strings'].append(self.indent_string)
|
||||
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
|
||||
def __exit__(self, type, value, traceback):
|
||||
self.shared['indent_strings'].pop()
|
||||
|
||||
|
||||
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),
|
||||
'\n' if newline else ''
|
||||
))
|
||||
else:
|
||||
indent_string = ''.join((
|
||||
('\x08' * (-1 * (indent - len(quote)))),
|
||||
str(quote))
|
||||
)
|
||||
stream(_str)
|
||||
|
||||
if len(indent_string):
|
||||
INDENT_STRINGS.append(indent_string)
|
||||
|
||||
# Public
|
||||
|
||||
def puts(s='', newline=True, stream=STDOUT):
|
||||
"""Prints given string to stdout."""
|
||||
if newline:
|
||||
s = tsplit(s, NEWLINES)
|
||||
s = map(str, s)
|
||||
indent = ''.join(INDENT_STRINGS)
|
||||
"""Prints given string to stdout via Writer interface."""
|
||||
Writer()(s, newline, stream=stream)
|
||||
|
||||
s = (str('\n' + indent)).join(s)
|
||||
|
||||
_str = ''.join((
|
||||
''.join(INDENT_STRINGS),
|
||||
str(s),
|
||||
'\n' if newline else ''
|
||||
))
|
||||
stream(_str)
|
||||
|
||||
def puts_err(s='', newline=True, stream=STDERR):
|
||||
"""Prints given string to stderr."""
|
||||
puts(s, newline, stream)
|
||||
"""Prints given string to stderr via Writer interface."""
|
||||
Writer()(s, newline, stream=stream)
|
||||
|
||||
def dedent():
|
||||
"""Dedent next strings, use only if you use indent otherwise than as a
|
||||
context."""
|
||||
INDENT_STRINGS.pop()
|
||||
|
||||
@contextmanager
|
||||
def _indent_context():
|
||||
"""Indentation context manager."""
|
||||
yield
|
||||
dedent()
|
||||
|
||||
def indent(indent=4, quote=''):
|
||||
"""Indentation manager, return an indentation context manager."""
|
||||
_indent(indent, quote)
|
||||
return _indent_context()
|
||||
"""Indentation context manager."""
|
||||
return Writer(indent=indent, quote=quote)
|
||||
|
||||
+31
-124
@@ -11,142 +11,27 @@ This module provides the progressbar functionality.
|
||||
from __future__ import absolute_import
|
||||
|
||||
import sys
|
||||
import time
|
||||
|
||||
STREAM = sys.stderr
|
||||
# Only show bar in terminals by default (better for piping, logging etc.)
|
||||
try:
|
||||
HIDE_DEFAULT = not STREAM.isatty()
|
||||
except AttributeError: # output does not support isatty()
|
||||
HIDE_DEFAULT = True
|
||||
|
||||
BAR_TEMPLATE = '%s[%s%s] %i/%i - %s\r'
|
||||
MILL_TEMPLATE = '%s %s %i/%i\r'
|
||||
BAR_TEMPLATE = '%s[%s%s] %i/%i\r'
|
||||
BAR_EMPTY_CHAR = '-'
|
||||
BAR_FILLED_CHAR = '='
|
||||
|
||||
DOTS_CHAR = '.'
|
||||
BAR_FILLED_CHAR = '#'
|
||||
BAR_EMPTY_CHAR = ' '
|
||||
MILL_CHARS = ['|', '/', '-', '\\']
|
||||
|
||||
#How long to wait before recalculating the ETA
|
||||
ETA_INTERVAL = 1
|
||||
#How many intervals (excluding the current one) to calculate the simple moving average
|
||||
ETA_SMA_WINDOW = 9
|
||||
|
||||
class Bar(object):
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||
self.done()
|
||||
return False # we're not surpressing exceptions
|
||||
|
||||
def __init__(self, label='', width=32, hide=None, empty_char=BAR_EMPTY_CHAR,
|
||||
filled_char=BAR_FILLED_CHAR, expected_size=None, every=1):
|
||||
self.label = label
|
||||
self.width = width
|
||||
self.hide = hide
|
||||
if hide is None:
|
||||
try:
|
||||
self.hide = not STREAM.isatty()
|
||||
except AttributeError: # output does not support isatty()
|
||||
self.hide = True
|
||||
self.empty_char = empty_char
|
||||
self.filled_char = filled_char
|
||||
self.expected_size = expected_size
|
||||
self.every = every
|
||||
self.start = time.time()
|
||||
self.ittimes = []
|
||||
self.eta = 0
|
||||
self.etadelta = time.time()
|
||||
self.etadisp = time.strftime('%H:%M:%S', time.gmtime(self.eta))
|
||||
if (self.expected_size):
|
||||
self.show(0)
|
||||
|
||||
def show(self, progress, count=None):
|
||||
if count is not None:
|
||||
self.expected_size = count
|
||||
if self.expected_size is None:
|
||||
raise Exception("expected_size not initialized")
|
||||
if (time.time() - self.etadelta) > ETA_INTERVAL:
|
||||
self.etadelta = time.time()
|
||||
self.ittimes = \
|
||||
self.ittimes[-ETA_SMA_WINDOW:]+\
|
||||
[-(self.start-time.time())/(progress+1)]
|
||||
self.eta = \
|
||||
sum(self.ittimes)/float(len(self.ittimes)) * \
|
||||
(self.expected_size-progress)
|
||||
self.etadisp = time.strftime('%H:%M:%S', time.gmtime(self.eta))
|
||||
x = int(self.width*progress/self.expected_size)
|
||||
if not self.hide:
|
||||
if ((progress % self.every)==0 or # True every "every" updates
|
||||
(progress == self.expected_size)): # And when we're done
|
||||
STREAM.write(BAR_TEMPLATE % (
|
||||
self.label, self.filled_char*x,
|
||||
self.empty_char*(self.width-x), progress,
|
||||
self.expected_size, self.etadisp))
|
||||
STREAM.flush()
|
||||
|
||||
def done(self):
|
||||
if not self.hide:
|
||||
STREAM.write('\n')
|
||||
STREAM.flush()
|
||||
|
||||
def bar(it, label='', width=32, hide=HIDE_DEFAULT, empty_char=BAR_EMPTY_CHAR, filled_char=BAR_FILLED_CHAR, expected_size=None, every=1):
|
||||
def bar(it, label='', width=32, hide=False):
|
||||
"""Progress iterator. Wrap your iterables with it."""
|
||||
|
||||
count = len(it) if expected_size is None else expected_size
|
||||
|
||||
with Bar(label=label, width=width, hide=hide, empty_char=BAR_EMPTY_CHAR,
|
||||
filled_char=BAR_FILLED_CHAR, expected_size=count, every=every) \
|
||||
as bar:
|
||||
|
||||
for i, item in enumerate(it):
|
||||
|
||||
yield item
|
||||
bar.show(i+1)
|
||||
|
||||
def dots(it, label='', hide=HIDE_DEFAULT, every=1):
|
||||
"""Progress iterator. Prints a dot for each item being iterated"""
|
||||
|
||||
count = 0
|
||||
|
||||
if not hide:
|
||||
STREAM.write(label)
|
||||
|
||||
for (i, item) in enumerate(it):
|
||||
if not hide:
|
||||
if (i % every)==0: # True every "every" updates
|
||||
STREAM.write(DOTS_CHAR)
|
||||
sys.stderr.flush()
|
||||
|
||||
count += 1
|
||||
|
||||
yield item
|
||||
|
||||
STREAM.write('\n')
|
||||
STREAM.flush()
|
||||
|
||||
|
||||
def mill(it, label='', hide=HIDE_DEFAULT, expected_size=None, every=1):
|
||||
"""Progress iterator. Prints a mill while iterating over the items."""
|
||||
|
||||
def _mill_char(_i):
|
||||
if _i >= count:
|
||||
return ' '
|
||||
else:
|
||||
return MILL_CHARS[(_i / every) % len(MILL_CHARS)]
|
||||
|
||||
def _show(_i):
|
||||
x = int(width*_i/count)
|
||||
if not hide:
|
||||
if ((_i % every)==0 or # True every "every" updates
|
||||
(_i == count)): # And when we're done
|
||||
STREAM.write(BAR_TEMPLATE % (
|
||||
label, BAR_FILLED_CHAR*x, BAR_EMPTY_CHAR*(width-x), _i, count))
|
||||
STREAM.flush()
|
||||
|
||||
STREAM.write(MILL_TEMPLATE % (
|
||||
label, _mill_char(_i), _i, count))
|
||||
STREAM.flush()
|
||||
|
||||
count = len(it) if expected_size is None else expected_size
|
||||
count = len(it)
|
||||
|
||||
if count:
|
||||
_show(0)
|
||||
@@ -159,3 +44,25 @@ def mill(it, label='', hide=HIDE_DEFAULT, expected_size=None, every=1):
|
||||
if not hide:
|
||||
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()
|
||||
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
# -*- coding: utf8 -*-
|
||||
|
||||
"""
|
||||
clint.textui.prompt
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Module for simple interactive prompts handling
|
||||
|
||||
"""
|
||||
|
||||
from __future__ import absolute_import, print_function
|
||||
|
||||
from re import match, I
|
||||
|
||||
def yn(prompt, default='y', batch=False):
|
||||
# A sanity check against default value
|
||||
# If not y/n then y is assumed
|
||||
if default not in ['y', 'n']:
|
||||
default = 'y'
|
||||
|
||||
# Let's build the prompt
|
||||
choicebox = '[Y/n]' if default == 'y' else '[y/N]'
|
||||
prompt = prompt + ' ' + choicebox + ' '
|
||||
|
||||
# If input is not a yes/no variant or empty
|
||||
# keep asking
|
||||
while True:
|
||||
# If batch option is True then auto reply
|
||||
# with default input
|
||||
if not batch:
|
||||
input = raw_input(prompt).strip()
|
||||
else:
|
||||
print(prompt)
|
||||
input = ''
|
||||
|
||||
# If input is empty default choice is assumed
|
||||
# so we return True
|
||||
if input == '':
|
||||
return True
|
||||
|
||||
# Given 'yes' as input if default choice is y
|
||||
# then return True, False otherwise
|
||||
if match('y(?:es)?', input, I):
|
||||
return True if default == 'y' else False
|
||||
|
||||
# Given 'no' as input if default choice is n
|
||||
# then return True, False otherwise
|
||||
elif match('n(?:o)?', input, I):
|
||||
return True if default == 'n' else False
|
||||
+32
-34
@@ -11,33 +11,9 @@ 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
|
||||
|
||||
try:
|
||||
basestring
|
||||
except NameError:
|
||||
basestring = str
|
||||
|
||||
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):
|
||||
@@ -53,7 +29,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:
|
||||
@@ -61,15 +37,37 @@ def mkdir_p(path):
|
||||
|
||||
def tsplit(string, delimiters):
|
||||
"""Behaves str.split but supports tuples of delimiters."""
|
||||
|
||||
delimiters = tuple(delimiters)
|
||||
if len(delimiters) < 1:
|
||||
return [string,]
|
||||
final_delimiter = delimiters[0]
|
||||
for i in delimiters[1:]:
|
||||
string = string.replace(i, final_delimiter)
|
||||
return string.split(final_delimiter)
|
||||
|
||||
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."""
|
||||
return [string[i:i+size] for i in range(0, len(string), size)]
|
||||
|
||||
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
|
||||
|
||||
Executable → Regular
+2
-5
@@ -6,11 +6,9 @@ import os
|
||||
|
||||
sys.path.insert(0, os.path.abspath('..'))
|
||||
|
||||
from clint.arguments import Args
|
||||
from clint import args
|
||||
from clint.textui import puts, colored, indent
|
||||
|
||||
args = Args()
|
||||
|
||||
with indent(4, quote='>>>'):
|
||||
puts(colored.red('Aruments passed in: ') + str(args.all))
|
||||
puts(colored.red('Flags detected: ') + str(args.flags))
|
||||
@@ -18,5 +16,4 @@ with indent(4, quote='>>>'):
|
||||
puts(colored.red('NOT Files detected: ') + str(args.not_files))
|
||||
puts(colored.red('Grouped Arguments: ') + str(dict(args.grouped)))
|
||||
|
||||
print
|
||||
|
||||
print
|
||||
@@ -1,8 +1,6 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
@@ -15,4 +13,4 @@ text = 'THIS TEXT IS COLORED %s!'
|
||||
if __name__ == '__main__':
|
||||
|
||||
for color in colored.COLORS:
|
||||
print(getattr(colored, color)(text % color.upper()))
|
||||
print getattr(colored, color)(text % color.upper())
|
||||
Executable
+40
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
sys.path.insert(0, os.path.abspath('..'))
|
||||
|
||||
from clint.textui import colored
|
||||
from clint.packages.colorama.conv import html
|
||||
from clint.packages.colorama import caps
|
||||
from clint.textui import columns
|
||||
|
||||
# Force capability to XTERM256 for now, otherwise it's boring
|
||||
# I'm not sure a reliable detection mechanism even exists...
|
||||
caps.capability = caps.ColorCapability.XTERM256
|
||||
|
||||
def colorizer(colors):
|
||||
for color in colors:
|
||||
yield colored.rgb(color, color)
|
||||
|
||||
if __name__ == '__main__':
|
||||
i = iter(colorizer(html.colors))
|
||||
while True:
|
||||
try:
|
||||
a = i.next()
|
||||
except StopIteration:
|
||||
break
|
||||
try:
|
||||
b = i.next()
|
||||
except StopIteration:
|
||||
b = ""
|
||||
try:
|
||||
c = i.next()
|
||||
except StopIteration:
|
||||
c = ""
|
||||
print columns([a,30],[b,30],[c,30])
|
||||
|
||||
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
#!/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 = [str(cs) for cs in 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,13 +0,0 @@
|
||||
#! /usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from clint.arguments import Args
|
||||
from clint.textui import puts, colored
|
||||
|
||||
all_args = Args().grouped
|
||||
|
||||
for item in all_args:
|
||||
if item is not '_':
|
||||
puts(colored.red("key:%s"%item))
|
||||
print(all_args[item].all)
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
echo "python get_each_args.py --name kracekumar --email me@kracekumar.com"
|
||||
python get_each_args.py --name kracekumar --email me@kracekumar.com
|
||||
echo "python get_each_args.py --languages python c html ruby --email me@kracekumar.com"
|
||||
python get_each_args.py --langauges python c html ruby --email me@kracekumar.com
|
||||
+1
-15
@@ -14,21 +14,7 @@ from clint.textui import progress
|
||||
if __name__ == '__main__':
|
||||
for i in progress.bar(range(100)):
|
||||
sleep(random() * 0.2)
|
||||
|
||||
with progress.Bar(label="nonlinear", expected_size=10) as bar:
|
||||
last_val = 0
|
||||
for val in (1,2,3,9,10):
|
||||
sleep(2 * (val - last_val))
|
||||
bar.show(val)
|
||||
last_val = val
|
||||
|
||||
for i in progress.dots(range(100)):
|
||||
sleep(random() * 0.2)
|
||||
|
||||
for i in progress.mill(range(100)):
|
||||
sleep(random() * 0.2)
|
||||
|
||||
# Override the expected_size, for iterables that don't support len()
|
||||
D = dict(zip(range(100), range(100)))
|
||||
for k, v in progress.bar(D.iteritems(), expected_size=len(D)):
|
||||
sleep(random() * 0.2)
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
@@ -15,16 +13,16 @@ resources.init('kennethreitz', 'clint')
|
||||
lorem = '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.'
|
||||
|
||||
|
||||
print('%s created.' % resources.user.path)
|
||||
print '%s created.' % resources.user.path
|
||||
|
||||
resources.user.write('lorem.txt', lorem)
|
||||
print('lorem.txt created')
|
||||
print 'lorem.txt created'
|
||||
|
||||
assert resources.user.read('lorem.txt') == lorem
|
||||
print('lorem.txt has correct contents')
|
||||
print 'lorem.txt has correct contents'
|
||||
|
||||
resources.user.delete('lorem.txt')
|
||||
print('lorem.txt deleted')
|
||||
print 'lorem.txt deleted'
|
||||
|
||||
assert resources.user.read('lorem.txt') == None
|
||||
print('lorem.txt deletion confirmed')
|
||||
print 'lorem.txt deletion confirmed'
|
||||
@@ -19,6 +19,5 @@ if __name__ == '__main__':
|
||||
|
||||
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]))
|
||||
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]))
|
||||
@@ -1,4 +0,0 @@
|
||||
{
|
||||
"title": "Bashō's 'old pond'",
|
||||
"text": "古池や蛙飛込む水の音"
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
import os
|
||||
import sys
|
||||
import codecs
|
||||
|
||||
sys.path.insert(0, os.path.abspath('..'))
|
||||
|
||||
try:
|
||||
import json
|
||||
except:
|
||||
import simplejson as json
|
||||
|
||||
from clint.arguments import Args
|
||||
from clint import piped_in
|
||||
from clint.textui import colored, puts, indent
|
||||
|
||||
args = Args()
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
puts('Test:')
|
||||
with indent(4):
|
||||
puts('%s Fake test 1.' % colored.green('✔'))
|
||||
puts('%s Fake test 2.' % colored.red('✖'))
|
||||
|
||||
puts('')
|
||||
puts('Greet:')
|
||||
with indent(4):
|
||||
puts(colored.red('Здравствуйте'))
|
||||
puts(colored.green('你好。'))
|
||||
puts(colored.yellow('سلام'))
|
||||
puts(colored.magenta('안녕하세요'))
|
||||
puts(colored.blue('नमस्ते'))
|
||||
puts(colored.cyan('γειά σου'))
|
||||
|
||||
puts('')
|
||||
puts('Arguments:')
|
||||
with indent(4):
|
||||
puts('%s' % colored.red(args[0]))
|
||||
|
||||
puts('')
|
||||
puts('File:')
|
||||
with indent(4):
|
||||
f = args.files[0]
|
||||
puts(colored.yellow('%s:' % f))
|
||||
with indent(2):
|
||||
fd = codecs.open(f, encoding='utf-8')
|
||||
for line in fd:
|
||||
line = line.strip('\n\r')
|
||||
puts(colored.yellow(' %s' % line))
|
||||
fd.close()
|
||||
|
||||
puts('')
|
||||
puts('Input:')
|
||||
with indent(4):
|
||||
in_data = json.loads(piped_in())
|
||||
title = in_data['title']
|
||||
text = in_data['text']
|
||||
puts(colored.blue('Title: %s' % title))
|
||||
puts(colored.magenta('Text: %s' % text))
|
||||
@@ -1,3 +0,0 @@
|
||||
#!/usr/bin/env sh
|
||||
|
||||
python unicode.py こんにちは。 unicode.json < unicode.json
|
||||
@@ -4,13 +4,12 @@
|
||||
import os
|
||||
import sys
|
||||
|
||||
try:
|
||||
from setuptools import setup
|
||||
except ImportError:
|
||||
from distutils.core import setup
|
||||
from distutils.core import setup
|
||||
|
||||
import clint
|
||||
|
||||
|
||||
|
||||
def publish():
|
||||
"""Publish to PyPi"""
|
||||
os.system("python setup.py sdist upload")
|
||||
@@ -19,7 +18,7 @@ if sys.argv[-1] == "publish":
|
||||
publish()
|
||||
sys.exit()
|
||||
|
||||
required = ['args']
|
||||
required = []
|
||||
|
||||
setup(
|
||||
name='clint',
|
||||
@@ -39,17 +38,14 @@ setup(
|
||||
license='ISC',
|
||||
classifiers=(
|
||||
# 'Development Status :: 5 - Production/Stable',
|
||||
'Environment :: Console',
|
||||
'Intended Audience :: Developers',
|
||||
'Natural Language :: English',
|
||||
'License :: OSI Approved :: ISC License (ISCL)',
|
||||
'Programming Language :: Python',
|
||||
'Programming Language :: Python :: 2',
|
||||
'Programming Language :: Python :: 2.5',
|
||||
'Programming Language :: Python :: 2.6',
|
||||
'Programming Language :: Python :: 2.7',
|
||||
'Programming Language :: Python :: 3',
|
||||
'Programming Language :: Python :: 3.1',
|
||||
'Programming Language :: Python :: 3.2',
|
||||
'Topic :: Terminals :: Terminal Emulators/X Terminals',
|
||||
# 'Programming Language :: Python :: 3.0',
|
||||
# 'Programming Language :: Python :: 3.1',
|
||||
),
|
||||
)
|
||||
|
||||
+2
-36
@@ -6,8 +6,8 @@
|
||||
import unittest
|
||||
|
||||
|
||||
class ClintTestCase(unittest.TestCase):
|
||||
"""Clint test cases."""
|
||||
class TablibTestCase(unittest.TestCase):
|
||||
"""Tablib test cases."""
|
||||
|
||||
def setUp(self):
|
||||
import clint
|
||||
@@ -16,39 +16,5 @@ class ClintTestCase(unittest.TestCase):
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
class ColoredStringTestCase(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
from clint.textui.colored import ColoredString
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
def test_split(self):
|
||||
from clint.textui.colored import ColoredString
|
||||
new_str = ColoredString('red', "hello world")
|
||||
output = new_str.split()
|
||||
assert output[0].s == "hello"
|
||||
|
||||
def test_find(self):
|
||||
from clint.textui.colored import ColoredString
|
||||
new_str = ColoredString('blue', "hello world")
|
||||
output = new_str.find('h')
|
||||
self.assertEqual(output, 0)
|
||||
|
||||
def test_replace(self):
|
||||
from clint.textui.colored import ColoredString
|
||||
new_str = ColoredString('green', "hello world")
|
||||
output = new_str.replace("world", "universe")
|
||||
assert output.s == "hello universe"
|
||||
|
||||
def test_py2_bytes_not_mangled(self):
|
||||
from clint.textui.colored import ColoredString
|
||||
# On python 2 make sure the same bytes come out as went in
|
||||
new_str = ColoredString('RED', '\xe4')
|
||||
assert '\xe4' in str(new_str)
|
||||
from clint.textui import puts
|
||||
puts(new_str)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user