@pablozaiden/terminatui 0.2.0 → 0.3.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.
Files changed (181) hide show
  1. package/README.md +64 -43
  2. package/package.json +11 -8
  3. package/src/__tests__/application.test.ts +87 -68
  4. package/src/__tests__/buildCliCommand.test.ts +99 -119
  5. package/src/__tests__/builtins.test.ts +27 -75
  6. package/src/__tests__/command.test.ts +100 -131
  7. package/src/__tests__/configOnChange.test.ts +63 -0
  8. package/src/__tests__/context.test.ts +1 -26
  9. package/src/__tests__/helpCore.test.ts +227 -0
  10. package/src/__tests__/parser.test.ts +98 -244
  11. package/src/__tests__/registry.test.ts +33 -160
  12. package/src/__tests__/schemaToFields.test.ts +75 -158
  13. package/src/builtins/help.ts +12 -4
  14. package/src/builtins/settings.ts +18 -32
  15. package/src/builtins/version.ts +3 -3
  16. package/src/cli/output/colors.ts +1 -1
  17. package/src/cli/parser.ts +26 -95
  18. package/src/core/application.ts +192 -110
  19. package/src/core/command.ts +26 -9
  20. package/src/core/context.ts +31 -20
  21. package/src/core/help.ts +24 -18
  22. package/src/core/knownCommands.ts +13 -0
  23. package/src/core/logger.ts +39 -42
  24. package/src/core/registry.ts +5 -12
  25. package/src/index.ts +22 -137
  26. package/src/tui/TuiApplication.tsx +63 -120
  27. package/src/tui/TuiRoot.tsx +135 -0
  28. package/src/tui/adapters/factory.ts +19 -0
  29. package/src/tui/adapters/ink/InkRenderer.tsx +139 -0
  30. package/src/tui/adapters/ink/components/Button.tsx +12 -0
  31. package/src/tui/adapters/ink/components/Code.tsx +6 -0
  32. package/src/tui/adapters/ink/components/CodeHighlight.tsx +6 -0
  33. package/src/tui/adapters/ink/components/Container.tsx +5 -0
  34. package/src/tui/adapters/ink/components/Field.tsx +12 -0
  35. package/src/tui/adapters/ink/components/Label.tsx +24 -0
  36. package/src/tui/adapters/ink/components/MenuButton.tsx +12 -0
  37. package/src/tui/adapters/ink/components/MenuItem.tsx +17 -0
  38. package/src/tui/adapters/ink/components/Overlay.tsx +5 -0
  39. package/src/tui/adapters/ink/components/Panel.tsx +15 -0
  40. package/src/tui/adapters/ink/components/ScrollView.tsx +5 -0
  41. package/src/tui/adapters/ink/components/Select.tsx +44 -0
  42. package/src/tui/adapters/ink/components/Spacer.tsx +15 -0
  43. package/src/tui/adapters/ink/components/Spinner.tsx +5 -0
  44. package/src/tui/adapters/ink/components/TextInput.tsx +22 -0
  45. package/src/tui/adapters/ink/components/Value.tsx +7 -0
  46. package/src/tui/adapters/ink/keyboard.ts +97 -0
  47. package/src/tui/adapters/ink/utils.ts +16 -0
  48. package/src/tui/adapters/opentui/OpenTuiRenderer.tsx +119 -0
  49. package/src/tui/adapters/opentui/components/Button.tsx +13 -0
  50. package/src/tui/adapters/opentui/components/Code.tsx +12 -0
  51. package/src/tui/adapters/opentui/components/CodeHighlight.tsx +24 -0
  52. package/src/tui/adapters/opentui/components/Container.tsx +56 -0
  53. package/src/tui/adapters/opentui/components/Field.tsx +18 -0
  54. package/src/tui/adapters/opentui/components/Label.tsx +15 -0
  55. package/src/tui/adapters/opentui/components/MenuButton.tsx +14 -0
  56. package/src/tui/adapters/opentui/components/MenuItem.tsx +29 -0
  57. package/src/tui/adapters/opentui/components/Overlay.tsx +21 -0
  58. package/src/tui/adapters/opentui/components/Panel.tsx +78 -0
  59. package/src/tui/adapters/opentui/components/ScrollView.tsx +85 -0
  60. package/src/tui/adapters/opentui/components/Select.tsx +59 -0
  61. package/src/tui/adapters/opentui/components/Spacer.tsx +5 -0
  62. package/src/tui/adapters/opentui/components/Spinner.tsx +12 -0
  63. package/src/tui/adapters/opentui/components/TextInput.tsx +13 -0
  64. package/src/tui/adapters/opentui/components/Value.tsx +13 -0
  65. package/src/tui/{hooks → adapters/opentui/hooks}/useSpinner.ts +2 -11
  66. package/src/tui/adapters/opentui/keyboard.ts +61 -0
  67. package/src/tui/adapters/types.ts +71 -0
  68. package/src/tui/components/ActionButton.tsx +0 -36
  69. package/src/tui/components/CommandSelector.tsx +45 -92
  70. package/src/tui/components/ConfigForm.tsx +68 -42
  71. package/src/tui/components/FieldRow.tsx +0 -30
  72. package/src/tui/components/Header.tsx +14 -13
  73. package/src/tui/components/JsonHighlight.tsx +10 -17
  74. package/src/tui/components/ModalBase.tsx +38 -0
  75. package/src/tui/components/ResultsPanel.tsx +27 -36
  76. package/src/tui/components/StatusBar.tsx +24 -39
  77. package/src/tui/components/logColors.ts +12 -0
  78. package/src/tui/context/ClipboardContext.tsx +87 -0
  79. package/src/tui/context/ExecutorContext.tsx +139 -0
  80. package/src/tui/context/KeyboardContext.tsx +85 -71
  81. package/src/tui/context/LogsContext.tsx +35 -0
  82. package/src/tui/context/NavigationContext.tsx +194 -0
  83. package/src/tui/context/RendererContext.tsx +20 -0
  84. package/src/tui/context/TuiAppContext.tsx +58 -0
  85. package/src/tui/hooks/useActiveKeyHandler.ts +75 -0
  86. package/src/tui/hooks/useBackHandler.ts +34 -0
  87. package/src/tui/hooks/useClipboard.ts +40 -25
  88. package/src/tui/hooks/useClipboardProvider.ts +42 -0
  89. package/src/tui/hooks/useGlobalKeyHandler.ts +54 -0
  90. package/src/tui/modals/CliModal.tsx +82 -0
  91. package/src/tui/modals/EditorModal.tsx +207 -0
  92. package/src/tui/modals/LogsModal.tsx +98 -0
  93. package/src/tui/registry.ts +102 -0
  94. package/src/tui/screens/CommandSelectScreen.tsx +162 -0
  95. package/src/tui/screens/ConfigScreen.tsx +165 -0
  96. package/src/tui/screens/ErrorScreen.tsx +58 -0
  97. package/src/tui/screens/ResultsScreen.tsx +68 -0
  98. package/src/tui/screens/RunningScreen.tsx +72 -0
  99. package/src/tui/screens/ScreenBase.ts +6 -0
  100. package/src/tui/semantic/Button.tsx +7 -0
  101. package/src/tui/semantic/Code.tsx +7 -0
  102. package/src/tui/semantic/CodeHighlight.tsx +7 -0
  103. package/src/tui/semantic/Container.tsx +7 -0
  104. package/src/tui/semantic/Field.tsx +7 -0
  105. package/src/tui/semantic/Label.tsx +7 -0
  106. package/src/tui/semantic/MenuButton.tsx +7 -0
  107. package/src/tui/semantic/MenuItem.tsx +7 -0
  108. package/src/tui/semantic/Overlay.tsx +7 -0
  109. package/src/tui/semantic/Panel.tsx +7 -0
  110. package/src/tui/semantic/ScrollView.tsx +9 -0
  111. package/src/tui/semantic/Select.tsx +7 -0
  112. package/src/tui/semantic/Spacer.tsx +7 -0
  113. package/src/tui/semantic/Spinner.tsx +7 -0
  114. package/src/tui/semantic/TextInput.tsx +7 -0
  115. package/src/tui/semantic/Value.tsx +7 -0
  116. package/src/tui/semantic/types.ts +195 -0
  117. package/src/tui/theme.ts +25 -14
  118. package/src/tui/utils/buildCliCommand.ts +1 -0
  119. package/src/tui/utils/getEnumKeys.ts +3 -0
  120. package/src/tui/utils/parameterPersistence.ts +1 -0
  121. package/src/types/command.ts +0 -60
  122. package/.devcontainer/devcontainer.json +0 -19
  123. package/.devcontainer/install-prerequisites.sh +0 -49
  124. package/.github/workflows/copilot-setup-steps.yml +0 -32
  125. package/.github/workflows/pull-request.yml +0 -27
  126. package/.github/workflows/release-npm-package.yml +0 -81
  127. package/AGENTS.md +0 -31
  128. package/bun.lock +0 -236
  129. package/examples/tui-app/commands/config/app/get.ts +0 -66
  130. package/examples/tui-app/commands/config/app/index.ts +0 -27
  131. package/examples/tui-app/commands/config/app/set.ts +0 -86
  132. package/examples/tui-app/commands/config/index.ts +0 -32
  133. package/examples/tui-app/commands/config/user/get.ts +0 -65
  134. package/examples/tui-app/commands/config/user/index.ts +0 -27
  135. package/examples/tui-app/commands/config/user/set.ts +0 -61
  136. package/examples/tui-app/commands/greet.ts +0 -76
  137. package/examples/tui-app/commands/index.ts +0 -4
  138. package/examples/tui-app/commands/math.ts +0 -115
  139. package/examples/tui-app/commands/status.ts +0 -77
  140. package/examples/tui-app/index.ts +0 -35
  141. package/guides/01-hello-world.md +0 -96
  142. package/guides/02-adding-options.md +0 -103
  143. package/guides/03-multiple-commands.md +0 -163
  144. package/guides/04-subcommands.md +0 -206
  145. package/guides/05-interactive-tui.md +0 -194
  146. package/guides/06-config-validation.md +0 -264
  147. package/guides/07-async-cancellation.md +0 -336
  148. package/guides/08-complete-application.md +0 -537
  149. package/guides/README.md +0 -74
  150. package/src/__tests__/colors.test.ts +0 -127
  151. package/src/__tests__/commandClass.test.ts +0 -130
  152. package/src/__tests__/help.test.ts +0 -412
  153. package/src/__tests__/registryNew.test.ts +0 -160
  154. package/src/__tests__/table.test.ts +0 -146
  155. package/src/__tests__/tui.test.ts +0 -26
  156. package/src/builtins/index.ts +0 -4
  157. package/src/cli/help.ts +0 -174
  158. package/src/cli/index.ts +0 -3
  159. package/src/cli/output/index.ts +0 -2
  160. package/src/cli/output/table.ts +0 -141
  161. package/src/commands/help.ts +0 -50
  162. package/src/commands/index.ts +0 -1
  163. package/src/components/index.ts +0 -147
  164. package/src/core/index.ts +0 -15
  165. package/src/hooks/index.ts +0 -131
  166. package/src/registry/commandRegistry.ts +0 -77
  167. package/src/registry/index.ts +0 -1
  168. package/src/tui/TuiApp.tsx +0 -619
  169. package/src/tui/app.ts +0 -29
  170. package/src/tui/components/CliModal.tsx +0 -81
  171. package/src/tui/components/EditorModal.tsx +0 -177
  172. package/src/tui/components/LogsPanel.tsx +0 -86
  173. package/src/tui/components/index.ts +0 -13
  174. package/src/tui/context/index.ts +0 -7
  175. package/src/tui/hooks/index.ts +0 -35
  176. package/src/tui/hooks/useKeyboardHandler.ts +0 -91
  177. package/src/tui/hooks/useLogStream.ts +0 -96
  178. package/src/tui/index.ts +0 -65
  179. package/src/tui/utils/index.ts +0 -13
  180. package/src/types/index.ts +0 -1
  181. package/tsconfig.json +0 -25
