From 073713dee3aad82c9943ddec605daf78655df517 Mon Sep 17 00:00:00 2001 From: Fantomas42 Date: Sat, 12 Apr 2025 13:23:20 +0200 Subject: [PATCH 1/3] Can orientate cube when printing --- magiccube/cube_print.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/magiccube/cube_print.py b/magiccube/cube_print.py index 6f6bfb0..0f5cf37 100644 --- a/magiccube/cube_print.py +++ b/magiccube/cube_print.py @@ -53,10 +53,13 @@ def _print_top_down_face(self, cube, face): result += "\n" return result - def print_cube(self): + def print_cube(self, orientation: str = ''): "Print the cube to stdout" cube = self.cube + if orientation: + cube.rotate([orientation]) + # flatten midle 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)) @@ -76,4 +79,11 @@ def print_cube(self): # BOTTOM result += self._print_top_down_face(cube, Face.D) + + if orientation: + cube.rotate([orientation]) + if cube._store_history: + cube._history.pop() + cube._history.pop() + return result From 4b5179878b04e8053eea8c6a5451149dab838249 Mon Sep 17 00:00:00 2001 From: Fantomas42 Date: Sat, 19 Apr 2025 20:01:17 +0200 Subject: [PATCH 2/3] More typing and use the new undo method --- magiccube/cube_print.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/magiccube/cube_print.py b/magiccube/cube_print.py index eccaebf..ddd64d0 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" @@ -61,7 +62,7 @@ def _print_top_down_face(self, cube, face): result += "\n" return result - def print_cube(self, orientation: str = ''): + def print_cube(self, orientation: str | CubeMove = None): "Print the cube to stdout" cube = self.cube @@ -91,7 +92,6 @@ def print_cube(self, orientation: str = ''): if orientation: cube.rotate([orientation]) if cube._store_history: - cube._history.pop() - cube._history.pop() + cube.undo(2) return result From ba54eeec6ef645f402168725217f86c996c9ad35 Mon Sep 17 00:00:00 2001 From: Fantomas42 Date: Sat, 19 Apr 2025 20:09:39 +0200 Subject: [PATCH 3/3] Add more typing hints and deport MagicCube.__str__ logic to a method wich can be parametred with orientation --- magiccube/cube.py | 9 ++++++--- magiccube/cube_print.py | 6 +++--- 2 files changed, 9 insertions(+), 6 deletions(-) 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 ddd64d0..96301cb 100644 --- a/magiccube/cube_print.py +++ b/magiccube/cube_print.py @@ -37,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) """ @@ -49,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: @@ -62,7 +62,7 @@ def _print_top_down_face(self, cube, face): result += "\n" return result - def print_cube(self, orientation: str | CubeMove = None): + def print_cube(self, orientation: CubeMove | str = '') -> str: "Print the cube to stdout" cube = self.cube