Skip to content

Commit 41171fc

Browse files
authored
Merge pull request #12 from trincaog/devel
Devel
2 parents 777d857 + 538e997 commit 41171fc

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

magiccube/cube_print.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
"""Stdout Cube Print implementation"""
2+
from enum import Enum
23
from magiccube.cube_base import Color,Face
4+
import os
35

4-
C_BG="\x1b[48;5;231m"
5-
#C_RESET="\x1b[49m\x1b[38;5;7m"
66
C_RESET="\x1b[0;0m"
7+
# C_BG="\x1b[48;5;231m"
8+
9+
10+
class Terminal(Enum):
11+
default=0
12+
x256=1
713

814
class CubePrintStr:
915
"""Prints a cube to stdout"""
10-
_color_map = {
16+
_xterm256_color_map = {
1117
Color.G: "\x1b[48;5;40m\x1b[38;5;232m",
1218
Color.B: "\x1b[48;5;21m\x1b[38;5;7m",
1319
Color.R: "\x1b[48;5;196m\x1b[38;5;232m",
@@ -18,21 +24,29 @@ class CubePrintStr:
1824

1925
def __init__(self, cube):
2026
self.cube = cube
27+
self.term = Terminal.x256 if os.environ["TERM"]=="xterm-256color" else Terminal.default
2128

2229
def _format_color(self, color:Color):
23-
"""Format color to TTY"""
24-
return CubePrintStr._color_map.get(color, "") + " " + color.name + " "+C_RESET
30+
"""Format color to TTY
31+
Only print colors on supported terminals (xterm-256color)
32+
"""
33+
formated_color = " " + color.name + " "
34+
35+
if self.term == Terminal.x256:
36+
formated_color = CubePrintStr._xterm256_color_map.get(color, "") + formated_color + C_RESET
37+
38+
return formated_color
2539

2640
def _print_top_down_face(self, cube, face):
2741
result =""
2842
for index,color in enumerate(cube.get_face_flat(face)):
2943
if index % cube.size == 0:
30-
result += C_BG+(" " * ((3*cube.size)))+C_RESET
44+
result += (" " * ((3*cube.size)))
3145

3246
result += self._format_color(color)
3347

3448
if index % cube.size == cube.size-1:
35-
result += C_BG+(" " * ((2*3*cube.size)))+C_RESET
49+
result += (" " * ((2*3*cube.size)))
3650
result += "\n"
3751
return result
3852

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "magiccube"
3-
version = "0.0.8"
3+
version = "0.0.9"
44
authors = [
55
{ name="Gonçalo Trincão Cunha", email="[email protected]" },
66
]

0 commit comments

Comments
 (0)