-
Notifications
You must be signed in to change notification settings - Fork 64
feat: detect image protocol #406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
raphamorim
wants to merge
3
commits into
main
Choose a base branch
from
feat-detect-image-protocol
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ use ( | |
| ./exp/strings | ||
| ./exp/teatest | ||
| ./exp/teatest/v2 | ||
| ./graphics | ||
| ./input | ||
| ./json | ||
| ./mosaic | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| package graphics | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "os" | ||
| "time" | ||
|
|
||
| "github.com/charmbracelet/x/term" | ||
| ) | ||
|
|
||
| // TODO: Verify if it's running with tmux for Kitty and ITerm2 | ||
| // TODO: Write a func for preferred protocol for the terminal | ||
| // TODO: Additional check if the terminal supports cell-size by `[16t` or `[14t` | ||
| // TODO: Write tests by mocking a terminal context ? | ||
|
|
||
| const ( | ||
| termProgramVariable = "TERM_PROGRAM" | ||
| lcTerminalVariable = "LC_TERMINAL" | ||
| ) | ||
|
|
||
| // Returns the availability of each image protocol. | ||
| type ImageProtocols struct { | ||
| Sixel bool | ||
| ITerm2 bool | ||
| Kitty bool | ||
| // Mosaic (Halfblocks) should work in all terminals, | ||
| // even if the font size could not be detected, with a 4:8 pixel ratio. | ||
| Mosaic bool | ||
| } | ||
|
|
||
| // Detect all availables image protocols and return as [ImageProtocols]. | ||
| func DetectImageProtocols() ImageProtocols { | ||
| return ImageProtocols{ | ||
| Sixel: detectSixel(), | ||
| // TODO: `_Gi=...`: Kitty graphics support. | ||
| Kitty: detectKitty(), | ||
| // TODO: `[1337n`: iTerm2 (some terminals implement the protocol but sadly not this custom CSI) | ||
| ITerm2: detectIterm2() || detectIterm2FromEnv(), | ||
| Mosaic: true, | ||
| } | ||
| } | ||
|
|
||
| func detectKitty() bool { | ||
| return false | ||
| } | ||
|
|
||
| func detectIterm2() bool { | ||
| return false | ||
| } | ||
|
|
||
| // This function detects iTerm2 protocol from environment variable. | ||
| func detectIterm2FromEnv() bool { | ||
| termProgram := os.Getenv(termProgramVariable) | ||
| if termProgram == "iTerm" || | ||
| termProgram == "WezTerm" || | ||
| termProgram == "mintty" || | ||
| termProgram == "vscode" || | ||
| termProgram == "Tabby" || | ||
| termProgram == "Hyper" || | ||
| termProgram == "rio" { | ||
| return true | ||
| } | ||
|
|
||
| lcTerminal := os.Getenv(lcTerminalVariable) | ||
| return lcTerminal == "iTerm" | ||
| } | ||
|
|
||
| func detectSixel() bool { | ||
| sixelSupportedTerminals := []string{ | ||
| "\x1b[?62;", // VT240 | ||
| "\x1b[?63;", // wsltty | ||
| "\x1b[?64;", // mintty | ||
| "\x1b[?65;", // RLogin | ||
| // NOTE: tmux does not return VT name. | ||
| "\x1b[?1;2;4c", // Tmux | ||
| } | ||
|
|
||
| if term.IsTerminal(os.Stdout.Fd()) { | ||
| return true | ||
| } | ||
| s, err := term.MakeRaw(1) | ||
| if err == nil { | ||
| defer term.Restore(1, s) // nolint:errcheck | ||
| } | ||
| _, err = os.Stdout.Write([]byte("\x1b[c")) | ||
| if err != nil { | ||
| return false | ||
| } | ||
| defer os.Stdout.SetReadDeadline(time.Time{}) // nolint:errcheck | ||
|
|
||
| var b [100]byte | ||
| n, err := os.Stdout.Read(b[:]) | ||
| if err != nil { | ||
| return false | ||
| } | ||
|
|
||
| for _, t := range bytes.Split(b[6:n], []byte(";")) { | ||
| // Check if 4 is present in terminal capabilities. | ||
| if len(t) == 1 && t[0] == '4' { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| for _, supportedTerminal := range sixelSupportedTerminals { | ||
| if bytes.HasPrefix(b[:n], []byte(supportedTerminal)) { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| return false | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| module github.com/charmbracelet/x/graphics | ||
|
|
||
| go 1.21 | ||
|
|
||
| require github.com/charmbracelet/x/term v0.2.1 | ||
|
|
||
| require golang.org/x/sys v0.26.0 // indirect |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ= | ||
| github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg= | ||
| golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= | ||
| golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if err != nil should probably stop here?