Skip to content

Commit

Permalink
color compound hy model reprs
Browse files Browse the repository at this point in the history
  • Loading branch information
gilch committed Sep 6, 2017
1 parent 04462a4 commit 877949a
Showing 1 changed file with 57 additions and 20 deletions.
77 changes: 57 additions & 20 deletions hy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,24 @@
# license. See the LICENSE.

from __future__ import unicode_literals
from contextlib import contextmanager
from math import isnan, isinf
from hy._compat import PY3, str_type, bytes_type, long_type, string_types
from fractions import Fraction
from clint.textui import colored


PRETTY = True


@contextmanager
def _pretty():
global PRETTY
old, PRETTY = PRETTY, True
try:
yield
finally:
PRETTY = old


class HyObject(object):
Expand Down Expand Up @@ -114,7 +129,7 @@ def __new__(cls, value):
return obj

def __repr__(self):
return "HyKeyword(%s)" % (repr(self[1:]))
return "%s(%s)" % (self.__class__.__name__, repr(self[1:]))


def strip_digit_separators(number):
Expand Down Expand Up @@ -222,13 +237,22 @@ def __getitem__(self, item):

return ret

color = staticmethod(colored.cyan)

def __repr__(self):
if self:
return "%s([\n %s])" % (
self.__class__.__name__,
",\n ".join([repr_indent(e) for e in self]))
else:
return self.__class__.__name__ + "()"
return str(self) if PRETTY else super(HyList, self).__repr__()

def __str__(self):
with _pretty():
c = self.color
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[tuple] = lambda t: HyList(wrap_value(x) for x in t)
Expand All @@ -239,17 +263,18 @@ class HyDict(HyList):
HyDict (just a representation of a dict)
"""

def __repr__(self):
def __str__(self):
g = colored.green
if self:
pairs = []
for k, v in zip(self[::2],self[1::2]):
k, v = repr_indent(k), repr_indent(v)
pairs.append("%s, %s," % (k, v))
pairs.append("{0}{c} {1}{c}".format(k, v, c=g(',')))
if len(self) % 2 == 1:
pairs.append("%s # odd\n" % repr_indent(self[-1]))
return "HyDict([\n %s])" % ("\n ".join(pairs),)
pairs.append("{} {}\n".format(repr_indent(self[-1]), g("# odd")))
return "{}\n {}{}".format(g("HyDict(["), ("\n ".join(pairs)), g("])"))
else:
return "HyDict()"
return '' + g("HyDict()")

def keys(self):
return self[0::2]
Expand All @@ -267,6 +292,7 @@ class HyExpression(HyList):
"""
Hy S-Expression. Basically just a list.
"""
color = staticmethod(colored.yellow)

_wrappers[HyExpression] = lambda e: HyExpression(wrap_value(x) for x in e)
_wrappers[Fraction] = lambda e: HyExpression(
Expand All @@ -277,6 +303,7 @@ class HySet(HyList):
"""
Hy set (just a representation of a set)
"""
color = staticmethod(colored.red)

_wrappers[set] = lambda s: HySet(wrap_value(x) for x in s)

Expand Down Expand Up @@ -352,14 +379,24 @@ def replace(self, other):
HyObject.replace(self, other)

def __repr__(self):
lines = ["<HyCons ("]
while True:
lines.append(" " + repr_indent(self.car))
if not isinstance(self.cdr, HyCons):
break
self = self.cdr
lines.append(". %s)>" % (repr_indent(self.cdr),))
return '\n'.join(lines)
if PRETTY:
return str(self)
else:
return "HyCons({}, {})".format(
repr(self.car), repr(self.cdr))

def __str__(self):
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):
return (
Expand Down

0 comments on commit 877949a

Please sign in to comment.