Skip to content

Commit

Permalink
Add representation for enums
Browse files Browse the repository at this point in the history
  • Loading branch information
breuleux committed Aug 15, 2024
1 parent 5af4e97 commit cbfa3f8
Show file tree
Hide file tree
Showing 3 changed files with 247 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/hrepr/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from collections import Counter
from dataclasses import fields as dataclass_fields
from dataclasses import is_dataclass
from enum import Enum
from pathlib import Path
from typing import Union

Expand Down Expand Up @@ -325,6 +326,12 @@ def hrepr(self, obj: Exception):
layout="h",
)["hrepr-error"]

# Enums

def hrepr_short(self, e: Enum):
typ = type(e).__name__
return H.span["hrepr-enum", f"hrepr-enum-{typ}"](f"{typ}.{e.name}")

# Functions and methods

def hrepr_short(self, obj: types.FunctionType):
Expand Down
8 changes: 8 additions & 0 deletions tests/test_hrepr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import re
import sys
from dataclasses import dataclass
from enum import Enum

import pytest
from hrepr import H
Expand All @@ -26,6 +27,12 @@ class Opaque:
pass


class Size(Enum):
SMALL = "small"
MEDIUM = "medium"
BIG = "big"


def hshort(x, **kw):
return hrepr(x, max_depth=0, **kw)

Expand Down Expand Up @@ -172,6 +179,7 @@ def _(obj, file_regression):
factory.dict_keys({"a": 1, "b": 2, "c": 3}.keys(), standard, depth(0))
factory.dict_values({"a": 1, "b": 2, "c": 3}.values(), standard, depth(0))
factory.dataclass(Point(1, 2), standard, depth(0))
factory.enum(Size.BIG, standard)
factory.unknown(Opaque, standard)

factory.list10(
Expand Down
232 changes: 232 additions & 0 deletions tests/test_hrepr/test_enum_standard_.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
<!DOCTYPE html><html><head><meta http-equiv="Content-type" content="text/html" charset="UTF-8" /><style>
:root, :host {
--instance-color: #00a;
--dict-color: #00a;
--tuple-color: #080;
--list-color: #080;
--set-color: #080;
--frozenset-color: #88f;
--error-color: #f00;
--struct-border: 2px;
}

/* Scalar/string formatting */

.hreprv-True, .hreprv-False, .hreprv-None, .hreprt-int, .hreprt-float {
font-weight: bold;
}

.hreprt-int, .hreprt-float {
color: #88f;
}

.hreprt-str {
color: #666;
white-space: pre-wrap;
}

.hreprt-str:before {
content: "“";
color: #888;
}

.hreprt-str:after {
content: "”";
color: #888;
}

.hreprt-bytes {
color: #666;
font-family: monospace;
white-space: pre-wrap;
}

.hreprt-bytes:before {
content: "b“";
}

.hreprt-bytes:after {
content: "”";
}

.hreprv-True {
color: green;
}

.hreprv-False {
color: red;
}

.hreprv-None {
color: grey;
}

.hreprk-class > .hrepr-defn-key {
color: blue;
}

.hreprk-module > .hrepr-defn-key {
color: purple;
}

.hreprk-function > .hrepr-defn-key,
.hreprk-builtin > .hrepr-defn-key,
.hreprk-method > .hrepr-defn-key,
.hreprk-descriptor > .hrepr-defn-key {
color: #080;
}

.hrepr-defn-key {
font-weight: bold;
}


/* Generic layouts */

.hreprl-h, .hreprl-s {
display: inline-flex;
flex-direction: row;
flex-wrap: wrap;
}

.hreprl-v {
display: inline-flex;
flex-direction: column;
}

.hrepr-body.hreprl-h,
.hrepr-body.hreprl-v {
gap: 4px;
background: #ddd;
}

.hrepr-body.hreprl-h {
align-items: center;
}

.hrepr-body.hreprl-v > *,
.hrepr-body.hreprl-h > * {
background: white;
display: inline-flex;
place-items: center;
}

.hrepr-body.hreprl-h > div > .hrepr-ellipsis,
.hrepr-body.hreprl-v > div > .hrepr-ellipsis {
background: #ddd;
}

.hrepr-body.hreprl-h > div > .hrepr-ellipsis {
padding-left: 5px;
padding-right: 5px;
}

.hrepr-body.hreprl-v > div > .hrepr-ellipsis {
padding-top: 5px;
padding-bottom: 5px;
}

.hrepr-body.hreprl-s > * {
padding: 1px;
}

.hrepr-bracketed {
display: inline-flex;
flex-direction: row;
place-items: center;
background: black;
border: var(--struct-border) solid black;
box-sizing: border-box;
}

.hrepr-bracketed > .hrepr-open {
padding-right: var(--struct-border);
}
.hrepr-bracketed > .hrepr-close {
padding-left: var(--struct-border);
}
.hrepr-bracketed > .hrepr-open,
.hrepr-bracketed > .hrepr-close,
.hrepr-instance > .hrepr-title,
.hreprl-s {
color: white;
font-weight: bold;
}

.hrepr-instance {
background: var(--instance-color);
border: var(--struct-border) solid var(--instance-color);
box-sizing: border-box;
}
.hrepr-instance > .hrepr-title {
color: white;
font-weight: bold;
}
.hrepr-instance.hreprl-v > .hrepr-title {
padding-bottom: var(--struct-border);
}
.hrepr-instance.hreprl-h > .hrepr-title,
.hrepr-instance.hreprl-s > .hrepr-title {
padding-right: var(--struct-border);
place-self: center;
}
.hrepr-instance.hrepr-error {
background: var(--error-color);
border: var(--struct-border) solid var(--error-color);
}


/* Colors for structures. */

.hreprt-dict {
background: var(--dict-color);
border-color: var(--dict-color);
}

.hreprt-list {
background: var(--list-color);
border-color: var(--list-color);
}

.hreprt-tuple {
background: var(--tuple-color);
border-color: var(--tuple-color);
}

.hreprt-set {
background: var(--set-color);
border-color: var(--set-color);
}

.hreprt-frozenset {
background: var(--frozenset-color);
border-color: var(--frozenset-color);
}

table.hrepr-body {
border-collapse: collapse;
width: 100%;
}

table.hrepr-body > tbody > tr:nth-child(even) {
background: #ddd;
}

table.hrepr-body > tbody > tr:nth-child(odd) {
background: #fff;
}


/* References */

.hrepr-ref {
font-weight: bold;
color: magenta;
}

.hrepr-refbox {
display: inline-flex;
position: relative;
place-items: center;
}
</style></head><body><h2>description</h2><pre>hrepr(obj)</pre><h2>obj</h2><pre>&lt;Size.BIG: &#x27;big&#x27;&gt;</pre><h2>result</h2><span class="hrepr-enum hrepr-enum-Size">Size.BIG</span></body></html>

0 comments on commit cbfa3f8

Please sign in to comment.