Skip to content

Commit

Permalink
Add support for hyperlink OSC
Browse files Browse the repository at this point in the history
  • Loading branch information
emersion committed Oct 31, 2021
1 parent 8fc0c2b commit 4dffded
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 77 deletions.
58 changes: 38 additions & 20 deletions style.go
Expand Up @@ -23,9 +23,10 @@ package tcell
//
// To use Style, just declare a variable of its type.
type Style struct {
fg Color
bg Color
attrs AttrMask
fg Color
bg Color
attrs AttrMask
hyperlink string
}

// StyleDefault represents a default style, based upon the context.
Expand All @@ -39,19 +40,21 @@ var styleInvalid = Style{attrs: AttrInvalid}
// as requested. ColorDefault can be used to select the global default.
func (s Style) Foreground(c Color) Style {
return Style{
fg: c,
bg: s.bg,
attrs: s.attrs,
fg: c,
bg: s.bg,
attrs: s.attrs,
hyperlink: s.hyperlink,
}
}

// Background returns a new style based on s, with the background color set
// as requested. ColorDefault can be used to select the global default.
func (s Style) Background(c Color) Style {
return Style{
fg: s.fg,
bg: c,
attrs: s.attrs,
fg: s.fg,
bg: c,
attrs: s.attrs,
hyperlink: s.hyperlink,
}
}

