1
1
mirror of https://github.com/walles/moar.git synced 2024-11-22 11:45:50 +03:00

Only request terminal bg if needed

Fixes #190 and #191.

4 realz.
This commit is contained in:
Johan Walles 2024-01-20 08:34:59 +01:00
parent 1249f08c10
commit b18dd4f943
3 changed files with 19 additions and 7 deletions

View File

@ -581,6 +581,7 @@ func pagerFromArgs(
var style chroma.Style
if *styleOption == nil {
t0 := time.Now()
screen.RequestTerminalBackgroundColor()
select {
case event := <-screen.Events():
// Event received, let's see if it's the one we want

View File

@ -72,6 +72,10 @@ func (screen *FakeScreen) Size() (width int, height int) {
return screen.width, screen.height
}
func (screen *FakeScreen) RequestTerminalBackgroundColor() {
// This method intentionally left blank
}
func (screen *FakeScreen) ShowCursorAt(_ int, _ int) {
// This method intentionally left blank
}

View File

@ -57,6 +57,14 @@ type Screen interface {
// If the position is outside of the screen, the cursor will be hidden.
ShowCursorAt(column int, row int)
// RequestTerminalBackgroundColor() asks the terminal to report its
// background color.
//
// If your terminal supports background color queries and it responds, the
// result will be reported as an EventTerminalBackgroundDetected on the
// Events() channel.
RequestTerminalBackgroundColor()
// This channel is what your main loop should be checking.
Events() chan Event
}
@ -142,13 +150,6 @@ func NewScreenWithMouseModeAndColorType(mouseMode MouseMode, terminalColorCount
screen.setAlternateScreenMode(true)
// Request to get terminal background color. Answer will be handled in our
// main loop.
//
// Ref:
// https://stackoverflow.com/questions/2507337/how-to-determine-a-terminals-background-color
fmt.Println("\x1b]11;?\x07")
if mouseMode == MouseModeAuto {
screen.enableMouseTracking(!terminalHasArrowKeysEmulation())
} else if mouseMode == MouseModeSelect {
@ -524,6 +525,12 @@ func (screen *UnixScreen) Size() (width int, height int) {
return screen.widthAccessFromSizeOnly, screen.heightAccessFromSizeOnly
}
func (screen *UnixScreen) RequestTerminalBackgroundColor() {
// Ref:
// https://stackoverflow.com/questions/2507337/how-to-determine-a-terminals-background-color
fmt.Println("\x1b]11;?\x07")
}
func parseTerminalBgColorResponse(responseBytes []byte) *Color {
prefix := "\x1b]11;rgb:"
suffix1 := "\x07"