@krysoworks/kryso 0.0.0-stage → 2.0.0-rc.1

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.
Files changed (162) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +152 -2
  3. package/bin/kryso +31 -0
  4. package/bin/ship-it-status +14 -0
  5. package/bin/ship-ledger +14 -0
  6. package/bin/ship-loop +14 -0
  7. package/bin/ship-pass +14 -0
  8. package/bin/warden +14 -0
  9. package/dist/answer.d.ts +38 -0
  10. package/dist/answer.js +48 -0
  11. package/dist/bytes.d.ts +5 -0
  12. package/dist/bytes.js +35 -0
  13. package/dist/cleanup.d.ts +1 -0
  14. package/dist/cleanup.js +629 -0
  15. package/dist/commands.d.ts +48 -0
  16. package/dist/commands.js +293 -0
  17. package/dist/completions.d.ts +24 -0
  18. package/dist/completions.js +239 -0
  19. package/dist/daemon.d.ts +15 -0
  20. package/dist/daemon.js +23 -0
  21. package/dist/diagnose.d.ts +47 -0
  22. package/dist/diagnose.js +314 -0
  23. package/dist/doctor.d.ts +1 -0
  24. package/dist/doctor.js +241 -0
  25. package/dist/env.d.ts +9 -0
  26. package/dist/env.js +17 -0
  27. package/dist/evidence.d.ts +39 -0
  28. package/dist/evidence.js +327 -0
  29. package/dist/home.d.ts +3 -0
  30. package/dist/home.js +27 -0
  31. package/dist/install.d.ts +59 -0
  32. package/dist/install.js +137 -0
  33. package/dist/isolate.d.ts +28 -0
  34. package/dist/isolate.js +402 -0
  35. package/dist/leftovers.d.ts +5 -0
  36. package/dist/leftovers.js +156 -0
  37. package/dist/machine.d.ts +6 -0
  38. package/dist/machine.js +185 -0
  39. package/dist/palette.d.ts +147 -0
  40. package/dist/palette.js +167 -0
  41. package/dist/plan.d.ts +1 -0
  42. package/dist/plan.js +133 -0
  43. package/dist/proc.d.ts +23 -0
  44. package/dist/proc.js +23 -0
  45. package/dist/read.d.ts +1 -0
  46. package/dist/read.js +297 -0
  47. package/dist/readiness.d.ts +5 -0
  48. package/dist/readiness.js +64 -0
  49. package/dist/remote.d.ts +6 -0
  50. package/dist/remote.js +85 -0
  51. package/dist/self-update.d.ts +1 -0
  52. package/dist/self-update.js +224 -0
  53. package/dist/semver.d.ts +7 -0
  54. package/dist/semver.js +42 -0
  55. package/dist/style.d.ts +71 -0
  56. package/dist/style.js +187 -0
  57. package/dist/update.d.ts +47 -0
  58. package/dist/update.js +190 -0
  59. package/dist/work.d.ts +25 -0
  60. package/dist/work.js +449 -0
  61. package/dist/workflows.d.ts +4 -0
  62. package/dist/workflows.js +73 -0
  63. package/docker/entrypoint.sh +44 -0
  64. package/docker/runner.Dockerfile +39 -0
  65. package/package.json +65 -4
  66. package/scripts/install-skills.sh +607 -0
  67. package/skills/code-review/SKILL.md +231 -0
  68. package/skills/kryso/SKILL.md +107 -0
  69. package/skills/kryso/golden/help.rounded.ansi +23 -0
  70. package/skills/kryso/golden/help.square.ansi +23 -0
  71. package/skills/kryso/golden/help.txt +23 -0
  72. package/skills/kryso/golden/table-narrow.json +11 -0
  73. package/skills/kryso/golden/table-narrow.rounded.ansi +4 -0
  74. package/skills/kryso/golden/table-narrow.square.ansi +4 -0
  75. package/skills/kryso/golden/table.ansi +5 -0
  76. package/skills/kryso/golden/table.json +11 -0
  77. package/skills/kryso/ledger.sh +2146 -0
  78. package/skills/kryso/palette.json +57 -0
  79. package/skills/kryso/reference/resuming.md +30 -0
  80. package/skills/kryso/reference/routing.md +23 -0
  81. package/skills/kryso/reference/stage-1-claim.md +79 -0
  82. package/skills/kryso/reference/stage-2-build.md +133 -0
  83. package/skills/kryso/reference/stage-3-prove.md +51 -0
  84. package/skills/kryso/reference/stage-4-pr.md +76 -0
  85. package/skills/kryso/reference/stage-5-reconcile.md +81 -0
  86. package/skills/kryso/reference/stage-6-merge-or-park.md +73 -0
  87. package/skills/kryso/reference/stage-7-close-out.md +62 -0
  88. package/skills/kryso/reference/terminal.md +7 -0
  89. package/skills/kryso/ship-it-status.sh +1503 -0
  90. package/skills/kryso/ui.py +274 -0
  91. package/skills/kryso/ui.sh +609 -0
  92. package/skills/kryso-build/SKILL.md +168 -0
  93. package/skills/kryso-shift/SKILL.md +167 -0
  94. package/skills/kryso-shift/archive.py +2784 -0
  95. package/skills/kryso-shift/archive_checker.py +462 -0
  96. package/skills/kryso-shift/archive_checker_lambda.py +155 -0
  97. package/skills/kryso-shift/archive_health.py +364 -0
  98. package/skills/kryso-shift/archive_launchd.py +140 -0
  99. package/skills/kryso-shift/archive_remote.py +990 -0
  100. package/skills/kryso-shift/archiving-checker.md +140 -0
  101. package/skills/kryso-shift/archiving-keep.md +115 -0
  102. package/skills/kryso-shift/archiving-recovery.md +112 -0
  103. package/skills/kryso-shift/archiving-remote.md +166 -0
  104. package/skills/kryso-shift/archiving.md +146 -0
  105. package/skills/kryso-shift/coverage.schema.json +225 -0
  106. package/skills/kryso-shift/deadline.sh +254 -0
  107. package/skills/kryso-shift/feedback.schema.json +35 -0
  108. package/skills/kryso-shift/feedback.sh +228 -0
  109. package/skills/kryso-shift/history-manifest.schema.json +331 -0
  110. package/skills/kryso-shift/keep-after.sh +55 -0
  111. package/skills/kryso-shift/measure.sh +611 -0
  112. package/skills/kryso-shift/merge-ready.sh +207 -0
  113. package/skills/kryso-shift/outcome.schema.json +16 -0
  114. package/skills/kryso-shift/pass-rules.md +8 -0
  115. package/skills/kryso-shift/profile.schema.json +42 -0
  116. package/skills/kryso-shift/reader.sh +692 -0
  117. package/skills/kryso-shift/reads.sh +151 -0
  118. package/skills/kryso-shift/reference/allowance.md +21 -0
  119. package/skills/kryso-shift/reference/driver.md +95 -0
  120. package/skills/kryso-shift/reference/measuring.md +67 -0
  121. package/skills/kryso-shift/reference/outputs.md +21 -0
  122. package/skills/kryso-shift/reference/pass-script.md +115 -0
  123. package/skills/kryso-shift/reference/reconcile-and-merge.md +55 -0
  124. package/skills/kryso-shift/reference/second-reader.md +9 -0
  125. package/skills/kryso-shift/reference/sweeps.md +83 -0
  126. package/skills/kryso-shift/repair.schema.json +14 -0
  127. package/skills/kryso-shift/report-template.html +679 -0
  128. package/skills/kryso-shift/report.py +389 -0
  129. package/skills/kryso-shift/report_fixture.py +65 -0
  130. package/skills/kryso-shift/reporting.md +58 -0
  131. package/skills/kryso-shift/result.sh +91 -0
  132. package/skills/kryso-shift/runner.sh +1178 -0
  133. package/skills/kryso-shift/ship-loop.sh +3977 -0
  134. package/skills/kryso-shift/ship-pass.sh +4987 -0
  135. package/skills/kryso-shift/steps.jq +19 -0
  136. package/skills/kryso-shift/workflow.mjs +830 -0
  137. package/skills/kryso-shift/workflows/default.json +22 -0
  138. package/skills/kryso-shift/workflows/ralph.json +13 -0
  139. package/skills/kryso-watch/SKILL.md +100 -0
  140. package/skills/kryso-watch/answer.schema.json +23 -0
  141. package/skills/kryso-watch/base-refs.sh +44 -0
  142. package/skills/kryso-watch/board.sh +58 -0
  143. package/skills/kryso-watch/reference/config.md +23 -0
  144. package/skills/kryso-watch/reference/outputs.md +30 -0
  145. package/skills/kryso-watch/reference/report.md +29 -0
  146. package/skills/kryso-watch/reference/scheduler.md +32 -0
  147. package/skills/kryso-watch/reference/settlement.md +29 -0
  148. package/skills/kryso-watch/reference/shift.md +44 -0
  149. package/skills/kryso-watch/reference/shim-limits.md +22 -0
  150. package/skills/kryso-watch/reference/triage.md +26 -0
  151. package/skills/kryso-watch/schema.jq +40 -0
  152. package/skills/kryso-watch/settle.schema.json +36 -0
  153. package/skills/kryso-watch/stage-common.sh +187 -0
  154. package/skills/kryso-watch/stages/report.sh +525 -0
  155. package/skills/kryso-watch/stages/settle.sh +722 -0
  156. package/skills/kryso-watch/stages/shift.sh +466 -0
  157. package/skills/kryso-watch/stages/triage.sh +379 -0
  158. package/skills/kryso-watch/tracker.sh +27 -0
  159. package/skills/kryso-watch/triage.schema.json +77 -0
  160. package/skills/kryso-watch/warden.launchd.plist +50 -0
  161. package/skills/kryso-watch/warden.schema.json +98 -0
  162. package/skills/kryso-watch/warden.sh +1004 -0
