Skip to content

Commit

Permalink
feat: renderer: optimize cursor movements with backspace
Browse files Browse the repository at this point in the history
This updates the renderer to use backspace to optimize cursor movements
when the terminal supports it. This is similar to the hard tabs
optimization, but for backspace to move the cursor left.
  • Loading branch information
aymanbagabas committed Feb 7, 2025
1 parent 70ab5cf commit 076054d
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 9 deletions.
5 changes: 4 additions & 1 deletion cursed_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ type cursedRenderer struct {
altScreen bool
cursorHidden bool
hardTabs bool // whether to use hard tabs to optimize cursor movements
backspace bool // whether to use backspace to optimize cursor movements
}

var _ renderer = &cursedRenderer{}

func newCursedRenderer(w io.Writer, term string, hardTabs bool) (s *cursedRenderer) {
func newCursedRenderer(w io.Writer, term string, hardTabs, backspace bool) (s *cursedRenderer) {
s = new(cursedRenderer)
s.w = w
s.term = term
s.hardTabs = hardTabs
s.backspace = backspace
s.reset()
return
}
Expand Down Expand Up @@ -118,6 +120,7 @@ func (s *cursedRenderer) reset() {
Width: s.width,
Height: s.height,
HardTabs: s.hardTabs,
Backspace: s.backspace,
})
s.mu.Unlock()
}
Expand Down
2 changes: 1 addition & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
github.com/aymanbagabas/go-udiff v0.2.0 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/charmbracelet/lipgloss v1.0.0 // indirect
github.com/charmbracelet/x/cellbuf v0.0.9-0.20250207213023-b9846c8eeefa // indirect
github.com/charmbracelet/x/cellbuf v0.0.9-0.20250207223800-1f1a6c27426e // indirect
github.com/charmbracelet/x/exp/golden v0.0.0-20241212170349-ad4b7ae0f25f // indirect
github.com/charmbracelet/x/input v0.3.2 // indirect
github.com/charmbracelet/x/term v0.2.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ github.com/charmbracelet/lipgloss/v2 v2.0.0-alpha.2.0.20250127174346-dc93e7a83a4
github.com/charmbracelet/lipgloss/v2 v2.0.0-alpha.2.0.20250127174346-dc93e7a83a4b/go.mod h1:wNdvvvSFeykFQxJ9rXcTA+IbZE9wgMEqhM4c8gV/faE=
github.com/charmbracelet/x/ansi v0.8.0 h1:9GTq3xq9caJW8ZrBTe0LIe2fvfLR/bYXKTx2llXn7xE=
github.com/charmbracelet/x/ansi v0.8.0/go.mod h1:wdYl/ONOLHLIVmQaxbIYEC/cRKOQyjTkowiI4blgS9Q=
github.com/charmbracelet/x/cellbuf v0.0.9-0.20250207213023-b9846c8eeefa h1:oKYvPGuwWdRdXun8kYXH6ib6G70XdWcmXwXuzRQb99k=
github.com/charmbracelet/x/cellbuf v0.0.9-0.20250207213023-b9846c8eeefa/go.mod h1:dKfNBxLovpvzzxAP6/GZfs5eb7vNxHlUDnwGhRmvIdY=
github.com/charmbracelet/x/cellbuf v0.0.9-0.20250207223800-1f1a6c27426e h1:hCO0Y612oQeo3mhM+TxywNLoZZvXMtGRc9EJtyZ83ag=
github.com/charmbracelet/x/cellbuf v0.0.9-0.20250207223800-1f1a6c27426e/go.mod h1:dKfNBxLovpvzzxAP6/GZfs5eb7vNxHlUDnwGhRmvIdY=
github.com/charmbracelet/x/exp/golden v0.0.0-20241212170349-ad4b7ae0f25f h1:UytXHv0UxnsDFmL/7Z9Q5SBYPwSuRLXHbwx+6LycZ2w=
github.com/charmbracelet/x/exp/golden v0.0.0-20241212170349-ad4b7ae0f25f/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=
github.com/charmbracelet/x/exp/teatest/v2 v2.0.0-20241016014612-3b4d04043233 h1:2bTR/MtnJuq9RrCZSPwCOO34YSDByKL6nzXQMnsKK6U=
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.18
require (
github.com/charmbracelet/colorprofile v0.2.0
github.com/charmbracelet/x/ansi v0.8.0
github.com/charmbracelet/x/cellbuf v0.0.9-0.20250207213023-b9846c8eeefa
github.com/charmbracelet/x/cellbuf v0.0.9-0.20250207223800-1f1a6c27426e
github.com/charmbracelet/x/exp/golden v0.0.0-20241212170349-ad4b7ae0f25f
github.com/charmbracelet/x/input v0.3.2
github.com/charmbracelet/x/term v0.2.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/charmbracelet/colorprofile v0.2.0 h1:iiIQlp3LSvoJPtR11KoDfIf9wqWm2mn/
github.com/charmbracelet/colorprofile v0.2.0/go.mod h1:6wPrSSR4QtwYtOY3h0bLRw5YOUAIKWlZIJ02CTAsZsk=
github.com/charmbracelet/x/ansi v0.8.0 h1:9GTq3xq9caJW8ZrBTe0LIe2fvfLR/bYXKTx2llXn7xE=
github.com/charmbracelet/x/ansi v0.8.0/go.mod h1:wdYl/ONOLHLIVmQaxbIYEC/cRKOQyjTkowiI4blgS9Q=
github.com/charmbracelet/x/cellbuf v0.0.9-0.20250207213023-b9846c8eeefa h1:oKYvPGuwWdRdXun8kYXH6ib6G70XdWcmXwXuzRQb99k=
github.com/charmbracelet/x/cellbuf v0.0.9-0.20250207213023-b9846c8eeefa/go.mod h1:dKfNBxLovpvzzxAP6/GZfs5eb7vNxHlUDnwGhRmvIdY=
github.com/charmbracelet/x/cellbuf v0.0.9-0.20250207223800-1f1a6c27426e h1:hCO0Y612oQeo3mhM+TxywNLoZZvXMtGRc9EJtyZ83ag=
github.com/charmbracelet/x/cellbuf v0.0.9-0.20250207223800-1f1a6c27426e/go.mod h1:dKfNBxLovpvzzxAP6/GZfs5eb7vNxHlUDnwGhRmvIdY=
github.com/charmbracelet/x/exp/golden v0.0.0-20241212170349-ad4b7ae0f25f h1:UytXHv0UxnsDFmL/7Z9Q5SBYPwSuRLXHbwx+6LycZ2w=
github.com/charmbracelet/x/exp/golden v0.0.0-20241212170349-ad4b7ae0f25f/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=
github.com/charmbracelet/x/input v0.3.2 h1:6h/d/40Og6UP3a4VDjIbdoUhilEH4K+mxEuaSPIJwNQ=
Expand Down
4 changes: 3 additions & 1 deletion tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ type Program struct {

// whether to use hard tabs to optimize cursor movements
useHardTabs bool
// whether to use backspace to optimize cursor movements
useBackspace bool
}

// Quit is a special command that tells the Bubble Tea program to exit.
Expand Down Expand Up @@ -822,7 +824,7 @@ func (p *Program) Run() (Model, error) {
p.renderer = newRenderer(p.output)
} else {
// If no renderer is set use the cursed one.
p.renderer = newCursedRenderer(p.output, p.getenv("TERM"), p.useHardTabs)
p.renderer = newCursedRenderer(p.output, p.getenv("TERM"), p.useHardTabs, p.useBackspace)
}
}

Expand Down
3 changes: 2 additions & 1 deletion tty_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func (p *Program) initInput() (err error) {

// 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 == 0
p.useHardTabs = p.previousTtyInputState.Oflag&unix.TABDLY == unix.TAB0
p.useBackspace = p.previousTtyInputState.Lflag&unix.BSDLY == unix.BS0
}

if f, ok := p.output.Writer().(term.File); ok && term.IsTerminal(f.Fd()) {
Expand Down

0 comments on commit 076054d

Please sign in to comment.