frontmost 0.0.0-stage → 0.6.2
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/LICENSE +21 -0
- package/README.md +177 -2
- package/bin/frontmost.js +18 -0
- package/openapi.json +4676 -0
- package/package.json +36 -4
- package/src/cli.js +363 -0
- package/src/config.js +114 -0
- package/src/errors.js +13 -0
- package/src/help.js +140 -0
- package/src/home.js +113 -0
- package/src/http.js +67 -0
- package/src/oauth.js +228 -0
- package/src/output.js +108 -0
- package/src/spec.js +141 -0
- package/src/style.js +102 -0
package/src/style.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// Color and marks for a terminal. Everything is plain text in a pipe, with NO_COLOR, and with TERM=dumb.
|
|
2
|
+
// The brand (wordmouth DESIGN.md): ink, warm grays, and one highlighter yellow. Yellow means "named you" and nothing else.
|
|
3
|
+
|
|
4
|
+
const YELLOW = { hex: [229, 245, 75], c256: 191, basic: 93 };
|
|
5
|
+
const YELLOW_ON_LIGHT = { hex: [168, 184, 0], c256: 142, basic: 33 };
|
|
6
|
+
const RED = { hex: [180, 35, 24], c256: 124, basic: 31 };
|
|
7
|
+
|
|
8
|
+
/** @typedef {{ on: boolean, truecolor?: boolean, light?: boolean }} Look */
|
|
9
|
+
|
|
10
|
+
/** @param {Look} [look] */
|
|
11
|
+
export function makeStyle({ on, truecolor = false, light = false } = { on: false }) {
|
|
12
|
+
/** @param {typeof YELLOW} c */
|
|
13
|
+
const fg = (c) => (truecolor ? `38;2;${c.hex.join(";")}` : `38;5;${c.c256}`);
|
|
14
|
+
/** @param {string} open @param {string} close @returns {(text: unknown) => string} */
|
|
15
|
+
const sgr = (open, close) => (text) => (on ? `\x1b[${open}m${text}\x1b[${close}m` : String(text));
|
|
16
|
+
const yellow = sgr(fg(light ? YELLOW_ON_LIGHT : YELLOW), "39");
|
|
17
|
+
const gray = sgr("90", "39");
|
|
18
|
+
return {
|
|
19
|
+
on,
|
|
20
|
+
bold: sgr("1", "22"),
|
|
21
|
+
dim: sgr("2", "22"),
|
|
22
|
+
gray,
|
|
23
|
+
/** Only for "named you": your dots, your row, your name. */
|
|
24
|
+
named: yellow,
|
|
25
|
+
/** The logo: a yellow quote mark, then the name. */
|
|
26
|
+
logo: () => `${yellow("”")} ${sgr("1", "22")("frontmost")}`,
|
|
27
|
+
/** The dot before an error message. */
|
|
28
|
+
errorDot: () => (on ? sgr(fg(RED), "39")("●") : "Error:"),
|
|
29
|
+
/**
|
|
30
|
+
* One dot for each answer: yellow when the answer named you, gray when it did not.
|
|
31
|
+
* Above `max` answers, each dot stands for an equal share of them.
|
|
32
|
+
* @param {number} named @param {number} total @param {number} [max]
|
|
33
|
+
*/
|
|
34
|
+
tally(named, total, max = 40) {
|
|
35
|
+
if (!(total > 0)) return "";
|
|
36
|
+
const dots = Math.min(total, max);
|
|
37
|
+
const lit = Math.max(0, Math.min(dots, Math.round((named / total) * dots)));
|
|
38
|
+
const rest = dots - lit;
|
|
39
|
+
return on ? yellow("●".repeat(lit)) + gray("●".repeat(rest)) : "●".repeat(lit) + "○".repeat(rest);
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** @typedef {ReturnType<typeof makeStyle>} Style */
|
|
45
|
+
export const PLAIN = makeStyle();
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The style for one output stream.
|
|
49
|
+
* @param {{ isTTY?: boolean }} stream @param {Record<string, string | undefined>} env
|
|
50
|
+
*/
|
|
51
|
+
export function styleFor(stream, env) {
|
|
52
|
+
const forced = env.FORCE_COLOR !== undefined && env.FORCE_COLOR !== "0";
|
|
53
|
+
const on = env.NO_COLOR ? false : forced || (Boolean(stream.isTTY) && env.TERM !== "dumb");
|
|
54
|
+
const truecolor = /^(truecolor|24bit)$/i.test(env.COLORTERM ?? "");
|
|
55
|
+
// COLORFGBG is "foreground;background". A background of 7 or 15 is a light theme.
|
|
56
|
+
const light = /;(7|15)$/.test(env.COLORFGBG ?? "");
|
|
57
|
+
return makeStyle({ on, truecolor, light });
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The width of text as the terminal shows it, without style codes. @param {string} text */
|
|
61
|
+
export const visible = (text) => text.replace(/\x1b\[[0-9;]*m/g, "").length;
|
|
62
|
+
|
|
63
|
+
/** @param {string} text @param {number} width */
|
|
64
|
+
export const padEnd = (text, width) => text + " ".repeat(Math.max(0, width - visible(text)));
|
|
65
|
+
/** @param {string} text @param {number} width */
|
|
66
|
+
export const padStart = (text, width) => " ".repeat(Math.max(0, width - visible(text))) + text;
|
|
67
|
+
|
|
68
|
+
const FRAMES = ["●○○", "○●○", "○○●", "○●○"];
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Three dots that move while a request runs. They show after a short wait, so a fast reply shows nothing.
|
|
72
|
+
* @param {{ write: (text: string) => unknown }} stream @param {Style} style
|
|
73
|
+
*/
|
|
74
|
+
export function spinner(stream, style) {
|
|
75
|
+
let active = 0;
|
|
76
|
+
/** @type {NodeJS.Timeout | undefined} */ let timer;
|
|
77
|
+
let shown = false;
|
|
78
|
+
let frame = 0;
|
|
79
|
+
const draw = () => {
|
|
80
|
+
shown = true;
|
|
81
|
+
stream.write(`\r${style.gray(FRAMES[frame++ % FRAMES.length])}`);
|
|
82
|
+
};
|
|
83
|
+
const clear = () => {
|
|
84
|
+
clearTimeout(timer);
|
|
85
|
+
clearInterval(timer);
|
|
86
|
+
timer = undefined;
|
|
87
|
+
if (shown) stream.write("\r\x1b[2K");
|
|
88
|
+
shown = false;
|
|
89
|
+
};
|
|
90
|
+
return {
|
|
91
|
+
clear,
|
|
92
|
+
/** @template T @param {Promise<T>} work @returns {Promise<T>} */
|
|
93
|
+
async around(work) {
|
|
94
|
+
if (active++ === 0) timer = setTimeout(() => ((timer = setInterval(draw, 120)), draw()), 300);
|
|
95
|
+
try {
|
|
96
|
+
return await work;
|
|
97
|
+
} finally {
|
|
98
|
+
if (--active === 0) clear();
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
}
|