ui.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { z } from "zod"
  2. import { EOL } from "os"
  3. import { NamedError } from "../util/error"
  4. export namespace UI {
  5. const LOGO = [
  6. [`█▀▀█ █▀▀█ █▀▀ █▀▀▄ `, `█▀▀ █▀▀█ █▀▀▄ █▀▀`],
  7. [`█░░█ █░░█ █▀▀ █░░█ `, `█░░ █░░█ █░░█ █▀▀`],
  8. [`▀▀▀▀ █▀▀▀ ▀▀▀ ▀ ▀ `, `▀▀▀ ▀▀▀▀ ▀▀▀ ▀▀▀`],
  9. ]
  10. export const CancelledError = NamedError.create("UICancelledError", z.void())
  11. export const Style = {
  12. TEXT_HIGHLIGHT: "\x1b[96m",
  13. TEXT_HIGHLIGHT_BOLD: "\x1b[96m\x1b[1m",
  14. TEXT_DIM: "\x1b[90m",
  15. TEXT_DIM_BOLD: "\x1b[90m\x1b[1m",
  16. TEXT_NORMAL: "\x1b[0m",
  17. TEXT_NORMAL_BOLD: "\x1b[1m",
  18. TEXT_WARNING: "\x1b[93m",
  19. TEXT_WARNING_BOLD: "\x1b[93m\x1b[1m",
  20. TEXT_DANGER: "\x1b[91m",
  21. TEXT_DANGER_BOLD: "\x1b[91m\x1b[1m",
  22. TEXT_SUCCESS: "\x1b[92m",
  23. TEXT_SUCCESS_BOLD: "\x1b[92m\x1b[1m",
  24. TEXT_INFO: "\x1b[94m",
  25. TEXT_INFO_BOLD: "\x1b[94m\x1b[1m",
  26. }
  27. export function println(...message: string[]) {
  28. print(...message)
  29. Bun.stderr.write(EOL)
  30. }
  31. export function print(...message: string[]) {
  32. blank = false
  33. Bun.stderr.write(message.join(" "))
  34. }
  35. let blank = false
  36. export function empty() {
  37. if (blank) return
  38. println("" + Style.TEXT_NORMAL)
  39. blank = true
  40. }
  41. export function logo(pad?: string) {
  42. const result = []
  43. for (const row of LOGO) {
  44. if (pad) result.push(pad)
  45. result.push(Bun.color("gray", "ansi"))
  46. result.push(row[0])
  47. result.push("\x1b[0m")
  48. result.push(row[1])
  49. result.push(EOL)
  50. }
  51. return result.join("").trimEnd()
  52. }
  53. export async function input(prompt: string): Promise<string> {
  54. const readline = require("readline")
  55. const rl = readline.createInterface({
  56. input: process.stdin,
  57. output: process.stdout,
  58. })
  59. return new Promise((resolve) => {
  60. rl.question(prompt, (answer: string) => {
  61. rl.close()
  62. resolve(answer.trim())
  63. })
  64. })
  65. }
  66. export function error(message: string) {
  67. println(Style.TEXT_DANGER_BOLD + "Error: " + Style.TEXT_NORMAL + message)
  68. }
  69. export function markdown(text: string): string {
  70. return text
  71. }
  72. }