-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.go
More file actions
67 lines (57 loc) · 1.5 KB
/
Copy pathprotocol.go
File metadata and controls
67 lines (57 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package termlatex
import (
"os"
"strings"
)
// autoName is the String() value for the auto/default Protocol and Backend.
const autoName = "auto"
// Protocol selects the terminal graphics protocol used to display an equation.
type Protocol int
const (
// AutoProtocol detects the best protocol from $TERM / $TERM_PROGRAM.
AutoProtocol Protocol = iota
// HalfBlock renders with Unicode half-block characters (▀). Works on any
// terminal with UTF-8 and 24-bit color.
HalfBlock
// Sixel uses the DEC Sixel protocol (xterm, foot, mlterm, WezTerm…).
Sixel
// Kitty uses the Kitty graphics protocol (kitty, Ghostty, WezTerm…).
Kitty
)
func (p Protocol) String() string {
switch p {
case AutoProtocol:
return autoName
case Kitty:
return "kitty"
case Sixel:
return "sixel"
case HalfBlock:
return "halfblock"
}
return autoName
}
// bestProtocol returns the best protocol the current terminal supports based on
// environment variables. It sends no ANSI queries, so it needs no TTY.
func bestProtocol() Protocol {
// KITTY_WINDOW_ID is set by kitty itself.
if os.Getenv("KITTY_WINDOW_ID") != "" {
return Kitty
}
term := os.Getenv("TERM")
termProg := strings.ToLower(os.Getenv("TERM_PROGRAM"))
if term == "ghostty" || strings.HasPrefix(term, "xterm-ghostty") || termProg == "ghostty" {
return Kitty
}
if termProg == "wezterm" {
return Kitty
}
switch termProg {
case "foot", "mlterm", "contour":
return Sixel
}
if strings.Contains(term, "sixel") {
return Sixel
}
return HalfBlock
}