Expand All @@ -64,23 +67,26 @@ func (s Style) Decompose() (fg Color, bg Color, attr AttrMask) {
func (s Style) setAttrs(attrs AttrMask, on bool) Style {
if on {
return Style{
fg: s.fg,
bg: s.bg,
attrs: s.attrs | attrs,
fg: s.fg,
bg: s.bg,
attrs: s.attrs | attrs,
hyperlink: s.hyperlink,
}
}
return Style{
fg: s.fg,
bg: s.bg,
attrs: s.attrs &^ attrs,
fg: s.fg,
bg: s.bg,
attrs: s.attrs &^ attrs,
hyperlink: s.hyperlink,
}
}

// Normal returns the style with all attributes disabled.
func (s Style) Normal() Style {
return Style{
fg: s.fg,
bg: s.bg,
fg: s.fg,
bg: s.bg,
hyperlink: s.hyperlink,
}
}

Expand Down Expand Up @@ -130,8 +136,20 @@ func (s Style) StrikeThrough(on bool) Style {
// specified.
func (s Style) Attributes(attrs AttrMask) Style {
return Style{
fg: s.fg,
bg: s.bg,
attrs: attrs,
fg: s.fg,
bg: s.bg,
attrs: attrs,
hyperlink: s.hyperlink,
}
}

// Hyperlink returns a new style based on s, with its hyperlink set to the
// specified URL. An empty string disables the hyperlink.
func (s Style) Hyperlink(url string) Style {
return Style{
fg: s.fg,
bg: s.bg,
attrs: s.attrs,
hyperlink: url,
}
}
118 changes: 61 additions & 57 deletions terminfo/f/foot/foot.go
Expand Up @@ -5,65 +5,69 @@ package foot
import "github.com/gdamore/tcell/v2/terminfo"

func init() {
const stringTerminator = "\x1b\\"
const hyperlink = "\x1b]8;;"

// foot terminal emulator
terminfo.AddTerminfo(&terminfo.Terminfo{
Name: "foot",
Columns: 80,
Lines: 24,
Colors: 256,
Bell: "\a",
Clear: "\x1b[H\x1b[2J",
EnterCA: "\x1b[?1049h\x1b[22;0;0t",
ExitCA: "\x1b[?1049l\x1b[23;0;0t",
ShowCursor: "\x1b[?12l\x1b[?25h",
HideCursor: "\x1b[?25l",
AttrOff: "\x1b(B\x1b[m",
Underline: "\x1b[4m",
Bold: "\x1b[1m",
Dim: "\x1b[2m",
Italic: "\x1b[3m",
Blink: "\x1b[5m",
Reverse: "\x1b[7m",
EnterKeypad: "\x1b[?1h\x1b=",
ExitKeypad: "\x1b[?1l\x1b>",
SetFg: "\x1b[%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38:5:%p1%d%;m",
SetBg: "\x1b[%?%p1%{8}%<%t4%p1%d%e%p1%{16}%<%t10%p1%{8}%-%d%e48:5:%p1%d%;m",
SetFgBg: "\x1b[%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38:5:%p1%d%;;%?%p2%{8}%<%t4%p2%d%e%p2%{16}%<%t10%p2%{8}%-%d%e48:5:%p2%d%;m",
ResetFgBg: "\x1b[39;49m",
AltChars: "``aaffggiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~",
EnterAcs: "\x1b(0",
ExitAcs: "\x1b(B",
StrikeThrough: "\x1b[9m",
Mouse: "\x1b[M",
SetCursor: "\x1b[%i%p1%d;%p2%dH",
CursorBack1: "\b",
CursorUp1: "\x1b[A",
KeyUp: "\x1bOA",
KeyDown: "\x1bOB",
KeyRight: "\x1bOC",
KeyLeft: "\x1bOD",
KeyInsert: "\x1b[2~",
KeyDelete: "\x1b[3~",
KeyBackspace: "\u007f",
KeyHome: "\x1bOH",
KeyEnd: "\x1bOF",
KeyPgUp: "\x1b[5~",
KeyPgDn: "\x1b[6~",
KeyF1: "\x1bOP",
KeyF2: "\x1bOQ",
KeyF3: "\x1bOR",
KeyF4: "\x1bOS",
KeyF5: "\x1b[15~",
KeyF6: "\x1b[17~",
KeyF7: "\x1b[18~",
KeyF8: "\x1b[19~",
KeyF9: "\x1b[20~",
KeyF10: "\x1b[21~",
KeyF11: "\x1b[23~",
KeyF12: "\x1b[24~",
KeyBacktab: "\x1b[Z",
Modifiers: 1,
AutoMargin: true,
Name: "foot",
Columns: 80,
Lines: 24,
Colors: 256,
Bell: "\a",
Clear: "\x1b[H\x1b[2J",
EnterCA: "\x1b[?1049h\x1b[22;0;0t",
ExitCA: "\x1b[?1049l\x1b[23;0;0t",
ShowCursor: "\x1b[?12l\x1b[?25h",
HideCursor: "\x1b[?25l",
AttrOff: "\x1b(B\x1b[m",
Underline: "\x1b[4m",
Bold: "\x1b[1m",
Dim: "\x1b[2m",
Italic: "\x1b[3m",
Blink: "\x1b[5m",
Reverse: "\x1b[7m",
EnterKeypad: "\x1b[?1h\x1b=",
ExitKeypad: "\x1b[?1l\x1b>",
SetFg: "\x1b[%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38:5:%p1%d%;m",
SetBg: "\x1b[%?%p1%{8}%<%t4%p1%d%e%p1%{16}%<%t10%p1%{8}%-%d%e48:5:%p1%d%;m",
SetFgBg: "\x1b[%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38:5:%p1%d%;;%?%p2%{8}%<%t4%p2%d%e%p2%{16}%<%t10%p2%{8}%-%d%e48:5:%p2%d%;m",
ResetFgBg: "\x1b[39;49m",
AltChars: "``aaffggiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~",
EnterAcs: "\x1b(0",
ExitAcs: "\x1b(B",
StrikeThrough: "\x1b[9m",
Mouse: "\x1b[M",
SetCursor: "\x1b[%i%p1%d;%p2%dH",
CursorBack1: "\b",
CursorUp1: "\x1b[A",
KeyUp: "\x1bOA",
KeyDown: "\x1bOB",
KeyRight: "\x1bOC",
KeyLeft: "\x1bOD",
KeyInsert: "\x1b[2~",
KeyDelete: "\x1b[3~",
KeyBackspace: "\u007f",
KeyHome: "\x1bOH",
KeyEnd: "\x1bOF",
KeyPgUp: "\x1b[5~",
KeyPgDn: "\x1b[6~",
KeyF1: "\x1bOP",
KeyF2: "\x1bOQ",
KeyF3: "\x1bOR",
KeyF4: "\x1bOS",
KeyF5: "\x1b[15~",
KeyF6: "\x1b[17~",
KeyF7: "\x1b[18~",
KeyF8: "\x1b[19~",
KeyF9: "\x1b[20~",
KeyF10: "\x1b[21~",
KeyF11: "\x1b[23~",
KeyF12: "\x1b[24~",
KeyBacktab: "\x1b[Z",
StartHyperlink: hyperlink + "%s" + stringTerminator,
EndHyperlink: hyperlink + stringTerminator,
Modifiers: 1,
AutoMargin: true,
})
}
6 changes: 6 additions & 0 deletions terminfo/terminfo.go
Expand Up @@ -227,6 +227,8 @@ type Terminfo struct {
CursorSteadyUnderline string
CursorBlinkingBar string
CursorSteadyBar string
StartHyperlink string
EndHyperlink string
}

const (
Expand Down Expand Up @@ -726,6 +728,10 @@ func (t *Terminfo) TColor(fi, bi int) string {
return rv
}

func (t *Terminfo) TStartHyperlink(url string) string {
return fmt.Sprintf(t.StartHyperlink, url)
}

var (
dblock sync.Mutex
terminfos = make(map[string]*Terminfo)
Expand Down
5 changes: 5 additions & 0 deletions tscreen.go
Expand Up @@ -749,6 +749,11 @@ func (t *tScreen) drawCell(x, y int) int {
if attrs&AttrStrikeThrough != 0 {
t.TPuts(ti.StrikeThrough)
}
if style.hyperlink != "" && ti.StartHyperlink != "" {
t.TPuts(ti.TStartHyperlink(style.hyperlink))
} else if t.curstyle.hyperlink != "" && ti.StartHyperlink != "" {
t.TPuts(ti.EndHyperlink)
}
t.curstyle = style
}
// now emit runes - taking care to not overrun width with a
Expand Down

0 comments on commit 4dffded

Please sign in to comment.