opencode_offline/packages/opencode/src/cli/ui.ts

117 lines
3.0 KiB
TypeScript
Raw Normal View History

import z from "zod"
2025-06-25 06:46:43 +08:00
import { EOL } from "os"
2025-11-25 01:56:00 +08:00
import { NamedError } from "@opencode-ai/util/error"
import { logo as glyphs } from "./logo"
2025-06-11 06:10:30 +08:00
2025-06-05 23:50:54 +08:00
export namespace UI {
export const CancelledError = NamedError.create("UICancelledError", z.void())
2025-06-05 23:50:54 +08:00
export const Style = {
TEXT_HIGHLIGHT: "\x1b[96m",
TEXT_HIGHLIGHT_BOLD: "\x1b[96m\x1b[1m",
TEXT_DIM: "\x1b[90m",
TEXT_DIM_BOLD: "\x1b[90m\x1b[1m",
TEXT_NORMAL: "\x1b[0m",
TEXT_NORMAL_BOLD: "\x1b[1m",
TEXT_WARNING: "\x1b[93m",
TEXT_WARNING_BOLD: "\x1b[93m\x1b[1m",
TEXT_DANGER: "\x1b[91m",
TEXT_DANGER_BOLD: "\x1b[91m\x1b[1m",
TEXT_SUCCESS: "\x1b[92m",
TEXT_SUCCESS_BOLD: "\x1b[92m\x1b[1m",
TEXT_INFO: "\x1b[94m",
TEXT_INFO_BOLD: "\x1b[94m\x1b[1m",
}
2025-06-11 01:30:08 +08:00
export function println(...message: string[]) {
print(...message)
2025-06-25 06:46:43 +08:00
Bun.stderr.write(EOL)
2025-06-11 01:30:08 +08:00
}
2025-06-05 23:50:54 +08:00
export function print(...message: string[]) {
2025-06-11 01:30:08 +08:00
blank = false
2025-06-05 23:50:54 +08:00
Bun.stderr.write(message.join(" "))
}
2025-06-11 01:30:08 +08:00
let blank = false
2025-06-05 23:50:54 +08:00
export function empty() {
2025-06-11 01:30:08 +08:00
if (blank) return
println("" + Style.TEXT_NORMAL)
blank = true
2025-06-05 23:50:54 +08:00
}
2025-06-12 07:00:09 +08:00
export function logo(pad?: string) {
const result: string[] = []
const reset = "\x1b[0m"
const left = {
fg: Bun.color("gray", "ansi") ?? "",
shadow: "\x1b[38;5;235m",
bg: "\x1b[48;5;235m",
}
const right = {
fg: reset,
shadow: "\x1b[38;5;238m",
bg: "\x1b[48;5;238m",
}
const gap = " "
const draw = (line: string, fg: string, shadow: string, bg: string) => {
const parts: string[] = []
for (const char of line) {
if (char === "_") {
parts.push(bg, " ", reset)
continue
}
if (char === "^") {
parts.push(fg, bg, "▀", reset)
continue
}
if (char === "~") {
parts.push(shadow, "▀", reset)
continue
}
if (char === " ") {
parts.push(" ")
continue
}
parts.push(fg, char, reset)
}
return parts.join("")
}
glyphs.left.forEach((row, index) => {
2025-06-12 07:00:09 +08:00
if (pad) result.push(pad)
result.push(draw(row, left.fg, left.shadow, left.bg))
result.push(gap)
const other = glyphs.right[index] ?? ""
result.push(draw(other, right.fg, right.shadow, right.bg))
2025-06-25 06:46:43 +08:00
result.push(EOL)
})
2025-06-12 07:00:09 +08:00
return result.join("").trimEnd()
2025-06-11 03:43:14 +08:00
}
2025-06-05 23:50:54 +08:00
export async function input(prompt: string): Promise<string> {
2025-06-11 01:30:08 +08:00
const readline = require("readline")
2025-06-05 23:50:54 +08:00
const rl = readline.createInterface({
input: process.stdin,
2025-06-11 01:30:08 +08:00
output: process.stdout,
2025-06-05 23:50:54 +08:00
})
2025-06-11 01:30:08 +08:00
2025-06-05 23:50:54 +08:00
return new Promise((resolve) => {
rl.question(prompt, (answer: string) => {
rl.close()
resolve(answer.trim())
})
})
}
export function error(message: string) {
if (message.startsWith("Error: ")) {
message = message.slice("Error: ".length)
}
println(Style.TEXT_DANGER_BOLD + "Error: " + Style.TEXT_NORMAL + message)
}
2025-07-14 05:22:11 +08:00
export function markdown(text: string): string {
2025-07-14 11:06:31 +08:00
return text
2025-07-14 05:22:11 +08:00
}
2025-06-05 23:50:54 +08:00
}