Skip to content

Commit

Permalink
fixes #300 Implement terminal hyperlinks
Browse files Browse the repository at this point in the history
fixes #526 tcell emits redundant attributes

This work is inspired by, and partly derived from, work submitted by
Simon Ser (@emersion).  However, we've modified the bottom half of
the terminfo parser to better support strings properly, and are using
proper terminfo syntax.

Instead of an attribute called Hyperlink, we have called it Url
for the sake of brevity.

While here we noticed and fixed bug #526, which could badly impact slow
terminals, or slow links. This likely makes things better for folks
coming over long distance SSH connections for example.

We've also provided OSC 8 handling for all terminals which appear to
support the mouse sequences; hopefully ones that don't handle this
sensibly will just ignore it.  (Limited testing seems to show this.)
  • Loading branch information
gdamore committed Apr 16, 2022
1 parent cb7cb02 commit e7b14a7
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 100 deletions.
91 changes: 91 additions & 0 deletions _demos/hyperlink.go
@@ -0,0 +1,91 @@
//go:build ignore
// +build ignore

// Copyright 2022 The TCell Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
// You may obtain a copy of the license at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"fmt"
"os"

"github.com/gdamore/tcell/v2"
"github.com/gdamore/tcell/v2/encoding"

"github.com/mattn/go-runewidth"
)

func emitStr(s tcell.Screen, x, y int, style tcell.Style, str string) int {
for _, c := range str {
var comb []rune
w := runewidth.RuneWidth(c)
if w == 0 {
comb = []rune{c}
c = ' '
w = 1
}
s.SetContent(x, y, c, comb, style)
x += w
}
return x
}

func displayDemo(s tcell.Screen) {
w, h := s.Size()
s.Clear()
style := tcell.StyleDefault
x := (w - 55) / 2
x = emitStr(s, x, h/2, style, "Please visit the ")
x = emitStr(s, x, h/2, style.Url("https://github.com/gdamore/tcell"), "GitHub Repository")
emitStr(s, x, h/2, style, " for the source code.")
style = tcell.StyleDefault.Foreground(tcell.ColorCadetBlue.TrueColor()).Background(tcell.ColorWhite)
emitStr(s, (w-18)/2, h/2+2, style, "Press ESC to exit.")
s.Show()
}

// This program just prints "Hello, World!". Press ESC to exit.
func main() {
encoding.Register()

s, e := tcell.NewScreen()
if e != nil {
fmt.Fprintf(os.Stderr, "%v\n", e)
os.Exit(1)
}
if e := s.Init(); e != nil {
fmt.Fprintf(os.Stderr, "%v\n", e)
os.Exit(1)
}

defStyle := tcell.StyleDefault.
Background(tcell.ColorBlack).
Foreground(tcell.ColorWhite)
s.SetStyle(defStyle)

displayDemo(s)

for {
switch ev := s.PollEvent().(type) {
case *tcell.EventResize:
s.Sync()
displayDemo(s)
case *tcell.EventKey:
if ev.Key() == tcell.KeyEscape {
s.Fini()
os.Exit(0)
}
}
}
}
22 changes: 20 additions & 2 deletions style.go
@@ -1,4 +1,4 @@
// Copyright 2020 The TCell Authors
// Copyright 2022 The TCell Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
Expand Down Expand Up @@ -26,6 +26,7 @@ type Style struct {
fg Color
bg Color
attrs AttrMask
url string
}

// StyleDefault represents a default style, based upon the context.
Expand All @@ -42,6 +43,7 @@ func (s Style) Foreground(c Color) Style {
fg: c,
bg: s.bg,
attrs: s.attrs,
url: s.url,
}
}

Expand All @@ -52,11 +54,12 @@ func (s Style) Background(c Color) Style {
fg: s.fg,
bg: c,
attrs: s.attrs,
url: s.url,
}
}

// Decompose breaks a style up, returning the foreground, background,
// and other attributes.
// and other attributes. The URL if set is not included.
func (s Style) Decompose() (fg Color, bg Color, attr AttrMask) {
return s.fg, s.bg, s.attrs
}
Expand All @@ -67,12 +70,14 @@ func (s Style) setAttrs(attrs AttrMask, on bool) Style {
fg: s.fg,
bg: s.bg,
attrs: s.attrs | attrs,
url: s.url,
}
}
return Style{
fg: s.fg,
bg: s.bg,
attrs: s.attrs &^ attrs,
url: s.url,
}
}

Expand Down Expand Up @@ -133,5 +138,18 @@ func (s Style) Attributes(attrs AttrMask) Style {
fg: s.fg,
bg: s.bg,
attrs: attrs,
url: s.url,
}
}

// Url returns a style with the Url set. If the provided Url is not empty,
// and the terminal supports it, text will typically be marked up as a clickable
// link to that Url. If the Url is empty, then this mode is turned off.
func (s Style) Url(url string) Style {
return Style{
fg: s.fg,
bg: s.bg,
attrs: s.attrs,
url: url,
}
}

0 comments on commit e7b14a7

Please sign in to comment.