@oh-my-pi/pi-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.
- package/README.md +699 -0
- package/package.json +45 -0
- package/src/autocomplete.ts +577 -0
- package/src/components/box.ts +134 -0
- package/src/components/cancellable-loader.ts +39 -0
- package/src/components/editor.ts +1390 -0
- package/src/components/image.ts +87 -0
- package/src/components/input.ts +344 -0
- package/src/components/loader.ts +59 -0
- package/src/components/markdown.ts +655 -0
- package/src/components/select-list.ts +187 -0
- package/src/components/settings-list.ts +188 -0
- package/src/components/spacer.ts +28 -0
- package/src/components/tab-bar.ts +140 -0
- package/src/components/text.ts +110 -0
- package/src/components/truncated-text.ts +65 -0
- package/src/index.ts +92 -0
- package/src/keys.ts +560 -0
- package/src/symbols.ts +25 -0
- package/src/terminal-image.ts +340 -0
- package/src/terminal.ts +163 -0
- package/src/tui.ts +379 -0
- package/src/utils.ts +712 -0
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import type { Component } from "../tui";
|
|
2
|
+
import { applyBackgroundToLine, visibleWidth } from "../utils";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Box component - a container that applies padding and background to all children
|
|
6
|
+
*/
|
|
7
|
+
export class Box implements Component {
|
|
8
|
+
children: Component[] = [];
|
|
9
|
+
private paddingX: number;
|
|
10
|
+
private paddingY: number;
|
|
11
|
+
private bgFn?: (text: string) => string;
|
|
12
|
+
|
|
13
|
+
// Cache for rendered output
|
|
14
|
+
private cachedWidth?: number;
|
|
15
|
+
private cachedChildLines?: string;
|
|
16
|
+
private cachedBgSample?: string;
|
|
17
|
+
private cachedLines?: string[];
|
|
18
|
+
|
|
19
|
+
constructor(paddingX = 1, paddingY = 1, bgFn?: (text: string) => string) {
|
|
20
|
+
this.paddingX = paddingX;
|
|
21
|
+
this.paddingY = paddingY;
|
|
22
|
+
this.bgFn = bgFn;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
addChild(component: Component): void {
|
|
26
|
+
this.children.push(component);
|
|
27
|
+
this.invalidateCache();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
removeChild(component: Component): void {
|
|
31
|
+
const index = this.children.indexOf(component);
|
|
32
|
+
if (index !== -1) {
|
|
33
|
+
this.children.splice(index, 1);
|
|
34
|
+
this.invalidateCache();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
clear(): void {
|
|
39
|
+
this.children = [];
|
|
40
|
+
this.invalidateCache();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
setBgFn(bgFn?: (text: string) => string): void {
|
|
44
|
+
this.bgFn = bgFn;
|
|
45
|
+
// Don't invalidate here - we'll detect bgFn changes by sampling output
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private invalidateCache(): void {
|
|
49
|
+
this.cachedWidth = undefined;
|
|
50
|
+
this.cachedChildLines = undefined;
|
|
51
|
+
this.cachedBgSample = undefined;
|
|
52
|
+
this.cachedLines = undefined;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
invalidate(): void {
|
|
56
|
+
this.invalidateCache();
|
|
57
|
+
for (const child of this.children) {
|
|
58
|
+
child.invalidate?.();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
render(width: number): string[] {
|
|
63
|
+
if (this.children.length === 0) {
|
|
64
|
+
return [];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const contentWidth = Math.max(1, width - this.paddingX * 2);
|
|
68
|
+
const leftPad = " ".repeat(this.paddingX);
|
|
69
|
+
|
|
70
|
+
// Render all children
|
|
71
|
+
const childLines: string[] = [];
|
|
72
|
+
for (const child of this.children) {
|
|
73
|
+
const lines = child.render(contentWidth);
|
|
74
|
+
for (const line of lines) {
|
|
75
|
+
childLines.push(leftPad + line);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (childLines.length === 0) {
|
|
80
|
+
return [];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Check if bgFn output changed by sampling
|
|
84
|
+
const bgSample = this.bgFn ? this.bgFn("test") : undefined;
|
|
85
|
+
|
|
86
|
+
// Check cache validity
|
|
87
|
+
const childLinesKey = childLines.join("\n");
|
|
88
|
+
if (
|
|
89
|
+
this.cachedLines &&
|
|
90
|
+
this.cachedWidth === width &&
|
|
91
|
+
this.cachedChildLines === childLinesKey &&
|
|
92
|
+
this.cachedBgSample === bgSample
|
|
93
|
+
) {
|
|
94
|
+
return this.cachedLines;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Apply background and padding
|
|
98
|
+
const result: string[] = [];
|
|
99
|
+
|
|
100
|
+
// Top padding
|
|
101
|
+
for (let i = 0; i < this.paddingY; i++) {
|
|
102
|
+
result.push(this.applyBg("", width));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Content
|
|
106
|
+
for (const line of childLines) {
|
|
107
|
+
result.push(this.applyBg(line, width));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Bottom padding
|
|
111
|
+
for (let i = 0; i < this.paddingY; i++) {
|
|
112
|
+
result.push(this.applyBg("", width));
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Update cache
|
|
116
|
+
this.cachedWidth = width;
|
|
117
|
+
this.cachedChildLines = childLinesKey;
|
|
118
|
+
this.cachedBgSample = bgSample;
|
|
119
|
+
this.cachedLines = result;
|
|
120
|
+
|
|
121
|
+
return result;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
private applyBg(line: string, width: number): string {
|
|
125
|
+
const visLen = visibleWidth(line);
|
|
126
|
+
const padNeeded = Math.max(0, width - visLen);
|
|
127
|
+
const padded = line + " ".repeat(padNeeded);
|
|
128
|
+
|
|
129
|
+
if (this.bgFn) {
|
|
130
|
+
return applyBackgroundToLine(padded, width, this.bgFn);
|
|
131
|
+
}
|
|
132
|
+
return padded;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { isEscape } from "../keys";
|
|
2
|
+
import { Loader } from "./loader";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Loader that can be cancelled with Escape.
|
|
6
|
+
* Extends Loader with an AbortSignal for cancelling async operations.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const loader = new CancellableLoader(tui, cyan, dim, "Working...");
|
|
10
|
+
* loader.onAbort = () => done(null);
|
|
11
|
+
* doWork(loader.signal).then(done);
|
|
12
|
+
*/
|
|
13
|
+
export class CancellableLoader extends Loader {
|
|
14
|
+
private abortController = new AbortController();
|
|
15
|
+
|
|
16
|
+
/** Called when user presses Escape */
|
|
17
|
+
onAbort?: () => void;
|
|
18
|
+
|
|
19
|
+
/** AbortSignal that is aborted when user presses Escape */
|
|
20
|
+
get signal(): AbortSignal {
|
|
21
|
+
return this.abortController.signal;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Whether the loader was aborted */
|
|
25
|
+
get aborted(): boolean {
|
|
26
|
+
return this.abortController.signal.aborted;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
handleInput(data: string): void {
|
|
30
|
+
if (isEscape(data)) {
|
|
31
|
+
this.abortController.abort();
|
|
32
|
+
this.onAbort?.();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
dispose(): void {
|
|
37
|
+
this.stop();
|
|
38
|
+
}
|
|
39
|
+
}
|