Skip to content

Commit

Permalink
feat(vt): handle hyperlinks (OSC 8)
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Feb 4, 2025
1 parent 7e3b991 commit 8304288
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
3 changes: 2 additions & 1 deletion vt/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const (

// Cursor represents a cursor in a terminal.
type Cursor struct {
Pen Style
Pen Style
Link Link

Position

Expand Down
6 changes: 6 additions & 0 deletions vt/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ func (t *Terminal) registerDefaultOscHandlers() {
})
}

t.RegisterOscHandler(8, func(data []byte) bool {
// Set/Query Hyperlink [ansi.SetHyperlink]
t.handleHyperlink(8, data)
return true
})

for _, cmd := range []int{
10, // Set/Query foreground color
11, // Set/Query background color
Expand Down
11 changes: 11 additions & 0 deletions vt/osc.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,14 @@ func (t *Terminal) handleDefaultColor(cmd int, data []byte) {

setCol(col)
}

func (t *Terminal) handleHyperlink(cmd int, data []byte) {
parts := bytes.Split(data, []byte{';'})
if len(parts) != 3 || cmd != 8 {
// Invalid, ignore
return
}

t.scr.cur.Link.URL = string(parts[1])
t.scr.cur.Link.URLID = string(parts[2])
}
7 changes: 7 additions & 0 deletions vt/screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,13 @@ func (s *Screen) cursorPen() Style {
return s.cur.Pen
}

// cursorLink returns the cursor link.
func (s *Screen) cursorLink() Link {
s.mu.RLock()
defer s.mu.RUnlock()
return s.cur.Link
}

// ShowCursor shows the cursor.
func (s *Screen) ShowCursor() {
s.setCursorHidden(false)
Expand Down
2 changes: 1 addition & 1 deletion vt/utf8.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (t *Terminal) handleGrapheme(content string, width int) {
}

cell.Style = t.scr.cursorPen()
cell.Link = Link{} // TODO: Link support
cell.Link = t.scr.cursorLink()

if t.scr.SetCell(x, y, cell) {
if width == 1 && len(content) == 1 {
Expand Down

0 comments on commit 8304288

Please sign in to comment.