@mark1russell7/text 0.0.18 → 0.0.20
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/README.md +40 -0
- package/build/error-6yypUDJz.js +37 -0
- package/build/error-6yypUDJz.js.map +1 -0
- package/build/index.js +1 -36
- package/build/plain-text.d.ts +16 -0
- package/build/plain-text.d.ts.map +1 -0
- package/build/plain-text.js +29 -0
- package/build/plain-text.js.map +1 -0
- package/build/print.d.ts +51 -0
- package/build/print.d.ts.map +1 -0
- package/build/print.js +55 -0
- package/build/print.js.map +1 -0
- package/build/references-BFiMj2LL.js +344 -0
- package/build/references-BFiMj2LL.js.map +1 -0
- package/build/references.js +1 -344
- package/package.json +13 -2
- package/src/plain-text.ts +27 -0
- package/src/print.ts +93 -0
- package/build/index.js.map +0 -1
- package/build/references.js.map +0 -1
package/README.md
CHANGED
|
@@ -40,6 +40,29 @@ escapeShell("my file's name"); // "'my file'\\''s name'"
|
|
|
40
40
|
escapeShell("$HOME"); // "'$HOME'"
|
|
41
41
|
```
|
|
42
42
|
|
|
43
|
+
## The output of a command-line tool
|
|
44
|
+
|
|
45
|
+
`createPrinter({ out, err?, secrets? })` gives a printer of the output of a command-line tool, for a person. `out` and `err` are streams with `write(text)`, for example `process.stdout` and `process.stderr`. The default of `err` is `out`. The entry imports no `node:` module, so a test can give a buffer in place of a stream.
|
|
46
|
+
|
|
47
|
+
- `line(text?)` writes a line to `out`.
|
|
48
|
+
- `table(rows, { head?, align?, gap? })` writes a table to `out`. It measures each cell in graphemes, and a line has no spaces at its end. The align `end` puts a column at the right, for numbers.
|
|
49
|
+
- `status(state, text)` writes `OK`, `INFO`, `WARN` or `FAIL` and the text. The state `fail` goes to `err`, and the other states go to `out`.
|
|
50
|
+
- `warn(text)` writes `warning: ` and the text to `err`.
|
|
51
|
+
- `error(thrown, context?)` writes the text of a thrown value (`errorMessage`) to `err`. A context comes before the text. It does not throw.
|
|
52
|
+
- `json(value)` writes the value as JSON with two spaces, to `out`.
|
|
53
|
+
|
|
54
|
+
Before each write, the printer puts `[secret]` in place of each known secret of `secrets` (`redactSecrets`) and each shape of a token (`redactSecretShapes`).
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { createPrinter } from "@mark1russell7/text/print";
|
|
58
|
+
|
|
59
|
+
const print = createPrinter({ out: process.stdout, err: process.stderr });
|
|
60
|
+
print.table([["alerts", 82], ["fleet", 121]], { head: ["repository", "calls"], align: ["start", "end"] });
|
|
61
|
+
print.status("fail", "test: 2 errors");
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
The printer is for output that a person reads. A diagnostic of a library or a service (a warning, a debug line, a caught error) is not output. Send it to a log record with its trace context, through the logger of OpenTelemetry.
|
|
65
|
+
|
|
43
66
|
## English word forms
|
|
44
67
|
|
|
45
68
|
`countOf(count, noun, plural?)` gives a count with a noun. Only the count 1 takes the singular noun. The default plural noun is the singular noun with "s" at the end.
|
|
@@ -340,6 +363,22 @@ decodeReferences(scanHtml("<p>Fish & chips</p>").text); // " Fish & chips "
|
|
|
340
363
|
|
|
341
364
|
The table comes from `entities.json` of the standard, under the license CC BY 4.0. The script `scripts/html-references.ts` makes the module `src/html-references.ts` from it. The table is in the entry `./references`. Thus a bundle that imports only `scanHtml` does not get the table.
|
|
342
365
|
|
|
366
|
+
### The plain text of HTML
|
|
367
|
+
|
|
368
|
+
`plainText(html)` of the entry `@mark1russell7/text/plain-text` gives the plain text of a part of HTML, for a summary or a pattern.
|
|
369
|
+
|
|
370
|
+
- It is `scanHtml`, then `decodeReferences`. Each run of white space becomes one space, and the ends have no space.
|
|
371
|
+
- `undefined`, and a text with no characters after these steps, give `undefined`.
|
|
372
|
+
- Each tag gives one space, as in `scanHtml`. Thus `<b>3</b>,` gives `3 ,`.
|
|
373
|
+
- A reference to `<` gives text, not a tag. The result is not safe HTML: escape it before a page shows it.
|
|
374
|
+
|
|
375
|
+
```ts
|
|
376
|
+
import { plainText } from "@mark1russell7/text/plain-text";
|
|
377
|
+
|
|
378
|
+
plainText("<p>Fish & chips</p>\n<p>at 10:00</p>"); // "Fish & chips at 10:00"
|
|
379
|
+
plainText("<p> </p>"); // undefined
|
|
380
|
+
```
|
|
381
|
+
|
|
343
382
|
## The copies that this package replaces
|
|
344
383
|
|
|
345
384
|
The tests run 58 copies of these functions from other repositories of the owner. Each copy comes from a list of the duplication atlas, at a pinned commit. The file `test/copies.json` has each copy, and `test/differences.ts` has each difference from the function of this package, with its reason. A copy that is not in the list gives the same output on each of the 1000 test strings.
|
|
@@ -354,6 +393,7 @@ The tests also examine 18 copies of a cut with a mark, from a later list of the
|
|
|
354
393
|
- `escapeShell` gives one word for a POSIX shell, not for `cmd.exe` or PowerShell. It throws a `RangeError` for a text with NUL.
|
|
355
394
|
- `wordSearch` keeps accents, so "cafe" does not find "café".
|
|
356
395
|
- `countOf` knows no irregular plural. Give the plural noun for each noun that does not add "s".
|
|
396
|
+
- `createPrinter` does not color the output and does not wrap a long line.
|
|
357
397
|
- `listOf` does not change the parts. A part with a comma makes a list that a reader cannot split again.
|
|
358
398
|
- `errorMessage` gives only the message. It does not add the name of the error, its `cause` or its stack.
|
|
359
399
|
- `redactSecrets` removes only the forms in its table. A secret in a JSON string with escapes, or in a different encoding, stays.
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// src/error.ts
|
|
2
|
+
/**
|
|
3
|
+
* This module has the text of a thrown value.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* This function gives the text of a thrown value, for a log line or a message on a screen. It does not throw.
|
|
7
|
+
*
|
|
8
|
+
* - An object with a `message` text gives that text. The check reads the field, not `instanceof Error`, so an error
|
|
9
|
+
* from another realm (a `vm` context or an `iframe`) also gives its message.
|
|
10
|
+
* - An `Error` with a `message` that is not a text gives `String(message)`. The brand check of
|
|
11
|
+
* `Object.prototype.toString` finds an `Error` of each realm.
|
|
12
|
+
* - Each other value gives `String(value)`.
|
|
13
|
+
* - When `String` throws, the function gives the tag of `Object.prototype.toString`, for example `[object Object]`.
|
|
14
|
+
* - A revoked `Proxy` gives its `typeof` in brackets: `[object]`.
|
|
15
|
+
*
|
|
16
|
+
* @param error - The thrown value. It can be any value.
|
|
17
|
+
* @returns The text of the value.
|
|
18
|
+
*/
|
|
19
|
+
function errorMessage(error) {
|
|
20
|
+
try {
|
|
21
|
+
if (typeof error === "object" && error !== null && "message" in error) {
|
|
22
|
+
const message = error.message;
|
|
23
|
+
if (typeof message === "string") return message;
|
|
24
|
+
if (Object.prototype.toString.call(error) === "[object Error]") return String(message);
|
|
25
|
+
}
|
|
26
|
+
return String(error);
|
|
27
|
+
} catch {
|
|
28
|
+
try {
|
|
29
|
+
return Object.prototype.toString.call(error);
|
|
30
|
+
} catch {
|
|
31
|
+
return `[${typeof error}]`;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export { errorMessage as t };
|
|
36
|
+
|
|
37
|
+
//# sourceMappingURL=error-6yypUDJz.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-6yypUDJz.js","names":[],"sources":["../src/error.ts"],"sourcesContent":["/**\n * This module has the text of a thrown value.\n */\n\n/**\n * This function gives the text of a thrown value, for a log line or a message on a screen. It does not throw.\n *\n * - An object with a `message` text gives that text. The check reads the field, not `instanceof Error`, so an error\n * from another realm (a `vm` context or an `iframe`) also gives its message.\n * - An `Error` with a `message` that is not a text gives `String(message)`. The brand check of\n * `Object.prototype.toString` finds an `Error` of each realm.\n * - Each other value gives `String(value)`.\n * - When `String` throws, the function gives the tag of `Object.prototype.toString`, for example `[object Object]`.\n * - A revoked `Proxy` gives its `typeof` in brackets: `[object]`.\n *\n * @param error - The thrown value. It can be any value.\n * @returns The text of the value.\n */\nexport function errorMessage(error: unknown): string {\n try {\n if (typeof error === \"object\" && error !== null && \"message\" in error) {\n const message: unknown = error.message;\n if (typeof message === \"string\") return message;\n if (Object.prototype.toString.call(error) === \"[object Error]\") return String(message);\n }\n return String(error);\n } catch {\n // String() throws for an object with no prototype, for a toString that throws and for a message getter that throws.\n try {\n return Object.prototype.toString.call(error);\n } catch {\n // A revoked Proxy throws also here. The operator typeof never throws.\n return `[${typeof error}]`;\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAkBA,SAAgB,aAAa,OAAwB;CACnD,IAAI;EACF,IAAI,OAAO,UAAU,YAAY,UAAU,QAAQ,aAAa,OAAO;GACrE,MAAM,UAAmB,MAAM;GAC/B,IAAI,OAAO,YAAY,UAAU,OAAO;GACxC,IAAI,OAAO,UAAU,SAAS,KAAK,KAAK,MAAM,kBAAkB,OAAO,OAAO,OAAO;EACvF;EACA,OAAO,OAAO,KAAK;CACrB,QAAQ;EAEN,IAAI;GACF,OAAO,OAAO,UAAU,SAAS,KAAK,KAAK;EAC7C,QAAQ;GAEN,OAAO,IAAI,OAAO,MAAM;EAC1B;CACF;AACF"}
|
package/build/index.js
CHANGED
|
@@ -1,40 +1,5 @@
|
|
|
1
1
|
import { countOf } from "./english.js";
|
|
2
|
+
import { t as errorMessage } from "./error-6yypUDJz.js";
|
|
2
3
|
import { a as escapeWorkflowData, i as escapeRegExp, n as escapeHtml, o as escapeWorkflowProperty, r as escapeMarkdown, t as escapeAttribute } from "./escape-vfOeIxac.js";
|
|
3
4
|
import { searchKey, wordSearch } from "./search.js";
|
|
4
|
-
// src/error.ts
|
|
5
|
-
/**
|
|
6
|
-
* This module has the text of a thrown value.
|
|
7
|
-
*/
|
|
8
|
-
/**
|
|
9
|
-
* This function gives the text of a thrown value, for a log line or a message on a screen. It does not throw.
|
|
10
|
-
*
|
|
11
|
-
* - An object with a `message` text gives that text. The check reads the field, not `instanceof Error`, so an error
|
|
12
|
-
* from another realm (a `vm` context or an `iframe`) also gives its message.
|
|
13
|
-
* - An `Error` with a `message` that is not a text gives `String(message)`. The brand check of
|
|
14
|
-
* `Object.prototype.toString` finds an `Error` of each realm.
|
|
15
|
-
* - Each other value gives `String(value)`.
|
|
16
|
-
* - When `String` throws, the function gives the tag of `Object.prototype.toString`, for example `[object Object]`.
|
|
17
|
-
* - A revoked `Proxy` gives its `typeof` in brackets: `[object]`.
|
|
18
|
-
*
|
|
19
|
-
* @param error - The thrown value. It can be any value.
|
|
20
|
-
* @returns The text of the value.
|
|
21
|
-
*/
|
|
22
|
-
function errorMessage(error) {
|
|
23
|
-
try {
|
|
24
|
-
if (typeof error === "object" && error !== null && "message" in error) {
|
|
25
|
-
const message = error.message;
|
|
26
|
-
if (typeof message === "string") return message;
|
|
27
|
-
if (Object.prototype.toString.call(error) === "[object Error]") return String(message);
|
|
28
|
-
}
|
|
29
|
-
return String(error);
|
|
30
|
-
} catch {
|
|
31
|
-
try {
|
|
32
|
-
return Object.prototype.toString.call(error);
|
|
33
|
-
} catch {
|
|
34
|
-
return `[${typeof error}]`;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
5
|
export { countOf, errorMessage, escapeAttribute, escapeHtml, escapeMarkdown, escapeRegExp, escapeWorkflowData, escapeWorkflowProperty, searchKey, wordSearch };
|
|
39
|
-
|
|
40
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This function gives the plain text of a part of HTML, for a summary or a pattern. It is `scanHtml`, then `decodeReferences`.
|
|
3
|
+
*
|
|
4
|
+
* - The scan removes the tags, and it skips scripts, styles and templates.
|
|
5
|
+
* - Each tag gives one space, as in `scanHtml`. Thus `<b>3</b>,` gives `3 ,`, and two words in two elements stay two words.
|
|
6
|
+
* - Each character reference gives its value. A reference to `<` gives text, not a tag.
|
|
7
|
+
* - Each run of white space becomes one space, and the text has no space at its start or its end.
|
|
8
|
+
* - `undefined`, and a text that is empty after these steps, give `undefined`.
|
|
9
|
+
*
|
|
10
|
+
* The function does not throw, and it is linear in the length of the HTML. The result is not safe HTML: escape it before a page shows it.
|
|
11
|
+
*
|
|
12
|
+
* @param html - The HTML, or `undefined` for a field that is not there.
|
|
13
|
+
* @returns The plain text, or `undefined` when it is empty.
|
|
14
|
+
*/
|
|
15
|
+
export declare function plainText(html: string | undefined): string | undefined;
|
|
16
|
+
//# sourceMappingURL=plain-text.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plain-text.d.ts","names":[],"sources":["../src/plain-text.ts"],"mappings":";;;;;;;;;;;;;;wBAsBgB,UAAU"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { scanHtml } from "./html.js";
|
|
2
|
+
import { t as decodeReferences } from "./references-BFiMj2LL.js";
|
|
3
|
+
// src/plain-text.ts
|
|
4
|
+
/**
|
|
5
|
+
* The plain text of a part of HTML: the entry `@mark1russell7/text/plain-text`. It is `scanHtml` of `./html`, then
|
|
6
|
+
* `decodeReferences` of `./references`. It is its own entry, so `./references` does not get the scanner.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* This function gives the plain text of a part of HTML, for a summary or a pattern. It is `scanHtml`, then `decodeReferences`.
|
|
10
|
+
*
|
|
11
|
+
* - The scan removes the tags, and it skips scripts, styles and templates.
|
|
12
|
+
* - Each tag gives one space, as in `scanHtml`. Thus `<b>3</b>,` gives `3 ,`, and two words in two elements stay two words.
|
|
13
|
+
* - Each character reference gives its value. A reference to `<` gives text, not a tag.
|
|
14
|
+
* - Each run of white space becomes one space, and the text has no space at its start or its end.
|
|
15
|
+
* - `undefined`, and a text that is empty after these steps, give `undefined`.
|
|
16
|
+
*
|
|
17
|
+
* The function does not throw, and it is linear in the length of the HTML. The result is not safe HTML: escape it before a page shows it.
|
|
18
|
+
*
|
|
19
|
+
* @param html - The HTML, or `undefined` for a field that is not there.
|
|
20
|
+
* @returns The plain text, or `undefined` when it is empty.
|
|
21
|
+
*/
|
|
22
|
+
function plainText(html) {
|
|
23
|
+
if (html === void 0) return void 0;
|
|
24
|
+
const text = decodeReferences(scanHtml(html).text).replace(/\s+/g, " ").trim();
|
|
25
|
+
return text === "" ? void 0 : text;
|
|
26
|
+
}
|
|
27
|
+
export { plainText };
|
|
28
|
+
|
|
29
|
+
//# sourceMappingURL=plain-text.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plain-text.js","names":[],"sources":["../src/plain-text.ts"],"sourcesContent":["/**\n * The plain text of a part of HTML: the entry `@mark1russell7/text/plain-text`. It is `scanHtml` of `./html`, then\n * `decodeReferences` of `./references`. It is its own entry, so `./references` does not get the scanner.\n */\n\nimport { scanHtml } from \"./html.ts\";\nimport { decodeReferences } from \"./references.ts\";\n\n/**\n * This function gives the plain text of a part of HTML, for a summary or a pattern. It is `scanHtml`, then `decodeReferences`.\n *\n * - The scan removes the tags, and it skips scripts, styles and templates.\n * - Each tag gives one space, as in `scanHtml`. Thus `<b>3</b>,` gives `3 ,`, and two words in two elements stay two words.\n * - Each character reference gives its value. A reference to `<` gives text, not a tag.\n * - Each run of white space becomes one space, and the text has no space at its start or its end.\n * - `undefined`, and a text that is empty after these steps, give `undefined`.\n *\n * The function does not throw, and it is linear in the length of the HTML. The result is not safe HTML: escape it before a page shows it.\n *\n * @param html - The HTML, or `undefined` for a field that is not there.\n * @returns The plain text, or `undefined` when it is empty.\n */\nexport function plainText(html: string | undefined): string | undefined {\n if (html === undefined) return undefined;\n const text = decodeReferences(scanHtml(html).text).replace(/\\s+/g, \" \").trim();\n return text === \"\" ? undefined : text;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAsBA,SAAgB,UAAU,MAA8C;CACtE,IAAI,SAAS,KAAA,GAAW,OAAO,KAAA;CAC/B,MAAM,OAAO,iBAAiB,SAAS,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,QAAQ,QAAQ,GAAG,CAAC,CAAC,KAAK;CAC7E,OAAO,SAAS,KAAK,KAAA,IAAY;AACnC"}
|
package/build/print.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Secret } from "./redact.js";
|
|
2
|
+
/** A stream of text, for example `process.stdout`. The printer uses only its `write`. */
|
|
3
|
+
export interface TextSink {
|
|
4
|
+
write(text: string): unknown;
|
|
5
|
+
}
|
|
6
|
+
/** The options of `createPrinter`. */
|
|
7
|
+
export interface PrinterOptions {
|
|
8
|
+
/** The stream of the output, for example `process.stdout`. */
|
|
9
|
+
readonly out: TextSink;
|
|
10
|
+
/** The stream of the errors and warnings, for example `process.stderr`. The default is `out`. */
|
|
11
|
+
readonly err?: TextSink;
|
|
12
|
+
/** The known secrets, for example the tokens of the command. The printer puts a mark in place of each before a write. */
|
|
13
|
+
readonly secrets?: readonly Secret[];
|
|
14
|
+
}
|
|
15
|
+
/** The side of a column of a table. `end` aligns the cells at the right, for numbers. */
|
|
16
|
+
export type ColumnAlign = "start" | "end";
|
|
17
|
+
/** The options of `Printer.table`. */
|
|
18
|
+
export interface TableOptions {
|
|
19
|
+
/** The head of each column. The printer writes it as the first row. */
|
|
20
|
+
readonly head?: readonly string[];
|
|
21
|
+
/** The side of each column. The default is `start` for each column. */
|
|
22
|
+
readonly align?: readonly ColumnAlign[];
|
|
23
|
+
/** The text between two columns. The default is two spaces. */
|
|
24
|
+
readonly gap?: string;
|
|
25
|
+
}
|
|
26
|
+
/** The state of a status line. */
|
|
27
|
+
export type StatusState = "ok" | "fail" | "warn" | "info";
|
|
28
|
+
/** A printer of the output of a command-line tool. */
|
|
29
|
+
export interface Printer {
|
|
30
|
+
/** This function writes a line to `out`. No text gives an empty line. */
|
|
31
|
+
line(text?: string): void;
|
|
32
|
+
/** This function writes a table to `out`. A cell is any value, and `String` gives its text. */
|
|
33
|
+
table(rows: readonly (readonly unknown[])[], options?: TableOptions): void;
|
|
34
|
+
/** This function writes a status line to `out`, for example `FAIL build: 2 errors`. The state `fail` goes to `err`. */
|
|
35
|
+
status(state: StatusState, text: string): void;
|
|
36
|
+
/** This function writes a warning line to `err`. */
|
|
37
|
+
warn(text: string): void;
|
|
38
|
+
/** This function writes the text of a thrown value to `err`. It does not throw. A `context` comes before the text. */
|
|
39
|
+
error(error: unknown, context?: string): void;
|
|
40
|
+
/** This function writes a value as JSON with two spaces, to `out`. It throws as `JSON.stringify` does, for a bigint or a cycle. */
|
|
41
|
+
json(value: unknown): void;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* This function makes a printer that writes to the streams of `options`. Each write puts a mark in place of each known
|
|
45
|
+
* secret and of each shape of a token (`redactSecretShapes`), and ends with a new line.
|
|
46
|
+
*
|
|
47
|
+
* @param options - The streams and the known secrets.
|
|
48
|
+
* @returns The printer.
|
|
49
|
+
*/
|
|
50
|
+
export declare function createPrinter(options: PrinterOptions): Printer;
|
|
51
|
+
//# sourceMappingURL=print.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"print.d.ts","names":[],"sources":["../src/print.ts"],"mappings":";;iBAciB;EACf,MAAM;;;iBAIS;;WAEN,KAAK;;WAEL,MAAM;;WAEN,mBAAmB;;;YAIlB;;iBAGK;;WAEN;;WAEA,iBAAiB;;WAEjB;;;YAIC;;iBAGK;;EAEf,KAAK;;EAEL,MAAM,uCAAuC,UAAU;;EAEvD,OAAO,OAAO,aAAa;;EAE3B,KAAK;;EAEL,MAAM,gBAAgB;;EAEtB,KAAK;;;;;;;;;wBAYS,cAAc,SAAS,iBAAiB"}
|
package/build/print.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { t as errorMessage } from "./error-6yypUDJz.js";
|
|
2
|
+
import { pad, textLength } from "./length.js";
|
|
3
|
+
import { redactSecretShapes, redactSecrets } from "./redact.js";
|
|
4
|
+
// src/print.ts
|
|
5
|
+
/**
|
|
6
|
+
* The entry `@mark1russell7/text/print`: the printer of the output of a command-line tool for a person. It writes lines,
|
|
7
|
+
* tables, status lines and the text of a thrown value to streams that the caller gives. Thus the entry imports no `node:`
|
|
8
|
+
* module, and a test gives a buffer in place of a stream.
|
|
9
|
+
*
|
|
10
|
+
* The printer is for output that a person reads: the text, the tables and the status lines of a CLI. A diagnostic of a
|
|
11
|
+
* library or a service (a warning, a debug line, a caught error) is not output. It goes to a log record with its trace
|
|
12
|
+
* context, through the logger of OpenTelemetry, and not to a printer.
|
|
13
|
+
*/
|
|
14
|
+
const MARKS = {
|
|
15
|
+
ok: "OK ",
|
|
16
|
+
fail: "FAIL",
|
|
17
|
+
warn: "WARN",
|
|
18
|
+
info: "INFO"
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* This function makes a printer that writes to the streams of `options`. Each write puts a mark in place of each known
|
|
22
|
+
* secret and of each shape of a token (`redactSecretShapes`), and ends with a new line.
|
|
23
|
+
*
|
|
24
|
+
* @param options - The streams and the known secrets.
|
|
25
|
+
* @returns The printer.
|
|
26
|
+
*/
|
|
27
|
+
function createPrinter(options) {
|
|
28
|
+
const { out, err = out, secrets = [] } = options;
|
|
29
|
+
const write = (sink, text) => {
|
|
30
|
+
sink.write(`${redactSecretShapes(secrets.length > 0 ? redactSecrets(text, secrets) : text)}\n`);
|
|
31
|
+
};
|
|
32
|
+
return {
|
|
33
|
+
line: (text = "") => write(out, text),
|
|
34
|
+
table: (rows, table = {}) => {
|
|
35
|
+
const { head, align = [], gap = " " } = table;
|
|
36
|
+
const cells = [...head ? [head] : [], ...rows].map((row) => row.map((cell) => String(cell ?? "")));
|
|
37
|
+
const widths = [];
|
|
38
|
+
for (const row of cells) row.forEach((cell, at) => widths[at] = Math.max(widths[at] ?? 0, textLength(cell, "grapheme")));
|
|
39
|
+
for (const row of cells) {
|
|
40
|
+
const parts = row.map((cell, at) => pad(cell, widths[at] ?? 0, {
|
|
41
|
+
unit: "grapheme",
|
|
42
|
+
side: align[at] === "end" ? "start" : "end"
|
|
43
|
+
}));
|
|
44
|
+
write(out, parts.join(gap).trimEnd());
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
status: (state, text) => write(state === "fail" ? err : out, `${MARKS[state]} ${text}`),
|
|
48
|
+
warn: (text) => write(err, `warning: ${text}`),
|
|
49
|
+
error: (error, context) => write(err, context === void 0 ? errorMessage(error) : `${context}: ${errorMessage(error)}`),
|
|
50
|
+
json: (value) => write(out, JSON.stringify(value, null, 2) ?? String(value))
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
export { createPrinter };
|
|
54
|
+
|
|
55
|
+
//# sourceMappingURL=print.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"print.js","names":[],"sources":["../src/print.ts"],"sourcesContent":["/**\n * The entry `@mark1russell7/text/print`: the printer of the output of a command-line tool for a person. It writes lines,\n * tables, status lines and the text of a thrown value to streams that the caller gives. Thus the entry imports no `node:`\n * module, and a test gives a buffer in place of a stream.\n *\n * The printer is for output that a person reads: the text, the tables and the status lines of a CLI. A diagnostic of a\n * library or a service (a warning, a debug line, a caught error) is not output. It goes to a log record with its trace\n * context, through the logger of OpenTelemetry, and not to a printer.\n */\nimport { errorMessage } from \"./error.ts\";\nimport { pad, textLength } from \"./length.ts\";\nimport { redactSecretShapes, redactSecrets, type Secret } from \"./redact.ts\";\n\n/** A stream of text, for example `process.stdout`. The printer uses only its `write`. */\nexport interface TextSink {\n write(text: string): unknown;\n}\n\n/** The options of `createPrinter`. */\nexport interface PrinterOptions {\n /** The stream of the output, for example `process.stdout`. */\n readonly out: TextSink;\n /** The stream of the errors and warnings, for example `process.stderr`. The default is `out`. */\n readonly err?: TextSink;\n /** The known secrets, for example the tokens of the command. The printer puts a mark in place of each before a write. */\n readonly secrets?: readonly Secret[];\n}\n\n/** The side of a column of a table. `end` aligns the cells at the right, for numbers. */\nexport type ColumnAlign = \"start\" | \"end\";\n\n/** The options of `Printer.table`. */\nexport interface TableOptions {\n /** The head of each column. The printer writes it as the first row. */\n readonly head?: readonly string[];\n /** The side of each column. The default is `start` for each column. */\n readonly align?: readonly ColumnAlign[];\n /** The text between two columns. The default is two spaces. */\n readonly gap?: string;\n}\n\n/** The state of a status line. */\nexport type StatusState = \"ok\" | \"fail\" | \"warn\" | \"info\";\n\n/** A printer of the output of a command-line tool. */\nexport interface Printer {\n /** This function writes a line to `out`. No text gives an empty line. */\n line(text?: string): void;\n /** This function writes a table to `out`. A cell is any value, and `String` gives its text. */\n table(rows: readonly (readonly unknown[])[], options?: TableOptions): void;\n /** This function writes a status line to `out`, for example `FAIL build: 2 errors`. The state `fail` goes to `err`. */\n status(state: StatusState, text: string): void;\n /** This function writes a warning line to `err`. */\n warn(text: string): void;\n /** This function writes the text of a thrown value to `err`. It does not throw. A `context` comes before the text. */\n error(error: unknown, context?: string): void;\n /** This function writes a value as JSON with two spaces, to `out`. It throws as `JSON.stringify` does, for a bigint or a cycle. */\n json(value: unknown): void;\n}\n\nconst MARKS: Readonly<Record<StatusState, string>> = { ok: \"OK \", fail: \"FAIL\", warn: \"WARN\", info: \"INFO\" };\n\n/**\n * This function makes a printer that writes to the streams of `options`. Each write puts a mark in place of each known\n * secret and of each shape of a token (`redactSecretShapes`), and ends with a new line.\n *\n * @param options - The streams and the known secrets.\n * @returns The printer.\n */\nexport function createPrinter(options: PrinterOptions): Printer {\n const { out, err = out, secrets = [] } = options;\n const write = (sink: TextSink, text: string): void => {\n sink.write(`${redactSecretShapes(secrets.length > 0 ? redactSecrets(text, secrets) : text)}\\n`);\n };\n return {\n line: (text = \"\") => write(out, text),\n table: (rows, table = {}) => {\n const { head, align = [], gap = \" \" } = table;\n const cells = [...(head ? [head] : []), ...rows].map((row) => row.map((cell) => String(cell ?? \"\")));\n const widths: number[] = [];\n for (const row of cells) row.forEach((cell, at) => (widths[at] = Math.max(widths[at] ?? 0, textLength(cell, \"grapheme\"))));\n for (const row of cells) {\n const parts = row.map((cell, at) => pad(cell, widths[at] ?? 0, { unit: \"grapheme\", side: align[at] === \"end\" ? \"start\" : \"end\" }));\n // A line has no spaces at its end, also after an empty last cell.\n write(out, parts.join(gap).trimEnd());\n }\n },\n status: (state, text) => write(state === \"fail\" ? err : out, `${MARKS[state]} ${text}`),\n warn: (text) => write(err, `warning: ${text}`),\n error: (error, context) => write(err, context === undefined ? errorMessage(error) : `${context}: ${errorMessage(error)}`),\n json: (value) => write(out, JSON.stringify(value, null, 2) ?? String(value)),\n };\n}\n"],"mappings":";;;;;;;;;;;;;AA4DA,MAAM,QAA+C;CAAE,IAAI;CAAQ,MAAM;CAAQ,MAAM;CAAQ,MAAM;AAAO;;;;;;;;AAS5G,SAAgB,cAAc,SAAkC;CAC9D,MAAM,EAAE,KAAK,MAAM,KAAK,UAAU,CAAC,MAAM;CACzC,MAAM,SAAS,MAAgB,SAAuB;EACpD,KAAK,MAAM,GAAG,mBAAmB,QAAQ,SAAS,IAAI,cAAc,MAAM,OAAO,IAAI,IAAI,EAAE,GAAG;CAChG;CACA,OAAO;EACL,OAAO,OAAO,OAAO,MAAM,KAAK,IAAI;EACpC,QAAQ,MAAM,QAAQ,CAAC,MAAM;GAC3B,MAAM,EAAE,MAAM,QAAQ,CAAC,GAAG,MAAM,SAAS;GACzC,MAAM,QAAQ,CAAC,GAAI,OAAO,CAAC,IAAI,IAAI,CAAC,GAAI,GAAG,IAAI,CAAC,CAAC,KAAK,QAAQ,IAAI,KAAK,SAAS,OAAO,QAAQ,EAAE,CAAC,CAAC;GACnG,MAAM,SAAmB,CAAC;GAC1B,KAAK,MAAM,OAAO,OAAO,IAAI,SAAS,MAAM,OAAQ,OAAO,MAAM,KAAK,IAAI,OAAO,OAAO,GAAG,WAAW,MAAM,UAAU,CAAC,CAAE;GACzH,KAAK,MAAM,OAAO,OAAO;IACvB,MAAM,QAAQ,IAAI,KAAK,MAAM,OAAO,IAAI,MAAM,OAAO,OAAO,GAAG;KAAE,MAAM;KAAY,MAAM,MAAM,QAAQ,QAAQ,UAAU;IAAM,CAAC,CAAC;IAEjI,MAAM,KAAK,MAAM,KAAK,GAAG,CAAC,CAAC,QAAQ,CAAC;GACtC;EACF;EACA,SAAS,OAAO,SAAS,MAAM,UAAU,SAAS,MAAM,KAAK,GAAG,MAAM,OAAO,GAAG,MAAM;EACtF,OAAO,SAAS,MAAM,KAAK,YAAY,MAAM;EAC7C,QAAQ,OAAO,YAAY,MAAM,KAAK,YAAY,KAAA,IAAY,aAAa,KAAK,IAAI,GAAG,QAAQ,IAAI,aAAa,KAAK,GAAG;EACxH,OAAO,UAAU,MAAM,KAAK,KAAK,UAAU,OAAO,MAAM,CAAC,KAAK,OAAO,KAAK,CAAC;CAC7E;AACF"}
|