color compound hy model reprs
This commit is contained in:
parent
021204af13
commit
2d8a318e0c
99
hy/models.py
99
hy/models.py
@ -3,9 +3,28 @@
|
|||||||
# license. See the LICENSE.
|
# license. See the LICENSE.
|
||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
from contextlib import contextmanager
|
||||||
from math import isnan, isinf
|
from math import isnan, isinf
|
||||||
from hy._compat import PY3, str_type, bytes_type, long_type, string_types
|
from hy._compat import PY3, str_type, bytes_type, long_type, string_types
|
||||||
from fractions import Fraction
|
from fractions import Fraction
|
||||||
|
from clint.textui import colored
|
||||||
|
|
||||||
|
|
||||||
|
PRETTY = True
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def pretty(pretty=True):
|
||||||
|
"""
|
||||||
|
Context manager to temporarily enable
|
||||||
|
or disable pretty-printing of Hy model reprs.
|
||||||
|
"""
|
||||||
|
global PRETTY
|
||||||
|
old, PRETTY = PRETTY, pretty
|
||||||
|
try:
|
||||||
|
yield
|
||||||
|
finally:
|
||||||
|
PRETTY = old
|
||||||
|
|
||||||
|
|
||||||
class HyObject(object):
|
class HyObject(object):
|
||||||
@ -114,7 +133,7 @@ class HyKeyword(HyObject, str_type):
|
|||||||
return obj
|
return obj
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "HyKeyword(%s)" % (repr(self[1:]))
|
return "%s(%s)" % (self.__class__.__name__, repr(self[1:]))
|
||||||
|
|
||||||
|
|
||||||
def strip_digit_separators(number):
|
def strip_digit_separators(number):
|
||||||
@ -222,13 +241,22 @@ class HyList(HyObject, list):
|
|||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
color = staticmethod(colored.cyan)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
if self:
|
return str(self) if PRETTY else super(HyList, self).__repr__()
|
||||||
return "%s([\n %s])" % (
|
|
||||||
self.__class__.__name__,
|
def __str__(self):
|
||||||
",\n ".join([repr_indent(e) for e in self]))
|
with pretty():
|
||||||
else:
|
c = self.color
|
||||||
return self.__class__.__name__ + "()"
|
if self:
|
||||||
|
return ("{}{}\n {}{}").format(
|
||||||
|
c(self.__class__.__name__),
|
||||||
|
c("(["),
|
||||||
|
(c(",") + "\n ").join([repr_indent(e) for e in self]),
|
||||||
|
c("])"))
|
||||||
|
else:
|
||||||
|
return '' + c(self.__class__.__name__ + "()")
|
||||||
|
|
||||||
_wrappers[list] = lambda l: HyList(wrap_value(x) for x in l)
|
_wrappers[list] = lambda l: HyList(wrap_value(x) for x in l)
|
||||||
_wrappers[tuple] = lambda t: HyList(wrap_value(x) for x in t)
|
_wrappers[tuple] = lambda t: HyList(wrap_value(x) for x in t)
|
||||||
@ -239,17 +267,24 @@ class HyDict(HyList):
|
|||||||
HyDict (just a representation of a dict)
|
HyDict (just a representation of a dict)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __repr__(self):
|
def __str__(self):
|
||||||
if self:
|
with pretty():
|
||||||
pairs = []
|
g = colored.green
|
||||||
for k, v in zip(self[::2],self[1::2]):
|
if self:
|
||||||
k, v = repr_indent(k), repr_indent(v)
|
pairs = []
|
||||||
pairs.append("%s, %s," % (k, v))
|
for k, v in zip(self[::2],self[1::2]):
|
||||||
if len(self) % 2 == 1:
|
k, v = repr_indent(k), repr_indent(v)
|
||||||
pairs.append("%s # odd\n" % repr_indent(self[-1]))
|
pairs.append(
|
||||||
return "HyDict([\n %s])" % ("\n ".join(pairs),)
|
("{0}{c}\n {1}\n {c}"
|
||||||
else:
|
if '\n' in k+v
|
||||||
return "HyDict()"
|
else "{0}{c} {1}{c}").format(k, v, c=g(',')))
|
||||||
|
if len(self) % 2 == 1:
|
||||||
|
pairs.append("{} {}\n".format(
|
||||||
|
repr_indent(self[-1]), g("# odd")))
|
||||||
|
return "{}\n {}{}".format(
|
||||||
|
g("HyDict(["), ("\n ".join(pairs)), g("])"))
|
||||||
|
else:
|
||||||
|
return '' + g("HyDict()")
|
||||||
|
|
||||||
def keys(self):
|
def keys(self):
|
||||||
return self[0::2]
|
return self[0::2]
|
||||||
@ -267,6 +302,7 @@ class HyExpression(HyList):
|
|||||||
"""
|
"""
|
||||||
Hy S-Expression. Basically just a list.
|
Hy S-Expression. Basically just a list.
|
||||||
"""
|
"""
|
||||||
|
color = staticmethod(colored.yellow)
|
||||||
|
|
||||||
_wrappers[HyExpression] = lambda e: HyExpression(wrap_value(x) for x in e)
|
_wrappers[HyExpression] = lambda e: HyExpression(wrap_value(x) for x in e)
|
||||||
_wrappers[Fraction] = lambda e: HyExpression(
|
_wrappers[Fraction] = lambda e: HyExpression(
|
||||||
@ -277,6 +313,7 @@ class HySet(HyList):
|
|||||||
"""
|
"""
|
||||||
Hy set (just a representation of a set)
|
Hy set (just a representation of a set)
|
||||||
"""
|
"""
|
||||||
|
color = staticmethod(colored.red)
|
||||||
|
|
||||||
_wrappers[set] = lambda s: HySet(wrap_value(x) for x in s)
|
_wrappers[set] = lambda s: HySet(wrap_value(x) for x in s)
|
||||||
|
|
||||||
@ -352,14 +389,24 @@ class HyCons(HyObject):
|
|||||||
HyObject.replace(self, other)
|
HyObject.replace(self, other)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
lines = ["<HyCons ("]
|
if PRETTY:
|
||||||
while True:
|
return str(self)
|
||||||
lines.append(" " + repr_indent(self.car))
|
else:
|
||||||
if not isinstance(self.cdr, HyCons):
|
return "HyCons({}, {})".format(
|
||||||
break
|
repr(self.car), repr(self.cdr))
|
||||||
self = self.cdr
|
|
||||||
lines.append(". %s)>" % (repr_indent(self.cdr),))
|
def __str__(self):
|
||||||
return '\n'.join(lines)
|
with pretty():
|
||||||
|
c = colored.yellow
|
||||||
|
lines = ['' + c("<HyCons (")]
|
||||||
|
while True:
|
||||||
|
lines.append(" " + repr_indent(self.car))
|
||||||
|
if not isinstance(self.cdr, HyCons):
|
||||||
|
break
|
||||||
|
self = self.cdr
|
||||||
|
lines.append("{} {}{}".format(
|
||||||
|
c("."), repr_indent(self.cdr), c(")>")))
|
||||||
|
return '\n'.join(lines)
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
return (
|
return (
|
||||||
|
Loading…
x
Reference in New Issue
Block a user