ui.ts 2.3 KB

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