@metricraven/cli 0.2.0 → 0.2.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.
- package/dist/commands.js +3 -2
- package/dist/format.js +16 -0
- package/package.json +1 -1
package/dist/commands.js
CHANGED
|
@@ -3,10 +3,11 @@ import { UsageError, buildFilters, positiveInt, rangeParams } from "./args.js";
|
|
|
3
3
|
import { customRange, validTimeZone } from "./range.js";
|
|
4
4
|
import { completionScript } from "./completion.js";
|
|
5
5
|
import { configPath, deleteConfigToken, readConfig, resolveSettings, updateConfig } from "./config.js";
|
|
6
|
-
import { bold, change, date, dim, duration, money, num, pct, section, table, truncate } from "./format.js";
|
|
6
|
+
import { bold, change, date, dim, duration, money, num, pct, section, table, terminalSafe, truncate } from "./format.js";
|
|
7
7
|
import { interactive, prompt, promptHidden, readStdin } from "./prompt.js";
|
|
8
8
|
export const TOKENS_URL = "https://metricraven.com/app/api";
|
|
9
|
-
|
|
9
|
+
/** Every line the CLI prints; terminalSafe strips escape sequences that came from report data. */
|
|
10
|
+
const out = (s) => process.stdout.write(`${terminalSafe(s)}\n`);
|
|
10
11
|
const printJson = (data) => out(JSON.stringify(data));
|
|
11
12
|
/**
|
|
12
13
|
* --fields: the API's ?fields= selection (comma-separated dot paths, period always kept). The
|
package/dist/format.js
CHANGED
|
@@ -5,6 +5,22 @@ export const dim = (s) => (color ? `\x1b[2m${s}\x1b[22m` : s);
|
|
|
5
5
|
const green = (s) => (color ? `\x1b[32m${s}\x1b[39m` : s);
|
|
6
6
|
const red = (s) => (color ? `\x1b[31m${s}\x1b[39m` : s);
|
|
7
7
|
const visibleLength = (s) => s.replace(/\x1b\[[0-9;]*m/g, "").length;
|
|
8
|
+
/** The only escape sequences the CLI writes itself: bold, dim, green, red and their resets. */
|
|
9
|
+
const OWN_STYLES = new Set(["\x1b[1m", "\x1b[22m", "\x1b[2m", "\x1b[32m", "\x1b[39m", "\x1b[31m"]);
|
|
10
|
+
/**
|
|
11
|
+
* Makes text safe to print to a terminal. Reports contain strings that visitors and third parties
|
|
12
|
+
* choose (event properties, names, mention text); escape sequences in them could clear or fake the
|
|
13
|
+
* screen, retitle the window, add hidden links or write the clipboard (OSC 52). Keeps newlines,
|
|
14
|
+
* tabs and the CLI's own colours; other escape sequences are removed and other control characters
|
|
15
|
+
* shown as "�".
|
|
16
|
+
*/
|
|
17
|
+
export function terminalSafe(s) {
|
|
18
|
+
return s
|
|
19
|
+
.replace(
|
|
20
|
+
// OSC (titles, links, clipboard) and DCS/PM/APC strings up to BEL or ST; CSI; any other escape.
|
|
21
|
+
/\x1b[\]P^_][\s\S]*?(?:\x07|\x1b\\|$)|\x1b\[[0-?]*[ -/]*[@-~]|\x1b[@-Z\\-_]?/g, (seq) => (OWN_STYLES.has(seq) ? seq : ""))
|
|
22
|
+
.replace(/[\u0000-\u0008\u000b-\u001a\u001c-\u001f\u007f-\u009f]/g, "\ufffd");
|
|
23
|
+
}
|
|
8
24
|
export function num(n, digits = 0) {
|
|
9
25
|
if (n === null || n === undefined || !Number.isFinite(n))
|
|
10
26
|
return "–";
|
package/package.json
CHANGED