|
4 | 4 | from typing import Annotated, List |
5 | 5 |
|
6 | 6 | import typer |
| 7 | +from rich.console import Console |
| 8 | +from rich.table import Table |
7 | 9 |
|
8 | 10 | from Pylette.src.cli_utils import PyletteProgress |
9 | 11 | from Pylette.src.color_extraction import batch_extract_colors |
@@ -52,8 +54,6 @@ def main( |
52 | 54 | typer.echo("Error: --output is required when using --export-json", err=True) |
53 | 55 | raise typer.Exit(1) |
54 | 56 |
|
55 | | - output_file_path = str(out_filename) if out_filename is not None else None |
56 | | - |
57 | 57 | # Set up progress bar for CLI |
58 | 58 | with PyletteProgress(palette_size=n) as progress: |
59 | 59 | task_id = progress.add_task("Extracting colors...", total=len(image_sources)) |
@@ -84,11 +84,9 @@ def progress_callback(task_number: int, result: BatchResult): |
84 | 84 |
|
85 | 85 | if export_json and output: |
86 | 86 | handle_json_export(successful, output, colorspace) |
87 | | - else: |
88 | | - # Original CSV behavior |
89 | | - for success in successful: |
90 | | - if success.palette is not None: |
91 | | - success.palette.to_csv(filename=output_file_path, frequency=True, stdout=stdout, colorspace=colorspace) |
| 87 | + elif stdout and successful: |
| 88 | + # Show clean palette summary |
| 89 | + display_palette_summary(successful, colorspace) |
92 | 90 |
|
93 | 91 | # Display colors if requested |
94 | 92 | if display_colors: |
@@ -151,6 +149,62 @@ def handle_json_export( |
151 | 149 | typer.echo(f"✓ Exported {len(successful_results)} palettes to {output_path}") |
152 | 150 |
|
153 | 151 |
|
| 152 | +def display_palette_summary(successful_results: list[BatchResult], colorspace: ColorSpace) -> None: |
| 153 | + """Display a clean summary of extracted palettes.""" |
| 154 | + console = Console() |
| 155 | + |
| 156 | + for result in successful_results: |
| 157 | + if result.palette is not None: |
| 158 | + palette = result.palette |
| 159 | + |
| 160 | + # Show extraction success message |
| 161 | + source = result.source |
| 162 | + if isinstance(source, str) and len(source) > 50: |
| 163 | + source = "..." + source[-47:] # Truncate long paths |
| 164 | + |
| 165 | + console.print(f"\n✓ Extracted {palette.number_of_colors} colors from [bold]{source}[/bold]") |
| 166 | + |
| 167 | + # Create color table |
| 168 | + table = Table(show_header=True, header_style="bold blue") |
| 169 | + table.add_column("Hex", style="bold", width=8) |
| 170 | + |
| 171 | + # Add colorspace-specific column |
| 172 | + colorspace_label = colorspace.value.upper() |
| 173 | + if colorspace == ColorSpace.RGB: |
| 174 | + table.add_column(f"{colorspace_label}", width=15) |
| 175 | + else: |
| 176 | + table.add_column(f"{colorspace_label}", width=20) |
| 177 | + table.add_column("RGB", width=15) |
| 178 | + |
| 179 | + table.add_column("Frequency", width=8, justify="right") |
| 180 | + |
| 181 | + # Add color rows |
| 182 | + for color in palette.colors: |
| 183 | + hex_color = color.hex |
| 184 | + frequency = f"{color.freq:.1%}" |
| 185 | + |
| 186 | + # Get colorspace values |
| 187 | + color_values = color.get_colors(colorspace) |
| 188 | + |
| 189 | + if colorspace == ColorSpace.RGB: |
| 190 | + rgb_str = f"({int(color_values[0])}, {int(color_values[1])}, {int(color_values[2])})" |
| 191 | + table.add_row(f"[{hex_color}]{hex_color}[/{hex_color}]", rgb_str, frequency) |
| 192 | + else: |
| 193 | + # For HSV/HLS, show both the colorspace values and RGB reference |
| 194 | + if colorspace == ColorSpace.HSV: |
| 195 | + cs_str = f"({color_values[0]:.2f}, {color_values[1]:.2f}, {color_values[2]:.2f})" |
| 196 | + else: # HLS |
| 197 | + cs_str = f"({color_values[0]:.2f}, {color_values[1]:.2f}, {color_values[2]:.2f})" |
| 198 | + |
| 199 | + rgb_str = f"({color.rgb[0]}, {color.rgb[1]}, {color.rgb[2]})" |
| 200 | + table.add_row(f"[{hex_color}]{hex_color}[/{hex_color}]", cs_str, rgb_str, frequency) |
| 201 | + |
| 202 | + console.print(table) |
| 203 | + |
| 204 | + # Show helpful usage message |
| 205 | + console.print("\n[dim]Use --export-json for structured data or --no-stdout to suppress output.[/dim]") |
| 206 | + |
| 207 | + |
154 | 208 | def print_extraction_summary(successful: list[BatchResult], failed: list[BatchResult]): |
155 | 209 | total = len(successful) + len(failed) |
156 | 210 |
|
|
0 commit comments