@space-lens/cli 0.2.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/README.md +83 -0
- package/dist/cli-config-AZ8GhaEx.mjs +41 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +75 -0
- package/dist/index.d.mts +113 -0
- package/dist/index.mjs +3 -0
- package/dist/model-BUAl_aPJ.mjs +132 -0
- package/dist/scanner-BwSuW3ks.mjs +27 -0
- package/dist/ui-BKn7TAnJ.mjs +223 -0
- package/package.json +49 -0
package/README.md
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# spacelens
|
|
2
|
+
|
|
3
|
+
OpenTUI-powered terminal viewer for Space Lens cleanup plans.
|
|
4
|
+
|
|
5
|
+
The command-line surface is defined with Effect CLI, and the startup workflow is
|
|
6
|
+
run as an Effect program before handing control to OpenTUI.
|
|
7
|
+
|
|
8
|
+
It has two modes:
|
|
9
|
+
|
|
10
|
+
- `scan`: inspect disk usage as a tree.
|
|
11
|
+
- `clean`: inspect cleanup candidates, select paths, and execute deletion after
|
|
12
|
+
confirmation.
|
|
13
|
+
|
|
14
|
+
## Runtime
|
|
15
|
+
|
|
16
|
+
OpenTUI `0.4.x` currently needs Bun for its native FFI runtime. Use the root
|
|
17
|
+
`yarn tui` script or run the app with `bun`.
|
|
18
|
+
|
|
19
|
+
Running the built CLI with `node` prints a runtime error instead of opening the
|
|
20
|
+
TUI.
|
|
21
|
+
|
|
22
|
+
## Run From The Repo Root
|
|
23
|
+
|
|
24
|
+
Use this for normal local scanning:
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
yarn tui ~/Dev --preset rust
|
|
28
|
+
yarn tui ~/Dev --preset node,gitignored --sort path
|
|
29
|
+
yarn tui . --preset rust
|
|
30
|
+
npx @space-lens/cli ~/Dev --preset rust
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
`yarn tui` intentionally runs `bun apps/tui/src/cli.ts` from the current working
|
|
34
|
+
directory, so relative paths like `.` mean the directory where you invoked the
|
|
35
|
+
command.
|
|
36
|
+
|
|
37
|
+
## Controls
|
|
38
|
+
|
|
39
|
+
```text
|
|
40
|
+
tab Switch scan/clean mode
|
|
41
|
+
j / down Move down
|
|
42
|
+
k / up Move up
|
|
43
|
+
space Toggle the active cleanup candidate in clean mode
|
|
44
|
+
a Select or unselect all visible cleanup candidates
|
|
45
|
+
x Ask to delete selected cleanup candidates
|
|
46
|
+
enter Confirm deletion after pressing x
|
|
47
|
+
esc Cancel deletion confirmation
|
|
48
|
+
q / ctrl+c Quit
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Deletion is only available from clean mode, only for selected rows, and only
|
|
52
|
+
after the `x` then `enter` confirmation flow. After deletion, the TUI refreshes
|
|
53
|
+
both the scan tree and cleanup plan.
|
|
54
|
+
|
|
55
|
+
## Run From The App Workspace
|
|
56
|
+
|
|
57
|
+
Useful while developing the TUI package itself:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
yarn workspace @space-lens/cli dev -- --preset rust .
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
This runs with `apps/tui` as the working directory, so `.` means `apps/tui`, not
|
|
64
|
+
the repository root.
|
|
65
|
+
|
|
66
|
+
## Options
|
|
67
|
+
|
|
68
|
+
```text
|
|
69
|
+
-p, --preset <name> Cleanup preset: node, rust, gitignored. Repeat or comma-separate.
|
|
70
|
+
--ignore-hidden Skip hidden paths while searching.
|
|
71
|
+
--sort <mode> Sort rows by size or path. Defaults to size.
|
|
72
|
+
-h, --help Show help.
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Press `Ctrl+C` to exit the TUI.
|
|
76
|
+
|
|
77
|
+
## Build And Verify
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
yarn workspace @space-lens/cli build
|
|
81
|
+
yarn workspace @space-lens/cli test
|
|
82
|
+
yarn workspace @space-lens/cli typecheck
|
|
83
|
+
```
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
//#region src/cli-config.ts
|
|
2
|
+
const PRESETS = [
|
|
3
|
+
"node",
|
|
4
|
+
"rust",
|
|
5
|
+
"gitignored"
|
|
6
|
+
];
|
|
7
|
+
const SORT_MODES = ["size", "path"];
|
|
8
|
+
function normalizeCliConfig(raw) {
|
|
9
|
+
return {
|
|
10
|
+
paths: raw.paths.length > 0 ? [...raw.paths] : ["."],
|
|
11
|
+
presets: parsePresetList(raw.presets),
|
|
12
|
+
ignoreHidden: raw.ignoreHidden,
|
|
13
|
+
sort: parseSortMode(raw.sort)
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function parsePresetList(raw) {
|
|
17
|
+
const seen = /* @__PURE__ */ new Set();
|
|
18
|
+
const presets = [];
|
|
19
|
+
for (const value of raw) for (const token of value.split(",")) {
|
|
20
|
+
const preset = token.trim();
|
|
21
|
+
if (preset.length === 0) continue;
|
|
22
|
+
if (!isPreset(preset)) throw new Error(`Unknown preset "${preset}". Use node, rust, or gitignored.`);
|
|
23
|
+
if (!seen.has(preset)) {
|
|
24
|
+
seen.add(preset);
|
|
25
|
+
presets.push(preset);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return presets;
|
|
29
|
+
}
|
|
30
|
+
function parseSortMode(raw) {
|
|
31
|
+
if (!isSortMode(raw)) throw new Error(`Unknown sort mode "${raw}". Use size or path.`);
|
|
32
|
+
return raw;
|
|
33
|
+
}
|
|
34
|
+
function isPreset(value) {
|
|
35
|
+
return PRESETS.includes(value);
|
|
36
|
+
}
|
|
37
|
+
function isSortMode(value) {
|
|
38
|
+
return SORT_MODES.includes(value);
|
|
39
|
+
}
|
|
40
|
+
//#endregion
|
|
41
|
+
export { parseSortMode as a, parsePresetList as i, SORT_MODES as n, normalizeCliConfig as r, PRESETS as t };
|
package/dist/cli.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
package/dist/cli.mjs
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
import { n as SORT_MODES, r as normalizeCliConfig } from "./cli-config-AZ8GhaEx.mjs";
|
|
3
|
+
import { Args, Command, Options } from "@effect/cli";
|
|
4
|
+
import * as NodeContext from "@effect/platform-node/NodeContext";
|
|
5
|
+
import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
|
|
6
|
+
import { Effect } from "effect";
|
|
7
|
+
//#region src/runtime.ts
|
|
8
|
+
function isBunRuntime(runtime = globalThis) {
|
|
9
|
+
return runtime.Bun !== void 0;
|
|
10
|
+
}
|
|
11
|
+
function assertSupportedRuntime(runtime = globalThis) {
|
|
12
|
+
if (!isBunRuntime(runtime)) throw new Error("OpenTUI currently requires Bun. Run this CLI with `bun` or `yarn tui`.");
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/cli-program.ts
|
|
16
|
+
const APP_VERSION = "0.2.0";
|
|
17
|
+
const commandConfig = {
|
|
18
|
+
paths: Args.text({ name: "path" }).pipe(Args.atLeast(0), Args.withDescription("Directory to scan. Repeat by passing multiple paths.")),
|
|
19
|
+
presets: Options.text("preset").pipe(Options.withAlias("p"), Options.repeated, Options.withDescription("Cleanup preset to include: node, rust, gitignored. Can be repeated or comma-separated.")),
|
|
20
|
+
ignoreHidden: Options.boolean("ignore-hidden").pipe(Options.withDescription("Skip hidden paths while scanning.")),
|
|
21
|
+
sort: Options.choice("sort", SORT_MODES).pipe(Options.withDefault("size"), Options.withDescription("Sort cleanup candidates by size or path."))
|
|
22
|
+
};
|
|
23
|
+
const spaceLensTuiCommand = Command.make("spacelens", commandConfig, (rawConfig) => Effect.gen(function* () {
|
|
24
|
+
yield* runTuiProgram(yield* Effect.try({
|
|
25
|
+
try: () => normalizeCliConfig(rawConfig),
|
|
26
|
+
catch: toError
|
|
27
|
+
}));
|
|
28
|
+
}).pipe(Effect.catchAll(reportCliError))).pipe(Command.withDescription("OpenTUI scanner and cleanup picker for Space Lens."));
|
|
29
|
+
const cli = Command.run(spaceLensTuiCommand, {
|
|
30
|
+
name: "Space Lens TUI",
|
|
31
|
+
version: APP_VERSION
|
|
32
|
+
});
|
|
33
|
+
function makeCliEffect(argv) {
|
|
34
|
+
return cli(argv).pipe(Effect.provide(NodeContext.layer));
|
|
35
|
+
}
|
|
36
|
+
function runCli(argv = process.argv) {
|
|
37
|
+
NodeRuntime.runMain(makeCliEffect(argv));
|
|
38
|
+
}
|
|
39
|
+
const runTuiProgram = Effect.fn("runTuiProgram")((options) => Effect.gen(function* () {
|
|
40
|
+
yield* Effect.try({
|
|
41
|
+
try: () => assertSupportedRuntime(),
|
|
42
|
+
catch: toError
|
|
43
|
+
});
|
|
44
|
+
const [{ executeCleanupEntries, loadSpaceLensData }, { runTui }] = yield* Effect.tryPromise({
|
|
45
|
+
try: () => Promise.all([import("./scanner-BwSuW3ks.mjs"), import("./ui-BKn7TAnJ.mjs")]),
|
|
46
|
+
catch: toError
|
|
47
|
+
});
|
|
48
|
+
const initialData = yield* Effect.try({
|
|
49
|
+
try: () => loadSpaceLensData(options),
|
|
50
|
+
catch: toError
|
|
51
|
+
});
|
|
52
|
+
yield* Effect.tryPromise({
|
|
53
|
+
try: () => runTui({
|
|
54
|
+
initialData,
|
|
55
|
+
sort: options.sort,
|
|
56
|
+
refreshData: () => loadSpaceLensData(options),
|
|
57
|
+
executeEntries: executeCleanupEntries
|
|
58
|
+
}),
|
|
59
|
+
catch: toError
|
|
60
|
+
});
|
|
61
|
+
}));
|
|
62
|
+
function reportCliError(error) {
|
|
63
|
+
return Effect.sync(() => {
|
|
64
|
+
process.stderr.write(`spacelens: ${error.message}\n`);
|
|
65
|
+
process.exitCode = 1;
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
function toError(error) {
|
|
69
|
+
return error instanceof Error ? error : new Error(String(error));
|
|
70
|
+
}
|
|
71
|
+
//#endregion
|
|
72
|
+
//#region src/cli.ts
|
|
73
|
+
runCli();
|
|
74
|
+
//#endregion
|
|
75
|
+
export {};
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
//#region src/model.d.ts
|
|
2
|
+
type Preset = 'node' | 'rust' | 'gitignored';
|
|
3
|
+
type SortMode = 'size' | 'path';
|
|
4
|
+
type TuiMode = 'scan' | 'clean';
|
|
5
|
+
interface CliOptions {
|
|
6
|
+
paths: string[];
|
|
7
|
+
presets: Preset[];
|
|
8
|
+
ignoreHidden: boolean;
|
|
9
|
+
sort: SortMode;
|
|
10
|
+
}
|
|
11
|
+
interface PlanEntry {
|
|
12
|
+
path: string;
|
|
13
|
+
size: number;
|
|
14
|
+
preset: string;
|
|
15
|
+
reason: string;
|
|
16
|
+
}
|
|
17
|
+
interface PlanLike {
|
|
18
|
+
entries: PlanEntry[];
|
|
19
|
+
totalSize: number;
|
|
20
|
+
errors: string[];
|
|
21
|
+
}
|
|
22
|
+
interface ViewRow extends PlanEntry {
|
|
23
|
+
index: number;
|
|
24
|
+
sizeLabel: string;
|
|
25
|
+
selected: boolean;
|
|
26
|
+
active: boolean;
|
|
27
|
+
}
|
|
28
|
+
interface DirectoryNode {
|
|
29
|
+
name: string;
|
|
30
|
+
path: string;
|
|
31
|
+
size: number;
|
|
32
|
+
children: DirectoryNode[];
|
|
33
|
+
depth: number;
|
|
34
|
+
ignored: boolean;
|
|
35
|
+
collapsed: boolean;
|
|
36
|
+
}
|
|
37
|
+
interface TreeRow {
|
|
38
|
+
index: number;
|
|
39
|
+
label: string;
|
|
40
|
+
path: string;
|
|
41
|
+
size: number;
|
|
42
|
+
sizeLabel: string;
|
|
43
|
+
depth: number;
|
|
44
|
+
ignored: boolean;
|
|
45
|
+
collapsed: boolean;
|
|
46
|
+
active: boolean;
|
|
47
|
+
}
|
|
48
|
+
interface CleanViewModel {
|
|
49
|
+
title: string;
|
|
50
|
+
summary: string;
|
|
51
|
+
rows: ViewRow[];
|
|
52
|
+
cursor: number;
|
|
53
|
+
errors: string[];
|
|
54
|
+
}
|
|
55
|
+
interface ScanViewModel {
|
|
56
|
+
title: string;
|
|
57
|
+
summary: string;
|
|
58
|
+
rows: TreeRow[];
|
|
59
|
+
cursor: number;
|
|
60
|
+
}
|
|
61
|
+
interface TuiState {
|
|
62
|
+
mode: TuiMode;
|
|
63
|
+
scanCursor: number;
|
|
64
|
+
cleanCursor: number;
|
|
65
|
+
selectedPaths: Set<string>;
|
|
66
|
+
confirmExecute: boolean;
|
|
67
|
+
status: string;
|
|
68
|
+
}
|
|
69
|
+
type TuiAction = {
|
|
70
|
+
type: 'switch-mode';
|
|
71
|
+
} | {
|
|
72
|
+
type: 'move';
|
|
73
|
+
delta: number;
|
|
74
|
+
rowCount: number;
|
|
75
|
+
} | {
|
|
76
|
+
type: 'toggle-selected';
|
|
77
|
+
path: string;
|
|
78
|
+
} | {
|
|
79
|
+
type: 'toggle-all';
|
|
80
|
+
paths: string[];
|
|
81
|
+
} | {
|
|
82
|
+
type: 'request-execute';
|
|
83
|
+
} | {
|
|
84
|
+
type: 'cancel-execute';
|
|
85
|
+
} | {
|
|
86
|
+
type: 'set-status';
|
|
87
|
+
status: string;
|
|
88
|
+
} | {
|
|
89
|
+
type: 'clear-selection';
|
|
90
|
+
};
|
|
91
|
+
declare function createInitialTuiState(): TuiState;
|
|
92
|
+
declare function createCleanViewModel(plan: PlanLike, options: Pick<CliOptions, 'sort'> & {
|
|
93
|
+
state: TuiState;
|
|
94
|
+
}): CleanViewModel;
|
|
95
|
+
declare function createScanViewModel(trees: DirectoryNode[], state: TuiState): ScanViewModel;
|
|
96
|
+
declare function applyTuiAction(state: TuiState, action: TuiAction): void;
|
|
97
|
+
declare function getSelectedEntries(state: Pick<TuiState, 'selectedPaths'>, entries: PlanEntry[]): PlanEntry[];
|
|
98
|
+
declare function formatBytes(input: number): string;
|
|
99
|
+
//#endregion
|
|
100
|
+
//#region src/cli-config.d.ts
|
|
101
|
+
declare const PRESETS: readonly ["node", "rust", "gitignored"];
|
|
102
|
+
declare const SORT_MODES: readonly ["size", "path"];
|
|
103
|
+
interface RawCliConfig {
|
|
104
|
+
readonly paths: readonly string[];
|
|
105
|
+
readonly presets: readonly string[];
|
|
106
|
+
readonly ignoreHidden: boolean;
|
|
107
|
+
readonly sort: string;
|
|
108
|
+
}
|
|
109
|
+
declare function normalizeCliConfig(raw: RawCliConfig): CliOptions;
|
|
110
|
+
declare function parsePresetList(raw: readonly string[]): Preset[];
|
|
111
|
+
declare function parseSortMode(raw: string): SortMode;
|
|
112
|
+
//#endregion
|
|
113
|
+
export { type CleanViewModel, type CliOptions, type DirectoryNode, PRESETS, type PlanEntry, type PlanLike, type Preset, SORT_MODES, type ScanViewModel, type SortMode, type TreeRow, type TuiAction, type TuiMode, type TuiState, type ViewRow, applyTuiAction, createCleanViewModel, createInitialTuiState, createScanViewModel, formatBytes, getSelectedEntries, normalizeCliConfig, parsePresetList, parseSortMode };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { a as formatBytes, i as createScanViewModel, n as createCleanViewModel, o as getSelectedEntries, r as createInitialTuiState, t as applyTuiAction } from "./model-BUAl_aPJ.mjs";
|
|
2
|
+
import { a as parseSortMode, i as parsePresetList, n as SORT_MODES, r as normalizeCliConfig, t as PRESETS } from "./cli-config-AZ8GhaEx.mjs";
|
|
3
|
+
export { PRESETS, SORT_MODES, applyTuiAction, createCleanViewModel, createInitialTuiState, createScanViewModel, formatBytes, getSelectedEntries, normalizeCliConfig, parsePresetList, parseSortMode };
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
//#region src/model.ts
|
|
2
|
+
function createInitialTuiState() {
|
|
3
|
+
return {
|
|
4
|
+
mode: "scan",
|
|
5
|
+
scanCursor: 0,
|
|
6
|
+
cleanCursor: 0,
|
|
7
|
+
selectedPaths: /* @__PURE__ */ new Set(),
|
|
8
|
+
confirmExecute: false,
|
|
9
|
+
status: ""
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
function createCleanViewModel(plan, options) {
|
|
13
|
+
const rows = [...plan.entries].sort(compareEntries(options.sort)).map((entry, index) => ({
|
|
14
|
+
...entry,
|
|
15
|
+
index: index + 1,
|
|
16
|
+
sizeLabel: formatBytes(entry.size),
|
|
17
|
+
selected: options.state.selectedPaths.has(entry.path),
|
|
18
|
+
active: index === clampCursor(options.state.cleanCursor, plan.entries.length)
|
|
19
|
+
}));
|
|
20
|
+
const noun = rows.length === 1 ? "candidate" : "candidates";
|
|
21
|
+
const selectedSize = rows.filter((row) => row.selected).reduce((total, row) => total + row.size, 0);
|
|
22
|
+
return {
|
|
23
|
+
title: "Space Lens",
|
|
24
|
+
summary: `${rows.length} ${noun} | ${formatBytes(plan.totalSize)} | selected ${formatBytes(selectedSize)}`,
|
|
25
|
+
rows,
|
|
26
|
+
cursor: clampCursor(options.state.cleanCursor, rows.length),
|
|
27
|
+
errors: plan.errors
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function createScanViewModel(trees, state) {
|
|
31
|
+
const rows = trees.flatMap((tree) => flattenTree(tree));
|
|
32
|
+
const totalSize = trees.reduce((total, tree) => total + tree.size, 0);
|
|
33
|
+
const cursor = clampCursor(state.scanCursor, rows.length);
|
|
34
|
+
return {
|
|
35
|
+
title: "Space Lens",
|
|
36
|
+
summary: `${rows.length} ${rows.length === 1 ? "node" : "nodes"} | ${formatBytes(totalSize)}`,
|
|
37
|
+
rows: rows.map((row, index) => ({
|
|
38
|
+
...row,
|
|
39
|
+
index: index + 1,
|
|
40
|
+
active: index === cursor
|
|
41
|
+
})),
|
|
42
|
+
cursor
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function applyTuiAction(state, action) {
|
|
46
|
+
switch (action.type) {
|
|
47
|
+
case "switch-mode":
|
|
48
|
+
state.mode = state.mode === "scan" ? "clean" : "scan";
|
|
49
|
+
state.confirmExecute = false;
|
|
50
|
+
return;
|
|
51
|
+
case "move":
|
|
52
|
+
if (state.mode === "scan") state.scanCursor = moveCursor(state.scanCursor, action.delta, action.rowCount);
|
|
53
|
+
else state.cleanCursor = moveCursor(state.cleanCursor, action.delta, action.rowCount);
|
|
54
|
+
state.confirmExecute = false;
|
|
55
|
+
return;
|
|
56
|
+
case "toggle-selected":
|
|
57
|
+
if (state.selectedPaths.has(action.path)) state.selectedPaths.delete(action.path);
|
|
58
|
+
else state.selectedPaths.add(action.path);
|
|
59
|
+
state.confirmExecute = false;
|
|
60
|
+
return;
|
|
61
|
+
case "toggle-all":
|
|
62
|
+
if (action.paths.length === 0) return;
|
|
63
|
+
if (action.paths.every((path) => state.selectedPaths.has(path))) for (const path of action.paths) state.selectedPaths.delete(path);
|
|
64
|
+
else for (const path of action.paths) state.selectedPaths.add(path);
|
|
65
|
+
state.confirmExecute = false;
|
|
66
|
+
return;
|
|
67
|
+
case "request-execute":
|
|
68
|
+
state.confirmExecute = true;
|
|
69
|
+
return;
|
|
70
|
+
case "cancel-execute":
|
|
71
|
+
state.confirmExecute = false;
|
|
72
|
+
return;
|
|
73
|
+
case "set-status":
|
|
74
|
+
state.status = action.status;
|
|
75
|
+
return;
|
|
76
|
+
case "clear-selection":
|
|
77
|
+
state.selectedPaths.clear();
|
|
78
|
+
state.confirmExecute = false;
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function getSelectedEntries(state, entries) {
|
|
83
|
+
return entries.filter((entry) => state.selectedPaths.has(entry.path));
|
|
84
|
+
}
|
|
85
|
+
function formatBytes(input) {
|
|
86
|
+
const units = [
|
|
87
|
+
"B",
|
|
88
|
+
"KiB",
|
|
89
|
+
"MiB",
|
|
90
|
+
"GiB",
|
|
91
|
+
"TiB",
|
|
92
|
+
"PiB",
|
|
93
|
+
"EiB"
|
|
94
|
+
];
|
|
95
|
+
const bytes = Math.max(0, Math.trunc(Number.isFinite(input) ? input : 0));
|
|
96
|
+
if (bytes < 1024) return `${bytes} B`;
|
|
97
|
+
let value = bytes;
|
|
98
|
+
let unit = 0;
|
|
99
|
+
while (value >= 1024 && unit < units.length - 1) {
|
|
100
|
+
value /= 1024;
|
|
101
|
+
unit += 1;
|
|
102
|
+
}
|
|
103
|
+
return `${value.toFixed(1)} ${units[unit]} (${bytes} bytes)`;
|
|
104
|
+
}
|
|
105
|
+
function compareEntries(sort) {
|
|
106
|
+
return (left, right) => {
|
|
107
|
+
if (sort === "path") return left.path.localeCompare(right.path);
|
|
108
|
+
return right.size - left.size || left.path.localeCompare(right.path);
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
function flattenTree(node, depth = 0) {
|
|
112
|
+
const suffix = [node.ignored ? "[ignored]" : "", node.collapsed ? "[collapsed]" : ""].filter(Boolean).join(" ");
|
|
113
|
+
return [{
|
|
114
|
+
label: `${" ".repeat(depth)}${node.name}${suffix ? ` ${suffix}` : ""}`,
|
|
115
|
+
path: node.path,
|
|
116
|
+
size: node.size,
|
|
117
|
+
sizeLabel: formatBytes(node.size),
|
|
118
|
+
depth,
|
|
119
|
+
ignored: node.ignored,
|
|
120
|
+
collapsed: node.collapsed
|
|
121
|
+
}, ...node.children.flatMap((child) => flattenTree(child, depth + 1))];
|
|
122
|
+
}
|
|
123
|
+
function moveCursor(cursor, delta, rowCount) {
|
|
124
|
+
if (rowCount <= 0) return 0;
|
|
125
|
+
return clampCursor(cursor + delta, rowCount);
|
|
126
|
+
}
|
|
127
|
+
function clampCursor(cursor, rowCount) {
|
|
128
|
+
if (rowCount <= 0) return 0;
|
|
129
|
+
return Math.max(0, Math.min(cursor, rowCount - 1));
|
|
130
|
+
}
|
|
131
|
+
//#endregion
|
|
132
|
+
export { formatBytes as a, createScanViewModel as i, createCleanViewModel as n, getSelectedEntries as o, createInitialTuiState as r, applyTuiAction as t };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { executeCleanup, planCleanup, scanDirectory } from "space-lens";
|
|
2
|
+
//#region src/scanner.ts
|
|
3
|
+
function loadSpaceLensData(options) {
|
|
4
|
+
return {
|
|
5
|
+
scanTrees: scanDirectory({
|
|
6
|
+
directories: options.paths,
|
|
7
|
+
ignoreHidden: options.ignoreHidden,
|
|
8
|
+
fullPath: false,
|
|
9
|
+
respectGitignore: true,
|
|
10
|
+
ignoredMode: "summarize"
|
|
11
|
+
}),
|
|
12
|
+
plan: planCleanup({
|
|
13
|
+
directories: options.paths,
|
|
14
|
+
presets: options.presets,
|
|
15
|
+
ignoreHidden: options.ignoreHidden
|
|
16
|
+
})
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function executeCleanupEntries(entries) {
|
|
20
|
+
return executeCleanup({
|
|
21
|
+
entries,
|
|
22
|
+
totalSize: entries.reduce((total, entry) => total + entry.size, 0),
|
|
23
|
+
errors: []
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
export { executeCleanupEntries, loadSpaceLensData };
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { a as formatBytes, i as createScanViewModel, n as createCleanViewModel, o as getSelectedEntries, r as createInitialTuiState, t as applyTuiAction } from "./model-BUAl_aPJ.mjs";
|
|
2
|
+
import { Box, ScrollBox, Text, createCliRenderer } from "@opentui/core";
|
|
3
|
+
//#region src/ui.ts
|
|
4
|
+
async function runTui(options) {
|
|
5
|
+
let data = options.initialData;
|
|
6
|
+
let executing = false;
|
|
7
|
+
const state = createInitialTuiState();
|
|
8
|
+
const renderer = await createCliRenderer({
|
|
9
|
+
exitOnCtrlC: true,
|
|
10
|
+
clearOnShutdown: true,
|
|
11
|
+
screenMode: "alternate-screen"
|
|
12
|
+
});
|
|
13
|
+
const render = () => {
|
|
14
|
+
clearRoot();
|
|
15
|
+
const scanView = createScanViewModel(data.scanTrees, state);
|
|
16
|
+
const cleanView = createCleanViewModel(data.plan, {
|
|
17
|
+
sort: options.sort,
|
|
18
|
+
state
|
|
19
|
+
});
|
|
20
|
+
const selected = getSelectedEntries(state, data.plan.entries);
|
|
21
|
+
const activeMode = state.mode === "scan" ? "SCAN" : "CLEAN";
|
|
22
|
+
renderer.root.add(Box({
|
|
23
|
+
flexDirection: "column",
|
|
24
|
+
gap: 1,
|
|
25
|
+
padding: 1,
|
|
26
|
+
width: "100%",
|
|
27
|
+
height: "100%"
|
|
28
|
+
}, Text({
|
|
29
|
+
content: `Space Lens | ${activeMode} | tab switch | j/k move | q quit`,
|
|
30
|
+
fg: "#7dd3fc",
|
|
31
|
+
height: 1,
|
|
32
|
+
truncate: true
|
|
33
|
+
}), Text({
|
|
34
|
+
content: state.mode === "scan" ? `${scanView.summary} | tree view` : `${cleanView.summary} | space select | a all | x delete`,
|
|
35
|
+
fg: "#d1d5db",
|
|
36
|
+
height: 1,
|
|
37
|
+
truncate: true
|
|
38
|
+
}), Text({
|
|
39
|
+
content: statusLine(selected, state.confirmExecute, executing, state.status),
|
|
40
|
+
fg: state.confirmExecute ? "#fca5a5" : "#9ca3af",
|
|
41
|
+
height: 1,
|
|
42
|
+
truncate: true
|
|
43
|
+
}), state.mode === "scan" ? renderScanPane(scanView.rows) : renderCleanPane(cleanView.rows, selected.length, state.confirmExecute), ...errorLines(cleanView.errors)));
|
|
44
|
+
};
|
|
45
|
+
const executeSelected = async () => {
|
|
46
|
+
const selected = getSelectedEntries(state, data.plan.entries);
|
|
47
|
+
if (selected.length === 0) {
|
|
48
|
+
applyTuiAction(state, {
|
|
49
|
+
type: "set-status",
|
|
50
|
+
status: "Nothing selected."
|
|
51
|
+
});
|
|
52
|
+
render();
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
executing = true;
|
|
56
|
+
applyTuiAction(state, {
|
|
57
|
+
type: "set-status",
|
|
58
|
+
status: `Removing ${selected.length} selected paths...`
|
|
59
|
+
});
|
|
60
|
+
render();
|
|
61
|
+
try {
|
|
62
|
+
const outcome = options.executeEntries(selected);
|
|
63
|
+
data = options.refreshData();
|
|
64
|
+
applyTuiAction(state, { type: "clear-selection" });
|
|
65
|
+
applyTuiAction(state, {
|
|
66
|
+
type: "set-status",
|
|
67
|
+
status: removalStatus(outcome)
|
|
68
|
+
});
|
|
69
|
+
} catch (error) {
|
|
70
|
+
applyTuiAction(state, {
|
|
71
|
+
type: "set-status",
|
|
72
|
+
status: `Delete failed: ${error instanceof Error ? error.message : String(error)}`
|
|
73
|
+
});
|
|
74
|
+
} finally {
|
|
75
|
+
executing = false;
|
|
76
|
+
render();
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
const handleInput = (sequence) => {
|
|
80
|
+
if (sequence === "") return false;
|
|
81
|
+
if (executing) return true;
|
|
82
|
+
const scanRows = createScanViewModel(data.scanTrees, state).rows.length;
|
|
83
|
+
const cleanRows = createCleanViewModel(data.plan, {
|
|
84
|
+
sort: options.sort,
|
|
85
|
+
state
|
|
86
|
+
}).rows;
|
|
87
|
+
if (!handleSequence(sequence, cleanRows)) return false;
|
|
88
|
+
if (sequence === "\r" || sequence === "\n") {
|
|
89
|
+
if (state.confirmExecute) {
|
|
90
|
+
executeSelected();
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (sequence === "j" || sequence === "\x1B[B") applyTuiAction(state, {
|
|
95
|
+
type: "move",
|
|
96
|
+
delta: 1,
|
|
97
|
+
rowCount: state.mode === "scan" ? scanRows : cleanRows.length
|
|
98
|
+
});
|
|
99
|
+
else if (sequence === "k" || sequence === "\x1B[A") applyTuiAction(state, {
|
|
100
|
+
type: "move",
|
|
101
|
+
delta: -1,
|
|
102
|
+
rowCount: state.mode === "scan" ? scanRows : cleanRows.length
|
|
103
|
+
});
|
|
104
|
+
render();
|
|
105
|
+
return true;
|
|
106
|
+
};
|
|
107
|
+
const handleSequence = (sequence, cleanRows) => {
|
|
108
|
+
switch (sequence) {
|
|
109
|
+
case "q":
|
|
110
|
+
renderer.destroy();
|
|
111
|
+
return true;
|
|
112
|
+
case " ":
|
|
113
|
+
applyTuiAction(state, { type: "switch-mode" });
|
|
114
|
+
return true;
|
|
115
|
+
case "j":
|
|
116
|
+
case "k":
|
|
117
|
+
case "\x1B[A":
|
|
118
|
+
case "\x1B[B": return true;
|
|
119
|
+
case " ":
|
|
120
|
+
if (state.mode === "clean") {
|
|
121
|
+
const active = cleanRows[state.cleanCursor];
|
|
122
|
+
if (active) applyTuiAction(state, {
|
|
123
|
+
type: "toggle-selected",
|
|
124
|
+
path: active.path
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
return true;
|
|
128
|
+
case "a":
|
|
129
|
+
if (state.mode === "clean") applyTuiAction(state, {
|
|
130
|
+
type: "toggle-all",
|
|
131
|
+
paths: cleanRows.map((row) => row.path)
|
|
132
|
+
});
|
|
133
|
+
return true;
|
|
134
|
+
case "x":
|
|
135
|
+
if (state.mode === "clean") applyTuiAction(state, { type: "request-execute" });
|
|
136
|
+
return true;
|
|
137
|
+
case "\r":
|
|
138
|
+
case "\n": return true;
|
|
139
|
+
case "\x1B":
|
|
140
|
+
applyTuiAction(state, { type: "cancel-execute" });
|
|
141
|
+
return true;
|
|
142
|
+
default: return false;
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
const clearRoot = () => {
|
|
146
|
+
for (const child of [...renderer.root.getChildren()]) renderer.root.remove(child.id);
|
|
147
|
+
};
|
|
148
|
+
renderer.addInputHandler(handleInput);
|
|
149
|
+
render();
|
|
150
|
+
}
|
|
151
|
+
function renderScanPane(rows) {
|
|
152
|
+
return ScrollBox({
|
|
153
|
+
border: true,
|
|
154
|
+
borderStyle: "rounded",
|
|
155
|
+
borderColor: "#374151",
|
|
156
|
+
flexGrow: 1,
|
|
157
|
+
padding: 1,
|
|
158
|
+
scrollY: true,
|
|
159
|
+
title: "scan tree"
|
|
160
|
+
}, ...rows.map((row) => Text({
|
|
161
|
+
content: `${row.active ? ">" : " "} ${row.sizeLabel.padEnd(24, " ")} ${row.label}`,
|
|
162
|
+
fg: row.active ? "#bfdbfe" : row.ignored ? "#fde68a" : "#e5e7eb",
|
|
163
|
+
height: 1,
|
|
164
|
+
truncate: true
|
|
165
|
+
})), ...emptyState(rows.length, "No scan results."));
|
|
166
|
+
}
|
|
167
|
+
function renderCleanPane(rows, selectedCount, confirmExecute) {
|
|
168
|
+
return ScrollBox({
|
|
169
|
+
border: true,
|
|
170
|
+
borderStyle: "rounded",
|
|
171
|
+
borderColor: confirmExecute ? "#991b1b" : "#374151",
|
|
172
|
+
flexGrow: 1,
|
|
173
|
+
padding: 1,
|
|
174
|
+
scrollY: true,
|
|
175
|
+
title: selectedCount > 0 ? `cleanup candidates (${selectedCount} selected)` : "cleanup candidates"
|
|
176
|
+
}, ...rows.map((row) => Text({
|
|
177
|
+
content: `${row.active ? ">" : " "} ${row.selected ? "[x]" : "[ ]"} ${row.sizeLabel.padEnd(24, " ")} ${row.preset.padEnd(10, " ")} ${row.path} ${row.reason}`,
|
|
178
|
+
fg: row.active ? "#bfdbfe" : colorForPreset(row.preset),
|
|
179
|
+
height: 1,
|
|
180
|
+
truncate: true
|
|
181
|
+
})), ...emptyState(rows.length, "No cleanup candidates found."));
|
|
182
|
+
}
|
|
183
|
+
function statusLine(selected, confirmExecute, executing, status) {
|
|
184
|
+
if (executing) return status;
|
|
185
|
+
if (confirmExecute) {
|
|
186
|
+
const size = selected.reduce((total, entry) => total + entry.size, 0);
|
|
187
|
+
return `Confirm delete ${selected.length} paths (${formatBytes(size)}): press enter to execute, esc to cancel.`;
|
|
188
|
+
}
|
|
189
|
+
if (status) return status;
|
|
190
|
+
return "Scan mode shows disk tree. Clean mode lets you select candidates and execute deletion.";
|
|
191
|
+
}
|
|
192
|
+
function removalStatus(outcome) {
|
|
193
|
+
const base = `Removed ${outcome.removed.length} paths, ${formatBytes(outcome.bytesRemoved)}.`;
|
|
194
|
+
if (outcome.errors.length === 0) return base;
|
|
195
|
+
return `${base} ${outcome.errors.length} errors.`;
|
|
196
|
+
}
|
|
197
|
+
function colorForPreset(preset) {
|
|
198
|
+
switch (preset) {
|
|
199
|
+
case "node": return "#86efac";
|
|
200
|
+
case "rust": return "#fca5a5";
|
|
201
|
+
case "gitignored": return "#fde68a";
|
|
202
|
+
default: return "#e5e7eb";
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
function emptyState(rows, message) {
|
|
206
|
+
if (rows > 0) return [];
|
|
207
|
+
return [Text({
|
|
208
|
+
content: message,
|
|
209
|
+
fg: "#9ca3af",
|
|
210
|
+
height: 1
|
|
211
|
+
})];
|
|
212
|
+
}
|
|
213
|
+
function errorLines(errors) {
|
|
214
|
+
if (errors.length === 0) return [];
|
|
215
|
+
return [Text({
|
|
216
|
+
content: `Warnings: ${errors.join("; ")}`,
|
|
217
|
+
fg: "#fbbf24",
|
|
218
|
+
height: 1,
|
|
219
|
+
truncate: true
|
|
220
|
+
})];
|
|
221
|
+
}
|
|
222
|
+
//#endregion
|
|
223
|
+
export { runTui };
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@space-lens/cli",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.2.0",
|
|
5
|
+
"description": "OpenTUI-powered CLI for Space Lens scanning and cleanup.",
|
|
6
|
+
"bin": {
|
|
7
|
+
"spacelens": "dist/cli.mjs"
|
|
8
|
+
},
|
|
9
|
+
"engines": {
|
|
10
|
+
"bun": ">=1.3.0"
|
|
11
|
+
},
|
|
12
|
+
"exports": {
|
|
13
|
+
".": "./dist/index.mjs",
|
|
14
|
+
"./cli": "./dist/cli.mjs",
|
|
15
|
+
"./package.json": "./package.json"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"registry": "https://registry.npmjs.org/",
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsdown",
|
|
26
|
+
"dev": "bun src/cli.ts",
|
|
27
|
+
"lint": "tsc --noEmit",
|
|
28
|
+
"test": "node --import tsx --test \"src/**/*.test.ts\"",
|
|
29
|
+
"typecheck": "tsc --noEmit"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@effect/cli": "^0.75.2",
|
|
33
|
+
"@effect/platform": "^0.96.1",
|
|
34
|
+
"@effect/platform-node": "^0.107.0",
|
|
35
|
+
"@effect/printer": "^0.49.0",
|
|
36
|
+
"@effect/printer-ansi": "^0.49.0",
|
|
37
|
+
"@effect/typeclass": "^0.40.0",
|
|
38
|
+
"@opentui/core": "^0.4.1",
|
|
39
|
+
"effect": "^3.21.3",
|
|
40
|
+
"space-lens": "^0.2.0",
|
|
41
|
+
"web-tree-sitter": "0.25.10"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@types/node": "^25.9.3",
|
|
45
|
+
"tsdown": "^0.22.2",
|
|
46
|
+
"tsx": "^4.22.4",
|
|
47
|
+
"typescript": "^6.0.3"
|
|
48
|
+
}
|
|
49
|
+
}
|