forked from anomalyco/opentui
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscroll-example.ts
More file actions
214 lines (185 loc) · 7.55 KB
/
scroll-example.ts
File metadata and controls
214 lines (185 loc) · 7.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import {
ASCIIFontRenderable,
BoxRenderable,
type CliRenderer,
createCliRenderer,
TextRenderable,
RGBA,
t,
fg,
bold,
underline,
italic,
} from "../index"
import { ScrollBoxRenderable } from "../renderables/ScrollBox"
import { setupCommonDemoKeys } from "./lib/standalone-keys"
let scrollBox: ScrollBoxRenderable | null = null
let renderer: CliRenderer | null = null
let mainContainer: BoxRenderable | null = null
let instructionsBox: BoxRenderable | null = null
let nextIndex = 1000
function addBox(i: number) {
if (!renderer || !scrollBox) return
const box = new BoxRenderable(renderer, {
id: `box-${i + 1}`,
width: "auto",
padding: 1,
marginBottom: 1,
backgroundColor: i % 2 === 0 ? "#292e42" : "#2f3449",
})
const content = makeMultilineContent(i)
const text = new TextRenderable(renderer, {
content,
})
box.add(text)
scrollBox.add(box)
}
function addAsciiRenderable(i: number) {
if (!renderer || !scrollBox) return
const fonts = ["tiny", "block", "shade", "slick"] as const
const font = fonts[i % fonts.length]
const colors = [
[RGBA.fromInts(166, 227, 161, 255), RGBA.fromInts(122, 162, 247, 255)],
[RGBA.fromInts(247, 118, 142, 255), RGBA.fromInts(245, 194, 231, 255)],
[RGBA.fromInts(125, 196, 228, 255), RGBA.fromInts(199, 146, 234, 255)],
[RGBA.fromInts(244, 191, 117, 255), RGBA.fromInts(249, 226, 175, 255)],
][i % 4]
const longText =
`ASCII FONT RENDERABLE #${i + 1} - ${font.toUpperCase()} STYLE - This is an extremely long piece of text that will definitely exceed the width of the scrollbox and trigger horizontal scrolling functionality. `.repeat(
15,
) +
`Additional content includes: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. `.repeat(
12,
) +
`The quick brown fox jumps over the lazy dog while the sly red panda silently observes from the treetops, contemplating the mysteries of the universe and wondering about the meaning of life. Meanwhile, technology continues to advance at an unprecedented rate, bringing both amazing opportunities and challenging ethical dilemmas to humanity's doorstep. From artificial intelligence to quantum computing, the future holds limitless possibilities that our ancestors could only dream of in their wildest imaginations.`.repeat(
8,
)
const asciiRenderable = new ASCIIFontRenderable(renderer, {
id: `ascii-${i + 1}`,
text: longText,
font: font,
fg: colors,
bg: RGBA.fromInts(26, 27, 38, 255),
selectionBg: "#f7768e",
selectionFg: "#c0caf5",
zIndex: 10,
})
scrollBox.add(asciiRenderable)
}
function makeMultilineContent(i: number) {
const palette = [fg("#7aa2f7"), fg("#9ece6a"), fg("#f7768e"), fg("#7dcfff"), fg("#bb9af7"), fg("#e0af68")]
const colorize = palette[i % palette.length]
const id = (i + 1).toString().padStart(4, "0")
const tag = i % 3 === 0 ? underline("INFO") : i % 3 === 1 ? bold("WARN") : bold(fg("#f7768e")("ERROR"))
const barUnits = 10 + (i % 30)
const bar = "█".repeat(Math.floor(barUnits * 0.6)).padEnd(barUnits, "░")
const details = "data ".repeat((i % 4) + 2)
return t`${fg("#565f89")(`[${id}]`)} ${bold(colorize(`Box ${i + 1}`))} ${fg("#565f89")("|")} ${tag}
${fg("#9aa5ce")("Multiline content with mixed styles for stress testing.")}
${colorize("• Title:")} ${bold(italic(`Lorem ipsum ${i}`))}
${fg("#9ece6a")("• Detail A:")} ${fg("#c0caf5")(details.trim())}
${fg("#bb9af7")("• Detail B:")} ${fg("#a9b1d6")("The quick brown fox jumps over the lazy dog.")}
${fg("#7dcfff")("• Progress:")} ${fg("#73daca")(bar)} ${fg("#565f89")(barUnits)}
${fg("#565f89")("— end of box —")}`
}
export function run(rendererInstance: CliRenderer): void {
renderer = rendererInstance
renderer.setBackgroundColor("#1a1b26")
mainContainer = new BoxRenderable(renderer, {
id: "main-container",
flexGrow: 1,
maxHeight: "100%",
maxWidth: "100%",
flexDirection: "column",
backgroundColor: "#1a1b26",
})
scrollBox = new ScrollBoxRenderable(renderer, {
id: "scroll-box",
rootOptions: {
backgroundColor: "#24283b",
border: true,
},
wrapperOptions: {
backgroundColor: "#1f2335",
},
viewportOptions: {
backgroundColor: "#1a1b26",
},
contentOptions: {
backgroundColor: "#16161e",
},
scrollbarOptions: {
// showArrows: true,
trackOptions: {
foregroundColor: "#7aa2f7",
backgroundColor: "#414868",
},
},
})
instructionsBox = new BoxRenderable(renderer, {
id: "instructions",
width: "100%",
flexDirection: "column",
backgroundColor: "#2a2b3a",
paddingLeft: 1,
flexShrink: 0,
})
const instructionsText1 = new TextRenderable(renderer, {
content: t`${bold(fg("#7aa2f7")("Controls:"))} ${fg("#c0caf5")("↑/↓/PgUp/PgDn/Home/End")} ${fg("#565f89")("|")} ${bold(fg("#9ece6a")("A"))} ${fg("#c0caf5")("Toggle arrows")} ${fg("#565f89")("|")} ${bold(fg("#bb9af7")("Tab"))} ${fg("#c0caf5")("Focus scrollbox")} ${fg("#565f89")("|")} ${bold(fg("#f7768e")("N"))} ${fg("#c0caf5")("Add child")}`,
})
const instructionsText2 = new TextRenderable(renderer, {
content: t`${bold(fg("#7aa2f7")("Scrollbars:"))} ${bold(fg("#e0af68")("V"))} ${fg("#c0caf5")("Toggle vertical")} ${fg("#565f89")("|")} ${bold(fg("#f7768e")("H"))} ${fg("#c0caf5")("Toggle horizontal")}`,
})
instructionsBox.add(instructionsText1)
instructionsBox.add(instructionsText2)
mainContainer.add(scrollBox)
mainContainer.add(instructionsBox)
renderer.root.add(mainContainer)
scrollBox.focus()
// Generate 1000 boxes, each with multiline styled text
// Add an ASCII renderable at the top (index 0) for immediate visibility
addAsciiRenderable(0)
for (let index = 1; index < nextIndex; index++) {
if ((index + 1) % 100 === 0) {
addAsciiRenderable(index)
} else {
addBox(index)
}
}
rendererInstance.keyInput.on("keypress", (key) => {
if (key.name === "a" && scrollBox) {
const currentState = scrollBox.verticalScrollBar?.showArrows ?? false
scrollBox.verticalScrollBar!.showArrows = !currentState
scrollBox.horizontalScrollBar!.showArrows = !currentState
console.log(`Arrows ${!currentState ? "enabled" : "disabled"}`)
} else if (key.name === "v" && scrollBox) {
const currentState = scrollBox.verticalScrollBar.visible
scrollBox.verticalScrollBar.visible = !currentState
console.log(`Vertical scrollbar ${!currentState ? "shown" : "hidden"}`)
} else if (key.name === "h" && scrollBox) {
const currentState = scrollBox.horizontalScrollBar.visible
scrollBox.horizontalScrollBar.visible = !currentState
console.log(`Horizontal scrollbar ${!currentState ? "shown" : "hidden"}`)
} else if (key.name === "n" && scrollBox) {
addBox(nextIndex)
nextIndex++
}
})
}
export function destroy(rendererInstance: CliRenderer): void {
if (mainContainer) {
rendererInstance.root.remove(mainContainer.id)
mainContainer.destroyRecursively()
mainContainer = null
}
scrollBox = null
instructionsBox = null
renderer = null
}
if (import.meta.main) {
const renderer = await createCliRenderer({
exitOnCtrlC: true,
})
run(renderer)
setupCommonDemoKeys(renderer)
}