-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.py
More file actions
206 lines (170 loc) · 6.88 KB
/
Copy pathui.py
File metadata and controls
206 lines (170 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"""
ui.py — Rich-powered terminal UI for File Organizer.
All console output is centralised here so organizer.py and main.py
stay focused on logic. Import ``console`` anywhere you need to print.
"""
from pathlib import Path
from rich import box
from rich.align import Align
from rich.console import Console
from rich.panel import Panel
from rich.progress import (
BarColumn,
MofNCompleteColumn,
Progress,
SpinnerColumn,
TaskProgressColumn,
TextColumn,
TimeElapsedColumn,
TimeRemainingColumn,
)
from rich.rule import Rule
from rich.table import Table
from rich.text import Text
from rich.theme import Theme
# ── Shared console (import this wherever you need console.print) ──────────────
_THEME = Theme(
{
"info": "bold cyan",
"success": "bold green",
"warning": "bold yellow",
"error": "bold red",
"muted": "dim white",
"path": "bright_cyan",
"moved": "bright_green",
"dupe": "bright_magenta",
"skipped": "yellow",
}
)
console = Console(theme=_THEME, highlight=False)
# ── Banner ────────────────────────────────────────────────────────────────────
def print_banner() -> None:
"""Render the opening title banner."""
title = Text.assemble(
("🗂️ File Organizer\n", "bold cyan"),
("Scan · Sort · Deduplicate · Log", "dim white"),
)
console.print(
Panel(
Align.center(title),
border_style="bright_cyan",
padding=(1, 8),
)
)
console.print()
# ── Configuration panel ───────────────────────────────────────────────────────
def print_config(
source: Path,
dest: Path,
dry_run: bool,
workers: "int | None",
) -> None:
"""Show the run configuration in a neat panel."""
tbl = Table(box=None, show_header=False, padding=(0, 2))
tbl.add_column(style="muted", no_wrap=True)
tbl.add_column(style="bold white")
tbl.add_row("📁 Source", f"[path]{source}[/]")
tbl.add_row("📂 Destination", f"[path]{dest}[/]")
tbl.add_row(
"🔍 Mode",
"[bold yellow]DRY-RUN — nothing will be moved[/]"
if dry_run else
"[bold green]LIVE — files will be moved[/]",
)
tbl.add_row(
"⚙️ Workers",
str(workers) if workers else "[muted]auto (all CPU cores)[/]",
)
console.print(
Panel(tbl, title="[bold cyan]⚙ Configuration[/]", border_style="cyan")
)
console.print()
# ── Hashing progress bar ──────────────────────────────────────────────────────
def make_progress() -> Progress:
"""Return a Rich Progress bar for the hashing phase."""
return Progress(
SpinnerColumn(spinner_name="dots2", style="bold cyan"),
TextColumn("[bold cyan]{task.description}"),
BarColumn(bar_width=42, style="cyan", complete_style="bold green"),
MofNCompleteColumn(),
TaskProgressColumn(),
TimeElapsedColumn(),
TimeRemainingColumn(),
console=console,
transient=False,
)
# ── Per-file event lines ──────────────────────────────────────────────────────
_FILE_MAX = 45 # max chars shown for a filename
_DEST_MAX = 32 # max chars shown for destination label
def _clip(text: str, max_len: int) -> str:
"""Truncate *text* with a trailing ellipsis if longer than *max_len*."""
return text if len(text) <= max_len else text[: max_len - 1] + "…"
def log_move(src: Path, dst: Path, dry_run: bool) -> None:
tag = "[bold yellow]DRY[/]" if dry_run else " "
name = _clip(src.name, _FILE_MAX)
dest = _clip(f"{dst.parent.name}/{dst.name}", _DEST_MAX)
console.print(
f" {tag} [moved]✔[/] [path]{name:<{_FILE_MAX}}[/] [muted]→[/] [moved]{dest}[/]",
no_wrap=True, overflow="ellipsis",
)
def log_duplicate(path: Path) -> None:
name = _clip(path.name, _FILE_MAX)
console.print(
f" [dupe]⊗ DUPE[/] [path]{name}[/]",
no_wrap=True, overflow="ellipsis",
)
def log_error(path: Path, exc: Exception) -> None:
name = _clip(path.name, _FILE_MAX)
console.print(
f" [error]✗ ERR [/] [path]{name}[/] [muted]—[/] {exc}",
no_wrap=True, overflow="ellipsis",
)
def log_project_move(project_dir: Path, target: Path, dry_run: bool) -> None:
"""Log a whole project folder being moved into Code/."""
tag = "[bold yellow]DRY[/]" if dry_run else " "
name = _clip(project_dir.name, _FILE_MAX)
dest = _clip(f"Code/{target.name}", _DEST_MAX)
console.print(
f" {tag} [moved]📁 PROJ[/] [path]{name:<{_FILE_MAX}}[/] [muted]→[/] [moved]{dest}[/]",
no_wrap=True, overflow="ellipsis",
)
# ── Summary panel ─────────────────────────────────────────────────────────────
def print_summary(stats: "dict[str, int]", dry_run: bool) -> None:
"""Render the final run-summary panel."""
tbl = Table(
box=box.ROUNDED,
show_header=True,
header_style="bold cyan",
padding=(0, 3),
min_width=46,
)
tbl.add_column("Metric", style="muted", no_wrap=True)
tbl.add_column("Count", justify="right", style="bold white", min_width=6)
tbl.add_column("", justify="center", min_width=3)
rows = [
("📄 Files scanned", "scanned", "", ""),
("✅ Files moved", "moved", "[green]✓[/]", ""),
("📁 Projects moved", "projects", "[green]✓[/]", ""),
("⊗ Duplicates found", "duplicates", "[magenta]![/]", "[green]✓[/]"),
("⏭ Skipped", "skipped", "", ""),
("❌ Errors", "errors", "[red]✗[/]", "[green]✓[/]"),
]
for label, key, bad_icon, good_icon in rows:
val = stats[key]
if key in ("duplicates", "errors"):
icon = bad_icon if val else good_icon
else:
icon = good_icon if val else ""
tbl.add_row(label, str(val), icon)
mode = " [bold yellow](dry-run)[/]" if dry_run else ""
console.print()
console.print(
Panel(
Align.center(tbl),
title=f"[bold green]✔ Run Complete{mode}[/]",
border_style="green",
padding=(1, 4),
)
)
console.print(Rule(style="dim"))
console.print()