|
| 1 | +# Copyright (c) 2025 github.com/SKOHscripts |
| 2 | +# |
| 3 | +# This software is licensed under the MIT License. |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | +""" |
| 23 | +Module for creating ASCII tables used in opening_report.py |
| 24 | +""" |
| 25 | + |
| 26 | + |
| 27 | +class AsciiTable: |
| 28 | + """Helper class for creating perfectly aligned ASCII tables with dynamic column widths""" |
| 29 | + |
| 30 | + @staticmethod |
| 31 | + def create(data, headers, alignments=None, padding=1, border_style="none", title=None): |
| 32 | + """ |
| 33 | + Create a perfectly aligned ASCII table with dynamic column widths |
| 34 | +
|
| 35 | + Args: |
| 36 | + data: List of rows (each row is a list of cells) |
| 37 | + headers: List of header names |
| 38 | + alignments: List of alignments ('<', '>', '^') for each column |
| 39 | + padding: Number of spaces between columns |
| 40 | + border_style: "none", "minimal", "full" |
| 41 | + title: Optional table title |
| 42 | +
|
| 43 | + Returns: |
| 44 | + str: Formatted table |
| 45 | + """ |
| 46 | + # Déterminer la largeur maximale de chaque colonne (incluant les en-têtes) |
| 47 | + all_rows = [headers] + data |
| 48 | + col_widths = [ |
| 49 | + max(len(str(row[i])) for row in all_rows if i < len(row)) |
| 50 | + |
| 51 | + for i in range(max(len(row) for row in all_rows)) |
| 52 | + ] |
| 53 | + |
| 54 | + # Appliquer le padding |
| 55 | + col_widths = [w + padding*2 for w in col_widths] |
| 56 | + |
| 57 | + # Définir les alignements par défaut (gauche pour le texte, droite pour les nombres) |
| 58 | + |
| 59 | + if not alignments: |
| 60 | + alignments = ['<' if i == 0 else '>' for i in range(len(col_widths))] |
| 61 | + |
| 62 | + # Créer les lignes du tableau |
| 63 | + lines = [] |
| 64 | + |
| 65 | + # Ajouter un titre si spécifié |
| 66 | + |
| 67 | + if title: |
| 68 | + lines.append(f"{title}") |
| 69 | + lines.append("─" * sum(col_widths + [len(col_widths) - 1])) |
| 70 | + |
| 71 | + # Ligne de séparation supérieure si nécessaire |
| 72 | + |
| 73 | + if border_style == "full": |
| 74 | + top_border = '+' + '+'.join(['─' * w for w in col_widths]) + '+' |
| 75 | + lines.append(top_border) |
| 76 | + |
| 77 | + # En-têtes |
| 78 | + header_line = '' |
| 79 | + |
| 80 | + if border_style in ["minimal", "full"]: |
| 81 | + header_line += '|' |
| 82 | + |
| 83 | + for i, header in enumerate(headers): |
| 84 | + fmt = f"{{:{alignments[i]}{col_widths[i]}}}" |
| 85 | + header_line += fmt.format(header) |
| 86 | + |
| 87 | + if border_style in ["minimal", "full"] or i < len(headers) - 1: |
| 88 | + header_line += '|' |
| 89 | + lines.append(header_line) |
| 90 | + |
| 91 | + # Ligne de séparation après les en-têtes si nécessaire |
| 92 | + |
| 93 | + if border_style == "full": |
| 94 | + separator = '+' + '+'.join(['─' * w for w in col_widths]) + '+' |
| 95 | + lines.append(separator) |
| 96 | + elif border_style == "minimal": |
| 97 | + separator = ':' + ':'.join(['-' * w for w in col_widths]) + ':' |
| 98 | + lines.append(separator) |
| 99 | + |
| 100 | + # Données |
| 101 | + |
| 102 | + for row in data: |
| 103 | + row_line = '' |
| 104 | + |
| 105 | + if border_style in ["minimal", "full"]: |
| 106 | + row_line += '|' |
| 107 | + |
| 108 | + for i, cell in enumerate(row): |
| 109 | + if i >= len(col_widths): |
| 110 | + continue |
| 111 | + fmt = f"{{:{alignments[i]}{col_widths[i]}}}" |
| 112 | + row_line += fmt.format(str(cell)) |
| 113 | + |
| 114 | + if border_style in ["minimal", "full"] or i < len(row) - 1: |
| 115 | + row_line += '|' |
| 116 | + lines.append(row_line) |
| 117 | + |
| 118 | + # Ligne de fermeture si nécessaire |
| 119 | + |
| 120 | + if border_style == "full": |
| 121 | + bottom_border = '+' + '+'.join(['─' * w for w in col_widths]) + '+' |
| 122 | + lines.append(bottom_border) |
| 123 | + |
| 124 | + return "\n".join(lines) |
| 125 | + |
| 126 | + @staticmethod |
| 127 | + def create_meter(value, width=20, symbol_filled='█', symbol_empty='░', show_value=True): |
| 128 | + """ |
| 129 | + Create a visual progress meter |
| 130 | +
|
| 131 | + Args: |
| 132 | + value: Value between 0 and 1 |
| 133 | + width: Total width of the meter |
| 134 | + symbol_filled: Symbol for filled portion |
| 135 | + symbol_empty: Symbol for empty portion |
| 136 | + show_value: Whether to show the percentage value |
| 137 | +
|
| 138 | + Returns: |
| 139 | + str: Formatted meter |
| 140 | + """ |
| 141 | + level = int(value * width) |
| 142 | + meter = symbol_filled * level + symbol_empty * (width - level) |
| 143 | + |
| 144 | + if show_value: |
| 145 | + return f"[{meter}] {value*100:.0f}%" |
| 146 | + |
| 147 | + return f"[{meter}]" |
| 148 | + |
| 149 | + @staticmethod |
| 150 | + def create_balance_meter(white, black, width=20): |
| 151 | + """ |
| 152 | + Create a visual balance meter for white/black distribution |
| 153 | +
|
| 154 | + Args: |
| 155 | + white: Count of white positions |
| 156 | + black: Count of black positions |
| 157 | + width: Total width of the meter |
| 158 | +
|
| 159 | + Returns: |
| 160 | + str: Formatted balance meter |
| 161 | + """ |
| 162 | + total = white + black |
| 163 | + |
| 164 | + if total == 0: |
| 165 | + return "[ ] 0.0%" |
| 166 | + |
| 167 | + white_pct = white / total * 100 |
| 168 | + |
| 169 | + white_width = int(white_pct * width / 100) |
| 170 | + black_width = width - white_width |
| 171 | + |
| 172 | + meter = "♔" * white_width + "♚" * black_width |
| 173 | + balance = 1.0 - abs(0.5 - white/total) * 2 |
| 174 | + status = "(Perfect)" if balance > 0.9 else "" |
| 175 | + |
| 176 | + return f"[{meter}] {balance*100:.1f}% {status}" |
0 commit comments