Skip to content

Commit

Permalink
Merge 'delthas/dev' to support hyperlink OSC
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Apr 15, 2022
2 parents 07c67d2 + 2362f49 commit c35c6f5
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 20 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,
}
}
3 changes: 3 additions & 0 deletions terminfo/f/foot/foot.go
Expand Up @@ -66,5 +66,8 @@ func init() {
KeyBacktab: "\x1b[Z",
Modifiers: 1,
AutoMargin: true,

Hyperlink: "\x1b]8;;",
StringTerminator: "\x1b\\",
})
}
2 changes: 2 additions & 0 deletions terminfo/terminfo.go
Expand Up @@ -227,6 +227,8 @@ type Terminfo struct {
CursorSteadyUnderline string
CursorBlinkingBar string
CursorSteadyBar string
StringTerminator string
Hyperlink string
}

const (
Expand Down
16 changes: 16 additions & 0 deletions tscreen.go
Expand Up @@ -58,6 +58,17 @@ func LookupTerminfo(name string) (ti *terminfo.Terminfo, e error) {
terminfo.AddTerminfo(ti)
}

if vteVersion := os.Getenv("VTE_VERSION"); vteVersion != "" && ti.Hyperlink == "" && ti.StringTerminator == "" {
v, err := strconv.Atoi(vteVersion)
if err == nil && v > 5000 {
// hyperlinks are supported in VTE-based terminal on VTE >= 0.50.0.
// some terminals do not support it even when VTE does, but they will ignore the escape code in those cases.
// see https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda#terminal-emulators
ti.Hyperlink = "\x1b]8;;"
ti.StringTerminator = "\x1b\\"
}
}

return
}

Expand Down Expand Up @@ -749,6 +760,11 @@ func (t *tScreen) drawCell(x, y int) int {
if attrs&AttrStrikeThrough != 0 {
t.TPuts(ti.StrikeThrough)
}
if style.hyperlink != "" && ti.Hyperlink != "" {
t.TPuts(ti.Hyperlink + style.hyperlink + ti.StringTerminator)
} else if t.curstyle.hyperlink != "" && ti.Hyperlink != "" {
t.TPuts(ti.Hyperlink + ti.StringTerminator)
}
t.curstyle = style
}
// now emit runes - taking care to not overrun width with a
Expand Down

0 comments on commit c35c6f5

Please sign in to comment.