picocode-core 0.9.206 → 0.9.207
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/terminal-theme.js +24 -9
package/package.json
CHANGED
package/src/terminal-theme.js
CHANGED
|
@@ -1,9 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
const OSC11_RE = /\]11;rgba?:([0-9a-f]+)\/([0-9a-f]+)\/([0-9a-f]+)/i
|
|
2
|
+
|
|
3
|
+
function osc11Channels(response) {
|
|
4
|
+
const match = response.match(OSC11_RE)
|
|
3
5
|
if (!match) return null
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
6
|
+
return [match[1], match[2], match[3]].map((hex) => parseInt(hex, 16) / (16 ** hex.length - 1))
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function parseOsc11(response) {
|
|
10
|
+
const channels = osc11Channels(response)
|
|
11
|
+
if (!channels) return null
|
|
12
|
+
const [r, g, b] = channels
|
|
13
|
+
return 0.2126 * r + 0.7152 * g + 0.0722 * b > 0.5 ? 'light' : 'dark'
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// the reported background as '#rrggbb'
|
|
17
|
+
export function parseOsc11Background(response) {
|
|
18
|
+
const channels = osc11Channels(response)
|
|
19
|
+
if (!channels) return null
|
|
20
|
+
return '#' + channels.map((c) => Math.round(c * 255).toString(16).padStart(2, '0')).join('')
|
|
7
21
|
}
|
|
8
22
|
|
|
9
23
|
export function themeFromColorfgbg(value) {
|
|
@@ -13,8 +27,9 @@ export function themeFromColorfgbg(value) {
|
|
|
13
27
|
return bg === 7 || bg === 15 ? 'light' : 'dark'
|
|
14
28
|
}
|
|
15
29
|
|
|
30
|
+
// resolves { theme: 'light' | 'dark', background: '#rrggbb' | null }
|
|
16
31
|
export function detectTerminalTheme({ timeoutMs = 150 } = {}) {
|
|
17
|
-
const fallback = () => themeFromColorfgbg(process.env.COLORFGBG) || 'dark'
|
|
32
|
+
const fallback = () => ({ theme: themeFromColorfgbg(process.env.COLORFGBG) || 'dark', background: null })
|
|
18
33
|
if (!process.stdin.isTTY || !process.stdout.isTTY) return Promise.resolve(fallback())
|
|
19
34
|
|
|
20
35
|
return new Promise((resolve) => {
|
|
@@ -25,19 +40,19 @@ export function detectTerminalTheme({ timeoutMs = 150 } = {}) {
|
|
|
25
40
|
// never pause() here: an explicit pause sticks, and trend's mount only
|
|
26
41
|
// attaches a data listener, which will not un-pause an explicitly
|
|
27
42
|
// paused stream - input would be frozen for the whole session
|
|
28
|
-
const finish = (
|
|
43
|
+
const finish = (result) => {
|
|
29
44
|
if (settled) return
|
|
30
45
|
settled = true
|
|
31
46
|
clearTimeout(timer)
|
|
32
47
|
process.stdin.off('data', onData)
|
|
33
48
|
process.stdin.setRawMode(wasRaw)
|
|
34
|
-
resolve(
|
|
49
|
+
resolve(result)
|
|
35
50
|
}
|
|
36
51
|
|
|
37
52
|
const onData = (chunk) => {
|
|
38
53
|
buffer += chunk.toString('latin1')
|
|
39
54
|
const theme = parseOsc11(buffer)
|
|
40
|
-
if (theme) finish(theme)
|
|
55
|
+
if (theme) finish({ theme, background: parseOsc11Background(buffer) })
|
|
41
56
|
}
|
|
42
57
|
|
|
43
58
|
const timer = setTimeout(() => finish(fallback()), timeoutMs)
|