ostia 0.6.1 → 0.7.0
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.
- package/package.json +1 -1
- package/src/cli/help.ts +10 -1
- package/src/cli/run.ts +5 -1
- package/src/terminal.ts +88 -3
package/package.json
CHANGED
package/src/cli/help.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { processEnv } from "../config/env.ts";
|
|
2
2
|
import {
|
|
3
3
|
createPaint,
|
|
4
|
+
fitTerminal,
|
|
4
5
|
MEASURE,
|
|
5
6
|
type Paint,
|
|
6
7
|
PLAIN,
|
|
@@ -665,6 +666,14 @@ export async function showHelp(
|
|
|
665
666
|
topic: HelpTopic,
|
|
666
667
|
requested: boolean,
|
|
667
668
|
): Promise<0 | 2> {
|
|
668
|
-
|
|
669
|
+
const { isatty } = await import("node:tty");
|
|
670
|
+
await Bun.write(
|
|
671
|
+
Bun.stdout,
|
|
672
|
+
fitTerminal(
|
|
673
|
+
helpText(topic, await paintFor(1)),
|
|
674
|
+
isatty(1),
|
|
675
|
+
process.stdout.columns,
|
|
676
|
+
),
|
|
677
|
+
);
|
|
669
678
|
return requested ? 0 : 2;
|
|
670
679
|
}
|
package/src/cli/run.ts
CHANGED
|
@@ -43,6 +43,7 @@ import type { MinimalProtocolContext } from "../renderers/minimal.ts";
|
|
|
43
43
|
import type { TimeSource, TimeUnit } from "../spawn/index.ts";
|
|
44
44
|
import {
|
|
45
45
|
createProgress,
|
|
46
|
+
isDumb,
|
|
46
47
|
type Paint,
|
|
47
48
|
PLAIN,
|
|
48
49
|
plural,
|
|
@@ -113,7 +114,10 @@ async function humanFor(args: {
|
|
|
113
114
|
}
|
|
114
115
|
const { isatty } = await import("node:tty");
|
|
115
116
|
const shown =
|
|
116
|
-
!args.quiet &&
|
|
117
|
+
!args.quiet &&
|
|
118
|
+
isatty(2) &&
|
|
119
|
+
!isDumb(processEnv()) &&
|
|
120
|
+
agentVariable(processEnv()) === undefined;
|
|
117
121
|
return {
|
|
118
122
|
paint: await paintFor(1, args.color),
|
|
119
123
|
progress: shown
|
package/src/terminal.ts
CHANGED
|
@@ -51,8 +51,9 @@ export function createPaint(): Paint {
|
|
|
51
51
|
|
|
52
52
|
// The person's explicit ask wins, because they know where the output
|
|
53
53
|
// goes: the flag, then NO_COLOR and FORCE_COLOR (no-color.org,
|
|
54
|
-
// force-color.org). Otherwise color only for a terminal
|
|
55
|
-
// reads, since
|
|
54
|
+
// force-color.org). Otherwise color only for a terminal that reads
|
|
55
|
+
// escape codes and no coding agent reads, since they're noise to
|
|
56
|
+
// a program.
|
|
56
57
|
export function wantsColor(
|
|
57
58
|
forced: boolean | undefined,
|
|
58
59
|
terminal: boolean,
|
|
@@ -63,9 +64,15 @@ export function wantsColor(
|
|
|
63
64
|
if ((env.NO_COLOR ?? "") !== "") return false;
|
|
64
65
|
const force = env.FORCE_COLOR;
|
|
65
66
|
if (force !== undefined && force !== "") return force !== "0";
|
|
66
|
-
return terminal && !agent;
|
|
67
|
+
return terminal && !agent && !isDumb(env);
|
|
67
68
|
}
|
|
68
69
|
|
|
70
|
+
// TERM=dumb says the terminal knows no escape sequence: no color, and no
|
|
71
|
+
// carriage-return redraw, so a progress line would print every frame.
|
|
72
|
+
export const isDumb = (
|
|
73
|
+
env: Readonly<Record<string, string | undefined>>,
|
|
74
|
+
): boolean => env.TERM === "dumb";
|
|
75
|
+
|
|
69
76
|
// "n/a", because an em dash for a missing value reads as punctuation.
|
|
70
77
|
export const MISSING = "n/a";
|
|
71
78
|
|
|
@@ -160,6 +167,8 @@ export function withGutter(
|
|
|
160
167
|
|
|
161
168
|
// eslint-disable-next-line no-control-regex -- escape codes are what it strips
|
|
162
169
|
const ESCAPE = /\x1b\[[0-9;]*m/g;
|
|
170
|
+
// eslint-disable-next-line no-control-regex -- escape codes are what it skips
|
|
171
|
+
const LEADING_ESCAPE = /^\x1b\[[0-9;]*m/;
|
|
163
172
|
|
|
164
173
|
// Widths come from Bun when it runs, since it counts wide characters;
|
|
165
174
|
// a package that also runs on Node counts one column per code point,
|
|
@@ -172,6 +181,82 @@ export const textWidth = (text: string): number =>
|
|
|
172
181
|
// 80 columns, because that's the width a terminal opens at.
|
|
173
182
|
export const MEASURE = 80;
|
|
174
183
|
|
|
184
|
+
// Because a help table's second column starts after a run of two or more
|
|
185
|
+
// spaces, a wrapped description continues there, not at the margin.
|
|
186
|
+
function hangOf(line: string): number {
|
|
187
|
+
const plain = line.replace(ESCAPE, "");
|
|
188
|
+
const lead = /^ */.exec(plain)?.[0].length ?? 0;
|
|
189
|
+
const gap = /^ *\S.*? {2,}(?=\S)/.exec(plain);
|
|
190
|
+
return gap ? textWidth(gap[0]) : lead;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// Columns count only what shows, since a color code in the first half
|
|
194
|
+
// would otherwise put the split early.
|
|
195
|
+
function splitAt(line: string, columns: number): [string, string] {
|
|
196
|
+
let seen = 0;
|
|
197
|
+
let i = 0;
|
|
198
|
+
while (i < line.length && seen < columns) {
|
|
199
|
+
const escape = LEADING_ESCAPE.exec(line.slice(i));
|
|
200
|
+
if (escape) {
|
|
201
|
+
i += escape[0].length;
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
const ch = String.fromCodePoint(line.codePointAt(i) ?? 32);
|
|
205
|
+
seen += textWidth(ch);
|
|
206
|
+
i += ch.length;
|
|
207
|
+
}
|
|
208
|
+
return [line.slice(0, i), line.slice(i)];
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// Text whose lines are wider than `width`, wrapped at words: help on a
|
|
212
|
+
// narrow terminal, where a long line would wrap mid-word. A continuation
|
|
213
|
+
// line starts under its line's second column, or under its indent, so a
|
|
214
|
+
// table of options and their descriptions stays a table.
|
|
215
|
+
// Help and other prose for a person: fit to the terminal, at most 80
|
|
216
|
+
// columns, and left whole when piped, since a program reads whole lines.
|
|
217
|
+
export const fitTerminal = (
|
|
218
|
+
text: string,
|
|
219
|
+
terminal: boolean,
|
|
220
|
+
columns: number | undefined,
|
|
221
|
+
): string =>
|
|
222
|
+
terminal ? fitWidth(text, Math.min(MEASURE, columns || MEASURE)) : text;
|
|
223
|
+
|
|
224
|
+
export function fitWidth(text: string, width: number): string {
|
|
225
|
+
return text
|
|
226
|
+
.split("\n")
|
|
227
|
+
.flatMap((line) => {
|
|
228
|
+
if (textWidth(line) <= width) return [line];
|
|
229
|
+
let hang = hangOf(line);
|
|
230
|
+
if (hang > width / 2)
|
|
231
|
+
hang = /^ */.exec(line.replace(ESCAPE, ""))?.[0].length ?? 0;
|
|
232
|
+
const [head, rest] = splitAt(line, hang);
|
|
233
|
+
const room = Math.max(1, width - hang);
|
|
234
|
+
const lines: string[] = [];
|
|
235
|
+
let current = "";
|
|
236
|
+
// The separator stays with the word after it, so no line ends on
|
|
237
|
+
// a dangling "·".
|
|
238
|
+
const words: string[] = [];
|
|
239
|
+
for (const word of rest.split(/ +/)) {
|
|
240
|
+
const last = words.at(-1);
|
|
241
|
+
if (last !== undefined && last.replace(ESCAPE, "") === "·") {
|
|
242
|
+
words[words.length - 1] = `${last} ${word}`;
|
|
243
|
+
} else if (word !== "") words.push(word);
|
|
244
|
+
}
|
|
245
|
+
for (const word of words) {
|
|
246
|
+
const next = current === "" ? word : `${current} ${word}`;
|
|
247
|
+
if (current !== "" && textWidth(next) > room) {
|
|
248
|
+
lines.push(current);
|
|
249
|
+
current = word;
|
|
250
|
+
} else {
|
|
251
|
+
current = next;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
if (current !== "") lines.push(current);
|
|
255
|
+
return lines.map((part, k) => (k === 0 ? head : " ".repeat(hang)) + part);
|
|
256
|
+
})
|
|
257
|
+
.join("\n");
|
|
258
|
+
}
|
|
259
|
+
|
|
175
260
|
// A cell that draws a rule as wide as its column, so totals read apart
|
|
176
261
|
// from the rows above them.
|
|
177
262
|
export const RULE = "\u0000rule";
|