fix: use platform specific termios settings to determine optimized cursor movements

This commit is contained in:
Ayman Bagabas 2025-02-12 12:32:11 -05:00
parent 12f6445fb2
commit 28e82b66f2
No known key found for this signature in database
GPG key ID: C8D51D2157C919AC
4 changed files with 39 additions and 5 deletions

13
termios_bsd.go Normal file
View file

@ -0,0 +1,13 @@
//go:build dragonfly || freebsd
// +build dragonfly freebsd
package tea
import (
"github.com/charmbracelet/x/term"
"golang.org/x/sys/unix"
)
func (p *Program) checkOptimizedMovements(s *term.State) {
p.useHardTabs = s.Oflag&unix.TABDLY == unix.TAB0
}

8
termios_other.go Normal file
View file

@ -0,0 +1,8 @@
//go:build !darwin && !dragonfly && !freebsd && !linux && !solaris && !aix
// +build !darwin,!dragonfly,!freebsd,!linux,!solaris,!aix
package tea
import "github.com/charmbracelet/x/term"
func (*Program) checkOptimizedMovements(*term.State) {}

14
termios_unix.go Normal file
View file

@ -0,0 +1,14 @@
//go:build darwin || linux || solaris || aix
// +build darwin linux solaris aix
package tea
import (
"github.com/charmbracelet/x/term"
"golang.org/x/sys/unix"
)
func (p *Program) checkOptimizedMovements(s *term.State) {
p.useHardTabs = s.Oflag&unix.TABDLY == unix.TAB0
p.useBackspace = s.Lflag&unix.BSDLY == unix.BS0
}

View file

@ -10,7 +10,6 @@ import (
"syscall"
"github.com/charmbracelet/x/term"
"golang.org/x/sys/unix"
)
func (p *Program) initInput() (err error) {
@ -22,10 +21,10 @@ func (p *Program) initInput() (err error) {
return fmt.Errorf("error entering raw mode: %w", err)
}
// OPTIM: We can use hard tabs to optimize cursor movements if the
// terminal doesn't have tab expansion enabled.
p.useHardTabs = p.previousTtyInputState.Oflag&unix.TABDLY == unix.TAB0
p.useBackspace = p.previousTtyInputState.Lflag&unix.BSDLY == unix.BS0
// OPTIM: We can use hard tabs and backspaces to optimize cursor
// movements. This is based on termios settings support and whether
// they exist and enabled.
p.checkOptimizedMovements(p.previousTtyInputState)
}
if f, ok := p.output.Writer().(term.File); ok && term.IsTerminal(f.Fd()) {