11"""Stdout Cube Print implementation"""
2+ from enum import Enum
23from 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"
66C_RESET = "\x1b [0;0m"
7+ # C_BG="\x1b[48;5;231m"
8+
9+
10+ class Terminal (Enum ):
11+ default = 0
12+ x256 = 1
713
814class 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
0 commit comments