@cedarjs/tui 0.0.4

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Cedar
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # TUI
2
+
3
+ Terminal User Interface for RedwoodJS. A collection of helpful functions to standardise and simplify terminal output.
4
+
5
+ ## API
6
+
7
+ **TODO**
@@ -0,0 +1,132 @@
1
+ import stream from 'stream';
2
+ import boxen from 'boxen';
3
+ import chalk from 'chalk';
4
+ import { prompt as enquirerPrompt } from 'enquirer';
5
+ /**
6
+ * A default set of styling for the TUI, designed for a cohesive look and feel around the Redwood CLI, CRWA and vairous plugins
7
+ */
8
+ export declare const RedwoodStyling: {
9
+ error: chalk.Chalk;
10
+ warning: chalk.Chalk;
11
+ success: chalk.Chalk;
12
+ info: chalk.Chalk;
13
+ header: chalk.Chalk;
14
+ cmd: chalk.Chalk;
15
+ redwood: chalk.Chalk;
16
+ love: chalk.Chalk;
17
+ green: chalk.Chalk;
18
+ };
19
+ /**
20
+ * An object used to describe a "reactive" TUI element, that is an element that is updated a number of times per second
21
+ */
22
+ export declare class ReactiveTUIContent {
23
+ private outStream?;
24
+ private mode;
25
+ private header;
26
+ private content;
27
+ private spinner;
28
+ private boxen;
29
+ private frameInterval;
30
+ private spinnerIndex;
31
+ constructor(options: {
32
+ mode?: 'text' | 'stream';
33
+ header?: string;
34
+ content?: string;
35
+ spinner?: {
36
+ enabled?: boolean;
37
+ characters?: string[];
38
+ };
39
+ boxen?: boxen.Options;
40
+ outStream?: stream.Readable;
41
+ frameInterval?: number;
42
+ });
43
+ update(options: {
44
+ mode?: 'text' | 'stream';
45
+ header?: string;
46
+ content?: string;
47
+ spinner?: {
48
+ enabled?: boolean;
49
+ characters?: string[];
50
+ };
51
+ boxen?: boxen.Options;
52
+ outStream?: stream.Readable;
53
+ frameInterval?: number;
54
+ }): void;
55
+ setOutStream(out: stream.Readable): void;
56
+ renderToString(): string;
57
+ getFrameInterval(): number;
58
+ }
59
+ /**
60
+ * Configuration for the TUI
61
+ *
62
+ * Accepts an out and err stream which the TUI will write to.
63
+ */
64
+ export interface RedwoodTUIConfig {
65
+ out?: NodeJS.WriteStream;
66
+ err?: NodeJS.WriteStream;
67
+ }
68
+ /**
69
+ * TODO: Documentation for this
70
+ */
71
+ export declare class RedwoodTUI {
72
+ private manager;
73
+ private outStream;
74
+ private errStream;
75
+ private timerId?;
76
+ private isReactive;
77
+ private reactiveContent?;
78
+ constructor({ out, err }?: RedwoodTUIConfig);
79
+ /**
80
+ * Enables rendering of a reactive component to the TUI
81
+ *
82
+ * @param reactiveContent A new ReactiveTUIContent object set as the current reactive content
83
+ */
84
+ startReactive(reactiveContent?: ReactiveTUIContent): void;
85
+ /**
86
+ * Stops any new draws of the current reactive content to the TUI
87
+ *
88
+ * @param clear If true, the last drawn content will be cleared
89
+ */
90
+ stopReactive(clear?: boolean): void;
91
+ /**
92
+ * Renders the current reactive content and draws it to the TUI
93
+ *
94
+ * @param force Force a draw even if the TUI is not reactive
95
+ */
96
+ private drawReactive;
97
+ /**
98
+ * Gets the current reactive TUI content if there is one
99
+ *
100
+ * @returns The current reactive content or undefined if there isn't one
101
+ */
102
+ getCurrentReactive(): ReactiveTUIContent | undefined;
103
+ /**
104
+ * Writes a string to the TUI output stream
105
+ *
106
+ * @param text The string to write out
107
+ */
108
+ drawText(text: string): void;
109
+ /**
110
+ * A wrapper around enquirer.prompt that disables the reactive TUI and prompts
111
+ *
112
+ * @param questions A question or array of questions to prompt the user with
113
+ *
114
+ * @returns The prompt result
115
+ */
116
+ prompt<T = object>(questions: Parameters<typeof enquirerPrompt>[0]): Promise<T>;
117
+ /**
118
+ * Display an error message in a box
119
+ *
120
+ * @param title Error box title
121
+ * @param message Error message
122
+ */
123
+ displayError(title: string, message: string): void;
124
+ /**
125
+ * Display a warning message in a box
126
+ *
127
+ * @param title Error box title
128
+ * @param message Error message
129
+ */
130
+ displayWarning(title: string, message: string): void;
131
+ }
132
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,MAAM,MAAM,QAAQ,CAAA;AAE3B,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,EAAE,MAAM,IAAI,cAAc,EAAE,MAAM,UAAU,CAAA;AAGnD;;GAEG;AACH,eAAO,MAAM,cAAc;;;;;;;;;;CAY1B,CAAA;AAED;;GAEG;AACH,qBAAa,kBAAkB;IAC7B,OAAO,CAAC,SAAS,CAAC,CAAiB;IAEnC,OAAO,CAAC,IAAI,CAAmB;IAC/B,OAAO,CAAC,MAAM,CAAQ;IACtB,OAAO,CAAC,OAAO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E9B,OAAO,CAAC,SAAS,CAAoB;IACrC,OAAO,CAAC,SAAS,CAAoB;IAErC,OAAO,CAAC,OAAO,CAAC,CAAgB;IAChC,OAAO,CAAC,UAAU,CAAQ;IAE1B,OAAO,CAAC,eAAe,CAAC,CAAoB;gBAEhC,EAAE,GAAG,EAAE,GAAG,EAAE,GAAE,gBAAqB;IAW/C;;;;OAIG;IACH,aAAa,CAAC,eAAe,CAAC,EAAE,kBAAkB;IAkClD;;;;OAIG;IACH,YAAY,CAAC,KAAK,UAAQ;IAyB1B;;;;OAIG;IACH,OAAO,CAAC,YAAY;IAgBpB;;;;OAIG;IACH,kBAAkB,IAAI,kBAAkB,GAAG,SAAS;IAIpD;;;;OAIG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM;IAKrB;;;;;;OAMG;IACG,MAAM,CAAC,CAAC,GAAG,MAAM,EACrB,SAAS,EAAE,UAAU,CAAC,OAAO,cAAc,CAAC,CAAC,CAAC,CAAC,GAC9C,OAAO,CAAC,CAAC,CAAC;IAYb;;;;;OAKG;IACH,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;IAW3C;;;;;OAKG;IACH,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;CAU9C"}
package/dist/index.js ADDED
@@ -0,0 +1,292 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var index_exports = {};
30
+ __export(index_exports, {
31
+ ReactiveTUIContent: () => ReactiveTUIContent,
32
+ RedwoodStyling: () => RedwoodStyling,
33
+ RedwoodTUI: () => RedwoodTUI
34
+ });
35
+ module.exports = __toCommonJS(index_exports);
36
+ var import_stream = __toESM(require("stream"));
37
+ var import_boxen = __toESM(require("boxen"));
38
+ var import_chalk = __toESM(require("chalk"));
39
+ var import_enquirer = require("enquirer");
40
+ var import_stdout_update = require("stdout-update");
41
+ const RedwoodStyling = {
42
+ error: import_chalk.default.bold.red,
43
+ warning: import_chalk.default.keyword("orange"),
44
+ success: import_chalk.default.greenBright,
45
+ info: import_chalk.default.grey,
46
+ header: import_chalk.default.bold.underline.hex("#e8e8e8"),
47
+ cmd: import_chalk.default.hex("#808080"),
48
+ redwood: import_chalk.default.hex("#ff845e"),
49
+ love: import_chalk.default.redBright,
50
+ green: import_chalk.default.green
51
+ };
52
+ class ReactiveTUIContent {
53
+ outStream;
54
+ mode;
55
+ header;
56
+ content;
57
+ spinner;
58
+ boxen;
59
+ frameInterval;
60
+ // TODO: Implement a progress bar
61
+ spinnerIndex = 0;
62
+ constructor(options) {
63
+ this.mode = options.mode || "text";
64
+ this.header = options.header || "";
65
+ this.content = options.content || "";
66
+ const defaultSpinner = {
67
+ enabled: false,
68
+ characters: ["\u280B", "\u2819", "\u2839", "\u2838", "\u283C", "\u2834", "\u2826", "\u2827", "\u2807", "\u280F"].map(
69
+ (c) => RedwoodStyling.redwood(c)
70
+ )
71
+ };
72
+ this.spinner = { ...defaultSpinner, ...options.spinner };
73
+ this.boxen = { ...options.boxen };
74
+ this.frameInterval = options.frameInterval || 80;
75
+ if (options.outStream) {
76
+ this.setOutStream(options.outStream);
77
+ }
78
+ }
79
+ update(options) {
80
+ if (options.mode) {
81
+ this.mode = options.mode;
82
+ }
83
+ if (options.header !== void 0) {
84
+ this.header = options.header;
85
+ }
86
+ if (options.content !== void 0) {
87
+ this.content = options.content;
88
+ }
89
+ if (options.spinner) {
90
+ this.spinner = { ...this.spinner, ...options.spinner };
91
+ }
92
+ if (options.boxen) {
93
+ this.boxen = { ...this.boxen, ...options.boxen };
94
+ }
95
+ if (options.outStream) {
96
+ this.setOutStream(options.outStream);
97
+ }
98
+ if (options.frameInterval) {
99
+ this.frameInterval = options.frameInterval;
100
+ }
101
+ }
102
+ setOutStream(out) {
103
+ this.outStream = new import_stream.default.Writable({
104
+ write: (chunk, _encoding, next) => {
105
+ if (this.content === "stream") {
106
+ this.content += chunk.toString("utf-8");
107
+ }
108
+ next();
109
+ return true;
110
+ }
111
+ });
112
+ out.pipe(this.outStream, { end: true });
113
+ }
114
+ renderToString() {
115
+ if (this.mode === "stream") {
116
+ return "Not implemented yet";
117
+ }
118
+ let renderedString = this.content;
119
+ if (this.header) {
120
+ renderedString = `${this.header}
121
+ ${renderedString}`;
122
+ }
123
+ if (this.spinner.enabled) {
124
+ renderedString = `${this.spinner.characters[this.spinnerIndex]} ${renderedString}`;
125
+ this.spinnerIndex += 1;
126
+ if (this.spinnerIndex >= this.spinner.characters.length) {
127
+ this.spinnerIndex = 0;
128
+ }
129
+ }
130
+ return renderedString;
131
+ }
132
+ getFrameInterval() {
133
+ return this.frameInterval;
134
+ }
135
+ }
136
+ class RedwoodTUI {
137
+ manager;
138
+ outStream;
139
+ errStream;
140
+ timerId;
141
+ isReactive = false;
142
+ reactiveContent;
143
+ constructor({ out, err } = {}) {
144
+ this.outStream = out || process.stdout;
145
+ this.errStream = err || process.stderr;
146
+ this.manager = import_stdout_update.UpdateManager.getInstance(this.outStream, this.errStream);
147
+ process.on("exit", () => {
148
+ this.stopReactive();
149
+ });
150
+ }
151
+ /**
152
+ * Enables rendering of a reactive component to the TUI
153
+ *
154
+ * @param reactiveContent A new ReactiveTUIContent object set as the current reactive content
155
+ */
156
+ startReactive(reactiveContent) {
157
+ if (this.isReactive) {
158
+ this.stopReactive();
159
+ }
160
+ if (reactiveContent) {
161
+ this.reactiveContent = reactiveContent;
162
+ }
163
+ if (!this.reactiveContent) {
164
+ throw new Error("TUI has no reactive content");
165
+ }
166
+ if (!this.outStream.isTTY) {
167
+ this.drawReactive(true);
168
+ return;
169
+ }
170
+ if (!this.manager.isHooked) {
171
+ this.manager.hook();
172
+ this.isReactive = true;
173
+ this.timerId = setInterval(() => {
174
+ this.drawReactive();
175
+ }, this.reactiveContent.getFrameInterval());
176
+ }
177
+ }
178
+ /**
179
+ * Stops any new draws of the current reactive content to the TUI
180
+ *
181
+ * @param clear If true, the last drawn content will be cleared
182
+ */
183
+ stopReactive(clear = false) {
184
+ if (!this.outStream.isTTY) {
185
+ this.drawReactive(true);
186
+ return;
187
+ }
188
+ if (this.manager.isHooked) {
189
+ this.isReactive = false;
190
+ clearInterval(this.timerId);
191
+ this.drawReactive(true);
192
+ if (clear) {
193
+ this.manager.erase(this.manager.lastLength);
194
+ }
195
+ this.manager.unhook();
196
+ }
197
+ }
198
+ /**
199
+ * Renders the current reactive content and draws it to the TUI
200
+ *
201
+ * @param force Force a draw even if the TUI is not reactive
202
+ */
203
+ drawReactive(force = false) {
204
+ if (this.isReactive || force) {
205
+ const wasHooked = this.manager.isHooked;
206
+ if (force && !wasHooked) {
207
+ this.manager.hook();
208
+ }
209
+ const content = this.reactiveContent?.renderToString();
210
+ if (content) {
211
+ this.manager.update(content.split("\n"));
212
+ }
213
+ if (force && !wasHooked) {
214
+ this.manager.unhook();
215
+ }
216
+ }
217
+ }
218
+ /**
219
+ * Gets the current reactive TUI content if there is one
220
+ *
221
+ * @returns The current reactive content or undefined if there isn't one
222
+ */
223
+ getCurrentReactive() {
224
+ return this.reactiveContent;
225
+ }
226
+ /**
227
+ * Writes a string to the TUI output stream
228
+ *
229
+ * @param text The string to write out
230
+ */
231
+ drawText(text) {
232
+ this.outStream.write(`${text}
233
+ `);
234
+ }
235
+ // TODO: Consider a custom prompting implementation for full control of look/feel/functionality etc...
236
+ /**
237
+ * A wrapper around enquirer.prompt that disables the reactive TUI and prompts
238
+ *
239
+ * @param questions A question or array of questions to prompt the user with
240
+ *
241
+ * @returns The prompt result
242
+ */
243
+ async prompt(questions) {
244
+ const wasReactive = this.isReactive;
245
+ if (wasReactive) {
246
+ this.stopReactive();
247
+ }
248
+ const result = await (0, import_enquirer.prompt)(questions);
249
+ if (wasReactive) {
250
+ this.startReactive();
251
+ }
252
+ return result;
253
+ }
254
+ /**
255
+ * Display an error message in a box
256
+ *
257
+ * @param title Error box title
258
+ * @param message Error message
259
+ */
260
+ displayError(title, message) {
261
+ this.drawText(
262
+ (0, import_boxen.default)(message, {
263
+ padding: 1,
264
+ borderColor: "red",
265
+ title: `\u26A0 Error: ${title}`,
266
+ titleAlignment: "left"
267
+ })
268
+ );
269
+ }
270
+ /**
271
+ * Display a warning message in a box
272
+ *
273
+ * @param title Error box title
274
+ * @param message Error message
275
+ */
276
+ displayWarning(title, message) {
277
+ this.drawText(
278
+ (0, import_boxen.default)(message, {
279
+ padding: 1,
280
+ borderColor: "yellow",
281
+ title: `\u26A0 Warning: ${title}`,
282
+ titleAlignment: "left"
283
+ })
284
+ );
285
+ }
286
+ }
287
+ // Annotate the CommonJS export names for ESM import in node:
288
+ 0 && (module.exports = {
289
+ ReactiveTUIContent,
290
+ RedwoodStyling,
291
+ RedwoodTUI
292
+ });
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@cedarjs/tui",
3
+ "version": "0.0.4",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "git+https://github.com/cedarjs/cedar.git",
7
+ "directory": "packages/tui"
8
+ },
9
+ "license": "MIT",
10
+ "main": "./dist/index.js",
11
+ "types": "./dist/index.d.ts",
12
+ "files": [
13
+ "dist"
14
+ ],
15
+ "scripts": {
16
+ "build": "tsx ./build.mts && yarn build:types",
17
+ "build:pack": "yarn pack -o cedar-tui.tgz",
18
+ "build:types": "tsc --build --verbose",
19
+ "build:watch": "nodemon --watch src --ext \"js,jsx,ts,tsx\" --ignore dist --exec \"yarn build\"",
20
+ "prepublishOnly": "NODE_ENV=production yarn build"
21
+ },
22
+ "dependencies": {
23
+ "boxen": "5.1.2",
24
+ "chalk": "4.1.2",
25
+ "enquirer": "2.4.1",
26
+ "stdout-update": "1.6.8"
27
+ },
28
+ "devDependencies": {
29
+ "@cedarjs/framework-tools": "0.0.4",
30
+ "tsx": "4.19.3",
31
+ "typescript": "5.6.2"
32
+ },
33
+ "publishConfig": {
34
+ "access": "public"
35
+ },
36
+ "gitHead": "5b4f77f985bd86ee31ee7338312627accf0cb85b"
37
+ }