@pencil-agent/nano-pencil 2.0.0-beta.2 → 2.0.0-beta.3
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/dist/build-meta.json +3 -3
- package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-continuations.d.ts +17 -0
- package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-continuations.js +60 -0
- package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-stream-events.d.ts +19 -0
- package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-stream-events.js +55 -0
- package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-tool-results.d.ts +10 -0
- package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-tool-results.js +137 -0
- package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-tool-summaries.d.ts +22 -0
- package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-tool-summaries.js +64 -0
- package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop.d.ts +26 -0
- package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop.js +913 -0
- package/dist/node_modules/@pencil-agent/agent-core/dist/agent-run-result.d.ts +9 -0
- package/dist/node_modules/@pencil-agent/agent-core/dist/agent-run-result.js +32 -0
- package/dist/node_modules/@pencil-agent/agent-core/dist/agent.d.ts +215 -0
- package/dist/node_modules/@pencil-agent/agent-core/dist/agent.js +522 -0
- package/dist/node_modules/@pencil-agent/agent-core/dist/errors.d.ts +62 -0
- package/dist/node_modules/@pencil-agent/agent-core/dist/errors.js +146 -0
- package/dist/node_modules/@pencil-agent/agent-core/dist/index.d.ts +14 -0
- package/dist/node_modules/@pencil-agent/agent-core/dist/index.js +19 -0
- package/dist/node_modules/@pencil-agent/agent-core/dist/proxy.d.ts +91 -0
- package/dist/node_modules/@pencil-agent/agent-core/dist/proxy.js +279 -0
- package/dist/node_modules/@pencil-agent/agent-core/dist/structured-adaptive-agent-loop.d.ts +15 -0
- package/dist/node_modules/@pencil-agent/agent-core/dist/structured-adaptive-agent-loop.js +625 -0
- package/dist/node_modules/@pencil-agent/agent-core/dist/structured-adaptive-streaming-tool-executor.d.ts +33 -0
- package/dist/node_modules/@pencil-agent/agent-core/dist/structured-adaptive-streaming-tool-executor.js +189 -0
- package/dist/node_modules/@pencil-agent/agent-core/dist/structured-adaptive-tool-orchestration.d.ts +35 -0
- package/dist/node_modules/@pencil-agent/agent-core/dist/structured-adaptive-tool-orchestration.js +319 -0
- package/dist/node_modules/@pencil-agent/agent-core/dist/types.d.ts +417 -0
- package/dist/node_modules/@pencil-agent/agent-core/dist/types.js +13 -0
- package/dist/node_modules/@pencil-agent/agent-core/package.json +28 -0
- package/dist/node_modules/@pencil-agent/ai/dist/api-registry.d.ts +27 -0
- package/dist/node_modules/@pencil-agent/ai/dist/api-registry.js +152 -0
- package/dist/node_modules/@pencil-agent/ai/dist/cli.d.ts +2 -0
- package/dist/node_modules/@pencil-agent/ai/dist/cli.js +121 -0
- package/dist/node_modules/@pencil-agent/ai/dist/config-path.d.ts +1 -0
- package/dist/node_modules/@pencil-agent/ai/dist/config-path.js +17 -0
- package/dist/node_modules/@pencil-agent/ai/dist/debug-logger.d.ts +94 -0
- package/dist/node_modules/@pencil-agent/ai/dist/debug-logger.js +218 -0
- package/dist/node_modules/@pencil-agent/ai/dist/env-api-keys.d.ts +8 -0
- package/dist/node_modules/@pencil-agent/ai/dist/env-api-keys.js +107 -0
- package/dist/node_modules/@pencil-agent/ai/dist/env.d.ts +7 -0
- package/dist/node_modules/@pencil-agent/ai/dist/env.js +7 -0
- package/dist/node_modules/@pencil-agent/ai/dist/events.d.ts +8 -0
- package/dist/node_modules/@pencil-agent/ai/dist/events.js +7 -0
- package/dist/node_modules/@pencil-agent/ai/dist/index.d.ts +27 -0
- package/dist/node_modules/@pencil-agent/ai/dist/index.js +20 -0
- package/dist/node_modules/@pencil-agent/ai/dist/json.d.ts +7 -0
- package/dist/node_modules/@pencil-agent/ai/dist/json.js +7 -0
- package/dist/node_modules/@pencil-agent/ai/dist/models.d.ts +31 -0
- package/dist/node_modules/@pencil-agent/ai/dist/models.generated.d.ts +15159 -0
- package/dist/node_modules/@pencil-agent/ai/dist/models.generated.js +14928 -0
- package/dist/node_modules/@pencil-agent/ai/dist/models.js +60 -0
- package/dist/node_modules/@pencil-agent/ai/dist/overflow.d.ts +7 -0
- package/dist/node_modules/@pencil-agent/ai/dist/overflow.js +7 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/amazon-bedrock.d.ts +20 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/amazon-bedrock.js +606 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/anthropic.d.ts +38 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/anthropic.js +737 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/azure-openai-responses.d.ts +21 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/azure-openai-responses.js +193 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/github-copilot-headers.d.ts +13 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/github-copilot-headers.js +34 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/google-gemini-cli.d.ts +79 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/google-gemini-cli.js +753 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/google-shared.d.ts +70 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/google-shared.js +311 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/google-vertex.d.ts +20 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/google-vertex.js +380 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/google.d.ts +18 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/google.js +360 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-codex-responses.d.ts +8 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-codex-responses.js +704 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-completions.d.ts +20 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-completions.js +870 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-responses-shared.d.ts +22 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-responses-shared.js +432 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-responses.d.ts +19 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-responses.js +207 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/register-builtins.d.ts +8 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/register-builtins.js +86 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/simple-options.d.ts +13 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/simple-options.js +40 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/transform-messages.d.ts +13 -0
- package/dist/node_modules/@pencil-agent/ai/dist/providers/transform-messages.js +175 -0
- package/dist/node_modules/@pencil-agent/ai/dist/registry.d.ts +8 -0
- package/dist/node_modules/@pencil-agent/ai/dist/registry.js +8 -0
- package/dist/node_modules/@pencil-agent/ai/dist/schema.d.ts +10 -0
- package/dist/node_modules/@pencil-agent/ai/dist/schema.js +9 -0
- package/dist/node_modules/@pencil-agent/ai/dist/stream.d.ts +25 -0
- package/dist/node_modules/@pencil-agent/ai/dist/stream.js +324 -0
- package/dist/node_modules/@pencil-agent/ai/dist/types.d.ts +306 -0
- package/dist/node_modules/@pencil-agent/ai/dist/types.js +7 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/event-stream-types.d.ts +12 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/event-stream-types.js +7 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/event-stream.d.ts +31 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/event-stream.js +98 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/http-proxy.d.ts +13 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/http-proxy.js +20 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/json-parse.d.ts +14 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/json-parse.js +34 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/anthropic.d.ts +22 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/anthropic.js +109 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/decode-credential.d.ts +12 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/decode-credential.js +25 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/github-copilot.d.ts +35 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/github-copilot.js +286 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/google-antigravity.d.ts +31 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/google-antigravity.js +378 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/google-gemini-cli.d.ts +31 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/google-gemini-cli.js +483 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/index.d.ts +60 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/index.js +131 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/openai-codex.d.ts +39 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/openai-codex.js +385 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/pkce.d.ts +18 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/pkce.js +36 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/types.d.ts +52 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/types.js +7 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/overflow.d.ts +57 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/overflow.js +120 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/sanitize-unicode.d.ts +16 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/sanitize-unicode.js +20 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/typebox-helpers.d.ts +22 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/typebox-helpers.js +26 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/validation.d.ts +23 -0
- package/dist/node_modules/@pencil-agent/ai/dist/utils/validation.js +78 -0
- package/dist/node_modules/@pencil-agent/ai/package.json +94 -0
- package/dist/node_modules/@pencil-agent/tui/dist/autocomplete.d.ts +62 -0
- package/dist/node_modules/@pencil-agent/tui/dist/autocomplete.js +624 -0
- package/dist/node_modules/@pencil-agent/tui/dist/components/box.d.ts +27 -0
- package/dist/node_modules/@pencil-agent/tui/dist/components/box.js +109 -0
- package/dist/node_modules/@pencil-agent/tui/dist/components/cancellable-loader.d.ts +27 -0
- package/dist/node_modules/@pencil-agent/tui/dist/components/cancellable-loader.js +40 -0
- package/dist/node_modules/@pencil-agent/tui/dist/components/editor.d.ts +218 -0
- package/dist/node_modules/@pencil-agent/tui/dist/components/editor.js +1697 -0
- package/dist/node_modules/@pencil-agent/tui/dist/components/image.d.ts +33 -0
- package/dist/node_modules/@pencil-agent/tui/dist/components/image.js +74 -0
- package/dist/node_modules/@pencil-agent/tui/dist/components/input.d.ts +42 -0
- package/dist/node_modules/@pencil-agent/tui/dist/components/input.js +438 -0
- package/dist/node_modules/@pencil-agent/tui/dist/components/loader.d.ts +26 -0
- package/dist/node_modules/@pencil-agent/tui/dist/components/loader.js +54 -0
- package/dist/node_modules/@pencil-agent/tui/dist/components/markdown.d.ts +100 -0
- package/dist/node_modules/@pencil-agent/tui/dist/components/markdown.js +634 -0
- package/dist/node_modules/@pencil-agent/tui/dist/components/select-list.d.ts +37 -0
- package/dist/node_modules/@pencil-agent/tui/dist/components/select-list.js +157 -0
- package/dist/node_modules/@pencil-agent/tui/dist/components/settings-list.d.ts +55 -0
- package/dist/node_modules/@pencil-agent/tui/dist/components/settings-list.js +190 -0
- package/dist/node_modules/@pencil-agent/tui/dist/components/spacer.d.ts +17 -0
- package/dist/node_modules/@pencil-agent/tui/dist/components/spacer.js +28 -0
- package/dist/node_modules/@pencil-agent/tui/dist/components/text.d.ts +24 -0
- package/dist/node_modules/@pencil-agent/tui/dist/components/text.js +94 -0
- package/dist/node_modules/@pencil-agent/tui/dist/components/truncated-text.d.ts +18 -0
- package/dist/node_modules/@pencil-agent/tui/dist/components/truncated-text.js +56 -0
- package/dist/node_modules/@pencil-agent/tui/dist/editor-component.d.ts +51 -0
- package/dist/node_modules/@pencil-agent/tui/dist/editor-component.js +7 -0
- package/dist/node_modules/@pencil-agent/tui/dist/fuzzy.d.ts +32 -0
- package/dist/node_modules/@pencil-agent/tui/dist/fuzzy.js +152 -0
- package/dist/node_modules/@pencil-agent/tui/dist/index.d.ts +28 -0
- package/dist/node_modules/@pencil-agent/tui/dist/index.js +37 -0
- package/dist/node_modules/@pencil-agent/tui/dist/keybindings.d.ts +44 -0
- package/dist/node_modules/@pencil-agent/tui/dist/keybindings.js +119 -0
- package/dist/node_modules/@pencil-agent/tui/dist/keys.d.ts +149 -0
- package/dist/node_modules/@pencil-agent/tui/dist/keys.js +948 -0
- package/dist/node_modules/@pencil-agent/tui/dist/kill-ring.d.ts +33 -0
- package/dist/node_modules/@pencil-agent/tui/dist/kill-ring.js +49 -0
- package/dist/node_modules/@pencil-agent/tui/dist/stdin-buffer.d.ts +38 -0
- package/dist/node_modules/@pencil-agent/tui/dist/stdin-buffer.js +307 -0
- package/dist/node_modules/@pencil-agent/tui/dist/terminal-image.d.ts +73 -0
- package/dist/node_modules/@pencil-agent/tui/dist/terminal-image.js +287 -0
- package/dist/node_modules/@pencil-agent/tui/dist/terminal.d.ts +86 -0
- package/dist/node_modules/@pencil-agent/tui/dist/terminal.js +266 -0
- package/dist/node_modules/@pencil-agent/tui/dist/tui.d.ts +219 -0
- package/dist/node_modules/@pencil-agent/tui/dist/tui.js +1001 -0
- package/dist/node_modules/@pencil-agent/tui/dist/undo-stack.d.ts +22 -0
- package/dist/node_modules/@pencil-agent/tui/dist/undo-stack.js +30 -0
- package/dist/node_modules/@pencil-agent/tui/dist/utils.d.ts +83 -0
- package/dist/node_modules/@pencil-agent/tui/dist/utils.js +811 -0
- package/dist/node_modules/@pencil-agent/tui/package.json +37 -0
- package/package.json +3 -2
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* [WHO]: SelectItem, SelectListTheme, SelectList
|
|
3
|
+
* [FROM]: Depends on ../keybindings.js, ../utils.js
|
|
4
|
+
* [TO]: Consumed by core/lib/tui/src/index.ts
|
|
5
|
+
* [HERE]: core/lib/tui/src/components/select-list.ts -
|
|
6
|
+
*/
|
|
7
|
+
import { getEditorKeybindings } from "../keybindings.js";
|
|
8
|
+
import { truncateToWidth } from "../utils.js";
|
|
9
|
+
const normalizeToSingleLine = (text) => text.replace(/[\r\n]+/g, " ").trim();
|
|
10
|
+
export class SelectList {
|
|
11
|
+
items = [];
|
|
12
|
+
filteredItems = [];
|
|
13
|
+
selectedIndex = 0;
|
|
14
|
+
maxVisible = 5;
|
|
15
|
+
theme;
|
|
16
|
+
onSelect;
|
|
17
|
+
onCancel;
|
|
18
|
+
onSelectionChange;
|
|
19
|
+
constructor(items, maxVisible, theme) {
|
|
20
|
+
this.items = items;
|
|
21
|
+
this.filteredItems = items;
|
|
22
|
+
this.maxVisible = maxVisible;
|
|
23
|
+
this.theme = theme;
|
|
24
|
+
}
|
|
25
|
+
setFilter(filter) {
|
|
26
|
+
this.filteredItems = this.items.filter((item) => item.value.toLowerCase().startsWith(filter.toLowerCase()));
|
|
27
|
+
// Reset selection when filter changes
|
|
28
|
+
this.selectedIndex = 0;
|
|
29
|
+
}
|
|
30
|
+
setSelectedIndex(index) {
|
|
31
|
+
this.selectedIndex = Math.max(0, Math.min(index, this.filteredItems.length - 1));
|
|
32
|
+
}
|
|
33
|
+
invalidate() {
|
|
34
|
+
// No cached state to invalidate currently
|
|
35
|
+
}
|
|
36
|
+
render(width) {
|
|
37
|
+
const lines = [];
|
|
38
|
+
// If no items match filter, show message
|
|
39
|
+
if (this.filteredItems.length === 0) {
|
|
40
|
+
lines.push(this.theme.noMatch(" No matching commands"));
|
|
41
|
+
return lines;
|
|
42
|
+
}
|
|
43
|
+
// Calculate visible range with scrolling
|
|
44
|
+
const startIndex = Math.max(0, Math.min(this.selectedIndex - Math.floor(this.maxVisible / 2), this.filteredItems.length - this.maxVisible));
|
|
45
|
+
const endIndex = Math.min(startIndex + this.maxVisible, this.filteredItems.length);
|
|
46
|
+
// Render visible items
|
|
47
|
+
for (let i = startIndex; i < endIndex; i++) {
|
|
48
|
+
const item = this.filteredItems[i];
|
|
49
|
+
if (!item)
|
|
50
|
+
continue;
|
|
51
|
+
const isSelected = i === this.selectedIndex;
|
|
52
|
+
const descriptionSingleLine = item.description ? normalizeToSingleLine(item.description) : undefined;
|
|
53
|
+
let line = "";
|
|
54
|
+
if (isSelected) {
|
|
55
|
+
// Use arrow indicator for selection - entire line uses selectedText color
|
|
56
|
+
const prefixWidth = 2; // "→ " is 2 characters visually
|
|
57
|
+
const displayValue = item.label || item.value;
|
|
58
|
+
if (descriptionSingleLine && width > 40) {
|
|
59
|
+
// Calculate how much space we have for value + description
|
|
60
|
+
const maxValueWidth = Math.min(30, width - prefixWidth - 4);
|
|
61
|
+
const truncatedValue = truncateToWidth(displayValue, maxValueWidth, "");
|
|
62
|
+
const spacing = " ".repeat(Math.max(1, 32 - truncatedValue.length));
|
|
63
|
+
// Calculate remaining space for description using visible widths
|
|
64
|
+
const descriptionStart = prefixWidth + truncatedValue.length + spacing.length;
|
|
65
|
+
const remainingWidth = width - descriptionStart - 2; // -2 for safety
|
|
66
|
+
if (remainingWidth > 10) {
|
|
67
|
+
const truncatedDesc = truncateToWidth(descriptionSingleLine, remainingWidth, "");
|
|
68
|
+
// Apply selectedText to entire line content
|
|
69
|
+
line = this.theme.selectedText(`→ ${truncatedValue}${spacing}${truncatedDesc}`);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
// Not enough space for description
|
|
73
|
+
const maxWidth = width - prefixWidth - 2;
|
|
74
|
+
line = this.theme.selectedText(`→ ${truncateToWidth(displayValue, maxWidth, "")}`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
// No description or not enough width
|
|
79
|
+
const maxWidth = width - prefixWidth - 2;
|
|
80
|
+
line = this.theme.selectedText(`→ ${truncateToWidth(displayValue, maxWidth, "")}`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
const displayValue = item.label || item.value;
|
|
85
|
+
const prefix = " ";
|
|
86
|
+
if (descriptionSingleLine && width > 40) {
|
|
87
|
+
// Calculate how much space we have for value + description
|
|
88
|
+
const maxValueWidth = Math.min(30, width - prefix.length - 4);
|
|
89
|
+
const truncatedValue = truncateToWidth(displayValue, maxValueWidth, "");
|
|
90
|
+
const spacing = " ".repeat(Math.max(1, 32 - truncatedValue.length));
|
|
91
|
+
// Calculate remaining space for description
|
|
92
|
+
const descriptionStart = prefix.length + truncatedValue.length + spacing.length;
|
|
93
|
+
const remainingWidth = width - descriptionStart - 2; // -2 for safety
|
|
94
|
+
if (remainingWidth > 10) {
|
|
95
|
+
const truncatedDesc = truncateToWidth(descriptionSingleLine, remainingWidth, "");
|
|
96
|
+
const descText = this.theme.description(spacing + truncatedDesc);
|
|
97
|
+
line = prefix + truncatedValue + descText;
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
// Not enough space for description
|
|
101
|
+
const maxWidth = width - prefix.length - 2;
|
|
102
|
+
line = prefix + truncateToWidth(displayValue, maxWidth, "");
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
// No description or not enough width
|
|
107
|
+
const maxWidth = width - prefix.length - 2;
|
|
108
|
+
line = prefix + truncateToWidth(displayValue, maxWidth, "");
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
lines.push(line);
|
|
112
|
+
}
|
|
113
|
+
// Add scroll indicators if needed
|
|
114
|
+
if (startIndex > 0 || endIndex < this.filteredItems.length) {
|
|
115
|
+
const scrollText = ` (${this.selectedIndex + 1}/${this.filteredItems.length})`;
|
|
116
|
+
// Truncate if too long for terminal
|
|
117
|
+
lines.push(this.theme.scrollInfo(truncateToWidth(scrollText, width - 2, "")));
|
|
118
|
+
}
|
|
119
|
+
return lines;
|
|
120
|
+
}
|
|
121
|
+
handleInput(keyData) {
|
|
122
|
+
const kb = getEditorKeybindings();
|
|
123
|
+
// Up arrow - wrap to bottom when at top
|
|
124
|
+
if (kb.matches(keyData, "selectUp")) {
|
|
125
|
+
this.selectedIndex = this.selectedIndex === 0 ? this.filteredItems.length - 1 : this.selectedIndex - 1;
|
|
126
|
+
this.notifySelectionChange();
|
|
127
|
+
}
|
|
128
|
+
// Down arrow - wrap to top when at bottom
|
|
129
|
+
else if (kb.matches(keyData, "selectDown")) {
|
|
130
|
+
this.selectedIndex = this.selectedIndex === this.filteredItems.length - 1 ? 0 : this.selectedIndex + 1;
|
|
131
|
+
this.notifySelectionChange();
|
|
132
|
+
}
|
|
133
|
+
// Enter
|
|
134
|
+
else if (kb.matches(keyData, "selectConfirm")) {
|
|
135
|
+
const selectedItem = this.filteredItems[this.selectedIndex];
|
|
136
|
+
if (selectedItem && this.onSelect) {
|
|
137
|
+
this.onSelect(selectedItem);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
// Escape or Ctrl+C
|
|
141
|
+
else if (kb.matches(keyData, "selectCancel")) {
|
|
142
|
+
if (this.onCancel) {
|
|
143
|
+
this.onCancel();
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
notifySelectionChange() {
|
|
148
|
+
const selectedItem = this.filteredItems[this.selectedIndex];
|
|
149
|
+
if (selectedItem && this.onSelectionChange) {
|
|
150
|
+
this.onSelectionChange(selectedItem);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
getSelectedItem() {
|
|
154
|
+
const item = this.filteredItems[this.selectedIndex];
|
|
155
|
+
return item || null;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* [WHO]: SettingItem, SettingsListTheme, SettingsListOptions, SettingsList
|
|
3
|
+
* [FROM]: Depends on ../fuzzy.js, ../keybindings.js, ../utils.js, ./input.js
|
|
4
|
+
* [TO]: Consumed by core/lib/tui/src/index.ts
|
|
5
|
+
* [HERE]: core/lib/tui/src/components/settings-list.ts -
|
|
6
|
+
*/
|
|
7
|
+
import type { Component } from "../tui.js";
|
|
8
|
+
export interface SettingItem {
|
|
9
|
+
/** Unique identifier for this setting */
|
|
10
|
+
id: string;
|
|
11
|
+
/** Display label (left side) */
|
|
12
|
+
label: string;
|
|
13
|
+
/** Optional description shown when selected */
|
|
14
|
+
description?: string;
|
|
15
|
+
/** Current value to display (right side) */
|
|
16
|
+
currentValue: string;
|
|
17
|
+
/** If provided, Enter/Space cycles through these values */
|
|
18
|
+
values?: string[];
|
|
19
|
+
/** If provided, Enter opens this submenu. Receives current value and done callback. */
|
|
20
|
+
submenu?: (currentValue: string, done: (selectedValue?: string) => void) => Component;
|
|
21
|
+
}
|
|
22
|
+
export interface SettingsListTheme {
|
|
23
|
+
label: (text: string, selected: boolean) => string;
|
|
24
|
+
value: (text: string, selected: boolean) => string;
|
|
25
|
+
description: (text: string) => string;
|
|
26
|
+
cursor: string;
|
|
27
|
+
hint: (text: string) => string;
|
|
28
|
+
}
|
|
29
|
+
export interface SettingsListOptions {
|
|
30
|
+
enableSearch?: boolean;
|
|
31
|
+
}
|
|
32
|
+
export declare class SettingsList implements Component {
|
|
33
|
+
private items;
|
|
34
|
+
private filteredItems;
|
|
35
|
+
private theme;
|
|
36
|
+
private selectedIndex;
|
|
37
|
+
private maxVisible;
|
|
38
|
+
private onChange;
|
|
39
|
+
private onCancel;
|
|
40
|
+
private searchInput?;
|
|
41
|
+
private searchEnabled;
|
|
42
|
+
private submenuComponent;
|
|
43
|
+
private submenuItemIndex;
|
|
44
|
+
constructor(items: SettingItem[], maxVisible: number, theme: SettingsListTheme, onChange: (id: string, newValue: string) => void, onCancel: () => void, options?: SettingsListOptions);
|
|
45
|
+
/** Update an item's currentValue */
|
|
46
|
+
updateValue(id: string, newValue: string): void;
|
|
47
|
+
invalidate(): void;
|
|
48
|
+
render(width: number): string[];
|
|
49
|
+
private renderMainList;
|
|
50
|
+
handleInput(data: string): void;
|
|
51
|
+
private activateItem;
|
|
52
|
+
private closeSubmenu;
|
|
53
|
+
private applyFilter;
|
|
54
|
+
private addHintLine;
|
|
55
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* [WHO]: SettingItem, SettingsListTheme, SettingsListOptions, SettingsList
|
|
3
|
+
* [FROM]: Depends on ../fuzzy.js, ../keybindings.js, ../utils.js, ./input.js
|
|
4
|
+
* [TO]: Consumed by core/lib/tui/src/index.ts
|
|
5
|
+
* [HERE]: core/lib/tui/src/components/settings-list.ts -
|
|
6
|
+
*/
|
|
7
|
+
import { fuzzyFilter } from "../fuzzy.js";
|
|
8
|
+
import { getEditorKeybindings } from "../keybindings.js";
|
|
9
|
+
import { truncateToWidth, visibleWidth, wrapTextWithAnsi } from "../utils.js";
|
|
10
|
+
import { Input } from "./input.js";
|
|
11
|
+
export class SettingsList {
|
|
12
|
+
items;
|
|
13
|
+
filteredItems;
|
|
14
|
+
theme;
|
|
15
|
+
selectedIndex = 0;
|
|
16
|
+
maxVisible;
|
|
17
|
+
onChange;
|
|
18
|
+
onCancel;
|
|
19
|
+
searchInput;
|
|
20
|
+
searchEnabled;
|
|
21
|
+
// Submenu state
|
|
22
|
+
submenuComponent = null;
|
|
23
|
+
submenuItemIndex = null;
|
|
24
|
+
constructor(items, maxVisible, theme, onChange, onCancel, options = {}) {
|
|
25
|
+
this.items = items;
|
|
26
|
+
this.filteredItems = items;
|
|
27
|
+
this.maxVisible = maxVisible;
|
|
28
|
+
this.theme = theme;
|
|
29
|
+
this.onChange = onChange;
|
|
30
|
+
this.onCancel = onCancel;
|
|
31
|
+
this.searchEnabled = options.enableSearch ?? false;
|
|
32
|
+
if (this.searchEnabled) {
|
|
33
|
+
this.searchInput = new Input();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/** Update an item's currentValue */
|
|
37
|
+
updateValue(id, newValue) {
|
|
38
|
+
const item = this.items.find((i) => i.id === id);
|
|
39
|
+
if (item) {
|
|
40
|
+
item.currentValue = newValue;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
invalidate() {
|
|
44
|
+
this.submenuComponent?.invalidate?.();
|
|
45
|
+
}
|
|
46
|
+
render(width) {
|
|
47
|
+
// If submenu is active, render it instead
|
|
48
|
+
if (this.submenuComponent) {
|
|
49
|
+
return this.submenuComponent.render(width);
|
|
50
|
+
}
|
|
51
|
+
return this.renderMainList(width);
|
|
52
|
+
}
|
|
53
|
+
renderMainList(width) {
|
|
54
|
+
const lines = [];
|
|
55
|
+
if (this.searchEnabled && this.searchInput) {
|
|
56
|
+
lines.push(...this.searchInput.render(width));
|
|
57
|
+
lines.push("");
|
|
58
|
+
}
|
|
59
|
+
if (this.items.length === 0) {
|
|
60
|
+
lines.push(this.theme.hint(" No settings available"));
|
|
61
|
+
if (this.searchEnabled) {
|
|
62
|
+
this.addHintLine(lines, width);
|
|
63
|
+
}
|
|
64
|
+
return lines;
|
|
65
|
+
}
|
|
66
|
+
const displayItems = this.searchEnabled ? this.filteredItems : this.items;
|
|
67
|
+
if (displayItems.length === 0) {
|
|
68
|
+
lines.push(truncateToWidth(this.theme.hint(" No matching settings"), width));
|
|
69
|
+
this.addHintLine(lines, width);
|
|
70
|
+
return lines;
|
|
71
|
+
}
|
|
72
|
+
// Calculate visible range with scrolling
|
|
73
|
+
const startIndex = Math.max(0, Math.min(this.selectedIndex - Math.floor(this.maxVisible / 2), displayItems.length - this.maxVisible));
|
|
74
|
+
const endIndex = Math.min(startIndex + this.maxVisible, displayItems.length);
|
|
75
|
+
// Calculate max label width for alignment
|
|
76
|
+
const maxLabelWidth = Math.min(30, Math.max(...this.items.map((item) => visibleWidth(item.label))));
|
|
77
|
+
// Render visible items
|
|
78
|
+
for (let i = startIndex; i < endIndex; i++) {
|
|
79
|
+
const item = displayItems[i];
|
|
80
|
+
if (!item)
|
|
81
|
+
continue;
|
|
82
|
+
const isSelected = i === this.selectedIndex;
|
|
83
|
+
const prefix = isSelected ? this.theme.cursor : " ";
|
|
84
|
+
const prefixWidth = visibleWidth(prefix);
|
|
85
|
+
// Pad label to align values
|
|
86
|
+
const labelPadded = item.label + " ".repeat(Math.max(0, maxLabelWidth - visibleWidth(item.label)));
|
|
87
|
+
const labelText = this.theme.label(labelPadded, isSelected);
|
|
88
|
+
// Calculate space for value
|
|
89
|
+
const separator = " ";
|
|
90
|
+
const usedWidth = prefixWidth + maxLabelWidth + visibleWidth(separator);
|
|
91
|
+
const valueMaxWidth = width - usedWidth - 2;
|
|
92
|
+
const valueText = this.theme.value(truncateToWidth(item.currentValue, valueMaxWidth, ""), isSelected);
|
|
93
|
+
lines.push(truncateToWidth(prefix + labelText + separator + valueText, width));
|
|
94
|
+
}
|
|
95
|
+
// Add scroll indicator if needed
|
|
96
|
+
if (startIndex > 0 || endIndex < displayItems.length) {
|
|
97
|
+
const scrollText = ` (${this.selectedIndex + 1}/${displayItems.length})`;
|
|
98
|
+
lines.push(this.theme.hint(truncateToWidth(scrollText, width - 2, "")));
|
|
99
|
+
}
|
|
100
|
+
// Add description for selected item
|
|
101
|
+
const selectedItem = displayItems[this.selectedIndex];
|
|
102
|
+
if (selectedItem?.description) {
|
|
103
|
+
lines.push("");
|
|
104
|
+
const wrappedDesc = wrapTextWithAnsi(selectedItem.description, width - 4);
|
|
105
|
+
for (const line of wrappedDesc) {
|
|
106
|
+
lines.push(this.theme.description(` ${line}`));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
// Add hint
|
|
110
|
+
this.addHintLine(lines, width);
|
|
111
|
+
return lines;
|
|
112
|
+
}
|
|
113
|
+
handleInput(data) {
|
|
114
|
+
// If submenu is active, delegate all input to it
|
|
115
|
+
// The submenu's onCancel (triggered by escape) will call done() which closes it
|
|
116
|
+
if (this.submenuComponent) {
|
|
117
|
+
this.submenuComponent.handleInput?.(data);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
// Main list input handling
|
|
121
|
+
const kb = getEditorKeybindings();
|
|
122
|
+
const displayItems = this.searchEnabled ? this.filteredItems : this.items;
|
|
123
|
+
if (kb.matches(data, "selectUp")) {
|
|
124
|
+
if (displayItems.length === 0)
|
|
125
|
+
return;
|
|
126
|
+
this.selectedIndex = this.selectedIndex === 0 ? displayItems.length - 1 : this.selectedIndex - 1;
|
|
127
|
+
}
|
|
128
|
+
else if (kb.matches(data, "selectDown")) {
|
|
129
|
+
if (displayItems.length === 0)
|
|
130
|
+
return;
|
|
131
|
+
this.selectedIndex = this.selectedIndex === displayItems.length - 1 ? 0 : this.selectedIndex + 1;
|
|
132
|
+
}
|
|
133
|
+
else if (kb.matches(data, "selectConfirm") || data === " ") {
|
|
134
|
+
this.activateItem();
|
|
135
|
+
}
|
|
136
|
+
else if (kb.matches(data, "selectCancel")) {
|
|
137
|
+
this.onCancel();
|
|
138
|
+
}
|
|
139
|
+
else if (this.searchEnabled && this.searchInput) {
|
|
140
|
+
const sanitized = data.replace(/ /g, "");
|
|
141
|
+
if (!sanitized) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
this.searchInput.handleInput(sanitized);
|
|
145
|
+
this.applyFilter(this.searchInput.getValue());
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
activateItem() {
|
|
149
|
+
const item = this.searchEnabled ? this.filteredItems[this.selectedIndex] : this.items[this.selectedIndex];
|
|
150
|
+
if (!item)
|
|
151
|
+
return;
|
|
152
|
+
if (item.submenu) {
|
|
153
|
+
// Open submenu, passing current value so it can pre-select correctly
|
|
154
|
+
this.submenuItemIndex = this.selectedIndex;
|
|
155
|
+
this.submenuComponent = item.submenu(item.currentValue, (selectedValue) => {
|
|
156
|
+
if (selectedValue !== undefined) {
|
|
157
|
+
item.currentValue = selectedValue;
|
|
158
|
+
this.onChange(item.id, selectedValue);
|
|
159
|
+
}
|
|
160
|
+
this.closeSubmenu();
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
else if (item.values && item.values.length > 0) {
|
|
164
|
+
// Cycle through values
|
|
165
|
+
const currentIndex = item.values.indexOf(item.currentValue);
|
|
166
|
+
const nextIndex = (currentIndex + 1) % item.values.length;
|
|
167
|
+
const newValue = item.values[nextIndex];
|
|
168
|
+
item.currentValue = newValue;
|
|
169
|
+
this.onChange(item.id, newValue);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
closeSubmenu() {
|
|
173
|
+
this.submenuComponent = null;
|
|
174
|
+
// Restore selection to the item that opened the submenu
|
|
175
|
+
if (this.submenuItemIndex !== null) {
|
|
176
|
+
this.selectedIndex = this.submenuItemIndex;
|
|
177
|
+
this.submenuItemIndex = null;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
applyFilter(query) {
|
|
181
|
+
this.filteredItems = fuzzyFilter(this.items, query, (item) => item.label);
|
|
182
|
+
this.selectedIndex = 0;
|
|
183
|
+
}
|
|
184
|
+
addHintLine(lines, width) {
|
|
185
|
+
lines.push("");
|
|
186
|
+
lines.push(truncateToWidth(this.theme.hint(this.searchEnabled
|
|
187
|
+
? " Type to search · Enter/Space to change · Esc to cancel"
|
|
188
|
+
: " Enter/Space to change · Esc to cancel"), width));
|
|
189
|
+
}
|
|
190
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* [WHO]: Spacer
|
|
3
|
+
* [FROM]: No external dependencies
|
|
4
|
+
* [TO]: Consumed by core/lib/tui/src/index.ts
|
|
5
|
+
* [HERE]: core/lib/tui/src/components/spacer.ts -
|
|
6
|
+
*/
|
|
7
|
+
import type { Component } from "../tui.js";
|
|
8
|
+
/**
|
|
9
|
+
* Spacer component that renders empty lines
|
|
10
|
+
*/
|
|
11
|
+
export declare class Spacer implements Component {
|
|
12
|
+
private lines;
|
|
13
|
+
constructor(lines?: number);
|
|
14
|
+
setLines(lines: number): void;
|
|
15
|
+
invalidate(): void;
|
|
16
|
+
render(_width: number): string[];
|
|
17
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* [WHO]: Spacer
|
|
3
|
+
* [FROM]: No external dependencies
|
|
4
|
+
* [TO]: Consumed by core/lib/tui/src/index.ts
|
|
5
|
+
* [HERE]: core/lib/tui/src/components/spacer.ts -
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Spacer component that renders empty lines
|
|
9
|
+
*/
|
|
10
|
+
export class Spacer {
|
|
11
|
+
lines;
|
|
12
|
+
constructor(lines = 1) {
|
|
13
|
+
this.lines = lines;
|
|
14
|
+
}
|
|
15
|
+
setLines(lines) {
|
|
16
|
+
this.lines = lines;
|
|
17
|
+
}
|
|
18
|
+
invalidate() {
|
|
19
|
+
// No cached state to invalidate currently
|
|
20
|
+
}
|
|
21
|
+
render(_width) {
|
|
22
|
+
const result = [];
|
|
23
|
+
for (let i = 0; i < this.lines; i++) {
|
|
24
|
+
result.push("");
|
|
25
|
+
}
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* [WHO]: Text
|
|
3
|
+
* [FROM]: Depends on ../utils.js
|
|
4
|
+
* [TO]: Consumed by core/lib/tui/src/index.ts
|
|
5
|
+
* [HERE]: core/lib/tui/src/components/text.ts -
|
|
6
|
+
*/
|
|
7
|
+
import type { Component } from "../tui.js";
|
|
8
|
+
/**
|
|
9
|
+
* Text component - displays multi-line text with word wrapping
|
|
10
|
+
*/
|
|
11
|
+
export declare class Text implements Component {
|
|
12
|
+
private text;
|
|
13
|
+
private paddingX;
|
|
14
|
+
private paddingY;
|
|
15
|
+
private customBgFn?;
|
|
16
|
+
private cachedText?;
|
|
17
|
+
private cachedWidth?;
|
|
18
|
+
private cachedLines?;
|
|
19
|
+
constructor(text?: string, paddingX?: number, paddingY?: number, customBgFn?: (text: string) => string);
|
|
20
|
+
setText(text: string): void;
|
|
21
|
+
setCustomBgFn(customBgFn?: (text: string) => string): void;
|
|
22
|
+
invalidate(): void;
|
|
23
|
+
render(width: number): string[];
|
|
24
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* [WHO]: Text
|
|
3
|
+
* [FROM]: Depends on ../utils.js
|
|
4
|
+
* [TO]: Consumed by core/lib/tui/src/index.ts
|
|
5
|
+
* [HERE]: core/lib/tui/src/components/text.ts -
|
|
6
|
+
*/
|
|
7
|
+
import { applyBackgroundToLine, visibleWidth, wrapTextWithAnsi } from "../utils.js";
|
|
8
|
+
/**
|
|
9
|
+
* Text component - displays multi-line text with word wrapping
|
|
10
|
+
*/
|
|
11
|
+
export class Text {
|
|
12
|
+
text;
|
|
13
|
+
paddingX; // Left/right padding
|
|
14
|
+
paddingY; // Top/bottom padding
|
|
15
|
+
customBgFn;
|
|
16
|
+
// Cache for rendered output
|
|
17
|
+
cachedText;
|
|
18
|
+
cachedWidth;
|
|
19
|
+
cachedLines;
|
|
20
|
+
constructor(text = "", paddingX = 1, paddingY = 1, customBgFn) {
|
|
21
|
+
this.text = text;
|
|
22
|
+
this.paddingX = paddingX;
|
|
23
|
+
this.paddingY = paddingY;
|
|
24
|
+
this.customBgFn = customBgFn;
|
|
25
|
+
}
|
|
26
|
+
setText(text) {
|
|
27
|
+
this.text = text;
|
|
28
|
+
this.cachedText = undefined;
|
|
29
|
+
this.cachedWidth = undefined;
|
|
30
|
+
this.cachedLines = undefined;
|
|
31
|
+
}
|
|
32
|
+
setCustomBgFn(customBgFn) {
|
|
33
|
+
this.customBgFn = customBgFn;
|
|
34
|
+
this.cachedText = undefined;
|
|
35
|
+
this.cachedWidth = undefined;
|
|
36
|
+
this.cachedLines = undefined;
|
|
37
|
+
}
|
|
38
|
+
invalidate() {
|
|
39
|
+
this.cachedText = undefined;
|
|
40
|
+
this.cachedWidth = undefined;
|
|
41
|
+
this.cachedLines = undefined;
|
|
42
|
+
}
|
|
43
|
+
render(width) {
|
|
44
|
+
// Check cache
|
|
45
|
+
if (this.cachedLines && this.cachedText === this.text && this.cachedWidth === width) {
|
|
46
|
+
return this.cachedLines;
|
|
47
|
+
}
|
|
48
|
+
// Don't render anything if there's no actual text
|
|
49
|
+
if (!this.text || this.text.trim() === "") {
|
|
50
|
+
const result = [];
|
|
51
|
+
this.cachedText = this.text;
|
|
52
|
+
this.cachedWidth = width;
|
|
53
|
+
this.cachedLines = result;
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
// Replace tabs with 3 spaces
|
|
57
|
+
const normalizedText = this.text.replace(/\t/g, " ");
|
|
58
|
+
// Calculate content width (subtract left/right margins)
|
|
59
|
+
const contentWidth = Math.max(1, width - this.paddingX * 2);
|
|
60
|
+
// Wrap text (this preserves ANSI codes but does NOT pad)
|
|
61
|
+
const wrappedLines = wrapTextWithAnsi(normalizedText, contentWidth);
|
|
62
|
+
// Add margins and background to each line
|
|
63
|
+
const leftMargin = " ".repeat(this.paddingX);
|
|
64
|
+
const rightMargin = " ".repeat(this.paddingX);
|
|
65
|
+
const contentLines = [];
|
|
66
|
+
for (const line of wrappedLines) {
|
|
67
|
+
// Add margins
|
|
68
|
+
const lineWithMargins = leftMargin + line + rightMargin;
|
|
69
|
+
// Apply background if specified (this also pads to full width)
|
|
70
|
+
if (this.customBgFn) {
|
|
71
|
+
contentLines.push(applyBackgroundToLine(lineWithMargins, width, this.customBgFn));
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
// No background - just pad to width with spaces
|
|
75
|
+
const visibleLen = visibleWidth(lineWithMargins);
|
|
76
|
+
const paddingNeeded = Math.max(0, width - visibleLen);
|
|
77
|
+
contentLines.push(lineWithMargins + " ".repeat(paddingNeeded));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
// Add top/bottom padding (empty lines)
|
|
81
|
+
const emptyLine = " ".repeat(width);
|
|
82
|
+
const emptyLines = [];
|
|
83
|
+
for (let i = 0; i < this.paddingY; i++) {
|
|
84
|
+
const line = this.customBgFn ? applyBackgroundToLine(emptyLine, width, this.customBgFn) : emptyLine;
|
|
85
|
+
emptyLines.push(line);
|
|
86
|
+
}
|
|
87
|
+
const result = [...emptyLines, ...contentLines, ...emptyLines];
|
|
88
|
+
// Update cache
|
|
89
|
+
this.cachedText = this.text;
|
|
90
|
+
this.cachedWidth = width;
|
|
91
|
+
this.cachedLines = result;
|
|
92
|
+
return result.length > 0 ? result : [""];
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* [WHO]: TruncatedText
|
|
3
|
+
* [FROM]: Depends on ../utils.js
|
|
4
|
+
* [TO]: Consumed by core/lib/tui/src/index.ts
|
|
5
|
+
* [HERE]: core/lib/tui/src/components/truncated-text.ts -
|
|
6
|
+
*/
|
|
7
|
+
import type { Component } from "../tui.js";
|
|
8
|
+
/**
|
|
9
|
+
* Text component that truncates to fit viewport width
|
|
10
|
+
*/
|
|
11
|
+
export declare class TruncatedText implements Component {
|
|
12
|
+
private text;
|
|
13
|
+
private paddingX;
|
|
14
|
+
private paddingY;
|
|
15
|
+
constructor(text: string, paddingX?: number, paddingY?: number);
|
|
16
|
+
invalidate(): void;
|
|
17
|
+
render(width: number): string[];
|
|
18
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* [WHO]: TruncatedText
|
|
3
|
+
* [FROM]: Depends on ../utils.js
|
|
4
|
+
* [TO]: Consumed by core/lib/tui/src/index.ts
|
|
5
|
+
* [HERE]: core/lib/tui/src/components/truncated-text.ts -
|
|
6
|
+
*/
|
|
7
|
+
import { truncateToWidth, visibleWidth } from "../utils.js";
|
|
8
|
+
/**
|
|
9
|
+
* Text component that truncates to fit viewport width
|
|
10
|
+
*/
|
|
11
|
+
export class TruncatedText {
|
|
12
|
+
text;
|
|
13
|
+
paddingX;
|
|
14
|
+
paddingY;
|
|
15
|
+
constructor(text, paddingX = 0, paddingY = 0) {
|
|
16
|
+
this.text = text;
|
|
17
|
+
this.paddingX = paddingX;
|
|
18
|
+
this.paddingY = paddingY;
|
|
19
|
+
}
|
|
20
|
+
invalidate() {
|
|
21
|
+
// No cached state to invalidate currently
|
|
22
|
+
}
|
|
23
|
+
render(width) {
|
|
24
|
+
const result = [];
|
|
25
|
+
// Empty line padded to width
|
|
26
|
+
const emptyLine = " ".repeat(width);
|
|
27
|
+
// Add vertical padding above
|
|
28
|
+
for (let i = 0; i < this.paddingY; i++) {
|
|
29
|
+
result.push(emptyLine);
|
|
30
|
+
}
|
|
31
|
+
// Calculate available width after horizontal padding
|
|
32
|
+
const availableWidth = Math.max(1, width - this.paddingX * 2);
|
|
33
|
+
// Take only the first line (stop at newline)
|
|
34
|
+
let singleLineText = this.text;
|
|
35
|
+
const newlineIndex = this.text.indexOf("\n");
|
|
36
|
+
if (newlineIndex !== -1) {
|
|
37
|
+
singleLineText = this.text.substring(0, newlineIndex);
|
|
38
|
+
}
|
|
39
|
+
// Truncate text if needed (accounting for ANSI codes)
|
|
40
|
+
const displayText = truncateToWidth(singleLineText, availableWidth);
|
|
41
|
+
// Add horizontal padding
|
|
42
|
+
const leftPadding = " ".repeat(this.paddingX);
|
|
43
|
+
const rightPadding = " ".repeat(this.paddingX);
|
|
44
|
+
const lineWithPadding = leftPadding + displayText + rightPadding;
|
|
45
|
+
// Pad line to exactly width characters
|
|
46
|
+
const lineVisibleWidth = visibleWidth(lineWithPadding);
|
|
47
|
+
const paddingNeeded = Math.max(0, width - lineVisibleWidth);
|
|
48
|
+
const finalLine = lineWithPadding + " ".repeat(paddingNeeded);
|
|
49
|
+
result.push(finalLine);
|
|
50
|
+
// Add vertical padding below
|
|
51
|
+
for (let i = 0; i < this.paddingY; i++) {
|
|
52
|
+
result.push(emptyLine);
|
|
53
|
+
}
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
}
|