commit f254040

delthas  ·  2022-10-17 08:33:07 +0000 UTC
parent d242b4f
Add OSC 8 hyperlink IDs

OSC 8 hyperlink ID support was merged into tcell. We can now use those
IDs to help terminal emulators identify links spanning multiple lines,
possibly highlighting the whole link whe it is hovered, or creating a
single link hotkey for them.
4 files changed,  +15, -10
M go.mod
M go.sum
+3, -0
 1@@ -4,6 +4,7 @@ import (
 2 	"flag"
 3 	"fmt"
 4 	"io/ioutil"
 5+	"math/rand"
 6 	"os"
 7 	"os/signal"
 8 	"path"
 9@@ -24,6 +25,8 @@ func main() {
10 	flag.BoolVar(&debug, "debug", false, "show raw protocol data in the home buffer")
11 	flag.Parse()
12 
13+	rand.Seed(time.Now().UnixNano())
14+
15 	if configPath == "" {
16 		configDir, err := os.UserConfigDir()
17 		if err != nil {
M go.mod
+2, -2
 1@@ -4,8 +4,8 @@ go 1.16
 2 
 3 require (
 4 	git.sr.ht/~emersion/go-scfg v0.0.0-20201019143924-142a8aa629fc
 5-	github.com/gdamore/tcell/v2 v2.5.4-0.20220911202027-96bb70f9efec
 6-	github.com/mattn/go-runewidth v0.0.13
 7+	github.com/gdamore/tcell/v2 v2.5.4-0.20221017053904-7557ac2a6c89
 8+	github.com/mattn/go-runewidth v0.0.14
 9 	golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd
10 	golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
11 	golang.org/x/time v0.0.0-20210611083556-38a9dc6acbc6
M go.sum
+4, -4
 1@@ -4,8 +4,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
 2 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 3 github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
 4 github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
 5-github.com/gdamore/tcell/v2 v2.5.4-0.20220911202027-96bb70f9efec h1:wvcbNOhEpZhHp4F9eIvB7fn0CDsQt9EXlHHyvxfb284=
 6-github.com/gdamore/tcell/v2 v2.5.4-0.20220911202027-96bb70f9efec/go.mod h1:wSkrPaXoiIWZqW/g7Px4xc79di6FTcpB8tvaKJ6uGBo=
 7+github.com/gdamore/tcell/v2 v2.5.4-0.20221017053904-7557ac2a6c89 h1:Q8/8bqReXToMTLZKmhNSQf6nabdAWcL0VtFz9DySzVc=
 8+github.com/gdamore/tcell/v2 v2.5.4-0.20221017053904-7557ac2a6c89/go.mod h1:02iy8Omp2FMM0oDqEohCtRma57vIEg/mnw8zkEmDc6I=
 9 github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
10 github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
11 github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
12@@ -13,8 +13,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
13 github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
14 github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
15 github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
16-github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
17-github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
18+github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
19+github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
20 github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
21 github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
22 github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
+6, -4
 1@@ -2,6 +2,7 @@ package ui
 2 
 3 import (
 4 	"fmt"
 5+	"math/rand"
 6 	"net/url"
 7 	"strconv"
 8 	"strings"
 9@@ -141,6 +142,7 @@ func (s StyledString) ParseURLs() StyledString {
10 		if u, err := url.Parse(link); err != nil || u.Scheme == "" {
11 			link = "https://" + link
12 		}
13+		id := fmt.Sprintf("_%10d", rand.Int31())
14 		// find last style starting before or at url begin
15 		for ; j < len(s.styles); j++ {
16 			st := s.styles[j]
17@@ -149,7 +151,7 @@ func (s StyledString) ParseURLs() StyledString {
18 			}
19 			if st.Start == ub {
20 				// a style already starts at this position, edit it
21-				lastStyle.Style = lastStyle.Style.Url(link)
22+				lastStyle.Style = lastStyle.Style.Url(link).UrlId(id)
23 			}
24 			lastStyle = st
25 			styles = append(styles, st)
26@@ -158,7 +160,7 @@ func (s StyledString) ParseURLs() StyledString {
27 			// no style existed at this position, add one from the last style
28 			styles = append(styles, rangedStyle{
29 				Start: ub,
30-				Style: lastStyle.Style.Url(link),
31+				Style: lastStyle.Style.Url(link).UrlId(id),
32 			})
33 		}
34 		// find last style starting before or at url end
35@@ -168,7 +170,7 @@ func (s StyledString) ParseURLs() StyledString {
36 				break
37 			}
38 			if st.Start < ue {
39-				st.Style = st.Style.Url(link)
40+				st.Style = st.Style.Url(link).UrlId(id)
41 			}
42 			lastStyle = st
43 			styles = append(styles, st)
44@@ -177,7 +179,7 @@ func (s StyledString) ParseURLs() StyledString {
45 			// no style existed at this position, add one from the last style without the hyperlink
46 			styles = append(styles, rangedStyle{
47 				Start: ue,
48-				Style: lastStyle.Style.Url(""),
49+				Style: lastStyle.Style.Url("").UrlId(""),
50 			})
51 		}
52 	}