@@ -0,0 +1,609 @@
1
+ #!/usr/bin/env bash
2
+ # ui.sh: styled output for the kryso drivers, the layer ADRs 0006 and 0042 put over their plain
3
+ # output. Sourced by ship-loop.sh, ship-pass.sh, ship-it-status.sh, warden.sh, ledger.sh,
4
+ # measure.sh and install-skills.sh. A script finds it the way it finds ledger.sh, in one order:
5
+ # the kryso directory beside the script's own, then .agents/skills/kryso under the repository
6
+ # and the home, then the .claude/skills compatibility path under both. ship-it-status.sh and
7
+ # ledger.sh sit in that directory, and install-skills.sh takes the checkout's copy, then the
8
+ # installed one. It defines functions and nothing else: no output, no variable and no option
9
+ # change on source.
10
+ #
11
+ # A script calls ui_stream once with the descriptor its plain lines use, 1 or 2. Every function
12
+ # after that but ui_table_row, which only writes ui_table's input, is a no-op unless the run is
13
+ # attended: NO_COLOR unset, and CLICOLOR_FORCE set or the stream a terminal. Everything draws with escapes from the palette, whose one source is
14
+ # palette.json beside this file, and needs no gum (ADR 0042), except the spinner and the pager,
15
+ # which make one gum call each with both of gum's streams sent to the script's descriptor, and do
16
+ # nothing without gum on PATH. The plain path never calls gum.
17
+ #
18
+ # ui_stream <fd> set the stream; prints the install hint when a terminal
19
+ # has no gum, once per run
20
+ # ui_attended status 0 on an attended run
21
+ # ui_gum status 0 on an attended run with gum on PATH
22
+ #
23
+ # Drawn from the palette. A pill is rounded with Nerd Font half circles, or verbump's square
24
+ # pill under KRYSO_PILLS=square, which also swaps the Nerd Font marks for plain ones. A colour is
25
+ # 24-bit when COLORTERM is truecolor or 24bit, else the nearest xterm 256-colour code.
26
+ #
27
+ # ui_brand <subcommand> [summary] the kryso pill, the subcommand in bold, the summary dim and
28
+ # ending at the last of COLUMNS (80 when unset), or on the
29
+ # next line when it does not fit
30
+ # ui_pill <kind> <label> a pill on its own line: primary, secondary, warn, error,
31
+ # prompt or sub
32
+ # ui_section <kind> <label> [note] a blank line, then the pill, the note dim after it
33
+ # ui_ok, ui_warn, ui_fail, ui_info, ui_arrow <message>
34
+ # a line that starts with its mark in its colour
35
+ # ui_event <stage> <outcome> [evidence]
36
+ # a ledger event as a mark line, the evidence dim, without
37
+ # its usage JSON and cut to the terminal's width
38
+ # ui_val <text> the text in the value colour, for a message above; plain
39
+ # when the run is not attended
40
+ # ui_tint <tone> <text> the text in a tone: value, dim, bold, ok, warn or fail;
41
+ # plain when the run is not attended
42
+ # ui_error <message> [hint] an ERROR pill and the message in bold, then the hint as
43
+ # ui_hint draws it, on stderr
44
+ # ui_hint <hint> a HINT pill and the hint dim, on stderr
45
+ # ui_help the plain help on stdin drawn in sections on stdout, or
46
+ # passed through when stdout is not attended
47
+ # ui_table [--indent N] [--header] [--width N] [--flex N]
48
+ # rows of tab-separated cells on stdin, in aligned columns,
49
+ # the flexible one cut to the width
50
+ # ui_table_row <cell>... one row for ui_table, on stdout: the cells joined by tabs,
51
+ # so a caller passes cells and never writes the row
52
+ # ui_err_attended status 0 when stderr is attended
53
+ # ui_glyph <name> one of the palette's glyphs for the font
54
+ # ui_outcome <word> the tone an outcome word is drawn in
55
+ #
56
+ # Drawn by gum.
57
+ #
58
+ # ui_spin <title> <command> [arg]...
59
+ # the command behind a spinner, its status passed back;
60
+ # without gum or unattended, the command does not run
61
+ # ui_pager gum's pager over stdin; otherwise stdin passes through
62
+ #
63
+ # The attended check reads NO_COLOR and CLICOLOR_FORCE and never FORCE_COLOR, in gum's order:
64
+ # NO_COLOR wins over CLICOLOR_FORCE (ADR 0042). An empty NO_COLOR counts as unset, as
65
+ # no-color.org has it.
66
+
67
+ # ui_stream <fd>: the descriptor every styled call goes to. UI_TTY keeps whether it was a
68
+ # terminal, for the attended check inside a command substitution. With no gum on PATH,
69
+ # NO_COLOR unset and the stream a terminal, or forced to count as one, one line on stderr names
70
+ # the two things gum adds. The guard is exported so a child script run under this one does not repeat it.
71
+ ui_stream() {
72
+ UI_FD="${1:-1}"
73
+ UI_TTY=0
74
+ [[ -t "$UI_FD" ]] && UI_TTY=1
75
+ command -v gum > /dev/null 2>&1 && return 0
76
+ [[ -z "${NO_COLOR:-}" && -z "${UI_GUM_HINTED:-}" ]] || return 0
77
+ [[ -t "$UI_FD" ]] || _ui_forced || return 0
78
+ export UI_GUM_HINTED=1
79
+ echo "the spinner and the pager need gum 0.12 or newer (brew install gum)" >&2
80
+ }
81
+
82
+ # ui_attended: never under NO_COLOR, then always under CLICOLOR_FORCE set to something other
83
+ # than 0, then only when the stream is a terminal, or was one when ui_stream named it: inside
84
+ # $(...) stdout is a pipe, and ui_val runs there. Whether gum is on PATH does not count.
85
+ ui_attended() {
86
+ [[ -z "${NO_COLOR:-}" ]] || return 1
87
+ _ui_forced || [[ "${UI_TTY:-0}" == 1 || -t "${UI_FD:-1}" ]]
88
+ }
89
+
90
+ # ui_err_attended: the same check for stderr, whatever stream ui_stream named. Every refusal and
91
+ # warning a script writes on stderr asks it before it draws, and so do ui_error and ui_hint, so
92
+ # a stderr that is a file always gets the plain line.
93
+ ui_err_attended() {
94
+ local UI_FD=2 UI_TTY=0
95
+ ui_attended
96
+ }
97
+
98
+ # ui_glyph <name>: one of the palette's glyphs, the Nerd Font form unless KRYSO_PILLS=square.
99
+ ui_glyph() {
100
+ local glyph font=nerd
101
+ [[ "${KRYSO_PILLS:-}" == square ]] && font=plain
102
+ _ui_palette glyph "glyph.$font.$1" || { echo "ui_glyph: no glyph '$1'" >&2; return 2; }
103
+ printf '%s' "$glyph"
104
+ }
105
+
106
+ # ui_outcome <word>: the tone an outcome word is drawn in, ok, warn or fail by the palette's
107
+ # outcomes, and info for a word it does not name. ui_event, the shift's pass lines and table and
108
+ # the pass's close all read it, so one word has one look.
109
+ ui_outcome() {
110
+ local tone
111
+ _ui_palette tone "outcome.$1" || tone=info
112
+ printf '%s' "$tone"
113
+ }
114
+
115
+ # ui_gum: an attended run with gum on PATH, the check before a call that draws through gum.
116
+ ui_gum() {
117
+ command -v gum > /dev/null 2>&1 && ui_attended
118
+ }
119
+
120
+ ui_brand() {
121
+ ui_attended || return 0
122
+ local sub="$1" summary="${2:-}" cols="${COLUMNS:-}" pill line spec dim left room gap
123
+ [[ "$cols" =~ ^[1-9][0-9]*$ ]] || cols=80
124
+ _ui_pill pill role.brand kryso
125
+ printf -v line '%s \e[1m%s\e[0m' "$pill" "$sub"
126
+ if [[ -z "$summary" ]]; then
127
+ printf '%s\n' "$line" >&"${UI_FD:-1}"
128
+ return 0
129
+ fi
130
+ _ui_palette spec role.dim
131
+ _ui_sgr dim fg "$spec"
132
+ # The pill is its label and two cells, a space or an end each side.
133
+ _ui_width left "kryso $sub"
134
+ left=$(( left + 2 ))
135
+ _ui_width room "$summary"
136
+ gap=$(( cols - left - room ))
137
+ if (( gap >= 2 )); then
138
+ printf '%s%*s\e[%sm%s\e[0m\n' "$line" "$gap" "" "$dim" "$summary" >&"${UI_FD:-1}"
139
+ else
140
+ gap=$(( cols > room ? cols - room : 0 ))
141
+ printf '%s\n%*s\e[%sm%s\e[0m\n' "$line" "$gap" "" "$dim" "$summary" >&"${UI_FD:-1}"
142
+ fi
143
+ }
144
+
145
+ ui_pill() {
146
+ ui_attended || return 0
147
+ local pill
148
+ if ! _ui_pill pill "pill.$1" "$2"; then
149
+ echo "ui_pill: no pill kind '$1'" >&2
150
+ return 2
151
+ fi
152
+ printf '%s\n' "$pill" >&"${UI_FD:-1}"
153
+ }
154
+
155
+ ui_ok() { _ui_line ok "$*"; }
156
+ ui_warn() { _ui_line warn "$*"; }
157
+ ui_fail() { _ui_line fail "$*"; }
158
+ ui_info() { _ui_line info "$*"; }
159
+
160
+ # ui_val ends its colour with the default foreground, not a reset, so the bold of a ui_error
161
+ # message carries on past the value.
162
+ ui_val() {
163
+ if ui_attended; then
164
+ local spec colour
165
+ _ui_palette spec role.value
166
+ _ui_sgr colour fg "$spec"
167
+ printf '\e[%sm%s\e[39m' "$colour" "$*"
168
+ else
169
+ printf '%s' "$*"
170
+ fi
171
+ }
172
+
173
+ # verbump's fail without the exit: a blank line, the pill, the message flush under it, then
174
+ # ui_hint's blank line before the hint, so the hint stands apart from the error.
175
+ ui_error() {
176
+ ui_err_attended || return 0
177
+ local UI_FD=2 UI_TTY=0
178
+ local pill
179
+ _ui_pill pill pill.error ERROR
180
+ printf '\n%s\n\e[1m%s\e[0m\n' "$pill" "$1" >&2
181
+ [[ -z "${2:-}" ]] || ui_hint "$2"
182
+ }
183
+
184
+ # ui_tint <tone> <text>: the text in a tone, for a message above, or the text alone when the run
185
+ # is not attended. A colour ends with the default foreground and bold with normal intensity, so a
186
+ # tint sits inside other styling, as ui_val does.
187
+ ui_tint() {
188
+ if ! ui_attended; then printf '%s' "$2"; return 0; fi
189
+ local spec colour
190
+ case "$1" in
191
+ bold) printf '\e[1m%s\e[22m' "$2"; return 0 ;;
192
+ value|dim) _ui_palette spec "role.$1" ;;
193
+ ok|warn|fail) _ui_palette spec "mark.plain.$1"; spec="${spec#* }" ;;
194
+ *) echo "ui_tint: no tone '$1'" >&2; return 2 ;;
195
+ esac
196
+ _ui_sgr colour fg "$spec"
197
+ printf '\e[%sm%s\e[39m' "$colour" "$2"
198
+ }
199
+
200
+ ui_arrow() { _ui_line arrow "$*"; }
201
+
202
+ # ui_section <kind> <label> [note]: a blank line, then the pill, with the note dim after it on
203
+ # the same line, so each section stands apart from the one before.
204
+ ui_section() {
205
+ ui_attended || return 0
206
+ local pill spec dim note=""
207
+ if ! _ui_pill pill "pill.$1" "$2"; then
208
+ echo "ui_section: no pill kind '$1'" >&2
209
+ return 2
210
+ fi
211
+ if [[ -n "${3:-}" ]]; then
212
+ _ui_palette spec role.dim
213
+ _ui_sgr dim fg "$spec"
214
+ printf -v note ' \e[%sm%s\e[0m' "$dim" "$3"
215
+ fi
216
+ printf '\n%s%s\n' "$pill" "$note" >&"${UI_FD:-1}"
217
+ }
218
+
219
+ # ui_event <stage> <outcome> [evidence]: one ledger event as a mark line, the mark from the
220
+ # outcome word by ui_outcome. The stage, when given, comes first, and the evidence follows dim
221
+ # (#117). The drawn line leaves out a usage= field, the host's raw usage JSON, and cuts the
222
+ # evidence to the terminal's width with the palette's ellipsis (#456). The ledger keeps both.
223
+ ui_event() {
224
+ ui_attended || return 0
225
+ local stage="$1" outcome="$2" evidence="${3:-}" line cols used room
226
+ line="${stage:+$stage }$(ui_tint bold "$outcome")"
227
+ _ui_drop_usage evidence "$evidence"
228
+ if [[ -n "$evidence" ]]; then
229
+ _ui_cols cols
230
+ # The mark and its space, the stage and its space, the outcome and the two-space gap.
231
+ _ui_width used "${stage:+$stage }$outcome"
232
+ room=$(( cols - used - 4 ))
233
+ _ui_width used "$evidence"
234
+ if (( used > room && room >= 2 )); then
235
+ _ui_cut evidence "$evidence" $(( room - 1 ))
236
+ evidence+="$(ui_glyph ellipsis)"
237
+ fi
238
+ (( room < 2 )) || line+=" $(ui_tint dim "$evidence")"
239
+ fi
240
+ "ui_$(ui_outcome "$outcome")" "$line"
241
+ }
242
+
243
+ # _ui_drop_usage <var> <evidence>: the evidence without its usage= field, the compact JSON from
244
+ # usage={ to the brace that closes it, or usage=null, with the space before it. A brace inside
245
+ # a JSON string, escapes and all, does not count.
246
+ _ui_drop_usage() {
247
+ local _ui_text="$2" _ui_head _ui_rest _ui_depth=0 _ui_i _ui_c _ui_str=0
248
+ if [[ "$_ui_text" == *" usage=null"* ]]; then
249
+ _ui_text="${_ui_text/ usage=null/}"
250
+ elif [[ "$_ui_text" == *" usage={"* ]]; then
251
+ _ui_head="${_ui_text%% usage=\{*}"
252
+ _ui_rest="${_ui_text#* usage=}"
253
+ for (( _ui_i = 0; _ui_i < ${#_ui_rest}; _ui_i++ )); do
254
+ _ui_c="${_ui_rest:_ui_i:1}"
255
+ if (( _ui_str )); then
256
+ if [[ "$_ui_c" == '\' ]]; then _ui_i=$(( _ui_i + 1 ))
257
+ elif [[ "$_ui_c" == '"' ]]; then _ui_str=0; fi
258
+ continue
259
+ fi
260
+ [[ "$_ui_c" == '"' ]] && { _ui_str=1; continue; }
261
+ [[ "$_ui_c" == "{" ]] && _ui_depth=$(( _ui_depth + 1 ))
262
+ if [[ "$_ui_c" == "}" ]]; then
263
+ _ui_depth=$(( _ui_depth - 1 ))
264
+ (( _ui_depth > 0 )) || break
265
+ fi
266
+ done
267
+ _ui_text="$_ui_head${_ui_rest:_ui_i+1}"
268
+ fi
269
+ printf -v "$1" %s "$_ui_text"
270
+ }
271
+
272
+ # _ui_cols <var>: the terminal's width: COLUMNS when it is a number, else the terminal's own
273
+ # when there is one, else 80.
274
+ _ui_cols() {
275
+ local _ui_n="${COLUMNS:-}" _ui_size
276
+ if ! [[ "$_ui_n" =~ ^[1-9][0-9]*$ ]]; then
277
+ _ui_size="$(stty size < /dev/tty 2>/dev/null)" || _ui_size=""
278
+ _ui_n="${_ui_size#* }"
279
+ [[ "$_ui_n" =~ ^[1-9][0-9]*$ ]] || _ui_n=80
280
+ fi
281
+ printf -v "$1" %s "$_ui_n"
282
+ }
283
+
284
+ # ui_hint <hint>: a blank line, a HINT pill and the hint dim, on stderr: ui_error's second half,
285
+ # for an error whose hint follows lines of its own.
286
+ ui_hint() {
287
+ ui_err_attended || return 0
288
+ local UI_FD=2 UI_TTY=0
289
+ local pill spec dim
290
+ _ui_pill pill pill.sub HINT
291
+ _ui_palette spec role.dim
292
+ _ui_sgr dim fg "$spec"
293
+ printf '\n%s\n\e[%sm%s\e[0m\n' "$pill" "$dim" "$1" >&2
294
+ }
295
+
296
+ # ui_help: a plain help text on stdin, drawn on stdout by the contract ADR 0042's amendment
297
+ # records (#345), or passed through as it is when stdout is not attended. The golden files in
298
+ # golden/ pin its bytes, the same ones style.ts's help draws.
299
+ ui_help() {
300
+ local UI_FD=1 UI_TTY=0
301
+ [[ -t 1 ]] && UI_TTY=1
302
+ if ! ui_attended; then cat; return 0; fi
303
+ local raw usage=0 text=0 last=1
304
+ while :; do
305
+ IFS= read -r raw || { [[ -n "$raw" ]] || break; last=0; }
306
+ _ui_help_line "$raw"
307
+ if (( last )); then printf '%s\n' "$_UI_LINE"; else printf '%s' "$_UI_LINE"; break; fi
308
+ done
309
+ unset _UI_LINE
310
+ }
311
+
312
+ # _ui_help_line <raw>: one line of a help drawn into _UI_LINE, with the usage and text state in
313
+ # ui_help's locals.
314
+ _ui_help_line() {
315
+ local raw="$1" indent lead term gap rest upper n
316
+ local re_usage='^Usage: (.*)$' re_more='^( +)(kryso .*)$' \
317
+ re_entry='^( +)([^[:space:]]+( [^[:space:]]+)*)( +)([^[:space:]].*)$' re_note='^(.* )(\([^()]*\))$'
318
+ if [[ "$raw" =~ $re_usage ]]; then
319
+ usage=1; text=0
320
+ _UI_LINE="$(ui_tint dim Usage:) $(ui_tint bold "${BASH_REMATCH[1]}")"
321
+ return 0
322
+ fi
323
+ if (( usage )) && [[ "$raw" =~ $re_more ]]; then
324
+ _UI_LINE="${BASH_REMATCH[1]}$(ui_tint bold "${BASH_REMATCH[2]}")"
325
+ return 0
326
+ fi
327
+ usage=0
328
+ if _ui_help_section "$raw"; then
329
+ text=0
330
+ upper="$(printf '%s' "${raw%:}" | LC_ALL=C tr '[:lower:]' '[:upper:]')"
331
+ _ui_pill _UI_LINE pill.sub "$upper"
332
+ return 0
333
+ fi
334
+ lead="${raw%%[! ]*}"
335
+ indent="${#lead}"
336
+ if (( indent > 0 )) && ! (( text > 0 && indent >= text )) \
337
+ && [[ "$raw" =~ $re_entry ]]; then
338
+ lead="${BASH_REMATCH[1]}"; term="${BASH_REMATCH[2]}"; gap="${BASH_REMATCH[4]}"; rest="${BASH_REMATCH[5]}"
339
+ _ui_width n "$lead$term$gap"
340
+ text="$n"
341
+ if [[ "$rest" =~ $re_note ]]; then
342
+ rest="${BASH_REMATCH[1]}$(ui_tint dim "${BASH_REMATCH[2]}")"
343
+ fi
344
+ _UI_LINE="$lead$(ui_tint value "$term")$gap$rest"
345
+ return 0
346
+ fi
347
+ (( indent > 0 )) || text=0
348
+ _UI_LINE="$raw"
349
+ }
350
+
351
+ # _ui_help_section <raw>: status 0 for a section line, one to three words, the first capitalised,
352
+ # and a colon. It runs in the C locale, so the ranges are ASCII.
353
+ _ui_help_section() {
354
+ local LC_ALL=C re='^[A-Z][A-Za-z]*( [A-Za-z]+){0,2}:$'
355
+ [[ "$1" =~ $re ]]
356
+ }
357
+
358
+ # ui_table [--indent N] [--header] [--width N] [--flex N]: rows on stdin, one a line, cells split
359
+ # by tabs, drawn in columns two spaces apart. A cell is its text, or a tone, the unit separator
360
+ # (\x1f) and the text, the tones ui_tint takes. A column is as wide as its widest cell in
361
+ # characters, the header row is bold, and a row ends after its last cell with text. A table wider
362
+ # than --width, COLUMNS or 80, cuts its flexible column, the last unless --flex names another by
363
+ # its number from 0, to the room the others leave, and a cell cut there ends in the palette's
364
+ # ellipsis. The rows are read whether or not they are drawn, so a writer piping into it on an
365
+ # unattended run never meets a closed pipe. The golden files in golden/ pin its bytes, the same
366
+ # ones style.ts's table draws.
367
+ ui_table() {
368
+ local indent=0 header=0 limit="${COLUMNS:-}" flex=""
369
+ [[ "$limit" =~ ^[1-9][0-9]*$ ]] || limit=80
370
+ while [[ $# -gt 0 ]]; do
371
+ case "$1" in
372
+ --indent) indent="$2"; shift 2 ;;
373
+ --header) header=1; shift ;;
374
+ --width) limit="$2"; shift 2 ;;
375
+ --flex) flex="$2"; shift 2 ;;
376
+ *) echo "ui_table: unknown option $1" >&2; cat > /dev/null; return 2 ;;
377
+ esac
378
+ done
379
+ if ! ui_attended; then cat > /dev/null; return 0; fi
380
+ local -a rows=() widths=()
381
+ local row cell text w i r last line pad out="" total room ellipsis
382
+ while IFS= read -r row || [[ -n "$row" ]]; do rows+=("$row"); done
383
+ (( ${#rows[@]} > 0 )) || return 0
384
+ for row in "${rows[@]}"; do
385
+ _ui_cells "$row"
386
+ for i in "${!_UI_CELLS[@]}"; do
387
+ text="${_UI_CELLS[i]#*$'\x1f'}"
388
+ _ui_width w "$text"
389
+ (( w <= ${widths[i]:-0} )) || widths[i]="$w"
390
+ done
391
+ done
392
+ # The table's widest line, and the cut that brings it to the limit.
393
+ [[ -n "$flex" ]] || flex=$(( ${#widths[@]} - 1 ))
394
+ total="$indent"
395
+ for i in "${!widths[@]}"; do total=$(( total + widths[i] + (i > 0 ? 2 : 0) )); done
396
+ room=""
397
+ if (( total > limit && flex >= 0 && flex < ${#widths[@]} )); then
398
+ room=$(( widths[flex] - (total - limit) ))
399
+ (( room >= 1 )) || room=1
400
+ widths[flex]="$room"
401
+ ellipsis="$(ui_glyph ellipsis)"
402
+ fi
403
+ printf -v pad '%*s' "$indent" ""
404
+ for r in "${!rows[@]}"; do
405
+ _ui_cells "${rows[r]}"
406
+ last=$(( ${#_UI_CELLS[@]} - 1 ))
407
+ while (( last > 0 )) && [[ -z "${_UI_CELLS[last]#*$'\x1f'}" ]]; do last=$(( last - 1 )); done
408
+ line="$pad"
409
+ for (( i = 0; i <= last; i++ )); do
410
+ cell="${_UI_CELLS[i]}"; text="${cell#*$'\x1f'}"
411
+ if [[ -n "$room" && "$i" -eq "$flex" ]]; then
412
+ _ui_width w "$text"
413
+ (( w <= room )) || { _ui_cut text "$text" $(( room - 1 )); text+="$ellipsis"; }
414
+ fi
415
+ (( i == 0 )) || line+=" "
416
+ if (( header && r == 0 )) && [[ -n "$text" ]]; then line+="$(ui_tint bold "$text")"
417
+ elif [[ "$cell" == *$'\x1f'* && -n "$text" ]]; then line+="$(ui_tint "${cell%%$'\x1f'*}" "$text")"
418
+ else line+="$text"; fi
419
+ if (( i < last )); then
420
+ _ui_width w "$text"
421
+ printf -v cell '%*s' "$(( widths[i] - w ))" ""
422
+ line+="$cell"
423
+ fi
424
+ done
425
+ out+="$line"$'\n'
426
+ done
427
+ unset _UI_CELLS
428
+ printf '%s' "$out" >&"${UI_FD:-1}"
429
+ }
430
+
431
+ # ui_table_row <cell>...: one row of ui_table's input. A cell is its text, or a tone, the unit
432
+ # separator and the text. A tab or a newline inside a cell becomes a space, so a cell never
433
+ # splits the row or starts another. It prints on every run, and to stdout rather than the
434
+ # stream: its row is ui_table's input and draws nothing by itself, and it runs inside a pipe or a
435
+ # command substitution, where stdout is never the terminal the attended check asks for.
436
+ ui_table_row() {
437
+ local cell sep="" row=""
438
+ for cell in "$@"; do
439
+ cell="${cell//$'\t'/ }"
440
+ row+="$sep${cell//$'\n'/ }"
441
+ sep=$'\t'
442
+ done
443
+ printf '%s\n' "$row"
444
+ }
445
+
446
+ # _ui_cut <var> <text> <n>: the text's first n characters, whatever the locale: the C locale's
447
+ # bytes are walked, and a UTF-8 continuation byte stays with the character before it. Its locals
448
+ # carry a prefix, so a caller's variable of any plain name can take the result.
449
+ _ui_cut() {
450
+ local LC_ALL=C _ui_text="$2" _ui_n="$3" _ui_i=0 _ui_chars=0
451
+ while (( _ui_i < ${#_ui_text} )); do
452
+ if [[ "${_ui_text:_ui_i:1}" != [$'\x80'-$'\xbf'] ]]; then
453
+ (( _ui_chars < _ui_n )) || break
454
+ _ui_chars=$(( _ui_chars + 1 ))
455
+ fi
456
+ _ui_i=$(( _ui_i + 1 ))
457
+ done
458
+ printf -v "$1" %s "${_ui_text:0:_ui_i}"
459
+ }
460
+
461
+ # _ui_cells <row>: the row split at every tab into _UI_CELLS, an empty cell kept, as read -a
462
+ # would not keep it.
463
+ _ui_cells() {
464
+ local row="$1"
465
+ _UI_CELLS=()
466
+ while [[ "$row" == *$'\t'* ]]; do
467
+ _UI_CELLS+=("${row%%$'\t'*}")
468
+ row="${row#*$'\t'}"
469
+ done
470
+ _UI_CELLS+=("$row")
471
+ }
472
+
473
+ ui_spin() {
474
+ ui_gum || return 0
475
+ local title="$1"; shift
476
+ _ui_gum spin --spinner dot --title "$title" -- "$@"
477
+ }
478
+
479
+ # ui_pager scrolls its stdin in gum's pager, which leaves on q. Unattended, stdin passes through.
480
+ ui_pager() {
481
+ if ! ui_gum; then cat; return 0; fi
482
+ gum pager >&"${UI_FD:-1}"
483
+ }
484
+
485
+ # _ui_pill <var> <palette key> <label>: a pill in the key's colour, with no newline.
486
+ _ui_pill() {
487
+ local spec ink fill edge left right
488
+ _ui_palette spec "$2" || return 2
489
+ _ui_palette ink role.pill-text
490
+ _ui_sgr ink fg "$ink"
491
+ _ui_sgr fill bg "$spec"
492
+ if [[ "${KRYSO_PILLS:-}" == square ]]; then
493
+ printf -v "$1" '\e[1;%s;%sm %s \e[0m' "$ink" "$fill" "$3"
494
+ return 0
495
+ fi
496
+ _ui_sgr edge fg "$spec"
497
+ _ui_palette left end.left
498
+ _ui_palette right end.right
499
+ printf -v "$1" '\e[%sm%s\e[1;%s;%sm%s\e[0;%sm%s\e[0m' "$edge" "$left" "$ink" "$fill" "$3" "$edge" "$right"
500
+ }
501
+
502
+ # _ui_line <mark> <message>: the mark in its colour, a space and the message, on the stream. The
503
+ # mark is the Nerd Font glyph, or the plain one under KRYSO_PILLS=square, which means no Nerd Font.
504
+ _ui_line() {
505
+ ui_attended || return 0
506
+ local spec colour font=nerd
507
+ [[ "${KRYSO_PILLS:-}" == square ]] && font=plain
508
+ _ui_palette spec "mark.$font.$1"
509
+ _ui_sgr colour fg "${spec#* }"
510
+ printf '\e[%sm%s\e[0m %s\n' "$colour" "${spec%% *}" "$2" >&"${UI_FD:-1}"
511
+ }
512
+
513
+ # _ui_sgr <var> <fg|bg> <colour>: the SGR parameters for a palette colour, "R;G;B N".
514
+ _ui_sgr() {
515
+ local layer=38
516
+ [[ "$2" == bg ]] && layer=48
517
+ case "${COLORTERM:-}" in
518
+ truecolor|24bit) printf -v "$1" '%s;2;%s' "$layer" "${3% *}" ;;
519
+ *) printf -v "$1" '%s;5;%s' "$layer" "${3##* }" ;;
520
+ esac
521
+ }
522
+
523
+ # _ui_width <var> <text>: the text's length in characters whatever the locale, by counting its
524
+ # bytes in the C locale less the UTF-8 continuation bytes. A wide character counts as one.
525
+ _ui_width() {
526
+ local LC_ALL=C text="$2"
527
+ text="${text//[$'\x80'-$'\xbf']/}"
528
+ printf -v "$1" %s "${#text}"
529
+ }
530
+
531
+ # >>> palette
532
+ # Written by `pnpm --filter kryso palette` from skills/kryso/palette.json. Edit that file, never this export.
533
+ # _ui_palette <var> <key>: sets <var> to the key's value and returns 0, or returns 1 for no
534
+ # such key. A colour is "R;G;B N", its 24-bit value then its nearest xterm code. A mark is its
535
+ # glyph, a space, then its colour, with a nerd and a plain row. An end is the glyph alone, and
536
+ # so is a glyph, with a nerd and a plain row. An outcome is the tone its word is drawn in.
537
+ _ui_palette() {
538
+ case "$2" in
539
+ role.brand) printf -v "$1" %s '120;220;232 116' ;;
540
+ role.pill-text) printf -v "$1" %s '45;42;46 236' ;;
541
+ role.value) printf -v "$1" %s '169;220;118 150' ;;
542
+ role.dim) printf -v "$1" %s '114;112;114 243' ;;
543
+ pill.primary) printf -v "$1" %s '120;220;232 116' ;;
544
+ pill.secondary) printf -v "$1" %s '169;220;118 150' ;;
545
+ pill.warn) printf -v "$1" %s '255;216;102 221' ;;
546
+ pill.error) printf -v "$1" %s '255;97;136 204' ;;
547
+ pill.prompt) printf -v "$1" %s '171;157;242 147' ;;
548
+ pill.sub) printf -v "$1" %s '147;146;147 246' ;;
549
+ mark.nerd.ok) printf -v "$1" %s $'\xef\x80\x8c 169;220;118 150' ;;
550
+ mark.nerd.warn) printf -v "$1" %s $'! 255;216;102 221' ;;
551
+ mark.nerd.fail) printf -v "$1" %s $'\xef\x80\x8d 255;97;136 204' ;;
552
+ mark.nerd.info) printf -v "$1" %s $'\xe2\x84\xb9 120;220;232 116' ;;
553
+ mark.nerd.bullet) printf -v "$1" %s $'\xe2\x80\xa2 171;157;242 147' ;;
554
+ mark.nerd.arrow) printf -v "$1" %s $'\xee\xaa\x9c 114;112;114 243' ;;
555
+ mark.nerd.trace) printf -v "$1" %s $'\xe2\x86\xb3 114;112;114 243' ;;
556
+ mark.plain.ok) printf -v "$1" %s $'\xe2\x9c\x94 169;220;118 150' ;;
557
+ mark.plain.warn) printf -v "$1" %s $'! 255;216;102 221' ;;
558
+ mark.plain.fail) printf -v "$1" %s $'\xe2\x9c\x96 255;97;136 204' ;;
559
+ mark.plain.info) printf -v "$1" %s $'\xe2\x84\xb9 120;220;232 116' ;;
560
+ mark.plain.bullet) printf -v "$1" %s $'\xe2\x80\xa2 171;157;242 147' ;;
561
+ mark.plain.arrow) printf -v "$1" %s $'\xe2\x86\x92 114;112;114 243' ;;
562
+ mark.plain.trace) printf -v "$1" %s $'\xe2\x86\xb3 114;112;114 243' ;;
563
+ end.left) printf -v "$1" %s $'\xee\x82\xb6' ;;
564
+ end.right) printf -v "$1" %s $'\xee\x82\xb4' ;;
565
+ glyph.nerd.bar-done) printf -v "$1" %s $'\xef\x83\x88' ;;
566
+ glyph.nerd.bar-left) printf -v "$1" %s $'\xef\x82\x96' ;;
567
+ glyph.nerd.dot) printf -v "$1" %s $'\xef\x84\x91' ;;
568
+ glyph.nerd.ellipsis) printf -v "$1" %s $'\xef\x85\x81' ;;
569
+ glyph.nerd.refresh) printf -v "$1" %s $'\xef\x80\xa1' ;;
570
+ glyph.nerd.pause) printf -v "$1" %s $'\xe2\x80\x96' ;;
571
+ glyph.nerd.alert) printf -v "$1" %s $'\xe2\x80\xbc' ;;
572
+ glyph.nerd.pending) printf -v "$1" %s $'\xe2\x97\x8c' ;;
573
+ glyph.nerd.minus) printf -v "$1" %s $'\xe2\x88\x92' ;;
574
+ glyph.nerd.ring) printf -v "$1" %s $'\xef\x84\x8c' ;;
575
+ glyph.plain.bar-done) printf -v "$1" %s $'\xe2\x96\xb0' ;;
576
+ glyph.plain.bar-left) printf -v "$1" %s $'\xe2\x96\xb1' ;;
577
+ glyph.plain.dot) printf -v "$1" %s $'\xe2\x97\x8f' ;;
578
+ glyph.plain.ellipsis) printf -v "$1" %s $'\xe2\x80\xa6' ;;
579
+ glyph.plain.refresh) printf -v "$1" %s $'\xe2\x86\xbb' ;;
580
+ glyph.plain.pause) printf -v "$1" %s $'\xe2\x80\x96' ;;
581
+ glyph.plain.alert) printf -v "$1" %s $'\xe2\x80\xbc' ;;
582
+ glyph.plain.pending) printf -v "$1" %s $'\xe2\x97\x8c' ;;
583
+ glyph.plain.minus) printf -v "$1" %s $'\xe2\x88\x92' ;;
584
+ glyph.plain.ring) printf -v "$1" %s $'\xe2\x97\x8b' ;;
585
+ outcome.passed) printf -v "$1" %s ok ;;
586
+ outcome.done) printf -v "$1" %s ok ;;
587
+ outcome.proved) printf -v "$1" %s ok ;;
588
+ outcome.shipped) printf -v "$1" %s ok ;;
589
+ outcome.merged) printf -v "$1" %s ok ;;
590
+ outcome.parked) printf -v "$1" %s warn ;;
591
+ outcome.skipped) printf -v "$1" %s warn ;;
592
+ outcome.locked) printf -v "$1" %s warn ;;
593
+ outcome.nothing-to-take) printf -v "$1" %s warn ;;
594
+ outcome.rate-limited) printf -v "$1" %s warn ;;
595
+ outcome.budget-stop) printf -v "$1" %s warn ;;
596
+ outcome.failed) printf -v "$1" %s fail ;;
597
+ outcome.check-failed) printf -v "$1" %s fail ;;
598
+ outcome.blocked) printf -v "$1" %s fail ;;
599
+ outcome.interrupted) printf -v "$1" %s fail ;;
600
+ outcome.none) printf -v "$1" %s fail ;;
601
+ *) return 1 ;;
602
+ esac
603
+ }
604
+ # <<< palette
605
+
606
+ _ui_forced() { [[ -n "${CLICOLOR_FORCE:-}" && "$CLICOLOR_FORCE" != 0 ]]; }
607
+
608
+ # Both of gum's streams to the script's one.
609
+ _ui_gum() { gum "$@" >&"${UI_FD:-1}" 2>&1; }