@hop-top/kit 0.4.0-alpha.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/CHANGELOG.md +86 -0
- package/LICENSE +25 -0
- package/README.md +189 -0
- package/dist/aim.d.ts +116 -0
- package/dist/aim.js +417 -0
- package/dist/alias.d.ts +73 -0
- package/dist/alias.js +207 -0
- package/dist/api.d.ts +62 -0
- package/dist/api.js +165 -0
- package/dist/auth.d.ts +26 -0
- package/dist/auth.js +41 -0
- package/dist/cli.d.ts +309 -0
- package/dist/cli.js +885 -0
- package/dist/config.d.ts +97 -0
- package/dist/config.js +79 -0
- package/dist/errcorrect.d.ts +37 -0
- package/dist/errcorrect.js +69 -0
- package/dist/llm.d.ts +188 -0
- package/dist/llm.js +356 -0
- package/dist/output.d.ts +213 -0
- package/dist/output.js +447 -0
- package/dist/progress.d.ts +30 -0
- package/dist/progress.js +56 -0
- package/dist/provenance.d.ts +24 -0
- package/dist/provenance.js +32 -0
- package/dist/routellm.d.ts +64 -0
- package/dist/routellm.js +258 -0
- package/dist/rpc.d.ts +25 -0
- package/dist/rpc.js +101 -0
- package/dist/safety.d.ts +24 -0
- package/dist/safety.js +44 -0
- package/dist/scope.d.ts +121 -0
- package/dist/scope.js +380 -0
- package/dist/sqlstore.d.ts +84 -0
- package/dist/sqlstore.js +85 -0
- package/dist/stream.d.ts +35 -0
- package/dist/stream.js +52 -0
- package/dist/tui/index.d.ts +300 -0
- package/dist/tui/index.js +332 -0
- package/dist/upgrade.d.ts +95 -0
- package/dist/upgrade.js +115 -0
- package/dist/xdg.d.ts +169 -0
- package/dist/xdg.js +129 -0
- package/eslint.config.mjs +17 -0
- package/package.json +133 -0
- package/src/README.md +3 -0
- package/src/aim.test.ts +331 -0
- package/src/aim.ts +388 -0
- package/src/alias.test.ts +423 -0
- package/src/alias.ts +256 -0
- package/src/api.test.ts +190 -0
- package/src/api.ts +214 -0
- package/src/auth.test.ts +24 -0
- package/src/auth.ts +37 -0
- package/src/bus.test.ts +178 -0
- package/src/bus.ts +114 -0
- package/src/cli.test.ts +690 -0
- package/src/cli.ts +741 -0
- package/src/completion-values.test.ts +202 -0
- package/src/completion-values.ts +173 -0
- package/src/completion.test.ts +212 -0
- package/src/completion.ts +158 -0
- package/src/compliance.test.ts +134 -0
- package/src/compliance.ts +669 -0
- package/src/config.test.ts +110 -0
- package/src/config.ts +164 -0
- package/src/errcorrect.test.ts +134 -0
- package/src/errcorrect.ts +84 -0
- package/src/flagregister.test.ts +103 -0
- package/src/flagregister.ts +78 -0
- package/src/gen/crud_connect.ts +76 -0
- package/src/gen/crud_pb.ts +269 -0
- package/src/hint.test.ts +315 -0
- package/src/hint.ts +178 -0
- package/src/llm.test.ts +612 -0
- package/src/llm.ts +577 -0
- package/src/log.test.ts +145 -0
- package/src/log.ts +155 -0
- package/src/output/builtins.ts +34 -0
- package/src/output/dispatch.ts +195 -0
- package/src/output/flags.ts +151 -0
- package/src/output/format_help.ts +80 -0
- package/src/output/formatter.ts +167 -0
- package/src/output/formatters/csv.ts +72 -0
- package/src/output/formatters/json.ts +44 -0
- package/src/output/formatters/table.ts +44 -0
- package/src/output/formatters/text.ts +109 -0
- package/src/output/formatters/yaml.ts +47 -0
- package/src/output/projection.ts +126 -0
- package/src/output/registry.ts +88 -0
- package/src/output/template.ts +63 -0
- package/src/output.test.ts +117 -0
- package/src/output.ts +90 -0
- package/src/progress.test.ts +93 -0
- package/src/progress.ts +58 -0
- package/src/provenance.test.ts +59 -0
- package/src/provenance.ts +26 -0
- package/src/routellm.test.ts +179 -0
- package/src/routellm.ts +365 -0
- package/src/router/bert.test.ts +180 -0
- package/src/router/bert.ts +145 -0
- package/src/router/controller.test.ts +183 -0
- package/src/router/controller.ts +165 -0
- package/src/router/integration.test.ts +309 -0
- package/src/router/intent.test.ts +187 -0
- package/src/router/intent.ts +208 -0
- package/src/router/mf.test.ts +66 -0
- package/src/router/mf.ts +52 -0
- package/src/router/random.test.ts +23 -0
- package/src/router/random.ts +13 -0
- package/src/router/router.test.ts +31 -0
- package/src/router/router.ts +62 -0
- package/src/router/server.test.ts +193 -0
- package/src/router/server.ts +134 -0
- package/src/router/sw.test.ts +199 -0
- package/src/router/sw.ts +232 -0
- package/src/rpc.test.ts +47 -0
- package/src/rpc.ts +91 -0
- package/src/safety.test.ts +53 -0
- package/src/safety.ts +42 -0
- package/src/scope-defaults.json +72 -0
- package/src/scope.test.ts +177 -0
- package/src/scope.ts +382 -0
- package/src/setflag.test.ts +110 -0
- package/src/setflag.ts +102 -0
- package/src/sqlstore.test.ts +110 -0
- package/src/sqlstore.ts +144 -0
- package/src/stream.test.ts +69 -0
- package/src/stream.ts +47 -0
- package/src/textflag.test.ts +105 -0
- package/src/textflag.ts +68 -0
- package/src/toolspec.test.ts +187 -0
- package/src/toolspec.ts +279 -0
- package/src/triton/client.test.ts +205 -0
- package/src/triton/client.ts +261 -0
- package/src/tui/anim.test.ts +146 -0
- package/src/tui/anim.ts +134 -0
- package/src/tui/badge.test.ts +41 -0
- package/src/tui/badge.ts +55 -0
- package/src/tui/confirm.test.ts +68 -0
- package/src/tui/confirm.ts +49 -0
- package/src/tui/index.ts +29 -0
- package/src/tui/list.test.ts +84 -0
- package/src/tui/list.ts +72 -0
- package/src/tui/parity.ts +41 -0
- package/src/tui/pills.test.ts +40 -0
- package/src/tui/pills.ts +43 -0
- package/src/tui/progress.test.ts +164 -0
- package/src/tui/progress.ts +120 -0
- package/src/tui/spinner.test.ts +111 -0
- package/src/tui/spinner.ts +96 -0
- package/src/tui/status.test.ts +68 -0
- package/src/tui/status.ts +66 -0
- package/src/upgrade.test.ts +298 -0
- package/src/upgrade.ts +205 -0
- package/src/uri.ts +199 -0
- package/src/wizard.test.ts +441 -0
- package/src/wizard.ts +662 -0
- package/src/xdg.test.ts +202 -0
- package/src/xdg.ts +280 -0
- package/test/output/cols.test.ts +148 -0
- package/test/output/csv.test.ts +129 -0
- package/test/output/dispatch.test.ts +194 -0
- package/test/output/format_help.test.ts +131 -0
- package/test/output/formatter.test.ts +137 -0
- package/test/output/output_flag.test.ts +124 -0
- package/test/output/registry.test.ts +127 -0
- package/test/output/smoke-capture.ts +83 -0
- package/test/output/smoke.test.ts +142 -0
- package/test/output/template.test.ts +95 -0
- package/test/output/text.test.ts +75 -0
- package/test/testdata/api-fixture.json +115 -0
- package/test/testdata/query-vectors.json +187 -0
- package/tsconfig.json +12 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module tui/confirm
|
|
3
|
+
* @package @hop-top/kit
|
|
4
|
+
*
|
|
5
|
+
* Interactive yes/no confirmation prompt.
|
|
6
|
+
* Wraps @clack/prompts confirm(); throws on cancel (Ctrl-C).
|
|
7
|
+
* Mirrors Go's NewConfirm from hop.top/kit/tui.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { confirm as clackConfirm, isCancel } from '@clack/prompts';
|
|
11
|
+
import type { Theme } from '../cli.js';
|
|
12
|
+
|
|
13
|
+
export interface ConfirmOpts {
|
|
14
|
+
/** Default selection when user presses Enter without input. */
|
|
15
|
+
initial?: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Prompt the user with a yes/no question.
|
|
20
|
+
*
|
|
21
|
+
* @param _theme - Active CLI theme (reserved for future accent styling).
|
|
22
|
+
* @param message - Question to display.
|
|
23
|
+
* @param opts - Optional config; `initial` sets the default selection.
|
|
24
|
+
* @returns `true` if accepted, `false` if denied.
|
|
25
|
+
* @throws Error('cancelled') if user presses Ctrl-C.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { buildTheme } from '@hop-top/kit/cli';
|
|
30
|
+
* import { confirm } from '@hop-top/kit/tui';
|
|
31
|
+
* const ok = await confirm(buildTheme(), 'Deploy now?');
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export async function confirm(
|
|
35
|
+
_theme: Theme,
|
|
36
|
+
message: string,
|
|
37
|
+
opts?: ConfirmOpts,
|
|
38
|
+
): Promise<boolean> {
|
|
39
|
+
const result = await clackConfirm({
|
|
40
|
+
message,
|
|
41
|
+
initialValue: opts?.initial,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
if (isCancel(result)) {
|
|
45
|
+
throw new Error('cancelled');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return result;
|
|
49
|
+
}
|
package/src/tui/index.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module tui
|
|
3
|
+
* @package @hop-top/kit
|
|
4
|
+
*
|
|
5
|
+
* Barrel re-export of TUI display components.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export { badge } from './badge.js';
|
|
9
|
+
export type { BadgeOpts } from './badge.js';
|
|
10
|
+
|
|
11
|
+
export { status } from './status.js';
|
|
12
|
+
export type { StatusKind } from './status.js';
|
|
13
|
+
|
|
14
|
+
export { pills } from './pills.js';
|
|
15
|
+
|
|
16
|
+
export { spinner } from './spinner.js';
|
|
17
|
+
export type { Spinner } from './spinner.js';
|
|
18
|
+
|
|
19
|
+
export { progress, clamp01, renderBarPlain } from './progress.js';
|
|
20
|
+
export type { Progress } from './progress.js';
|
|
21
|
+
|
|
22
|
+
export { anim, makeGradient } from './anim.js';
|
|
23
|
+
export type { Anim, AnimOpts } from './anim.js';
|
|
24
|
+
|
|
25
|
+
export { confirm } from './confirm.js';
|
|
26
|
+
export type { ConfirmOpts } from './confirm.js';
|
|
27
|
+
|
|
28
|
+
export { list } from './list.js';
|
|
29
|
+
export type { ListItem, ListOpts } from './list.js';
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module tui/list.test
|
|
3
|
+
* Tests for the list() interactive prompt wrapper.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
7
|
+
import { buildTheme } from '../cli.js';
|
|
8
|
+
|
|
9
|
+
// Mock @clack/prompts before importing list.
|
|
10
|
+
vi.mock('@clack/prompts', () => ({
|
|
11
|
+
select: vi.fn(),
|
|
12
|
+
isCancel: vi.fn(),
|
|
13
|
+
}));
|
|
14
|
+
|
|
15
|
+
import * as clack from '@clack/prompts';
|
|
16
|
+
import { list } from './list.js';
|
|
17
|
+
import type { ListItem } from './list.js';
|
|
18
|
+
|
|
19
|
+
const theme = buildTheme();
|
|
20
|
+
|
|
21
|
+
const items: ListItem[] = [
|
|
22
|
+
{ label: 'Production', value: 'prod' },
|
|
23
|
+
{ label: 'Staging', value: 'staging', hint: 'safer' },
|
|
24
|
+
{ label: 'Dev', value: 'dev' },
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
describe('list', () => {
|
|
28
|
+
beforeEach(() => {
|
|
29
|
+
vi.clearAllMocks();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('returns the selected value', async () => {
|
|
33
|
+
vi.mocked(clack.select).mockResolvedValue('staging');
|
|
34
|
+
vi.mocked(clack.isCancel).mockReturnValue(false);
|
|
35
|
+
|
|
36
|
+
const result = await list(theme, 'Pick environment', items);
|
|
37
|
+
expect(result).toBe('staging');
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('throws Error("cancelled") when user cancels (Ctrl-C)', async () => {
|
|
41
|
+
const cancelSymbol = Symbol('cancel');
|
|
42
|
+
vi.mocked(clack.select).mockResolvedValue(cancelSymbol as unknown as string);
|
|
43
|
+
vi.mocked(clack.isCancel).mockReturnValue(true);
|
|
44
|
+
|
|
45
|
+
await expect(list(theme, 'Pick environment', items)).rejects.toThrow('cancelled');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('passes items correctly to clack select', async () => {
|
|
49
|
+
vi.mocked(clack.select).mockResolvedValue('prod');
|
|
50
|
+
vi.mocked(clack.isCancel).mockReturnValue(false);
|
|
51
|
+
|
|
52
|
+
await list(theme, 'Pick environment', items);
|
|
53
|
+
|
|
54
|
+
expect(clack.select).toHaveBeenCalledWith(
|
|
55
|
+
expect.objectContaining({
|
|
56
|
+
options: [
|
|
57
|
+
{ label: 'Production', value: 'prod', hint: undefined },
|
|
58
|
+
{ label: 'Staging', value: 'staging', hint: 'safer' },
|
|
59
|
+
{ label: 'Dev', value: 'dev', hint: undefined },
|
|
60
|
+
],
|
|
61
|
+
}),
|
|
62
|
+
);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('forwards message to clack select', async () => {
|
|
66
|
+
vi.mocked(clack.select).mockResolvedValue('dev');
|
|
67
|
+
vi.mocked(clack.isCancel).mockReturnValue(false);
|
|
68
|
+
|
|
69
|
+
await list(theme, 'Choose target', items);
|
|
70
|
+
expect(clack.select).toHaveBeenCalledWith(
|
|
71
|
+
expect.objectContaining({ message: 'Choose target' }),
|
|
72
|
+
);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('forwards initial option to clack select', async () => {
|
|
76
|
+
vi.mocked(clack.select).mockResolvedValue('staging');
|
|
77
|
+
vi.mocked(clack.isCancel).mockReturnValue(false);
|
|
78
|
+
|
|
79
|
+
await list(theme, 'Pick environment', items, { initial: 'staging' });
|
|
80
|
+
expect(clack.select).toHaveBeenCalledWith(
|
|
81
|
+
expect.objectContaining({ initialValue: 'staging' }),
|
|
82
|
+
);
|
|
83
|
+
});
|
|
84
|
+
});
|
package/src/tui/list.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module tui/list
|
|
3
|
+
* @package @hop-top/kit
|
|
4
|
+
*
|
|
5
|
+
* Interactive selection list prompt.
|
|
6
|
+
* Wraps @clack/prompts select(); throws on cancel (Ctrl-C).
|
|
7
|
+
* Mirrors Go's NewList from hop.top/kit/tui (selection variant).
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { select, isCancel } from '@clack/prompts';
|
|
11
|
+
import type { Theme } from '../cli.js';
|
|
12
|
+
|
|
13
|
+
export interface ListItem {
|
|
14
|
+
/** Display label shown in the prompt. */
|
|
15
|
+
label: string;
|
|
16
|
+
/** Value returned when this item is selected. */
|
|
17
|
+
value: string;
|
|
18
|
+
/** Optional hint shown alongside the label. */
|
|
19
|
+
hint?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface ListOpts {
|
|
23
|
+
/** Value of the initially highlighted item. */
|
|
24
|
+
initial?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Prompt the user to pick one item from a list.
|
|
29
|
+
*
|
|
30
|
+
* @param _theme - Active CLI theme (reserved for future accent styling).
|
|
31
|
+
* @param message - Prompt label shown above the list.
|
|
32
|
+
* @param items - Selectable options.
|
|
33
|
+
* @param opts - Optional config; `initial` sets the pre-selected value.
|
|
34
|
+
* @returns The `value` of the selected item.
|
|
35
|
+
* @throws Error('cancelled') if user presses Ctrl-C.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* import { buildTheme } from '@hop-top/kit/cli';
|
|
40
|
+
* import { list } from '@hop-top/kit/tui';
|
|
41
|
+
* const env = await list(buildTheme(), 'Pick environment', [
|
|
42
|
+
* { label: 'Production', value: 'prod' },
|
|
43
|
+
* { label: 'Staging', value: 'staging', hint: 'default' },
|
|
44
|
+
* ]);
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export async function list(
|
|
48
|
+
_theme: Theme,
|
|
49
|
+
message: string,
|
|
50
|
+
items: ListItem[],
|
|
51
|
+
opts?: ListOpts,
|
|
52
|
+
): Promise<string> {
|
|
53
|
+
const options = items.map((item) => ({
|
|
54
|
+
label: item.label,
|
|
55
|
+
value: item.value,
|
|
56
|
+
hint: item.hint,
|
|
57
|
+
}));
|
|
58
|
+
|
|
59
|
+
const initialValue = opts?.initial;
|
|
60
|
+
|
|
61
|
+
const result = await select({
|
|
62
|
+
message,
|
|
63
|
+
options,
|
|
64
|
+
initialValue,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
if (isCancel(result)) {
|
|
68
|
+
throw new Error('cancelled');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return result as string;
|
|
72
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module tui/parity
|
|
3
|
+
* @package @hop-top/kit
|
|
4
|
+
*
|
|
5
|
+
* Loads tui/parity.json — the cross-language parity constants SoT.
|
|
6
|
+
* All TUI modules should import constants from here, not hardcode them.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
10
|
+
import { resolve } from 'node:path';
|
|
11
|
+
|
|
12
|
+
interface ParityData {
|
|
13
|
+
status: {
|
|
14
|
+
symbols: Record<'info' | 'success' | 'error' | 'warn', string>;
|
|
15
|
+
};
|
|
16
|
+
spinner: {
|
|
17
|
+
frames: string[];
|
|
18
|
+
interval_ms: number;
|
|
19
|
+
};
|
|
20
|
+
anim: {
|
|
21
|
+
runes: string;
|
|
22
|
+
interval_ms: number;
|
|
23
|
+
default_width: number;
|
|
24
|
+
};
|
|
25
|
+
help: {
|
|
26
|
+
/** Fang-vocabulary section names in render order. */
|
|
27
|
+
section_order: string[];
|
|
28
|
+
/** Display metadata keyed by fang section name. */
|
|
29
|
+
sections: Record<string, { title: string }>;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Resolve from source, built dist, or current workspace checkout.
|
|
34
|
+
const _candidates = [
|
|
35
|
+
resolve(__dirname, '..', '..', '..', '..', 'contracts', 'parity', 'parity.json'),
|
|
36
|
+
resolve(__dirname, '..', '..', '..', '..', '..', 'contracts', 'parity', 'parity.json'),
|
|
37
|
+
resolve(process.cwd(), 'contracts', 'parity', 'parity.json'),
|
|
38
|
+
];
|
|
39
|
+
const _path = _candidates.find((path) => existsSync(path)) ?? _candidates[0];
|
|
40
|
+
|
|
41
|
+
export const parity: ParityData = JSON.parse(readFileSync(_path, 'utf8'));
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { buildTheme } from '../cli.js';
|
|
3
|
+
import { pills } from './pills.js';
|
|
4
|
+
|
|
5
|
+
const stripAnsi = (s: string) => s.replace(/\x1b\[[0-9;]*m/g, '');
|
|
6
|
+
|
|
7
|
+
describe('pills()', () => {
|
|
8
|
+
const theme = buildTheme();
|
|
9
|
+
|
|
10
|
+
it('returns empty string for empty array', () => {
|
|
11
|
+
expect(pills(theme, [])).toBe('');
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('renders single item with surrounding spaces', () => {
|
|
15
|
+
const out = stripAnsi(pills(theme, ['main']));
|
|
16
|
+
expect(out).toBe(' main ');
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('joins multiple items with a space separator', () => {
|
|
20
|
+
const out = stripAnsi(pills(theme, ['branch: main', 'env: prod']));
|
|
21
|
+
expect(out).toBe(' branch: main env: prod ');
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('returns ANSI-colored output', () => {
|
|
25
|
+
const out = pills(theme, ['test']);
|
|
26
|
+
expect(out).toContain('\x1b[');
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('uses theme.secondary color', () => {
|
|
30
|
+
// secondary = #FF00FF → rgb(255,0,255)
|
|
31
|
+
const out = pills(theme, ['item']);
|
|
32
|
+
expect(out).toContain('38;2;255;0;255');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('each pill ends with reset sequence', () => {
|
|
36
|
+
const out = pills(theme, ['a', 'b']);
|
|
37
|
+
const resets = (out.match(/\x1b\[0m/g) ?? []).length;
|
|
38
|
+
expect(resets).toBe(2);
|
|
39
|
+
});
|
|
40
|
+
});
|
package/src/tui/pills.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module tui/pills
|
|
3
|
+
* @package @hop-top/kit
|
|
4
|
+
*
|
|
5
|
+
* Renders a horizontal list of styled pills.
|
|
6
|
+
* Mirrors Go's Pill/PillBar concept from hop.top/kit/tui — pure string output.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { Theme } from '../cli.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Renders a space-separated row of styled pills.
|
|
13
|
+
*
|
|
14
|
+
* Each item is wrapped in `theme.secondary` foreground color with one space of
|
|
15
|
+
* padding on each side, mirroring lipgloss Padding(0, 1) on a blurred pill.
|
|
16
|
+
*
|
|
17
|
+
* @param theme - Active CLI theme.
|
|
18
|
+
* @param items - Labels to render as pills.
|
|
19
|
+
* @returns Space-joined ANSI-styled pill string. Empty string when no items.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { buildTheme } from '@hop-top/kit/cli';
|
|
24
|
+
* import { pills } from '@hop-top/kit/tui';
|
|
25
|
+
* console.log(pills(buildTheme(), ['branch: main', 'env: prod']));
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export function pills(theme: Theme, items: string[]): string {
|
|
29
|
+
if (items.length === 0) return '';
|
|
30
|
+
return items.map(item => renderPill(theme.secondary, item)).join(' ');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// ---------------------------------------------------------------------------
|
|
34
|
+
// Helpers
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
|
|
37
|
+
function renderPill(hex: string, text: string): string {
|
|
38
|
+
const h = hex.replace('#', '');
|
|
39
|
+
const r = parseInt(h.slice(0, 2), 16);
|
|
40
|
+
const g = parseInt(h.slice(2, 4), 16);
|
|
41
|
+
const b = parseInt(h.slice(4, 6), 16);
|
|
42
|
+
return `\x1b[38;2;${r};${g};${b}m ${text} \x1b[0m`;
|
|
43
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { describe, it, expect, vi, afterEach } from 'vitest';
|
|
2
|
+
import { buildTheme } from '../cli.js';
|
|
3
|
+
import { progress, clamp01, renderBarPlain } from './progress.js';
|
|
4
|
+
|
|
5
|
+
const stripAnsi = (s: string) => s.replace(/\x1b\[[0-9;]*m/g, '');
|
|
6
|
+
|
|
7
|
+
function captureStderr(): { output: string[]; restore: () => void } {
|
|
8
|
+
const output: string[] = [];
|
|
9
|
+
vi.spyOn(process.stderr, 'write').mockImplementation((chunk: unknown) => {
|
|
10
|
+
output.push(String(chunk));
|
|
11
|
+
return true;
|
|
12
|
+
});
|
|
13
|
+
return { output, restore: () => vi.restoreAllMocks() };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
describe('clamp01()', () => {
|
|
17
|
+
it('clamps below 0 to 0', () => expect(clamp01(-1)).toBe(0));
|
|
18
|
+
it('clamps above 1 to 1', () => expect(clamp01(2)).toBe(1));
|
|
19
|
+
it('passes through 0', () => expect(clamp01(0)).toBe(0));
|
|
20
|
+
it('passes through 1', () => expect(clamp01(1)).toBe(1));
|
|
21
|
+
it('passes through mid value', () => expect(clamp01(0.5)).toBe(0.5));
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
describe('renderBarPlain()', () => {
|
|
25
|
+
it('full bar at 1.0', () => {
|
|
26
|
+
const bar = renderBarPlain(1.0);
|
|
27
|
+
expect(bar).toBe('█'.repeat(30));
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('empty bar at 0.0', () => {
|
|
31
|
+
const bar = renderBarPlain(0.0);
|
|
32
|
+
expect(bar).toBe('░'.repeat(30));
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('half bar at 0.5', () => {
|
|
36
|
+
const bar = renderBarPlain(0.5);
|
|
37
|
+
expect(bar).toBe('█'.repeat(15) + '░'.repeat(15));
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('bar total width is always 30', () => {
|
|
41
|
+
for (const v of [0, 0.1, 0.33, 0.5, 0.75, 1]) {
|
|
42
|
+
const bar = renderBarPlain(v);
|
|
43
|
+
// Count runes (all are single-char here)
|
|
44
|
+
expect([...bar].length).toBe(30);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
describe('progress()', () => {
|
|
50
|
+
const theme = buildTheme();
|
|
51
|
+
|
|
52
|
+
afterEach(() => vi.restoreAllMocks());
|
|
53
|
+
|
|
54
|
+
it('start() renders bar to stderr', () => {
|
|
55
|
+
const { output, restore } = captureStderr();
|
|
56
|
+
const p = progress(theme);
|
|
57
|
+
p.start('loading');
|
|
58
|
+
expect(output.length).toBeGreaterThan(0);
|
|
59
|
+
const plain = stripAnsi(output.join(''));
|
|
60
|
+
expect(plain).toContain('loading');
|
|
61
|
+
restore();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('start() includes 0% indicator', () => {
|
|
65
|
+
const { output, restore } = captureStderr();
|
|
66
|
+
const p = progress(theme);
|
|
67
|
+
p.start();
|
|
68
|
+
const plain = stripAnsi(output.join(''));
|
|
69
|
+
expect(plain).toContain('0%');
|
|
70
|
+
restore();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('update() renders updated percentage', () => {
|
|
74
|
+
const { output, restore } = captureStderr();
|
|
75
|
+
const p = progress(theme);
|
|
76
|
+
p.start();
|
|
77
|
+
output.length = 0;
|
|
78
|
+
p.update(0.5);
|
|
79
|
+
const plain = stripAnsi(output.join(''));
|
|
80
|
+
expect(plain).toContain('50%');
|
|
81
|
+
restore();
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('update() clamps negative value to 0', () => {
|
|
85
|
+
const { output, restore } = captureStderr();
|
|
86
|
+
const p = progress(theme);
|
|
87
|
+
p.start();
|
|
88
|
+
output.length = 0;
|
|
89
|
+
p.update(-0.5);
|
|
90
|
+
const plain = stripAnsi(output.join(''));
|
|
91
|
+
expect(plain).toContain('0%');
|
|
92
|
+
restore();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('update() clamps value > 1 to 100%', () => {
|
|
96
|
+
const { output, restore } = captureStderr();
|
|
97
|
+
const p = progress(theme);
|
|
98
|
+
p.start();
|
|
99
|
+
output.length = 0;
|
|
100
|
+
p.update(1.5);
|
|
101
|
+
const plain = stripAnsi(output.join(''));
|
|
102
|
+
expect(plain).toContain('100%');
|
|
103
|
+
restore();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('update() includes optional message', () => {
|
|
107
|
+
const { output, restore } = captureStderr();
|
|
108
|
+
const p = progress(theme);
|
|
109
|
+
p.start();
|
|
110
|
+
output.length = 0;
|
|
111
|
+
p.update(0.3, 'fetching');
|
|
112
|
+
const plain = stripAnsi(output.join(''));
|
|
113
|
+
expect(plain).toContain('fetching');
|
|
114
|
+
restore();
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('stop() clears line', () => {
|
|
118
|
+
const { output, restore } = captureStderr();
|
|
119
|
+
const p = progress(theme);
|
|
120
|
+
p.start();
|
|
121
|
+
output.length = 0;
|
|
122
|
+
p.stop();
|
|
123
|
+
expect(output.join('')).toContain('\r');
|
|
124
|
+
restore();
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('stop() prints final message', () => {
|
|
128
|
+
const { output, restore } = captureStderr();
|
|
129
|
+
const p = progress(theme);
|
|
130
|
+
p.start();
|
|
131
|
+
output.length = 0;
|
|
132
|
+
p.stop('finished');
|
|
133
|
+
expect(output.join('')).toContain('finished');
|
|
134
|
+
restore();
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('renders accent color for filled chars', () => {
|
|
138
|
+
const { output, restore } = captureStderr();
|
|
139
|
+
const p = progress(theme);
|
|
140
|
+
p.start();
|
|
141
|
+
p.update(1.0);
|
|
142
|
+
// accent #7ED957 → 38;2;126;217;87
|
|
143
|
+
expect(output.join('')).toContain('38;2;126;217;87');
|
|
144
|
+
restore();
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('renders muted color for empty chars', () => {
|
|
148
|
+
const { output, restore } = captureStderr();
|
|
149
|
+
const p = progress(theme);
|
|
150
|
+
p.start();
|
|
151
|
+
p.update(0.0);
|
|
152
|
+
// muted #858183 → 38;2;133;129;131
|
|
153
|
+
expect(output.join('')).toContain('38;2;133;129;131');
|
|
154
|
+
restore();
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('accepts optional total parameter (ignored for ratio)', () => {
|
|
158
|
+
// Should not throw when total is provided.
|
|
159
|
+
const p = progress(theme, 100);
|
|
160
|
+
const { restore } = captureStderr();
|
|
161
|
+
expect(() => { p.start(); p.update(0.5); p.stop(); }).not.toThrow();
|
|
162
|
+
restore();
|
|
163
|
+
});
|
|
164
|
+
});
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module tui/progress
|
|
3
|
+
* @package @hop-top/kit
|
|
4
|
+
*
|
|
5
|
+
* Terminal progress bar component.
|
|
6
|
+
* Mirrors Go's Progress from hop.top/kit/tui — pure ANSI, no bubbletea dep.
|
|
7
|
+
*
|
|
8
|
+
* Renders a 30-char ASCII bar: filled chars in theme.accent, empty in theme.muted.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { Theme } from '../cli.js';
|
|
12
|
+
|
|
13
|
+
const BAR_WIDTH = 30;
|
|
14
|
+
const FILL_CHAR = '█';
|
|
15
|
+
const EMPTY_CHAR = '░';
|
|
16
|
+
|
|
17
|
+
export interface Progress {
|
|
18
|
+
/** Start the progress bar with optional message. */
|
|
19
|
+
start(msg?: string): void;
|
|
20
|
+
/** Update progress value (0.0–1.0) with optional message. */
|
|
21
|
+
update(value: number, msg?: string): void;
|
|
22
|
+
/** Stop the progress bar with optional final message. */
|
|
23
|
+
stop(msg?: string): void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Creates a terminal progress bar styled with theme colors.
|
|
28
|
+
*
|
|
29
|
+
* Fill chars rendered in theme.accent; empty track in theme.muted.
|
|
30
|
+
* Renders to stderr using \r to overwrite the current line.
|
|
31
|
+
*
|
|
32
|
+
* @param theme - Active CLI theme.
|
|
33
|
+
* @param total - Optional total units (unused — value is always 0.0–1.0 ratio).
|
|
34
|
+
* @returns Progress control object.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* import { buildTheme } from '@hop-top/kit/cli';
|
|
39
|
+
* import { progress } from '@hop-top/kit/tui';
|
|
40
|
+
* const p = progress(buildTheme());
|
|
41
|
+
* p.start('Downloading…');
|
|
42
|
+
* p.update(0.5, 'Half done');
|
|
43
|
+
* p.stop('Complete');
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export function progress(theme: Theme, _total?: number): Progress {
|
|
47
|
+
const [ar, ag, ab] = hexToRgb(theme.accent);
|
|
48
|
+
const [mr, mg, mb] = hexToRgb(theme.muted);
|
|
49
|
+
const accentOpen = `\x1b[38;2;${ar};${ag};${ab}m`;
|
|
50
|
+
const mutedOpen = `\x1b[38;2;${mr};${mg};${mb}m`;
|
|
51
|
+
const reset = `\x1b[0m`;
|
|
52
|
+
|
|
53
|
+
let currentValue = 0;
|
|
54
|
+
let currentMsg = '';
|
|
55
|
+
let running = false;
|
|
56
|
+
|
|
57
|
+
function renderBar(value: number): string {
|
|
58
|
+
const clamped = clamp01(value);
|
|
59
|
+
const filled = Math.round(clamped * BAR_WIDTH);
|
|
60
|
+
const empty = BAR_WIDTH - filled;
|
|
61
|
+
const bar =
|
|
62
|
+
accentOpen + FILL_CHAR.repeat(filled) + reset +
|
|
63
|
+
mutedOpen + EMPTY_CHAR.repeat(empty) + reset;
|
|
64
|
+
const pct = Math.round(clamped * 100);
|
|
65
|
+
return `\r${bar} ${pct}%${currentMsg ? ' ' + currentMsg : ''}`;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
start(msg = ''): void {
|
|
70
|
+
running = true;
|
|
71
|
+
currentMsg = msg;
|
|
72
|
+
currentValue = 0;
|
|
73
|
+
process.stderr.write(renderBar(0));
|
|
74
|
+
},
|
|
75
|
+
|
|
76
|
+
update(value: number, msg?: string): void {
|
|
77
|
+
currentValue = clamp01(value);
|
|
78
|
+
if (msg !== undefined) currentMsg = msg;
|
|
79
|
+
if (running) {
|
|
80
|
+
process.stderr.write(renderBar(currentValue));
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
|
|
84
|
+
stop(msg?: string): void {
|
|
85
|
+
running = false;
|
|
86
|
+
// Clear line.
|
|
87
|
+
process.stderr.write('\r\x1b[K');
|
|
88
|
+
if (msg !== undefined) {
|
|
89
|
+
process.stderr.write(`${msg}\n`);
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// ---------------------------------------------------------------------------
|
|
96
|
+
// Helpers
|
|
97
|
+
// ---------------------------------------------------------------------------
|
|
98
|
+
|
|
99
|
+
export function clamp01(v: number): number {
|
|
100
|
+
if (v < 0) return 0;
|
|
101
|
+
if (v > 1) return 1;
|
|
102
|
+
return v;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Render a plain bar string (no ANSI) for testing. */
|
|
106
|
+
export function renderBarPlain(value: number): string {
|
|
107
|
+
const clamped = clamp01(value);
|
|
108
|
+
const filled = Math.round(clamped * BAR_WIDTH);
|
|
109
|
+
const empty = BAR_WIDTH - filled;
|
|
110
|
+
return FILL_CHAR.repeat(filled) + EMPTY_CHAR.repeat(empty);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function hexToRgb(hex: string): [number, number, number] {
|
|
114
|
+
const h = hex.replace('#', '');
|
|
115
|
+
return [
|
|
116
|
+
parseInt(h.slice(0, 2), 16),
|
|
117
|
+
parseInt(h.slice(2, 4), 16),
|
|
118
|
+
parseInt(h.slice(4, 6), 16),
|
|
119
|
+
];
|
|
120
|
+
}
|