waymark-cli 0.1.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.
- waymark/__init__.py +3 -0
- waymark/__main__.py +8 -0
- waymark/adapters/__init__.py +102 -0
- waymark/adapters/claude_code.py +238 -0
- waymark/assets/__init__.py +43 -0
- waymark/assets/agents/wm-implementer.md +45 -0
- waymark/assets/agents/wm-reviewer.md +43 -0
- waymark/assets/skills/wm-apply/SKILL.md +58 -0
- waymark/assets/skills/wm-archive/SKILL.md +32 -0
- waymark/assets/skills/wm-explore/SKILL.md +34 -0
- waymark/assets/skills/wm-kickoff/SKILL.md +34 -0
- waymark/assets/skills/wm-onboard/SKILL.md +27 -0
- waymark/assets/skills/wm-pr/SKILL.md +24 -0
- waymark/assets/skills/wm-propose/SKILL.md +32 -0
- waymark/assets/skills/wm-update/SKILL.md +31 -0
- waymark/cli.py +537 -0
- waymark/hooks/__init__.py +6 -0
- waymark/hooks/commands.py +190 -0
- waymark/hooks/decide.py +314 -0
- waymark/hooks/events.py +79 -0
- waymark/py.typed +0 -0
- waymark/record/__init__.py +5 -0
- waymark/record/atlas.py +156 -0
- waymark/record/changes.py +202 -0
- waymark/record/charter.py +75 -0
- waymark/record/commits.py +78 -0
- waymark/record/config.py +215 -0
- waymark/record/deltas.py +318 -0
- waymark/record/design.py +80 -0
- waymark/record/edits.py +95 -0
- waymark/record/evidence.py +58 -0
- waymark/record/explorations.py +66 -0
- waymark/record/files.py +75 -0
- waymark/record/fingerprint.py +73 -0
- waymark/record/git.py +167 -0
- waymark/record/githooks.py +106 -0
- waymark/record/links.py +104 -0
- waymark/record/milestones.py +84 -0
- waymark/record/moves.py +60 -0
- waymark/record/names.py +39 -0
- waymark/record/outline.py +144 -0
- waymark/record/pointer.py +56 -0
- waymark/record/problems.py +20 -0
- waymark/record/project.py +118 -0
- waymark/record/proposal.py +171 -0
- waymark/record/record.py +340 -0
- waymark/record/registry.py +158 -0
- waymark/record/specs.py +227 -0
- waymark/record/state.py +472 -0
- waymark/record/store.py +88 -0
- waymark/record/tasks.py +415 -0
- waymark/record/templates/charter.md +45 -0
- waymark/record/templates/ci-workflow.yml +29 -0
- waymark/record/templates/config-atlas.toml +8 -0
- waymark/record/templates/config.toml +19 -0
- waymark/record/templates/delta.md +29 -0
- waymark/record/templates/design.md +20 -0
- waymark/record/templates/exploration.md +22 -0
- waymark/record/templates/goal.md +13 -0
- waymark/record/templates/hook-commit-msg +8 -0
- waymark/record/templates/hook-pre-commit +8 -0
- waymark/record/templates/proposal.md +18 -0
- waymark/record/templates/roadmap.md +4 -0
- waymark/record/templates/tasks.md +10 -0
- waymark/record/templates.py +30 -0
- waymark/record/terms.py +51 -0
- waymark/record/text.py +197 -0
- waymark/services/__init__.py +1 -0
- waymark/services/atlas.py +162 -0
- waymark/services/check.py +129 -0
- waymark/services/ci.py +44 -0
- waymark/services/common.py +154 -0
- waymark/services/context.py +296 -0
- waymark/services/doctor.py +367 -0
- waymark/services/githooks.py +132 -0
- waymark/services/instructions.py +304 -0
- waymark/services/integration.py +170 -0
- waymark/services/lifecycle.py +454 -0
- waymark/services/migrations.py +90 -0
- waymark/services/pr.py +177 -0
- waymark/services/references.py +27 -0
- waymark/services/setup.py +127 -0
- waymark/views/__init__.py +5 -0
- waymark/views/payloads.py +568 -0
- waymark/views/render.py +506 -0
- waymark/views/schemas.py +413 -0
- waymark/views/style.py +102 -0
- waymark_cli-0.1.0.dist-info/METADATA +313 -0
- waymark_cli-0.1.0.dist-info/RECORD +92 -0
- waymark_cli-0.1.0.dist-info/WHEEL +4 -0
- waymark_cli-0.1.0.dist-info/entry_points.txt +2 -0
- waymark_cli-0.1.0.dist-info/licenses/LICENSE +21 -0
waymark/views/schemas.py
ADDED
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
"""The schema of every ``--json`` payload, as JSON Schema.
|
|
2
|
+
|
|
3
|
+
Each payload names its schema and carries a version. Adding a field keeps the
|
|
4
|
+
version; removing or changing one bumps it.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from waymark.record.githooks import HooksState
|
|
10
|
+
from waymark.record.state import (
|
|
11
|
+
AcceptanceState,
|
|
12
|
+
ConflictKind,
|
|
13
|
+
ExplorationStatus,
|
|
14
|
+
RoadmapStatus,
|
|
15
|
+
StepKind,
|
|
16
|
+
)
|
|
17
|
+
from waymark.record.tasks import RecordKind
|
|
18
|
+
from waymark.record.terms import Layout, Phase, ReviewLevel, Verdict
|
|
19
|
+
from waymark.views.payloads import KINDS, VERSIONS
|
|
20
|
+
|
|
21
|
+
Schema = dict[str, Any]
|
|
22
|
+
|
|
23
|
+
STRING: Schema = {"type": "string"}
|
|
24
|
+
TEXT_OR_NULL: Schema = {"type": ["string", "null"]}
|
|
25
|
+
INTEGER: Schema = {"type": "integer"}
|
|
26
|
+
BOOLEAN: Schema = {"type": "boolean"}
|
|
27
|
+
DATE: Schema = {"type": "string", "pattern": r"^\d{4}-\d{2}-\d{2}$"}
|
|
28
|
+
DATE_OR_NULL: Schema = {"type": ["string", "null"], "pattern": r"^\d{4}-\d{2}-\d{2}$"}
|
|
29
|
+
NAMES: Schema = {"type": "array", "items": STRING}
|
|
30
|
+
SECTIONS: Schema = {"type": "object", "additionalProperties": STRING}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _enum(values: Any, *, nullable: bool = False) -> Schema:
|
|
34
|
+
choices: list[str | None] = [str(value.value) for value in values]
|
|
35
|
+
return {"enum": [*choices, None] if nullable else choices}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _object(properties: dict[str, Schema]) -> Schema:
|
|
39
|
+
"""An object with exactly these properties, all required."""
|
|
40
|
+
return {
|
|
41
|
+
"type": "object",
|
|
42
|
+
"properties": properties,
|
|
43
|
+
"required": list(properties),
|
|
44
|
+
"additionalProperties": False,
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _array(items: Schema) -> Schema:
|
|
49
|
+
return {"type": "array", "items": items}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _nullable(schema: Schema) -> Schema:
|
|
53
|
+
return {"anyOf": [schema, {"type": "null"}]}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _payload(name: str, properties: dict[str, Schema]) -> Schema:
|
|
57
|
+
return {
|
|
58
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
59
|
+
"title": f"wm.{name}",
|
|
60
|
+
**_object(
|
|
61
|
+
{
|
|
62
|
+
"schema": {"const": f"wm.{name}"},
|
|
63
|
+
"version": {"const": VERSIONS[name]},
|
|
64
|
+
**properties,
|
|
65
|
+
}
|
|
66
|
+
),
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
CONFLICT = _object(
|
|
71
|
+
{
|
|
72
|
+
"kind": _enum(ConflictKind),
|
|
73
|
+
"spec": STRING,
|
|
74
|
+
"requirement": TEXT_OR_NULL,
|
|
75
|
+
"changes": NAMES,
|
|
76
|
+
"message": STRING,
|
|
77
|
+
}
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
CHANGE_STATE = _object(
|
|
81
|
+
{
|
|
82
|
+
"name": STRING,
|
|
83
|
+
"owner": TEXT_OR_NULL,
|
|
84
|
+
"phase": _enum(Phase),
|
|
85
|
+
"short_path": BOOLEAN,
|
|
86
|
+
"acceptance": _enum(AcceptanceState),
|
|
87
|
+
"ticked": INTEGER,
|
|
88
|
+
"total": INTEGER,
|
|
89
|
+
"next_step": _object(
|
|
90
|
+
{"kind": _enum(StepKind), "item": TEXT_OR_NULL, "human": BOOLEAN, "reason": STRING}
|
|
91
|
+
),
|
|
92
|
+
"conflicts": _array(CONFLICT),
|
|
93
|
+
"repos": _array(
|
|
94
|
+
_object(
|
|
95
|
+
{
|
|
96
|
+
"name": STRING,
|
|
97
|
+
"kind": {"enum": ["atlas", "code"]},
|
|
98
|
+
"found": BOOLEAN,
|
|
99
|
+
"branch": TEXT_OR_NULL,
|
|
100
|
+
"matches": BOOLEAN,
|
|
101
|
+
"warn": BOOLEAN,
|
|
102
|
+
}
|
|
103
|
+
)
|
|
104
|
+
),
|
|
105
|
+
}
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
DELTA_NAMES = {"added": NAMES, "modified": NAMES, "removed": NAMES}
|
|
109
|
+
|
|
110
|
+
ITEM = _object(
|
|
111
|
+
{
|
|
112
|
+
"number": STRING,
|
|
113
|
+
"title": STRING,
|
|
114
|
+
"ticked": BOOLEAN,
|
|
115
|
+
"level": _enum(ReviewLevel, nullable=True),
|
|
116
|
+
"reviewed": {"type": ["boolean", "null"]},
|
|
117
|
+
"needs_human": TEXT_OR_NULL,
|
|
118
|
+
"criteria": TEXT_OR_NULL,
|
|
119
|
+
"verify": TEXT_OR_NULL,
|
|
120
|
+
"repo": TEXT_OR_NULL,
|
|
121
|
+
"blocked": TEXT_OR_NULL,
|
|
122
|
+
"spec_gap": TEXT_OR_NULL,
|
|
123
|
+
"notes": NAMES,
|
|
124
|
+
"records": _array(
|
|
125
|
+
_object(
|
|
126
|
+
{
|
|
127
|
+
"kind": _enum(RecordKind),
|
|
128
|
+
"date": DATE,
|
|
129
|
+
"verdict": _enum(Verdict, nullable=True),
|
|
130
|
+
"text": STRING,
|
|
131
|
+
}
|
|
132
|
+
)
|
|
133
|
+
),
|
|
134
|
+
}
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
SCHEMAS: dict[str, Schema] = {
|
|
138
|
+
"status": _payload(
|
|
139
|
+
"status",
|
|
140
|
+
{
|
|
141
|
+
"layout": _enum(Layout),
|
|
142
|
+
"atlas": TEXT_OR_NULL,
|
|
143
|
+
"wm_version": STRING,
|
|
144
|
+
"pinned_version": TEXT_OR_NULL,
|
|
145
|
+
"writable": BOOLEAN,
|
|
146
|
+
"changes": _array(CHANGE_STATE),
|
|
147
|
+
"milestones": _array(
|
|
148
|
+
_object({"name": STRING, "archived": INTEGER, "total": INTEGER, "planned": INTEGER})
|
|
149
|
+
),
|
|
150
|
+
"needs_human": INTEGER,
|
|
151
|
+
"recently_archived": _array(_object({"name": STRING, "date": DATE})),
|
|
152
|
+
"problems": INTEGER,
|
|
153
|
+
"git_hooks": _enum(HooksState),
|
|
154
|
+
},
|
|
155
|
+
),
|
|
156
|
+
"inbox": _payload(
|
|
157
|
+
"inbox",
|
|
158
|
+
{
|
|
159
|
+
"entries": _array(
|
|
160
|
+
_object(
|
|
161
|
+
{
|
|
162
|
+
"kind": {
|
|
163
|
+
"enum": [
|
|
164
|
+
"accept",
|
|
165
|
+
"spec-gap",
|
|
166
|
+
"verdict",
|
|
167
|
+
"blocked",
|
|
168
|
+
"conflict",
|
|
169
|
+
"archive",
|
|
170
|
+
"question",
|
|
171
|
+
]
|
|
172
|
+
},
|
|
173
|
+
"change": TEXT_OR_NULL,
|
|
174
|
+
"item": TEXT_OR_NULL,
|
|
175
|
+
"exploration": TEXT_OR_NULL,
|
|
176
|
+
"command": TEXT_OR_NULL,
|
|
177
|
+
"summary": STRING,
|
|
178
|
+
}
|
|
179
|
+
)
|
|
180
|
+
)
|
|
181
|
+
},
|
|
182
|
+
),
|
|
183
|
+
"ls": _payload(
|
|
184
|
+
"ls",
|
|
185
|
+
{
|
|
186
|
+
"kind": {"enum": list(KINDS)},
|
|
187
|
+
"from": TEXT_OR_NULL,
|
|
188
|
+
"entries": _array(_object({"name": STRING, "state": STRING, "detail": STRING})),
|
|
189
|
+
},
|
|
190
|
+
),
|
|
191
|
+
"check": _payload(
|
|
192
|
+
"check",
|
|
193
|
+
{
|
|
194
|
+
"findings": _array(
|
|
195
|
+
_object(
|
|
196
|
+
{
|
|
197
|
+
"severity": {"enum": ["error", "warning"]},
|
|
198
|
+
"path": STRING,
|
|
199
|
+
"line": INTEGER,
|
|
200
|
+
"code": STRING,
|
|
201
|
+
"message": STRING,
|
|
202
|
+
}
|
|
203
|
+
)
|
|
204
|
+
),
|
|
205
|
+
"errors": INTEGER,
|
|
206
|
+
"warnings": INTEGER,
|
|
207
|
+
},
|
|
208
|
+
),
|
|
209
|
+
"show.change": _payload(
|
|
210
|
+
"show.change",
|
|
211
|
+
{
|
|
212
|
+
"name": STRING,
|
|
213
|
+
"path": STRING,
|
|
214
|
+
"archived_on": DATE_OR_NULL,
|
|
215
|
+
"title": TEXT_OR_NULL,
|
|
216
|
+
"state": _nullable(CHANGE_STATE),
|
|
217
|
+
"owner": TEXT_OR_NULL,
|
|
218
|
+
"accepted": _nullable(_object({"date": DATE, "by": STRING})),
|
|
219
|
+
"synced_on": DATE_OR_NULL,
|
|
220
|
+
"proposal": _nullable(SECTIONS),
|
|
221
|
+
"outcome": TEXT_OR_NULL,
|
|
222
|
+
"deltas": _array(_object({"spec": STRING, **DELTA_NAMES})),
|
|
223
|
+
"design": _nullable(_object({"decisions": NAMES})),
|
|
224
|
+
"items": _array(ITEM),
|
|
225
|
+
"problems": _array(
|
|
226
|
+
_object({"path": STRING, "line": INTEGER, "code": STRING, "message": STRING})
|
|
227
|
+
),
|
|
228
|
+
},
|
|
229
|
+
),
|
|
230
|
+
"show.spec": _payload(
|
|
231
|
+
"show.spec",
|
|
232
|
+
{
|
|
233
|
+
"from": TEXT_OR_NULL,
|
|
234
|
+
"name": STRING,
|
|
235
|
+
"title": STRING,
|
|
236
|
+
"purpose": STRING,
|
|
237
|
+
"repos": NAMES,
|
|
238
|
+
"requirements": _array(
|
|
239
|
+
_object(
|
|
240
|
+
{
|
|
241
|
+
"name": STRING,
|
|
242
|
+
"statement": STRING,
|
|
243
|
+
"scenarios": _array(
|
|
244
|
+
_object(
|
|
245
|
+
{
|
|
246
|
+
"name": STRING,
|
|
247
|
+
"steps": _array(_object({"keyword": STRING, "text": STRING})),
|
|
248
|
+
}
|
|
249
|
+
)
|
|
250
|
+
),
|
|
251
|
+
}
|
|
252
|
+
)
|
|
253
|
+
),
|
|
254
|
+
"pending": _array(_object({"change": STRING, **DELTA_NAMES})),
|
|
255
|
+
"history": _array(_object({"date": DATE, "change": STRING, "outcome": TEXT_OR_NULL})),
|
|
256
|
+
},
|
|
257
|
+
),
|
|
258
|
+
"show.milestone": _payload(
|
|
259
|
+
"show.milestone",
|
|
260
|
+
{
|
|
261
|
+
"name": STRING,
|
|
262
|
+
"title": TEXT_OR_NULL,
|
|
263
|
+
"goal": _nullable(SECTIONS),
|
|
264
|
+
"lines": _array(
|
|
265
|
+
_object({"text": STRING, "change": TEXT_OR_NULL, "status": _enum(RoadmapStatus)})
|
|
266
|
+
),
|
|
267
|
+
"archived": INTEGER,
|
|
268
|
+
"total": INTEGER,
|
|
269
|
+
},
|
|
270
|
+
),
|
|
271
|
+
"show.exploration": _payload(
|
|
272
|
+
"show.exploration",
|
|
273
|
+
{
|
|
274
|
+
"name": STRING,
|
|
275
|
+
"title": STRING,
|
|
276
|
+
"status": _enum(ExplorationStatus),
|
|
277
|
+
"detail": TEXT_OR_NULL,
|
|
278
|
+
"sections": SECTIONS,
|
|
279
|
+
},
|
|
280
|
+
),
|
|
281
|
+
}
|
|
282
|
+
"""Every payload's schema, by the name after ``wm.`` in its ``schema`` field."""
|
|
283
|
+
|
|
284
|
+
_STEPS = [*(kind.value for kind in StepKind), "explore"]
|
|
285
|
+
|
|
286
|
+
_PACK_ITEM = _object(
|
|
287
|
+
{
|
|
288
|
+
"number": STRING,
|
|
289
|
+
"title": STRING,
|
|
290
|
+
"criteria": TEXT_OR_NULL,
|
|
291
|
+
"verify": TEXT_OR_NULL,
|
|
292
|
+
"repo": TEXT_OR_NULL,
|
|
293
|
+
"repo_found": {"type": ["boolean", "null"]},
|
|
294
|
+
"review_level": _enum(ReviewLevel),
|
|
295
|
+
"notes": NAMES,
|
|
296
|
+
"reviews": _array(
|
|
297
|
+
_object({"date": DATE, "verdict": _enum(Verdict, nullable=True), "text": STRING})
|
|
298
|
+
),
|
|
299
|
+
"resolutions": NAMES,
|
|
300
|
+
}
|
|
301
|
+
)
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def _delegated(role: str) -> Schema:
|
|
305
|
+
return _payload(
|
|
306
|
+
f"context.{role}",
|
|
307
|
+
{
|
|
308
|
+
"for": {"const": role},
|
|
309
|
+
"change": STRING,
|
|
310
|
+
"item": _PACK_ITEM,
|
|
311
|
+
"context": STRING,
|
|
312
|
+
"rules": STRING,
|
|
313
|
+
"proposal": _nullable(SECTIONS),
|
|
314
|
+
"deltas": _array(_object({"spec": STRING, "text": STRING})),
|
|
315
|
+
"requirements": _array(_object({"spec": STRING, "name": STRING, "text": STRING})),
|
|
316
|
+
"design": TEXT_OR_NULL,
|
|
317
|
+
"ends_with": STRING,
|
|
318
|
+
},
|
|
319
|
+
)
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
SCHEMAS.update(
|
|
323
|
+
{
|
|
324
|
+
"instructions": _payload(
|
|
325
|
+
"instructions",
|
|
326
|
+
{
|
|
327
|
+
"change": TEXT_OR_NULL,
|
|
328
|
+
"step": {"enum": _STEPS},
|
|
329
|
+
"item": TEXT_OR_NULL,
|
|
330
|
+
"ready": BOOLEAN,
|
|
331
|
+
"reason": STRING,
|
|
332
|
+
"outputs": _array(
|
|
333
|
+
_object({"path": STRING, "exists": BOOLEAN, "template": TEXT_OR_NULL})
|
|
334
|
+
),
|
|
335
|
+
"rules": STRING,
|
|
336
|
+
"context": STRING,
|
|
337
|
+
"read": NAMES,
|
|
338
|
+
"guidance": NAMES,
|
|
339
|
+
"commands": NAMES,
|
|
340
|
+
"unlocks": STRING,
|
|
341
|
+
},
|
|
342
|
+
),
|
|
343
|
+
"context.explore": _payload(
|
|
344
|
+
"context.explore",
|
|
345
|
+
{
|
|
346
|
+
"for": {"const": "explore"},
|
|
347
|
+
"charter": SECTIONS,
|
|
348
|
+
"rules": STRING,
|
|
349
|
+
"specs": _array(
|
|
350
|
+
_object(
|
|
351
|
+
{"name": STRING, "title": STRING, "purpose": STRING, "requirements": NAMES}
|
|
352
|
+
)
|
|
353
|
+
),
|
|
354
|
+
"changes": _array(
|
|
355
|
+
_object({"name": STRING, "title": STRING, "phase": _enum(Phase)})
|
|
356
|
+
),
|
|
357
|
+
"milestones": _array(
|
|
358
|
+
_object({"name": STRING, "archived": INTEGER, "total": INTEGER})
|
|
359
|
+
),
|
|
360
|
+
"explorations": _array(
|
|
361
|
+
_object(
|
|
362
|
+
{
|
|
363
|
+
"name": STRING,
|
|
364
|
+
"title": STRING,
|
|
365
|
+
"status": _enum(ExplorationStatus),
|
|
366
|
+
"detail": TEXT_OR_NULL,
|
|
367
|
+
}
|
|
368
|
+
)
|
|
369
|
+
),
|
|
370
|
+
"references": _array(
|
|
371
|
+
_object(
|
|
372
|
+
{
|
|
373
|
+
"atlas": STRING,
|
|
374
|
+
"found": BOOLEAN,
|
|
375
|
+
"specs": _array(
|
|
376
|
+
_object({"name": STRING, "title": STRING, "purpose": STRING})
|
|
377
|
+
),
|
|
378
|
+
}
|
|
379
|
+
)
|
|
380
|
+
),
|
|
381
|
+
},
|
|
382
|
+
),
|
|
383
|
+
"context.implement": _delegated("implement"),
|
|
384
|
+
"context.review": _delegated("review"),
|
|
385
|
+
"pr": _payload(
|
|
386
|
+
"pr",
|
|
387
|
+
{
|
|
388
|
+
"change": STRING,
|
|
389
|
+
"repo": TEXT_OR_NULL,
|
|
390
|
+
"branch": STRING,
|
|
391
|
+
"title": STRING,
|
|
392
|
+
"body": STRING,
|
|
393
|
+
"links": _array(_object({"name": STRING, "url": STRING})),
|
|
394
|
+
},
|
|
395
|
+
),
|
|
396
|
+
"doctor": _payload(
|
|
397
|
+
"doctor",
|
|
398
|
+
{
|
|
399
|
+
"scope": {"enum": ["project", "atlas"]},
|
|
400
|
+
"ok": BOOLEAN,
|
|
401
|
+
"checks": _array(
|
|
402
|
+
_object(
|
|
403
|
+
{
|
|
404
|
+
"name": STRING,
|
|
405
|
+
"status": {"enum": ["ok", "warning", "error"]},
|
|
406
|
+
"message": STRING,
|
|
407
|
+
}
|
|
408
|
+
)
|
|
409
|
+
),
|
|
410
|
+
},
|
|
411
|
+
),
|
|
412
|
+
}
|
|
413
|
+
)
|
waymark/views/style.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"""A small styling helper: color, symbols, progress bars and section rules.
|
|
2
|
+
|
|
3
|
+
Output is plain when it isn't going to a terminal or when ``NO_COLOR`` is set,
|
|
4
|
+
and falls back to ASCII symbols when the console can't show the glyphs, as on a
|
|
5
|
+
legacy Windows console.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import sys
|
|
9
|
+
from collections.abc import Mapping
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
from typing import TextIO
|
|
12
|
+
|
|
13
|
+
RESET = "\x1b[0m"
|
|
14
|
+
BOLD = "1"
|
|
15
|
+
DIM = "2"
|
|
16
|
+
RED = "31"
|
|
17
|
+
GREEN = "32"
|
|
18
|
+
YELLOW = "33"
|
|
19
|
+
CYAN = "36"
|
|
20
|
+
|
|
21
|
+
BAR_WIDTH = 10
|
|
22
|
+
RULE_WIDTH = 72
|
|
23
|
+
|
|
24
|
+
_GLYPHS = {
|
|
25
|
+
"done": ("✓", "x"),
|
|
26
|
+
"todo": ("○", "o"),
|
|
27
|
+
"arrow": ("→", "->"),
|
|
28
|
+
"human": ("◆", "*"),
|
|
29
|
+
"error": ("✗", "X"),
|
|
30
|
+
"warning": ("!", "!"),
|
|
31
|
+
"bullet": ("•", "-"),
|
|
32
|
+
"full": ("█", "#"),
|
|
33
|
+
"empty": ("░", "."),
|
|
34
|
+
"rule": ("─", "-"),
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _can_encode(stream: TextIO) -> bool:
|
|
39
|
+
"""Whether the stream's encoding can show every glyph the views use."""
|
|
40
|
+
encoding = getattr(stream, "encoding", None) or "ascii"
|
|
41
|
+
try:
|
|
42
|
+
"".join(glyph for glyph, _ in _GLYPHS.values()).encode(encoding)
|
|
43
|
+
except (UnicodeEncodeError, LookupError):
|
|
44
|
+
return False
|
|
45
|
+
return True
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _enable_windows_colors() -> bool:
|
|
49
|
+
"""Turn on escape-sequence handling in a Windows console; False when it can't be."""
|
|
50
|
+
enabled = True
|
|
51
|
+
if sys.platform == "win32":
|
|
52
|
+
import ctypes # noqa: PLC0415 - only needed, and only importable, on Windows
|
|
53
|
+
|
|
54
|
+
kernel = ctypes.windll.kernel32
|
|
55
|
+
handle = kernel.GetStdHandle(-11)
|
|
56
|
+
mode = ctypes.c_uint32()
|
|
57
|
+
enabled = bool(kernel.GetConsoleMode(handle, ctypes.byref(mode))) and bool(
|
|
58
|
+
kernel.SetConsoleMode(handle, mode.value | 0x0004)
|
|
59
|
+
)
|
|
60
|
+
return enabled
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@dataclass(frozen=True)
|
|
64
|
+
class Style:
|
|
65
|
+
"""How to draw: with or without color, with glyphs or ASCII."""
|
|
66
|
+
|
|
67
|
+
color: bool = False
|
|
68
|
+
unicode: bool = False
|
|
69
|
+
|
|
70
|
+
@classmethod
|
|
71
|
+
def detect(cls, stream: TextIO, environment: Mapping[str, str]) -> "Style":
|
|
72
|
+
"""Choose a style for a stream from what it is and what the environment asks for."""
|
|
73
|
+
terminal = bool(getattr(stream, "isatty", lambda: False)())
|
|
74
|
+
wants_color = (
|
|
75
|
+
terminal and "NO_COLOR" not in environment and environment.get("TERM") != "dumb"
|
|
76
|
+
)
|
|
77
|
+
return cls(color=wants_color and _enable_windows_colors(), unicode=_can_encode(stream))
|
|
78
|
+
|
|
79
|
+
def paint(self, text: str, *codes: str) -> str:
|
|
80
|
+
"""Text in the given colors, or unchanged when color is off."""
|
|
81
|
+
if not self.color or not codes or not text:
|
|
82
|
+
return text
|
|
83
|
+
return f"\x1b[{';'.join(codes)}m{text}{RESET}"
|
|
84
|
+
|
|
85
|
+
def symbol(self, name: str) -> str:
|
|
86
|
+
"""A named symbol, as a glyph or as its ASCII stand-in."""
|
|
87
|
+
glyph, plain = _GLYPHS[name]
|
|
88
|
+
return glyph if self.unicode else plain
|
|
89
|
+
|
|
90
|
+
def bar(self, done: int, total: int, width: int = BAR_WIDTH) -> str:
|
|
91
|
+
"""A progress bar for ``done`` out of ``total``."""
|
|
92
|
+
filled = 0 if total <= 0 else min(width, done * width // total)
|
|
93
|
+
if total > 0 and done >= total:
|
|
94
|
+
filled = width
|
|
95
|
+
bar = self.symbol("full") * filled + self.symbol("empty") * (width - filled)
|
|
96
|
+
return bar if self.unicode else f"[{bar}]"
|
|
97
|
+
|
|
98
|
+
def rule(self, title: str, width: int = RULE_WIDTH) -> str:
|
|
99
|
+
"""A section rule with a title."""
|
|
100
|
+
line = self.symbol("rule")
|
|
101
|
+
head = f"{line * 2} {title} "
|
|
102
|
+
return self.paint(head + line * max(2, width - len(head)), BOLD)
|