@@ -1,76 +0,0 @@
1
- import {
2
- Command,
3
- type AppContext,
4
- type OptionSchema,
5
- type OptionValues,
6
- type CommandResult
7
- } from "../../../src/index.ts";
8
-
9
- const greetOptions = {
10
- name: {
11
- type: "string",
12
- description: "Name to greet",
13
- required: true,
14
- label: "Name",
15
- order: 1,
16
- group: "Required",
17
- placeholder: "Enter name...",
18
- },
19
- loud: {
20
- type: "boolean",
21
- description: "Use uppercase",
22
- alias: "l",
23
- default: false,
24
- label: "Loud Mode",
25
- order: 2,
26
- group: "Options",
27
- },
28
- times: {
29
- type: "number",
30
- description: "Number of times to greet",
31
- default: 1,
32
- label: "Repeat Count",
33
- order: 3,
34
- group: "Options",
35
- },
36
- } as const satisfies OptionSchema;
37
-
38
- export class GreetCommand extends Command<typeof greetOptions> {
39
- readonly name = "greet";
40
- override displayName = "Greet";
41
- readonly description = "Greet someone with a friendly message";
42
- readonly options = greetOptions;
43
-
44
- override readonly actionLabel = "Say Hello";
45
-
46
- override readonly examples = [
47
- { command: "greet --name World", description: "Simple greeting" },
48
- { command: "greet --name World --loud --times 3", description: "Loud greeting 3 times" },
49
- ];
50
-
51
- override async execute(ctx: AppContext, opts: OptionValues<typeof greetOptions>): Promise<CommandResult> {
52
- const greeting = this.createGreeting(opts);
53
- ctx.logger.info(greeting);
54
- return {
55
- success: true,
56
- data: { greeting, timestamp: new Date().toISOString() },
57
- message: greeting,
58
- };
59
- }
60
-
61
- override getClipboardContent(result: CommandResult): string | undefined {
62
- const data = result.data as { greeting?: string } | undefined;
63
- return data?.greeting;
64
- }
65
-
66
- private createGreeting(opts: OptionValues<typeof greetOptions>): string {
67
- const name = opts.name as string;
68
- const loud = opts.loud as boolean;
69
- const times = (opts.times as number) || 1;
70
-
71
- let message = `Hello, ${name}!`;
72
- if (loud) message = message.toUpperCase();
73
-
74
- return Array(times).fill(message).join("\n");
75
- }
76
- }
@@ -1,4 +0,0 @@
1
- export { GreetCommand } from "./greet.ts";
2
- export { MathCommand } from "./math.ts";
3
- export { StatusCommand } from "./status.ts";
4
- export { ConfigCommand } from "./config/index.ts";
@@ -1,115 +0,0 @@
1
- import {
2
- Command,
3
- type AppContext,
4
- type OptionSchema,
5
- type OptionValues,
6
- type CommandResult
7
- } from "../../../src/index.ts";
8
-
9
- const mathOptions = {
10
- operation: {
11
- type: "string",
12
- description: "Math operation to perform",
13
- required: true,
14
- enum: ["add", "subtract", "multiply", "divide"] as const,
15
- label: "Operation",
16
- order: 1,
17
- group: "Required",
18
- },
19
- a: {
20
- type: "number",
21
- description: "First number",
22
- required: true,
23
- label: "First Number",
24
- order: 2,
25
- group: "Required",
26
- },
27
- b: {
28
- type: "number",
29
- description: "Second number",
30
- required: true,
31
- label: "Second Number",
32
- order: 3,
33
- group: "Required",
34
- },
35
- showSteps: {
36
- type: "boolean",
37
- description: "Show calculation steps",
38
- default: false,
39
- label: "Show Steps",
40
- order: 4,
41
- group: "Options",
42
- },
43
- } as const satisfies OptionSchema;
44
-
45
- export class MathCommand extends Command<typeof mathOptions> {
46
- readonly name = "math";
47
- override displayName = "Math Operations";
48
- readonly description = "Perform basic math operations";
49
- readonly options = mathOptions;
50
-
51
- override readonly actionLabel = "Calculate";
52
-
53
- override async execute(ctx: AppContext, opts: OptionValues<typeof mathOptions>): Promise<CommandResult> {
54
- const result = this.calculate(opts);
55
- if (!result.success) {
56
- ctx.logger.error(result.message || "Calculation failed");
57
- }
58
- return result;
59
- }
60
-
61
- override renderResult(result: CommandResult): string {
62
- if (!result.success) return result.message || "Error";
63
- const data = result.data as { expression: string; result: number; steps?: string[] };
64
- let output = `${data.expression} = ${data.result}`;
65
- if (data.steps) {
66
- output += "\n\nSteps:\n" + data.steps.map((s, i) => ` ${i + 1}. ${s}`).join("\n");
67
- }
68
- return output;
69
- }
70
-
71
- private calculate(opts: OptionValues<typeof mathOptions>): CommandResult {
72
- const op = opts.operation as string;
73
- const a = opts.a as number;
74
- const b = opts.b as number;
75
- const showSteps = opts.showSteps as boolean;
76
-
77
- let result: number;
78
- let expression: string;
79
- const steps: string[] = [];
80
-
81
- switch (op) {
82
- case "add":
83
- result = a + b;
84
- expression = `${a} + ${b}`;
85
- if (showSteps) steps.push(`Adding ${a} and ${b}`, `Result: ${result}`);
86
- break;
87
- case "subtract":
88
- result = a - b;
89
- expression = `${a} - ${b}`;
90
- if (showSteps) steps.push(`Subtracting ${b} from ${a}`, `Result: ${result}`);
91
- break;
92
- case "multiply":
93
- result = a * b;
94
- expression = `${a} × ${b}`;
95
- if (showSteps) steps.push(`Multiplying ${a} by ${b}`, `Result: ${result}`);
96
- break;
97
- case "divide":
98
- if (b === 0) {
99
- return { success: false, message: "Cannot divide by zero" };
100
- }
101
- result = a / b;
102
- expression = `${a} ÷ ${b}`;
103
- if (showSteps) steps.push(`Dividing ${a} by ${b}`, `Result: ${result}`);
104
- break;
105
- default:
106
- return { success: false, message: `Unknown operation: ${op}` };
107
- }
108
-
109
- return {
110
- success: true,
111
- data: { expression, result, steps: showSteps ? steps : undefined },
112
- message: `${expression} = ${result}`,
113
- };
114
- }
115
- }
@@ -1,77 +0,0 @@
1
- import {
2
- Command,
3
- type AppContext,
4
- type OptionSchema,
5
- type OptionValues,
6
- type CommandResult
7
- } from "../../../src/index.ts";
8
-
9
- const statusOptions = {
10
- detailed: {
11
- type: "boolean",
12
- description: "Show detailed status",
13
- default: false,
14
- label: "Detailed",
15
- alias: "d",
16
- order: 1,
17
- },
18
- } as const satisfies OptionSchema;
19
-
20
- export class StatusCommand extends Command<typeof statusOptions> {
21
- readonly name = "status";
22
- override displayName = "Status";
23
- readonly description = "Show application status";
24
- readonly options = statusOptions;
25
-
26
- override readonly actionLabel = "Check Status";
27
- override readonly immediateExecution = true; // No required fields
28
-
29
- override async execute(ctx: AppContext, opts: OptionValues<typeof statusOptions>): Promise<CommandResult> {
30
- const result = await this.getStatus(opts);
31
- ctx.logger.info(result.message || "Status check complete");
32
- return result;
33
- }
34
-
35
- override renderResult(result: CommandResult): string {
36
- if (!result.success) return "❌ Status check failed";
37
- const data = result.data as { uptime: string; memory: string; platform: string; version: string };
38
- return [
39
- "✓ Application Status",
40
- "",
41
- ` Uptime: ${data.uptime}`,
42
- ` Memory: ${data.memory}`,
43
- ` Platform: ${data.platform}`,
44
- ` Bun: ${data.version}`,
45
- ].join("\n");
46
- }
47
-
48
- private async getStatus(opts: OptionValues<typeof statusOptions>): Promise<CommandResult> {
49
- const detailed = opts.detailed as boolean;
50
-
51
- // Simulate some async work
52
- await new Promise((resolve) => setTimeout(resolve, 500));
53
-
54
- const memMB = Math.round(process.memoryUsage().heapUsed / 1024 / 1024);
55
- const uptimeSec = Math.round(process.uptime());
56
-
57
- const data: Record<string, unknown> = {
58
- uptime: `${uptimeSec} seconds`,
59
- memory: `${memMB} MB`,
60
- platform: process.platform,
61
- version: Bun.version,
62
- };
63
-
64
- if (detailed) {
65
- Object.assign(data, {
66
- cwd: process.cwd(),
67
- pid: process.pid,
68
- });
69
- }
70
-
71
- return {
72
- success: true,
73
- data,
74
- message: "All systems operational",
75
- };
76
- }
77
- }
@@ -1,35 +0,0 @@
1
- #!/usr/bin/env bun
2
- /**
3
- * Example TUI Application
4
- *
5
- * Demonstrates how to use TuiApplication to build a CLI/TUI app
6
- * with minimal effort. Just run:
7
- *
8
- * bun examples/tui-app/index.ts
9
- *
10
- * Or in CLI mode:
11
- *
12
- * bun examples/tui-app/index.ts greet --name "World" --loud
13
- */
14
-
15
- import { TuiApplication } from "../../src/index.ts";
16
- import { GreetCommand, MathCommand, StatusCommand, ConfigCommand } from "./commands/index.ts";
17
-
18
- class ExampleApp extends TuiApplication {
19
- constructor() {
20
- super({
21
- name: "example",
22
- version: "1.0.0",
23
- commands: [
24
- new GreetCommand(),
25
- new MathCommand(),
26
- new StatusCommand(),
27
- new ConfigCommand(),
28
- ],
29
- enableTui: true,
30
- });
31
- }
32
- }
33
-
34
- // Run the app
35
- await new ExampleApp().run(Bun.argv.slice(2));
@@ -1,96 +0,0 @@
1
- # Guide 1: Hello World CLI (Super Simple)
2
-
3
- Create your first CLI app with Terminatui in under 5 minutes.
4
-
5
- ## What You'll Build
6
-
7
- A simple CLI that greets the user by name.
8
-
9
- ```bash
10
- myapp greet --name Alice
11
- # Output: Hello, Alice!
12
- ```
13
-
14
- ## Prerequisites
15
-
16
- - Bun installed (`curl -fsSL https://bun.sh/install | bash`)
17
-
18
- ## Step 1: Create Project
19
-
20
- ```bash
21
- mkdir hello-cli && cd hello-cli
22
- bun init -y
23
- bun add @pablozaiden/terminatui
24
- ```
25
-
26
- ## Step 2: Create the Command
27
-
28
- Create `src/greet.ts`:
29
-
30
- ```typescript
31
- import { Command, type AppContext, type OptionSchema, type CommandResult } from "@pablozaiden/terminatui";
32
-
33
- const options = {
34
- name: {
35
- type: "string",
36
- description: "Name to greet",
37
- required: true,
38
- },
39
- } satisfies OptionSchema;
40
-
41
- export class GreetCommand extends Command<typeof options> {
42
- readonly name = "greet";
43
- readonly description = "Greet someone";
44
- readonly options = options;
45
-
46
- execute(_ctx: AppContext, config: { name: string }): CommandResult {
47
- console.log(`Hello, ${config.name}!`);
48
- return { success: true };
49
- }
50
- }
51
- ```
52
-
53
- ## Step 3: Create the App
54
-
55
- Create `src/index.ts`:
56
-
57
- ```typescript
58
- import { Application } from "@pablozaiden/terminatui";
59
- import { GreetCommand } from "./greet";
60
-
61
- class MyApp extends Application {
62
- constructor() {
63
- super({
64
- name: "myapp",
65
- version: "1.0.0",
66
- commands: [new GreetCommand()],
67
- });
68
- }
69
- }
70
-
71
- await new MyApp().run();
72
- ```
73
-
74
- ## Step 4: Run It
75
-
76
- ```bash
77
- bun src/index.ts greet --name Alice
78
- # Output: Hello, Alice!
79
-
80
- bun src/index.ts help
81
- # Shows available commands
82
-
83
- bun src/index.ts greet help
84
- # Shows greet options
85
- ```
86
-
87
- ## What You Learned
88
-
89
- - Commands extend the `Command` class
90
- - Options are defined with `OptionSchema`
91
- - The `execute()` method handles the logic
92
- - `Application` ties everything together
93
-
94
- ## Next Steps
95
-
96
- → [Guide 2: Adding Options](02-adding-options.md)
@@ -1,103 +0,0 @@
1
- # Guide 2: Adding Options (Super Simple)
2
-
3
- Add boolean and string options to make your CLI more flexible.
4
-
5
- ## What You'll Build
6
-
7
- Extend the greeting command with a `--loud` flag:
8
-
9
- ```bash
10
- myapp greet --name Alice
11
- # Output: Hello, Alice!
12
-
13
- myapp greet --name Alice --loud
14
- # Output: HELLO, ALICE!
15
- ```
16
-
17
- ## Step 1: Add the Option
18
-
19
- Update `src/greet.ts`:
20
-
21
- ```typescript
22
- import { Command, type AppContext, type OptionSchema, type CommandResult } from "@pablozaiden/terminatui";
23
-
24
- const options = {
25
- name: {
26
- type: "string",
27
- description: "Name to greet",
28
- required: true,
29
- },
30
- loud: {
31
- type: "boolean",
32
- description: "Shout the greeting",
33
- alias: "l", // Short flag: -l
34
- default: false,
35
- },
36
- } satisfies OptionSchema;
37
-
38
- export class GreetCommand extends Command<typeof options> {
39
- readonly name = "greet";
40
- readonly description = "Greet someone";
41
- readonly options = options;
42
-
43
- execute(_ctx: AppContext, config: { name: string; loud: boolean }): CommandResult {
44
- const message = `Hello, ${config.name}!`;
45
- console.log(config.loud ? message.toUpperCase() : message);
46
- return { success: true };
47
- }
48
- }
49
- ```
50
-
51
- ## Step 2: Test It
52
-
53
- ```bash
54
- # Normal greeting
55
- bun src/index.ts greet --name Alice
56
- # Hello, Alice!
57
-
58
- # Loud greeting with long flag
59
- bun src/index.ts greet --name Bob --loud
60
- # HELLO, BOB!
61
-
62
- # Loud greeting with short flag
63
- bun src/index.ts greet --name Charlie -l
64
- # HELLO, CHARLIE!
65
-
66
- # View help to see options
67
- bun src/index.ts greet help
68
- ```
69
-
70
- ## Key Concepts
71
-
72
- ### Option Types
73
-
74
- | Type | Description | Example |
75
- |------|-------------|---------|
76
- | `string` | Text value | `--name Alice` |
77
- | `boolean` | Flag (true/false) | `--loud` or `--no-loud` |
78
- | `number` | Numeric value | `--count 5` |
79
- | `array` | Multiple values | `--tags a --tags b` |
80
-
81
- ### Option Properties
82
-
83
- ```typescript
84
- {
85
- type: "string",
86
- description: "Help text", // Required
87
- required: true, // Must be provided
88
- default: "value", // Default if not provided
89
- alias: "n", // Short flag (-n)
90
- enum: ["a", "b", "c"], // Restrict to values
91
- }
92
- ```
93
-
94
- ## What You Learned
95
-
96
- - Add options with different types
97
- - Use `alias` for short flags (-l instead of --loud)
98
- - Use `default` for optional values
99
- - Boolean flags can be negated with `--no-` prefix
100
-
101
- ## Next Steps
102
-
103
- → [Guide 3: Multiple Commands](03-multiple-commands.md)
@@ -1,163 +0,0 @@
1
- # Guide 3: Multiple Commands (Basic)
2
-
3
- Build a CLI with multiple commands that share common functionality.
4
-
5
- ## What You'll Build
6
-
7
- A file utility CLI with `list` and `count` commands:
8
-
9
- ```bash
10
- fileutil list --dir ./src
11
- # Lists files in directory
12
-
13
- fileutil count --dir ./src --ext .ts
14
- # Counts files with extension
15
- ```
16
-
17
- ## Step 1: Create the List Command
18
-
19
- Create `src/commands/list.ts`:
20
-
21
- ```typescript
22
- import { readdirSync } from "fs";
23
- import { Command, type AppContext, type OptionSchema, type CommandResult } from "@pablozaiden/terminatui";
24
-
25
- const options = {
26
- dir: {
27
- type: "string",
28
- description: "Directory to list",
29
- required: true,
30
- alias: "d",
31
- },
32
- } satisfies OptionSchema;
33
-
34
- export class ListCommand extends Command<typeof options> {
35
- readonly name = "list";
36
- readonly description = "List files in a directory";
37
- readonly options = options;
38
-
39
- execute(_ctx: AppContext, config: { dir: string }): CommandResult {
40
- try {
41
- const files = readdirSync(config.dir);
42
- console.log(`Files in ${config.dir}:`);
43
- files.forEach((file) => console.log(` ${file}`));
44
- return { success: true, message: `Found ${files.length} files` };
45
- } catch (error) {
46
- return { success: false, error: String(error) };
47
- }
48
- }
49
- }
50
- ```
51
-
52
- ## Step 2: Create the Count Command
53
-
54
- Create `src/commands/count.ts`:
55
-
56
- ```typescript
57
- import { readdirSync } from "fs";
58
- import { Command, type AppContext, type OptionSchema, type CommandResult } from "@pablozaiden/terminatui";
59
-
60
- const options = {
61
- dir: {
62
- type: "string",
63
- description: "Directory to search",
64
- required: true,
65
- alias: "d",
66
- },
67
- ext: {
68
- type: "string",
69
- description: "File extension to count (e.g., .ts)",
70
- alias: "e",
71
- },
72
- } satisfies OptionSchema;
73
-
74
- export class CountCommand extends Command<typeof options> {
75
- readonly name = "count";
76
- readonly description = "Count files in a directory";
77
- readonly options = options;
78
-
79
- execute(_ctx: AppContext, config: { dir: string; ext?: string }): CommandResult {
80
- try {
81
- let files = readdirSync(config.dir);
82
-
83
- if (config.ext) {
84
- files = files.filter((f) => f.endsWith(config.ext!));
85
- }
86
-
87
- console.log(`Found ${files.length} files${config.ext ? ` with ${config.ext}` : ""}`);
88
- return { success: true, data: { count: files.length } };
89
- } catch (error) {
90
- return { success: false, error: String(error) };
91
- }
92
- }
93
- }
94
- ```
95
-
96
- ## Step 3: Create the Application
97
-
98
- Create `src/index.ts`:
99
-
100
- ```typescript
101
- import { Application } from "@pablozaiden/terminatui";
102
- import { ListCommand } from "./commands/list";
103
- import { CountCommand } from "./commands/count";
104
-
105
- class FileUtilApp extends Application {
106
- constructor() {
107
- super({
108
- name: "fileutil",
109
- version: "1.0.0",
110
- description: "File utility commands",
111
- commands: [
112
- new ListCommand(),
113
- new CountCommand(),
114
- ],
115
- });
116
- }
117
- }
118
-
119
- await new FileUtilApp().run();
120
- ```
121
-
122
- ## Step 4: Test It
123
-
124
- ```bash
125
- # List files
126
- bun src/index.ts list --dir ./src
127
- # Files in ./src:
128
- # commands
129
- # index.ts
130
-
131
- # Count all files
132
- bun src/index.ts count -d ./src
133
- # Found 2 files
134
-
135
- # Count only .ts files
136
- bun src/index.ts count -d ./src -e .ts
137
- # Found 1 files with .ts
138
-
139
- # Show help
140
- bun src/index.ts help
141
- # Lists: list, count, version, help
142
- ```
143
-
144
- ## Project Structure
145
-
146
- ```
147
- src/
148
- ├── index.ts # App entry point
149
- └── commands/
150
- ├── list.ts # List command
151
- └── count.ts # Count command
152
- ```
153
-
154
- ## What You Learned
155
-
156
- - Create multiple commands in separate files
157
- - Return structured `CommandResult` with data
158
- - Handle errors gracefully
159
- - Use consistent option patterns across commands
160
-
161
- ## Next Steps
162
-
163
- → [Guide 4: Subcommands](04-subcommands.md)