Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.25.0
require (
charm.land/bubbletea/v2 v2.0.7
github.com/charmbracelet/ultraviolet v0.0.0-20260615092913-2399af76d5b1
github.com/charmbracelet/x/ansi v0.11.7
github.com/charmbracelet/x/term v0.2.2
github.com/charmbracelet/x/vt v0.0.0-20260615092313-b57e5e6d29bb
github.com/charmbracelet/x/xpty v0.1.3
Expand All @@ -13,7 +14,6 @@ require (
require (
github.com/aymanbagabas/go-udiff v0.4.1 // indirect
github.com/charmbracelet/colorprofile v0.4.3 // indirect
github.com/charmbracelet/x/ansi v0.11.7 // indirect
github.com/charmbracelet/x/conpty v0.1.1 // indirect
github.com/charmbracelet/x/errors v0.0.0-20240508181413-e8d8b6e2de86 // indirect
github.com/charmbracelet/x/exp/golden v0.0.0-20250806222409-83e3a29d542f // indirect
Expand Down
10 changes: 3 additions & 7 deletions scroll_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,14 @@ func TestPageKeysScroll(t *testing.T) {
}
m = m.Focus()

m, _ = m.Update(tea.KeyPressMsg{Code: tea.KeyPgUp})
m, _ = m.Update(tea.KeyPressMsg{Code: tea.KeyPgUp, Mod: tea.ModShift})
if m.scrollOffset != m.Height() {
t.Fatalf("offset = %d after pgup, want %d", m.scrollOffset, m.Height())
}

m, _ = m.Update(tea.KeyPressMsg{Code: tea.KeyPgDown})
if m.scrollOffset != 0 {
t.Errorf("offset = %d after pgdown, want 0", m.scrollOffset)
}

m.scrollOffset = 0
m = m.Blur()
m, _ = m.Update(tea.KeyPressMsg{Code: tea.KeyPgUp})
m, _ = m.Update(tea.KeyPressMsg{Code: tea.KeyPgUp, Mod: tea.ModShift})
if m.scrollOffset != 0 {
t.Errorf("blurred terminal scrolled to %d", m.scrollOffset)
}
Expand Down
94 changes: 85 additions & 9 deletions terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"sync/atomic"

tea "charm.land/bubbletea/v2"
uv "github.com/charmbracelet/ultraviolet"
"github.com/charmbracelet/x/ansi"
"github.com/charmbracelet/x/term"
"github.com/charmbracelet/x/vt"
"github.com/charmbracelet/x/xpty"
Expand All @@ -28,11 +30,12 @@ func nextID() int {
}

type session struct {
showCursor atomic.Bool
closed atomic.Bool
exited chan struct{}
exitCode int
exitErr error
showCursor atomic.Bool
mouseTracking atomic.Uint32 // bitmask of enabled DEC mouse modes
closed atomic.Bool
exited chan struct{}
exitCode int
exitErr error
}

// Model embeds a shell in Bubble Tea using x/vt for emulation and xpty for the pseudo-terminal.
Expand Down Expand Up @@ -101,6 +104,16 @@ func New(opts ...Option) (Model, error) {
CursorVisibility: func(visible bool) {
state.showCursor.Store(visible)
},
EnableMode: func(mode ansi.Mode) {
if bit := mouseTrackingBit(mode); bit != 0 {
state.mouseTracking.Or(bit)
}
},
DisableMode: func(mode ansi.Mode) {
if bit := mouseTrackingBit(mode); bit != 0 {
state.mouseTracking.And(^bit)
}
},
})

m.pty = pty
Expand Down Expand Up @@ -191,10 +204,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.resize(msg.Width, msg.Height)
if m.Closed() {
return m, nil
}
return m, m.ptyToTerminalView()
return m, nil
case OutputMsg:
if m.id != msg.ID {
return m, nil
Expand Down Expand Up @@ -257,6 +267,26 @@ func (m Model) handleKeyPressMsg(msg tea.KeyPressMsg) (Model, tea.Cmd) {
case "shift+down":
m.scrollDown(1)
return m, nil
case "pgup":
if m.isSelecting {
m.scrollUp(m.height)
return m, nil
}
case "pgdown":
if m.isSelecting {
m.scrollDown(m.height)
return m, nil
}
case "up":
if m.isSelecting {
m.scrollUp(1)
return m, nil
}
case "down":
if m.isSelecting {
m.scrollDown(1)
return m, nil
}
}
}

Expand All @@ -280,6 +310,48 @@ func (m Model) bufferY(screenY int) int {
return screenY + m.emu.ScrollbackLen() - m.scrollOffset
}

// mouseTrackingBit maps DEC mouse-tracking modes to bits in session.mouseTracking.
// SendMouse only emits sequences when at least one of these modes is set.
func mouseTrackingBit(mode ansi.Mode) uint32 {
switch mode {
case ansi.ModeMouseX10:
return 1 << 0
case ansi.ModeMouseNormal:
return 1 << 1
case ansi.ModeMouseHighlight:
return 1 << 2
case ansi.ModeMouseButtonEvent:
return 1 << 3
case ansi.ModeMouseAnyEvent:
return 1 << 4
default:
return 0
}
}

// forwardAltScreenWheel sends the wheel to the child. When the child has enabled
// DEC mouse tracking, emit a mouse sequence; otherwise fall back to cursor keys
// (xterm-style) so pagers like less still scroll.
func (m Model) forwardAltScreenWheel(msg tea.MouseWheelMsg) {
if m.state != nil && m.state.mouseTracking.Load() != 0 {
m.emu.SendMouse(uv.MouseWheelEvent{
X: msg.X,
Y: msg.Y,
Button: msg.Button,
Mod: msg.Mod,
})
return
}

code := uv.KeyDown
if msg.Button == tea.MouseWheelUp {
code = uv.KeyUp
}
for range mouseScrollStep {
m.emu.SendKey(uv.KeyPressEvent{Code: code})
}
}

func (m Model) handleMouseMsg(msg tea.MouseMsg) (Model, tea.Cmd) {
if !m.Focused() {
return m, nil
Expand Down Expand Up @@ -309,6 +381,10 @@ func (m Model) handleMouseMsg(msg tea.MouseMsg) (Model, tea.Cmd) {
}
m.isSelecting = false
case tea.MouseWheelMsg:
if m.emu != nil && m.emu.IsAltScreen() {
m.forwardAltScreenWheel(msg)
return m, nil
}
// Requires the Bubble Tea view to set MouseMode (e.g. CellMotion).
// That also captures click/drag, so host text selection usually breaks.
switch msg.Button {
Expand Down
15 changes: 15 additions & 0 deletions terminal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package termview

import (
"testing"

tea "charm.land/bubbletea/v2"
)

func TestResizeDoesNotStartAnotherPtyRead(t *testing.T) {
m := newTestModel(20, 5)
_, cmd := m.Update(tea.WindowSizeMsg{Width: 30, Height: 10})
if cmd != nil {
t.Fatal("WindowSizeMsg returned a command; a second PTY reader would race the existing one")
}
}
Loading