@cli-use/tui 0.1.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.
@@ -0,0 +1,272 @@
1
+ import { ReadStream, WriteStream } from 'node:tty';
2
+ import * as readline from 'node:readline';
3
+ import * as React from 'react';
4
+
5
+ /**
6
+ * Terminal cell representation
7
+ */
8
+ interface Cell {
9
+ char: string;
10
+ fg?: number;
11
+ bg?: number;
12
+ bold?: boolean;
13
+ dim?: boolean;
14
+ italic?: boolean;
15
+ underline?: boolean;
16
+ strikethrough?: boolean;
17
+ }
18
+ /**
19
+ * Terminal buffer - a 2D grid of cells
20
+ */
21
+ declare class Buffer {
22
+ width: number;
23
+ height: number;
24
+ cells: Cell[][];
25
+ constructor(width: number, height: number, cells?: Cell[][]);
26
+ setCell(x: number, y: number, cell: Cell): void;
27
+ getCell(x: number, y: number): Cell | undefined;
28
+ clear(): void;
29
+ resize(width: number, height: number): void;
30
+ clone(): Buffer;
31
+ }
32
+ /**
33
+ * Terminal size
34
+ */
35
+ interface Size {
36
+ cols: number;
37
+ rows: number;
38
+ }
39
+
40
+ type ProcessStdin = ReadStream & NodeJS.ReadStream;
41
+ type ProcessStdout = WriteStream & NodeJS.WriteStream;
42
+ /**
43
+ * ANSI escape codes for terminal control
44
+ */
45
+ declare const ANSI: {
46
+ readonly CLEAR_SCREEN: "\u001B[2J";
47
+ readonly RESET_CURSOR: "\u001B[H";
48
+ readonly ALTERNATE_SCREEN_ENABLE: "\u001B[?1049h";
49
+ readonly ALTERNATE_SCREEN_DISABLE: "\u001B[?1049l";
50
+ readonly HIDE_CURSOR: "\u001B[?25l";
51
+ readonly SHOW_CURSOR: "\u001B[?25h";
52
+ readonly MOVE_CURSOR: (x: number, y: number) => string;
53
+ readonly RESET_STYLE: "\u001B[0m";
54
+ readonly FG_COLOR_256: (color: number) => string;
55
+ readonly BG_COLOR_256: (color: number) => string;
56
+ readonly FG_COLOR_RGB: (r: number, g: number, b: number) => string;
57
+ readonly BG_COLOR_RGB: (r: number, g: number, b: number) => string;
58
+ readonly BOLD: "\u001B[1m";
59
+ readonly DIM: "\u001B[2m";
60
+ readonly ITALIC: "\u001B[3m";
61
+ readonly UNDERLINE: "\u001B[4m";
62
+ readonly STRIKETHROUGH: "\u001B[9m";
63
+ readonly BOLD_OFF: "\u001B[22m";
64
+ readonly DIM_OFF: "\u001B[22m";
65
+ readonly ITALIC_OFF: "\u001B[23m";
66
+ readonly UNDERLINE_OFF: "\u001B[24m";
67
+ readonly STRIKETHROUGH_OFF: "\u001B[29m";
68
+ };
69
+ /**
70
+ * Terminal interface - handles low-level terminal I/O
71
+ */
72
+ declare class Terminal {
73
+ private stdin;
74
+ private stdout;
75
+ private _size;
76
+ private rawMode;
77
+ private alternateScreen;
78
+ constructor(stdin?: ProcessStdin, stdout?: ProcessStdout);
79
+ get size(): Size;
80
+ /**
81
+ * Enable raw mode for character-by-character input
82
+ */
83
+ enableRawMode(): void;
84
+ /**
85
+ * Disable raw mode
86
+ */
87
+ disableRawMode(): void;
88
+ /**
89
+ * Enable alternate screen buffer
90
+ */
91
+ enableAlternateScreen(): void;
92
+ /**
93
+ * Disable alternate screen buffer
94
+ */
95
+ disableAlternateScreen(): void;
96
+ /**
97
+ * Hide cursor
98
+ */
99
+ hideCursor(): void;
100
+ /**
101
+ * Show cursor
102
+ */
103
+ showCursor(): void;
104
+ /**
105
+ * Clear the entire screen
106
+ */
107
+ clear(): void;
108
+ /**
109
+ * Write buffer to terminal
110
+ */
111
+ write(buffer: Buffer): void;
112
+ /**
113
+ * Build ANSI style string from cell
114
+ */
115
+ private buildStyleString;
116
+ /**
117
+ * Set up SIGWINCH handler for terminal resize
118
+ */
119
+ onResize(callback: (size: Size) => void): () => void;
120
+ /**
121
+ * Set up input handler
122
+ */
123
+ onInput(callback: (chunk: Buffer, key: readline.Key) => void): () => void;
124
+ /**
125
+ * Clean up terminal state
126
+ */
127
+ restore(): void;
128
+ }
129
+
130
+ /**
131
+ * Renderer - manages the rendering lifecycle
132
+ */
133
+ declare class Renderer {
134
+ private terminal;
135
+ private buffer?;
136
+ private previousBuffer?;
137
+ private currentSize;
138
+ private running;
139
+ constructor(terminal?: Terminal);
140
+ /**
141
+ * Start the renderer
142
+ */
143
+ start(): void;
144
+ /**
145
+ * Stop the renderer
146
+ */
147
+ stop(): void;
148
+ /**
149
+ * Get a writable buffer for the current frame
150
+ */
151
+ getBuffer(): Buffer;
152
+ /**
153
+ * Present the current buffer to the terminal
154
+ */
155
+ present(): void;
156
+ /**
157
+ * Get the current terminal size
158
+ */
159
+ getSize(): Size;
160
+ /**
161
+ * Get the terminal instance for direct access
162
+ */
163
+ getTerminal(): Terminal;
164
+ }
165
+
166
+ interface AppOptions {
167
+ fullscreen?: boolean;
168
+ alternateScreen?: boolean;
169
+ mouseCapture?: boolean;
170
+ }
171
+ /**
172
+ * useApp - Hook for managing the application lifecycle
173
+ */
174
+ declare const useApp: (options?: AppOptions) => {
175
+ renderer: Renderer | null;
176
+ size: {
177
+ cols: number;
178
+ rows: number;
179
+ };
180
+ running: boolean;
181
+ exit: () => void;
182
+ };
183
+
184
+ interface InputEvent {
185
+ key: string;
186
+ name: string;
187
+ ctrl: boolean;
188
+ meta: boolean;
189
+ shift: boolean;
190
+ sequence: string;
191
+ }
192
+ type InputCallback = (input: InputEvent) => void;
193
+ /**
194
+ * useInput - Hook for capturing keyboard input
195
+ */
196
+ declare const useInput: (callback: InputCallback, deps?: any[]) => void;
197
+ /**
198
+ * useKey - Hook for capturing specific key presses
199
+ */
200
+ declare const useKey: (keyName: string | string[], callback: () => void, deps?: any[]) => void;
201
+
202
+ /**
203
+ * useFocus - Hook for managing focus state in forms and lists
204
+ */
205
+ declare const useFocus: (initialFocus: number | undefined, itemCount: number) => {
206
+ focusedIndex: number;
207
+ focusNext: () => void;
208
+ focusPrevious: () => void;
209
+ setFocus: (index: number) => void;
210
+ isFocused: (index: number) => boolean;
211
+ };
212
+
213
+ /**
214
+ * useStdoutDimensions - Hook for getting terminal dimensions
215
+ */
216
+ declare const useStdoutDimensions: () => {
217
+ columns: number;
218
+ rows: number;
219
+ };
220
+ /**
221
+ * useStdout - Hook for stdout operations
222
+ */
223
+ declare const useStdout: () => {
224
+ write: (data: string) => void;
225
+ };
226
+
227
+ /**
228
+ * useInterval - Hook for setting up intervals
229
+ */
230
+ declare const useInterval: (callback: () => void, delay: number | null) => void;
231
+ /**
232
+ * useTimeout - Hook for setting up timeouts
233
+ */
234
+ declare const useTimeout: (callback: () => void, delay: number | null) => void;
235
+
236
+ type AppState = 'idle' | 'loading' | 'success' | 'error';
237
+ /**
238
+ * useAppState - Hook for managing application state
239
+ */
240
+ declare const useAppState: (initialState?: AppState) => {
241
+ state: AppState;
242
+ error: Error | null;
243
+ isLoading: boolean;
244
+ isSuccess: boolean;
245
+ isError: boolean;
246
+ isIdle: boolean;
247
+ setLoading: () => void;
248
+ setSuccess: () => void;
249
+ setError: (err: Error) => void;
250
+ setIdle: () => void;
251
+ };
252
+
253
+ interface ListOptions<T> {
254
+ initialItems?: T[];
255
+ initialIndex?: number;
256
+ loop?: boolean;
257
+ }
258
+ /**
259
+ * useList - Hook for managing list navigation
260
+ */
261
+ declare const useList: <T>({ initialItems, initialIndex, loop, }?: ListOptions<T>) => {
262
+ items: T[];
263
+ setItems: React.Dispatch<React.SetStateAction<T[]>>;
264
+ index: number;
265
+ setIndex: React.Dispatch<React.SetStateAction<number>>;
266
+ selectedItem: NonNullable<T> | null;
267
+ next: () => void;
268
+ previous: () => void;
269
+ select: (item: T) => void;
270
+ };
271
+
272
+ export { ANSI as A, Buffer as B, Renderer as R, Terminal as T, useAppState as a, useFocus as b, useInput as c, useInterval as d, useKey as e, useList as f, useStdout as g, useStdoutDimensions as h, useTimeout as i, useApp as u };