ostia 0.6.0 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ostia",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "Fast profiling and benchmarking for Bun.",
5
5
  "license": "MIT",
6
6
  "author": "Eric Liu (https://github.com/metonym)",
package/src/cli/flags.ts CHANGED
@@ -234,7 +234,7 @@ export function parseFlags<T extends object>(
234
234
  const eq = arg.startsWith("--") ? arg.indexOf("=") : -1;
235
235
  const name = eq > 0 ? arg.slice(0, eq) : arg;
236
236
  const flagDef = Object.hasOwn(flags, name) ? flags[name] : undefined;
237
- if (!flagDef || (eq > 0 && flagDef.spec.kind === "bool")) {
237
+ if (!flagDef) {
238
238
  if (arg.startsWith("-") || !positional) {
239
239
  throw new CliUsageError(
240
240
  `Unknown flag "${arg}" for "ostia ${command}".`,
@@ -269,9 +269,18 @@ export function parseFlags<T extends object>(
269
269
  set(dest, n);
270
270
  break;
271
271
  }
272
- case "bool":
273
- set(dest, spec.value);
272
+ case "bool": {
273
+ // --flag=true and --flag=false, as a script that passes a variable
274
+ // writes them; false inverts, so --no-color=false turns color on.
275
+ const raw = eq > 0 ? arg.slice(eq + 1) : "true";
276
+ if (raw !== "true" && raw !== "false") {
277
+ throw new CliUsageError(
278
+ `Invalid ${name} "${raw}": expected true or false`,
279
+ );
280
+ }
281
+ set(dest, raw === "true" ? spec.value : !spec.value);
274
282
  break;
283
+ }
275
284
  case "list":
276
285
  listAt(args, dest).push(value());
277
286
  break;
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
- await Bun.write(Bun.stdout, helpText(topic, await paintFor(1)));
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 && isatty(2) && agentVariable(processEnv()) === undefined;
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 no coding agent
55
- // reads, since escape codes are noise to a program.
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";