MCPcopy Create free account
hub / github.com/arctic-cli/interface / getTerminalBackgroundColor

Function getTerminalBackgroundColor

packages/arctic/src/cli/cmd/tui/app.tsx:62–120  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

60import { ToastProvider, useToast } from "./ui/toast"
61
62async function getTerminalBackgroundColor(): Promise<"dark" | "light"> {
63 // can't set raw mode if not a TTY
64 if (!process.stdin.isTTY) return "dark"
65
66 return new Promise((resolve) => {
67 let timeout: NodeJS.Timeout
68
69 const cleanup = () => {
70 process.stdin.setRawMode(false)
71 process.stdin.removeListener("data", handler)
72 clearTimeout(timeout)
73 }
74
75 const handler = (data: Buffer) => {
76 const str = data.toString()
77 const match = str.match(/\x1b]11;([^\x07\x1b]+)/)
78 if (match) {
79 cleanup()
80 const color = match[1]
81 // Parse RGB values from color string
82 // Formats: rgb:RR/GG/BB or #RRGGBB or rgb(R,G,B)
83 let r = 0,
84 g = 0,
85 b = 0
86
87 if (color.startsWith("rgb:")) {
88 const parts = color.substring(4).split("/")
89 r = parseInt(parts[0], 16) >> 8 // Convert 16-bit to 8-bit
90 g = parseInt(parts[1], 16) >> 8 // Convert 16-bit to 8-bit
91 b = parseInt(parts[2], 16) >> 8 // Convert 16-bit to 8-bit
92 } else if (color.startsWith("#")) {
93 r = parseInt(color.substring(1, 3), 16)
94 g = parseInt(color.substring(3, 5), 16)
95 b = parseInt(color.substring(5, 7), 16)
96 } else if (color.startsWith("rgb(")) {
97 const parts = color.substring(4, color.length - 1).split(",")
98 r = parseInt(parts[0])
99 g = parseInt(parts[1])
100 b = parseInt(parts[2])
101 }
102
103 // Calculate luminance using relative luminance formula
104 const luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255
105
106 // Determine if dark or light based on luminance threshold
107 resolve(luminance > 0.5 ? "light" : "dark")
108 }
109 }
110
111 process.stdin.setRawMode(true)
112 process.stdin.on("data", handler)
113 process.stdout.write("\x1b]11;?\x07")
114
115 timeout = setTimeout(() => {
116 cleanup()
117 resolve("dark")
118 }, 1000)
119 })

Callers 1

tuiFunction · 0.70

Calls 2

resolveFunction · 0.85
cleanupFunction · 0.70

Tested by

no test coverage detected