This commit is contained in:
Bingxi Zhao (Frank)
2026-04-23 19:29:16 +08:00
parent 6a99ba721c
commit 77f29cbcc7
348 changed files with 9325 additions and 4928 deletions

View File

@@ -4,21 +4,23 @@ Provides banner, inline arrow-key selector, confirm, text input, step
headers, coloured log helpers, spinner, and countdown. Designed for a
minimal / industrial aesthetic inspired by Claude Code.
"""
from __future__ import annotations
from contextlib import contextmanager
import getpass
import os
import shutil
import sys
import textwrap
import time
from contextlib import contextmanager
from typing import Generator
# ---------------------------------------------------------------------------
# Colour helpers
# ---------------------------------------------------------------------------
def _is_color() -> bool:
return sys.stdout.isatty() and os.environ.get("TERM", "dumb") != "dumb"
@@ -57,6 +59,7 @@ def bold(text: str) -> str:
# Terminal geometry
# ---------------------------------------------------------------------------
def term_width() -> int:
return min(max(shutil.get_terminal_size((80, 24)).columns, 60), 100)
@@ -65,11 +68,12 @@ def term_width() -> int:
# Low-level key reading (Unix / Windows)
# ---------------------------------------------------------------------------
def _read_key() -> str:
"""Read a single keypress. Returns 'up', 'down', 'enter', or char."""
try:
import tty
import termios
import tty
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
@@ -113,6 +117,7 @@ def _read_key() -> str:
# Banner
# ---------------------------------------------------------------------------
def banner(title: str, lines: list[str] | None = None) -> None:
w = term_width()
inner = w - 4 # usable chars between "│ " and " │"
@@ -136,6 +141,7 @@ def banner(title: str, lines: list[str] | None = None) -> None:
# Inline arrow-key selector
# ---------------------------------------------------------------------------
def select(prompt: str, options: list[tuple[str, str, str]]) -> str:
"""Arrow-key selector. *options* is a list of ``(value, label, desc)``.
@@ -172,7 +178,7 @@ def select(prompt: str, options: list[tuple[str, str, str]]) -> str:
elif key == "enter":
# Move past the option block
sys.stdout.write(f"\033[{total}A")
sys.stdout.write(f"\033[J")
sys.stdout.write("\033[J")
chosen_label = options[idx][1]
chosen_desc = options[idx][2]
print(f" {accent('>')} {bold(chosen_label)} {dim(chosen_desc)}")
@@ -185,7 +191,7 @@ def select(prompt: str, options: list[tuple[str, str, str]]) -> str:
# Redraw: move cursor up by `total` lines, clear, re-render
sys.stdout.write(f"\033[{total}A")
sys.stdout.write(f"\033[J")
sys.stdout.write("\033[J")
_render()
@@ -207,6 +213,7 @@ def _select_fallback(prompt: str, options: list[tuple[str, str, str]]) -> str:
# Confirm (Y/n)
# ---------------------------------------------------------------------------
def confirm(prompt: str, default: bool = True) -> bool:
hint = "[Y/n]" if default else "[y/N]"
while True:
@@ -224,6 +231,7 @@ def confirm(prompt: str, default: bool = True) -> bool:
# Text input
# ---------------------------------------------------------------------------
def text_input(prompt: str, default: str = "", secret: bool = False) -> str:
display = f" {prompt}"
if default and not secret:
@@ -241,6 +249,7 @@ def text_input(prompt: str, default: str = "", secret: bool = False) -> str:
# Step header
# ---------------------------------------------------------------------------
def step(current: int, total: int | str, title: str) -> None:
print()
w = term_width()
@@ -256,6 +265,7 @@ def step(current: int, total: int | str, title: str) -> None:
# Log helpers
# ---------------------------------------------------------------------------
def log_info(msg: str) -> None:
print(f" {accent('·')} {msg}")
@@ -310,6 +320,7 @@ def spinner(label: str) -> Generator[None, None, None]:
# Countdown
# ---------------------------------------------------------------------------
def countdown(seconds: int, label: str = "Starting in") -> None:
for remaining in range(seconds, 0, -1):
sys.stdout.write(f"\r {dim(label)} {accent(str(remaining))}s ")