Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions magiccube/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,12 @@ def undo(self, num_moves=1) -> None:
for _ in range(2*num_moves):
self._history.pop()

def print(self, orientation: CubeMove | str = '') -> str:
printer = CubePrintStr(self)
return printer.print_cube(orientation)

def __repr__(self):
return str(self.cube)

def __str__(self):
printer = CubePrintStr(self)
return printer.print_cube()
def __str__(self) -> str:
return self.print()
16 changes: 13 additions & 3 deletions magiccube/cube_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
from enum import Enum
from magiccube.cube_base import Color, Face
from magiccube.cube_move import CubeMove

C_RESET = "\x1b[0;0m"
C_GREEN = "\x1b[48;5;40m\x1b[38;5;232m"
Expand Down Expand Up @@ -36,7 +37,7 @@ def __init__(self, cube, terminal: Terminal | None = None):
self.term = Terminal.x256 if os.environ.get(
"TERM") == "xterm-256color" else Terminal.default

def _format_color(self, color: Color):
def _format_color(self, color: Color) -> str:
"""Format color to TTY
Only print colors on supported terminals (xterm-256color)
"""
Expand All @@ -48,7 +49,7 @@ def _format_color(self, color: Color):

return formated_color

def _print_top_down_face(self, cube, face):
def _print_top_down_face(self, cube, face) -> str:
result = ""
for index, color in enumerate(cube.get_face_flat(face)):
if index % cube.size == 0:
Expand All @@ -61,10 +62,13 @@ def _print_top_down_face(self, cube, face):
result += "\n"
return result

def print_cube(self):
def print_cube(self, orientation: CubeMove | str = '') -> str:
"Print the cube to stdout"
cube = self.cube

if orientation:
cube.rotate([orientation])

# flatten middle layer
print_order_mid = zip(cube.get_face(Face.L), cube.get_face(Face.F),
cube.get_face(Face.R), cube.get_face(Face.B))
Expand All @@ -84,4 +88,10 @@ def print_cube(self):

# BOTTOM
result += self._print_top_down_face(cube, Face.D)

if orientation:
cube.rotate([orientation])
if cube._store_history:
cube.undo(2)

return result