ollama2cpp 0.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
ollama2cpp/ui.py ADDED
@@ -0,0 +1,172 @@
1
+ """Console output seam: every user-facing message goes through Rich here."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import contextlib
6
+ import sys
7
+ from collections.abc import Generator, Sequence
8
+ from typing import Literal
9
+
10
+ from rich.console import Console
11
+ from rich.panel import Panel
12
+ from rich.progress import (
13
+ BarColumn,
14
+ DownloadColumn,
15
+ Progress,
16
+ SpinnerColumn,
17
+ TextColumn,
18
+ TimeElapsedColumn,
19
+ TimeRemainingColumn,
20
+ )
21
+ from rich.prompt import Confirm
22
+ from rich.table import Table
23
+ from rich.text import Text
24
+
25
+ __all__ = [
26
+ "Level",
27
+ "confirm",
28
+ "console",
29
+ "panel",
30
+ "progress",
31
+ "show_command",
32
+ "show_notice",
33
+ "show_table",
34
+ "status_text",
35
+ "steps_progress",
36
+ "use_stderr",
37
+ ]
38
+
39
+ Level = Literal["info", "ok", "warn", "error"]
40
+
41
+ console = Console()
42
+
43
+ _STYLES: dict[str, tuple[str, str]] = {
44
+ "info": ("blue", "i"),
45
+ "ok": ("green", "ok"),
46
+ "warn": ("yellow", "!"),
47
+ "error": ("red", "x"),
48
+ }
49
+
50
+
51
+ def show_notice(level: Level, message: str) -> None:
52
+ """Print a one-line notice styled by severity.
53
+
54
+ Args:
55
+ level: One of ``info``, ``ok``, ``warn`` or ``error``.
56
+ message: Text to show (Rich markup is not interpreted).
57
+ """
58
+ style, tag = _STYLES[level]
59
+ console.print(f"[{style}]{tag}[/{style}] ", end="")
60
+ console.print(message, markup=False, highlight=False)
61
+
62
+
63
+ def confirm(question: str, *, assume_yes: bool = False, default: bool = True) -> bool:
64
+ """Ask a yes/no question.
65
+
66
+ Args:
67
+ question: The question to show.
68
+ assume_yes: Answer yes without asking.
69
+ default: The answer when the user just presses Enter (use False for destructive actions).
70
+
71
+ Returns:
72
+ bool: True when ``assume_yes`` is set or stdin is not a TTY, else the user's answer.
73
+ """
74
+ if assume_yes or not sys.stdin.isatty():
75
+ return True
76
+ return Confirm.ask(question, console=console, default=default)
77
+
78
+
79
+ def panel(title: str, body: str) -> None:
80
+ """Print a bordered panel (the body is plain text, Rich markup is not interpreted).
81
+
82
+ Args:
83
+ title: Panel title.
84
+ body: Panel text, may span several lines.
85
+ """
86
+ console.print(Panel(Text(body), title=title, title_align="left", expand=False))
87
+
88
+
89
+ def show_command(command: str) -> None:
90
+ """Print a command line verbatim on one logical line (never wrapped or restyled).
91
+
92
+ Args:
93
+ command: The command line.
94
+ """
95
+ console.print(command, markup=False, highlight=False, soft_wrap=True)
96
+
97
+
98
+ def progress() -> Progress:
99
+ """Build a download-style progress bar bound to the shared console.
100
+
101
+ Returns:
102
+ Progress: A Rich progress instance, to be used as a context manager.
103
+ """
104
+ return Progress(
105
+ TextColumn("[bold]{task.description}"),
106
+ BarColumn(),
107
+ DownloadColumn(),
108
+ TimeRemainingColumn(),
109
+ console=console,
110
+ )
111
+
112
+
113
+ def steps_progress() -> Progress:
114
+ """Build a progress display for work of unknown length (a spinner, a step counter and the elapsed time).
115
+
116
+ Returns:
117
+ Progress: A Rich progress instance, to be used as a context manager.
118
+ """
119
+ return Progress(
120
+ SpinnerColumn(),
121
+ TextColumn("[bold]{task.description}"),
122
+ TextColumn("{task.completed} candidates"),
123
+ TimeElapsedColumn(),
124
+ console=console,
125
+ transient=True,
126
+ )
127
+
128
+
129
+ def status_text(level: Literal["ok", "warn", "fail"]) -> Text:
130
+ """Render a check status word in its severity colour.
131
+
132
+ Args:
133
+ level: One of ``ok``, ``warn`` or ``fail``.
134
+
135
+ Returns:
136
+ Text: The word, coloured green, yellow or red.
137
+ """
138
+ style = {"ok": "green", "warn": "yellow", "fail": "red"}[level]
139
+ return Text(level, style=style)
140
+
141
+
142
+ def show_table(title: str, columns: Sequence[str], rows: Sequence[Sequence[str | Text]]) -> None:
143
+ """Print a table whose cells are plain text (Rich markup in cells is not interpreted).
144
+
145
+ Args:
146
+ title: Table title.
147
+ columns: Column headers.
148
+ rows: One sequence of cells per row; a ``Text`` cell keeps its own style.
149
+ """
150
+ table = Table(title=title, title_justify="left", header_style="bold")
151
+ for column in columns:
152
+ table.add_column(column, overflow="fold")
153
+ for row in rows:
154
+ table.add_row(*(cell if isinstance(cell, Text) else Text(cell) for cell in row))
155
+ console.print(table)
156
+
157
+
158
+ @contextlib.contextmanager
159
+ def use_stderr() -> Generator[None]:
160
+ """Send every message, panel, prompt and progress display to stderr for the duration of the block.
161
+
162
+ Used by commands whose stdout is machine output (``list --json``), so notices stay visible without polluting it.
163
+
164
+ Yields:
165
+ None: Control to the caller; the console goes back to stdout afterwards.
166
+ """
167
+ previous = console.stderr
168
+ console.stderr = True
169
+ try:
170
+ yield
171
+ finally:
172
+ console.stderr = previous