diff --git a/magiccube/cube.py b/magiccube/cube.py index c86d4b2..32bf547 100644 --- a/magiccube/cube.py +++ b/magiccube/cube.py @@ -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() diff --git a/magiccube/cube_print.py b/magiccube/cube_print.py index 180612a..96301cb 100644 --- a/magiccube/cube_print.py +++ b/magiccube/cube_print.py @@ -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" @@ -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) """ @@ -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: @@ -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)) @@ -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