mmit-clitheme 0.15.0__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.
- cli_theme/__init__.py +47 -0
- cli_theme/arguments.py +265 -0
- cli_theme/bash/cli.lib.sh +475 -0
- cli_theme/bash/make.lib.sh +138 -0
- cli_theme/cli.py +276 -0
- cli_theme/deno/deno.json +10 -0
- cli_theme/deno/help.ts +150 -0
- cli_theme/deno/mod.ts +413 -0
- cli_theme/export.py +221 -0
- cli_theme/i18n.py +18 -0
- cli_theme/locales/de/LC_MESSAGES/cli_theme.mo +0 -0
- cli_theme/locales/de/LC_MESSAGES/cli_theme.po +328 -0
- cli_theme/output.py +261 -0
- cli_theme/skill.py +107 -0
- cli_theme/templates/classic.theme +46 -0
- cli_theme/templates/earth.theme +46 -0
- cli_theme/templates/forest.theme +46 -0
- cli_theme/templates/mono.theme +46 -0
- cli_theme/templates/neon.theme +46 -0
- cli_theme/templates/night.theme +46 -0
- cli_theme/templates/ocean.theme +46 -0
- cli_theme/templates/shell.theme +46 -0
- cli_theme/templates/sunset.theme +46 -0
- cli_theme/theme.py +422 -0
- mmit_clitheme-0.15.0.data/scripts/clilib-path +74 -0
- mmit_clitheme-0.15.0.dist-info/METADATA +482 -0
- mmit_clitheme-0.15.0.dist-info/RECORD +33 -0
- mmit_clitheme-0.15.0.dist-info/WHEEL +5 -0
- mmit_clitheme-0.15.0.dist-info/entry_points.txt +3 -0
- mmit_clitheme-0.15.0.dist-info/licenses/LICENSE +287 -0
- mmit_clitheme-0.15.0.dist-info/licenses/LICENSE.de.txt +101 -0
- mmit_clitheme-0.15.0.dist-info/licenses/LICENSING.md +97 -0
- mmit_clitheme-0.15.0.dist-info/top_level.txt +1 -0
cli_theme/cli.py
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
"""Befehl clitheme (bis 0.5 cli-theme, als zweiter Name erhalten): Vorlagen anlegen, Themes
|
|
2
|
+
auflisten und wählen, Ort nennen, Rollen und Abstände zeigen, für Bash, Make und Deno exportieren.
|
|
3
|
+
Hilfe und Hinweise für Makefiles schreibt BashLib (make.lib.sh)."""
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import os
|
|
7
|
+
import sys
|
|
8
|
+
from importlib.metadata import version
|
|
9
|
+
|
|
10
|
+
from rich.text import Text
|
|
11
|
+
|
|
12
|
+
from cli_theme.arguments import ArgumentParser
|
|
13
|
+
from cli_theme.export import RENDER, deno_module, export, theme_values
|
|
14
|
+
from cli_theme.i18n import N_, _
|
|
15
|
+
from cli_theme.output import Output, styles
|
|
16
|
+
from cli_theme.skill import SkillAction
|
|
17
|
+
from cli_theme.theme import (
|
|
18
|
+
PACKAGE,
|
|
19
|
+
ROLES,
|
|
20
|
+
SPACING,
|
|
21
|
+
ThemeError,
|
|
22
|
+
adopt_legacy_themes,
|
|
23
|
+
install_themes,
|
|
24
|
+
load_theme,
|
|
25
|
+
read_theme_file,
|
|
26
|
+
select_theme,
|
|
27
|
+
selection_error,
|
|
28
|
+
source_path,
|
|
29
|
+
theme_name,
|
|
30
|
+
theme_names,
|
|
31
|
+
theme_origin,
|
|
32
|
+
theme_path,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
SAMPLE = N_("Sample")
|
|
36
|
+
EXPORT_HELP = N_(
|
|
37
|
+
"Resolved THEME_* values of the chosen theme (CLI_THEME or clitheme use, file under"
|
|
38
|
+
" XDG_CONFIG_HOME)."
|
|
39
|
+
" Without a valid file: no colors, classic spacing and THEME_NOTICE with the finding;"
|
|
40
|
+
" exit 0. The result is cached under ${XDG_CACHE_HOME:-~/.cache}/clitheme/."
|
|
41
|
+
' Bash: eval "$(clitheme export --bash)".'
|
|
42
|
+
" Make: -include $(shell clitheme export --make 2>/dev/null)."
|
|
43
|
+
" Other languages: --json, one JSON object with the same names."
|
|
44
|
+
" Deno: --deno, path of mod.ts of the package @mmit/clitheme in this installation."
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def install(out: Output) -> int:
|
|
49
|
+
"""Übernimmt Dateien aus dem alten Ordner cli-theme und legt fehlende Vorlagen an;
|
|
50
|
+
vorhandene Dateien bleiben unverändert."""
|
|
51
|
+
out.head(_("Theme files"))
|
|
52
|
+
adopted, skipped = adopt_legacy_themes()
|
|
53
|
+
for path in adopted:
|
|
54
|
+
label = Text(f"{_('adopted'):<10}", style=out.styles["ok"])
|
|
55
|
+
out.row(label, Text(str(path), style=out.styles["val"]))
|
|
56
|
+
for path, error in skipped:
|
|
57
|
+
label = Text(f"{_('skipped'):<10}", style=out.styles["warn"])
|
|
58
|
+
out.row(label, Text(str(path), style=out.styles["val"]))
|
|
59
|
+
out.hint(str(error))
|
|
60
|
+
for path, created in install_themes():
|
|
61
|
+
state = _("created") if created else _("unchanged")
|
|
62
|
+
style = out.styles["ok"] if created else out.styles["hint"]
|
|
63
|
+
out.row(Text(f"{state:<10}", style=style), Text(str(path), style=out.styles["val"]))
|
|
64
|
+
return 0
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def show(out: Output) -> int:
|
|
68
|
+
"""Zeigt Ort, Farbrollen mit Muster und Abstände des gewählten Themes."""
|
|
69
|
+
theme = load_theme()
|
|
70
|
+
out.head(_("Theme"))
|
|
71
|
+
out.row(_("Name"), theme_name())
|
|
72
|
+
out.row(_("Chosen by"), _(theme_origin()))
|
|
73
|
+
out.row(_("File"), str(theme.path))
|
|
74
|
+
damaged_selection(out)
|
|
75
|
+
if not theme.found:
|
|
76
|
+
out.hint(_("File missing: no colors, spacing from the classic template."))
|
|
77
|
+
out.hint(_("Create it with: clitheme install"))
|
|
78
|
+
elif theme.legacy:
|
|
79
|
+
out.hint(_("File in the old folder cli-theme; move it with: clitheme install"))
|
|
80
|
+
rich = styles(theme)
|
|
81
|
+
out.head(_("Colors"))
|
|
82
|
+
for role in ROLES:
|
|
83
|
+
value = theme.colors[role]
|
|
84
|
+
sample = Text(f"{value or '-':<10}", style=out.styles["val"]) + Text(
|
|
85
|
+
_(SAMPLE), style=rich[role]
|
|
86
|
+
)
|
|
87
|
+
out.row(f"C_{role.upper()}", sample)
|
|
88
|
+
out.head(_("Spacing"))
|
|
89
|
+
for name in SPACING:
|
|
90
|
+
out.row(name.upper(), theme.spacing[name])
|
|
91
|
+
return 0
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def damaged_selection(out: Output) -> None:
|
|
95
|
+
"""Hinweis, wenn die gespeicherte Wahl gelten soll, aber unbrauchbar ist; es gilt classic."""
|
|
96
|
+
error = selection_error()
|
|
97
|
+
if error is not None:
|
|
98
|
+
out.hint(str(error))
|
|
99
|
+
out.hint(_("Using {name}; choose again with: clitheme use NAME").format(name=theme_name()))
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def list_themes(out: Output) -> int:
|
|
103
|
+
"""Listet die Vorlagen und eigenen Theme-Dateien mit Ort und markiert das wirksame, auch wenn
|
|
104
|
+
CLI_THEME ein Theme ohne Datei nennt."""
|
|
105
|
+
chosen = theme_name()
|
|
106
|
+
names = theme_names()
|
|
107
|
+
out.head(_("Themes"))
|
|
108
|
+
for name in names if chosen in names else (*names, chosen):
|
|
109
|
+
path = source_path(name)
|
|
110
|
+
place = str(path) if path.exists() else _("not installed")
|
|
111
|
+
value = Text(place, style=out.styles["val"])
|
|
112
|
+
if name == chosen:
|
|
113
|
+
mark = _("active, {origin}").format(origin=_(theme_origin()))
|
|
114
|
+
value += Text(f" ({mark})", style=out.styles["mark"])
|
|
115
|
+
out.row(name, value)
|
|
116
|
+
damaged_selection(out)
|
|
117
|
+
out.hint(_("Switch with: clitheme use NAME"))
|
|
118
|
+
return 0
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def use(out: Output, name: str) -> int:
|
|
122
|
+
"""Speichert name als Wahl für neue Prozesse; weist darauf hin, wenn CLI_THEME sie
|
|
123
|
+
überdeckt. Der Hinweis nennt CLI_THEME wörtlich: auch ein ungültiger Wert überdeckt die
|
|
124
|
+
gespeicherte Wahl, und die ist dann schon geschrieben."""
|
|
125
|
+
created = select_theme(name)
|
|
126
|
+
out.head(_("Theme"))
|
|
127
|
+
out.row(_("Chosen"), name)
|
|
128
|
+
if created:
|
|
129
|
+
out.row(_("created"), str(theme_path(name)))
|
|
130
|
+
if theme_origin() == "CLI_THEME":
|
|
131
|
+
out.hint(
|
|
132
|
+
_("CLI_THEME={name} is set and wins over this choice; it takes effect after:").format(
|
|
133
|
+
name=os.environ["CLI_THEME"]
|
|
134
|
+
)
|
|
135
|
+
)
|
|
136
|
+
out.write(Text(out.indent("i_hint")) + Text("unset CLI_THEME", style=out.styles["val"]))
|
|
137
|
+
return 0
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def export_theme(form: str) -> int:
|
|
141
|
+
"""Gibt für bash die Zuweisungen aus, für json das JSON-Objekt, für make den Pfad der
|
|
142
|
+
.mk-Datei im Cache, für deno den Pfad von mod.ts. Lässt sich der Cache nicht schreiben,
|
|
143
|
+
rechnen bash und json die Werte ohne ihn; make braucht die Datei."""
|
|
144
|
+
if form == "deno":
|
|
145
|
+
print(deno_module())
|
|
146
|
+
return 0
|
|
147
|
+
try:
|
|
148
|
+
path = export(form)
|
|
149
|
+
text = read_theme_file(path) if form != "make" else None
|
|
150
|
+
except (ThemeError, OSError):
|
|
151
|
+
if form == "make":
|
|
152
|
+
raise
|
|
153
|
+
text = None
|
|
154
|
+
if form == "make":
|
|
155
|
+
print(path)
|
|
156
|
+
else:
|
|
157
|
+
print(text or RENDER[form](theme_values()), end="")
|
|
158
|
+
return 0
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def translate_standard_texts(parser: ArgumentParser, positionals: str) -> None:
|
|
162
|
+
"""Hilfe von -h und die Abschnittstitel, die argparse selbst setzt, aus dem eigenen
|
|
163
|
+
Katalog; sonst stünden sie auch in der deutschen Hilfe englisch."""
|
|
164
|
+
parser._positionals.title = positionals
|
|
165
|
+
parser._optionals.title = _("options")
|
|
166
|
+
for action in parser._actions:
|
|
167
|
+
if isinstance(action, argparse._HelpAction):
|
|
168
|
+
action.help = _("show this help")
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def parser() -> ArgumentParser:
|
|
172
|
+
"""Argumente: install, list, use, path, show, export, --version und --skill."""
|
|
173
|
+
result = ArgumentParser(
|
|
174
|
+
prog="clitheme",
|
|
175
|
+
description=_("Shared CLI theme for Python, Bash and Make: colors and spacing."),
|
|
176
|
+
)
|
|
177
|
+
result.add_argument(
|
|
178
|
+
"--version",
|
|
179
|
+
action="version",
|
|
180
|
+
version=f"%(prog)s {version(PACKAGE)}",
|
|
181
|
+
help=_("show the installed version"),
|
|
182
|
+
)
|
|
183
|
+
result.add_argument(
|
|
184
|
+
"--skill", action=SkillAction, help=_("describe the commands for AI agents (Markdown)")
|
|
185
|
+
)
|
|
186
|
+
translate_standard_texts(result, _("commands"))
|
|
187
|
+
commands = result.add_subparsers(dest="command", metavar="COMMAND")
|
|
188
|
+
commands.add_parser("install", help=_("create missing theme files, keep existing ones"))
|
|
189
|
+
commands.add_parser(
|
|
190
|
+
"list",
|
|
191
|
+
help=_("list the available themes and mark the active one"),
|
|
192
|
+
description=_(
|
|
193
|
+
"Lists the templates and own theme files with their location. The active theme"
|
|
194
|
+
" comes from CLI_THEME, else from clitheme use, else classic."
|
|
195
|
+
),
|
|
196
|
+
)
|
|
197
|
+
choosing = commands.add_parser(
|
|
198
|
+
"use",
|
|
199
|
+
help=_("choose the theme for new processes"),
|
|
200
|
+
description=_(
|
|
201
|
+
"Saves NAME as the theme for new processes in"
|
|
202
|
+
" ${XDG_CONFIG_HOME:-~/.config}/clitheme/selected; a missing template file is"
|
|
203
|
+
" created. CLI_THEME wins over this choice."
|
|
204
|
+
),
|
|
205
|
+
)
|
|
206
|
+
choosing.add_argument("name", metavar="NAME", help=_("name from clitheme list"))
|
|
207
|
+
commands.add_parser("path", help=_("print the path of the chosen theme file"))
|
|
208
|
+
commands.add_parser("show", help=_("show roles and spacing of the chosen theme"))
|
|
209
|
+
exporting = commands.add_parser(
|
|
210
|
+
"export",
|
|
211
|
+
help=_("print THEME_* values for Bash, a .mk file for Make, JSON, or the Deno package"),
|
|
212
|
+
description=_(EXPORT_HELP),
|
|
213
|
+
)
|
|
214
|
+
form = exporting.add_mutually_exclusive_group(required=True)
|
|
215
|
+
form.add_argument(
|
|
216
|
+
"--bash",
|
|
217
|
+
dest="form",
|
|
218
|
+
action="store_const",
|
|
219
|
+
const="bash",
|
|
220
|
+
help=_("print assignments for eval, each value quoted"),
|
|
221
|
+
)
|
|
222
|
+
form.add_argument(
|
|
223
|
+
"--make",
|
|
224
|
+
dest="form",
|
|
225
|
+
action="store_const",
|
|
226
|
+
const="make",
|
|
227
|
+
help=_("print the path of a .mk file in the cache, for -include"),
|
|
228
|
+
)
|
|
229
|
+
form.add_argument(
|
|
230
|
+
"--json",
|
|
231
|
+
dest="form",
|
|
232
|
+
action="store_const",
|
|
233
|
+
const="json",
|
|
234
|
+
help=_("print one JSON object, numbers for widths and spacing, else strings"),
|
|
235
|
+
)
|
|
236
|
+
form.add_argument(
|
|
237
|
+
"--deno",
|
|
238
|
+
dest="form",
|
|
239
|
+
action="store_const",
|
|
240
|
+
const="deno",
|
|
241
|
+
help=_("print the path of mod.ts of the Deno package @mmit/clitheme"),
|
|
242
|
+
)
|
|
243
|
+
for command in commands.choices.values():
|
|
244
|
+
translate_standard_texts(command, _("arguments"))
|
|
245
|
+
return result
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def main(argv: list[str] | None = None) -> int:
|
|
249
|
+
"""Einstieg des Befehls clitheme, auch unter dem alten Namen cli-theme; Fehler im Theme
|
|
250
|
+
enden mit Exit 1, außer bei export. Jede Ausgabe außer path, export und --skill endet mit
|
|
251
|
+
einer Leerzeile."""
|
|
252
|
+
arguments = parser().parse_args(argv)
|
|
253
|
+
if arguments.command is None:
|
|
254
|
+
parser().print_help()
|
|
255
|
+
return 0
|
|
256
|
+
with Output() as out:
|
|
257
|
+
try:
|
|
258
|
+
if arguments.command == "path":
|
|
259
|
+
# Zum Kopieren: print statt Output, daher ohne Leerzeile am Ende.
|
|
260
|
+
print(theme_path())
|
|
261
|
+
return 0
|
|
262
|
+
if arguments.command == "export":
|
|
263
|
+
# Zum Einlesen mit eval oder include: print, ohne Leerzeile am Ende.
|
|
264
|
+
return export_theme(arguments.form)
|
|
265
|
+
if arguments.command == "list":
|
|
266
|
+
return list_themes(out)
|
|
267
|
+
if arguments.command == "use":
|
|
268
|
+
return use(out, arguments.name)
|
|
269
|
+
return install(out) if arguments.command == "install" else show(out)
|
|
270
|
+
except (ThemeError, OSError) as error:
|
|
271
|
+
out.error(f"✗ {error}")
|
|
272
|
+
return 1
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
if __name__ == "__main__":
|
|
276
|
+
sys.exit(main())
|
cli_theme/deno/deno.json
ADDED
cli_theme/deno/help.ts
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* --help-Ausgabe im Theme von clitheme, wie die Hilfe von changelog aus ProjectTools.
|
|
3
|
+
*
|
|
4
|
+
* Vorlage ist HelpFormatter in ProjectTools src/python/projecttools/ui/colors.py (argparse mit
|
|
5
|
+
* RawDescriptionHelpFormatter) samt ScriptArgumentParser in changelog.py: Aufrufzeile,
|
|
6
|
+
* Beschreibung, Abschnitte mit Option · Beschreibung in den Spalten von Theme.row und ein
|
|
7
|
+
* Schlusstext, dessen Zeilen mit „:“ als Überschrift und eingerückte Zeilen als Beispiel
|
|
8
|
+
* erscheinen. Die Beschreibung einer Option bricht wie textwrap in Python um.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { guard, plain, type Theme } from "./mod.ts";
|
|
12
|
+
|
|
13
|
+
/** Ein Eintrag der Hilfe: Option („-o“, „--output“) samt Metavariable, oder ein Argument. */
|
|
14
|
+
export interface HelpItem {
|
|
15
|
+
/** Namen der Option oder des Arguments; mehrere stehen als „-o | --output“. */
|
|
16
|
+
names: string[];
|
|
17
|
+
/** Platzhalter für den Wert, etwa OUTPUT; ohne Angabe nimmt die Option keinen Wert. */
|
|
18
|
+
metavar?: string;
|
|
19
|
+
/** Beschreibung; lange Texte brechen in der Beschreibungsspalte um. */
|
|
20
|
+
help?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Abschnitt der Hilfe, etwa „Options“; ohne Einträge entfällt er wie bei argparse. */
|
|
24
|
+
export interface HelpSection {
|
|
25
|
+
title: string;
|
|
26
|
+
items: HelpItem[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface HelpSpec {
|
|
30
|
+
/**
|
|
31
|
+
* Aufruf ohne „usage: “, etwa `changelog (-g | -n | -c) [-o OUTPUT] [-h]`. Steht wörtlich da,
|
|
32
|
+
* ohne Umbruch: argparse bricht seine selbst erzeugte Aufrufzeile um, hier kommt sie fertig,
|
|
33
|
+
* mehrzeilig mit eingerückten Folgezeilen.
|
|
34
|
+
*/
|
|
35
|
+
usage?: string;
|
|
36
|
+
/** Text unter der Aufrufzeile; mehrzeilig wie der Schlusstext. */
|
|
37
|
+
description?: string;
|
|
38
|
+
sections?: HelpSection[];
|
|
39
|
+
/** Schlusstext: Zeilen mit „:“ am Ende als Überschrift, eingerückte als Beispiel. */
|
|
40
|
+
epilog?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Platz der Kurzform „-x | “; eine Option nur mit Langform steht damit bündig. */
|
|
44
|
+
const SHORT_PAD = " ";
|
|
45
|
+
|
|
46
|
+
/** Bezeichnung wie _format_action_invocation in colors.py. */
|
|
47
|
+
function label(item: HelpItem): string {
|
|
48
|
+
let text = item.names.join(" | ");
|
|
49
|
+
if (item.names.length === 1 && text.startsWith("--")) text = SHORT_PAD + text;
|
|
50
|
+
return item.metavar ? `${text} ${item.metavar}` : text;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Teilt Text in Stücke wie textwrap in Python: Wörter, Leerraum dazwischen und Teile eines
|
|
55
|
+
* Worts nach einem Bindestrich zwischen Buchstaben („Release-“ „Tags“).
|
|
56
|
+
*/
|
|
57
|
+
function chunks(text: string): string[] {
|
|
58
|
+
const result: string[] = [];
|
|
59
|
+
for (const part of text.split(/(\s+)/)) {
|
|
60
|
+
if (!part) continue;
|
|
61
|
+
if (/^\s/.test(part)) result.push(part);
|
|
62
|
+
else result.push(...part.split(/(?<=\p{L}{2}-)(?=\p{L}-?\p{L})/u));
|
|
63
|
+
}
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Zeilen von höchstens `width` Zeichen wie textwrap.wrap: Leerraum wird zu einem Leerzeichen,
|
|
69
|
+
* gebrochen wird an Leerzeichen und nach Bindestrichen; ein Wort länger als die Zeile wird
|
|
70
|
+
* geteilt, bevorzugt nach einem Bindestrich.
|
|
71
|
+
*/
|
|
72
|
+
export function wrapText(text: string, width: number): string[] {
|
|
73
|
+
const pending = chunks(text.replace(/\s+/g, " ").trim()).reverse();
|
|
74
|
+
const lines: string[] = [];
|
|
75
|
+
const length = (chunk: string) => [...chunk].length;
|
|
76
|
+
while (pending.length) {
|
|
77
|
+
const line: string[] = [];
|
|
78
|
+
let used = 0;
|
|
79
|
+
if (lines.length && pending[pending.length - 1].trim() === "") pending.pop();
|
|
80
|
+
while (pending.length && used + length(pending[pending.length - 1]) <= width) {
|
|
81
|
+
const chunk = pending.pop()!;
|
|
82
|
+
line.push(chunk);
|
|
83
|
+
used += length(chunk);
|
|
84
|
+
}
|
|
85
|
+
if (pending.length && length(pending[pending.length - 1]) > width) {
|
|
86
|
+
const chars = [...pending.pop()!];
|
|
87
|
+
const room = Math.max(1, width - used);
|
|
88
|
+
let end = room;
|
|
89
|
+
const hyphen = chars.slice(0, room).lastIndexOf("-");
|
|
90
|
+
if (hyphen > 0 && chars.slice(0, hyphen).some((char) => char !== "-")) end = hyphen + 1;
|
|
91
|
+
line.push(chars.slice(0, end).join(""));
|
|
92
|
+
pending.push(chars.slice(end).join(""));
|
|
93
|
+
}
|
|
94
|
+
if (line.length && line[line.length - 1].trim() === "") line.pop();
|
|
95
|
+
if (line.length) lines.push(line.join(""));
|
|
96
|
+
}
|
|
97
|
+
return lines;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Text ohne Rolle: Steuerzeichen entschärft, fremde Farbfolgen nur mit Farben. */
|
|
101
|
+
function raw(theme: Theme, value: string): string {
|
|
102
|
+
return theme.color ? guard(value) : plain(guard(value));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Freier Text wie _format_text in colors.py: mehrzeilig mit Überschriften und Beispielen. */
|
|
106
|
+
function text(theme: Theme, value: string): string {
|
|
107
|
+
if (!value.includes("\n")) return raw(theme, value);
|
|
108
|
+
const { THEME_INDENT_GROUP, THEME_INDENT_TARGET } = theme.values;
|
|
109
|
+
return value.split("\n").map((line) => {
|
|
110
|
+
if (line.endsWith(":") && !line.startsWith(" ")) {
|
|
111
|
+
return THEME_INDENT_GROUP + theme.style(line, "GROUP");
|
|
112
|
+
}
|
|
113
|
+
if (line.startsWith(" ")) return THEME_INDENT_TARGET + theme.style(line.trim(), "SUCCESS");
|
|
114
|
+
return raw(theme, line);
|
|
115
|
+
}).join("\n");
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Zeile einer Option oder eines Arguments, die Beschreibung umbrochen unter ihrer Spalte. */
|
|
119
|
+
function item(theme: Theme, entry: HelpItem): string {
|
|
120
|
+
const values = theme.values;
|
|
121
|
+
const gap = Math.max(1, Number(values.THEME_COLUMN_GAP) || 0);
|
|
122
|
+
const column = values.THEME_INDENT_TARGET.length + Number(values.THEME_WIDTH_TARGET) + gap;
|
|
123
|
+
// Wie der HelpFormatter von changelog: nach THEME_WIDTH_HELP, nicht nach dem Terminal, auch
|
|
124
|
+
// in Pipes; mindestens 20 Zeichen, so auch bei WIDTH=0.
|
|
125
|
+
const width = Math.max(20, (Number(values.THEME_WIDTH_HELP) || 0) - column);
|
|
126
|
+
const [first = "", ...rest] = wrapText(entry.help ?? "", width);
|
|
127
|
+
return [
|
|
128
|
+
theme.row(label(entry), first),
|
|
129
|
+
...rest.map((line) => " ".repeat(column) + theme.style(line, "DESC")),
|
|
130
|
+
].join("\n");
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Die ganze Hilfe zum Ausgeben mit console.log: davor eine Leerzeile, danach eine, wie bei
|
|
135
|
+
* changelog. Zwischen den Blöcken stehen THEME_GROUP_SPACING Leerzeilen.
|
|
136
|
+
*/
|
|
137
|
+
export function formatHelp(theme: Theme, spec: HelpSpec): string {
|
|
138
|
+
const blocks: string[] = [];
|
|
139
|
+
if (spec.usage) blocks.push(`usage: ${raw(theme, spec.usage)}`);
|
|
140
|
+
if (spec.description) blocks.push(text(theme, spec.description));
|
|
141
|
+
for (const section of spec.sections ?? []) {
|
|
142
|
+
if (!section.items.length) continue;
|
|
143
|
+
const heading = theme.values.THEME_INDENT_GROUP + theme.style(section.title, "GROUP") + ":";
|
|
144
|
+
blocks.push([heading, ...section.items.map((entry) => item(theme, entry))].join("\n"));
|
|
145
|
+
}
|
|
146
|
+
if (spec.epilog) blocks.push(text(theme, spec.epilog));
|
|
147
|
+
const spacing = Math.max(0, Number(theme.values.THEME_GROUP_SPACING) || 0);
|
|
148
|
+
const body = blocks.map((block) => block.replace(/^\n+|\n+$/g, "")).join("\n\n");
|
|
149
|
+
return `\n${body.replace(/\n{2,}/g, "\n".repeat(spacing + 1))}\n`;
|
|
150
|
+
}
|