From b6fba5230aa3c8cba41e6a7aa3e60c969b3ba1d6 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sat, 16 Apr 2022 19:59:34 +0300 Subject: [PATCH] Enable inline URLs by default on VTE terminals --- config/config.go | 16 +++++++++++++++- go.mod | 4 ++-- go.sum | 8 ++++---- ui/commands.go | 10 +++++++++- ui/messages/filemessage.go | 2 +- ui/messages/html/parser.go | 4 ++-- ui/messages/parser.go | 2 +- 7 files changed, 34 insertions(+), 12 deletions(-) diff --git a/config/config.go b/config/config.go index ed9c544..87bf01b 100644 --- a/config/config.go +++ b/config/config.go @@ -23,6 +23,7 @@ import ( "io/ioutil" "os" "path/filepath" + "strconv" "strings" "gopkg.in/yaml.v3" @@ -58,8 +59,21 @@ type UserPreferences struct { DisableDownloads bool `yaml:"disable_downloads"` DisableNotifications bool `yaml:"disable_notifications"` DisableShowURLs bool `yaml:"disable_show_urls"` - InlineURLs bool `yaml:"inline_urls"` AltEnterToSend bool `yaml:"alt_enter_to_send"` + + InlineURLMode string `yaml:"inline_url_mode"` +} + +var InlineURLsProbablySupported bool + +func init() { + v, _ := strconv.Atoi(os.Getenv("VTE_VERSION")) + // Enable inline URLs by default on VTE 0.50.0+ + InlineURLsProbablySupported = v > 5000 +} + +func (up *UserPreferences) EnableInlineURLs() bool { + return up.InlineURLMode == "enable" || (InlineURLsProbablySupported && up.InlineURLMode != "disable") } type Keybind struct { diff --git a/go.mod b/go.mod index 3ce7133..3b2972f 100644 --- a/go.mod +++ b/go.mod @@ -17,8 +17,8 @@ require ( github.com/zyedidia/clipboard v1.0.3 go.etcd.io/bbolt v1.3.6 go.mau.fi/cbind v0.0.0-20220415094356-e1d579b7925e - go.mau.fi/mauview v0.1.4-0.20220415192914-be7d4f257167 - go.mau.fi/tcell v0.0.0-20220415185117-592f364693a2 + go.mau.fi/mauview v0.1.4-0.20220416164650-aac1ebd3a472 + go.mau.fi/tcell v0.0.0-20220416164612-f5dbb33f6c9c golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9 golang.org/x/net v0.0.0-20220412020605-290c469a71a5 gopkg.in/toast.v1 v1.0.0-20180812000517-0a84660828b2 diff --git a/go.sum b/go.sum index cbb74af..bd560fd 100644 --- a/go.sum +++ b/go.sum @@ -63,11 +63,11 @@ go.etcd.io/bbolt v1.3.6 h1:/ecaJf0sk1l4l6V4awd65v2C3ILy7MSj+s/x1ADCIMU= go.etcd.io/bbolt v1.3.6/go.mod h1:qXsaaIqmgQH0T+OPdb99Bf+PKfBBQVAdyD6TY9G8XM4= go.mau.fi/cbind v0.0.0-20220415094356-e1d579b7925e h1:zY4TZmHAaUhrMFJQfh02dqxDYSfnnXlw/qRoFanxZTw= go.mau.fi/cbind v0.0.0-20220415094356-e1d579b7925e/go.mod h1:9nnzlslhUo/xO+8tsQgkFqG/W+SgD+r0iTYAuglzlmA= -go.mau.fi/mauview v0.1.4-0.20220415192914-be7d4f257167 h1:WCqpsiR7IuOJ36aDd9pbPxr9AtjnXvPHhPKw9xx9x2Q= -go.mau.fi/mauview v0.1.4-0.20220415192914-be7d4f257167/go.mod h1:1rzvl7kqQ9lv8EVZeAwUlxR4/Q8LM3y2Xogg0yNx0qU= +go.mau.fi/mauview v0.1.4-0.20220416164650-aac1ebd3a472 h1:1Rx+gZAORRrkevGQuLfDEM8E5cYxNRAA3lx2Rc19H5g= +go.mau.fi/mauview v0.1.4-0.20220416164650-aac1ebd3a472/go.mod h1:3W0lnT2QE0M/HF0d5dgyCG71QEc+pS/+RQf1lGybEzE= go.mau.fi/tcell v0.0.0-20220415093808-07c67d224693/go.mod h1:HQLPCz9v8YfYewMetOKrg9pe87XEyNcIfCYYq8VxQbU= -go.mau.fi/tcell v0.0.0-20220415185117-592f364693a2 h1:UocCXayiOk0dzu7GeTyiH6UdpJnp5d645NRDwr6RjLY= -go.mau.fi/tcell v0.0.0-20220415185117-592f364693a2/go.mod h1:HQLPCz9v8YfYewMetOKrg9pe87XEyNcIfCYYq8VxQbU= +go.mau.fi/tcell v0.0.0-20220416164612-f5dbb33f6c9c h1:BdwxPvI30uM9Yhr1t99sHHoEzbK8JLfRntQDpm/QCdg= +go.mau.fi/tcell v0.0.0-20220416164612-f5dbb33f6c9c/go.mod h1:HQLPCz9v8YfYewMetOKrg9pe87XEyNcIfCYYq8VxQbU= golang.org/x/crypto v0.0.0-20220408190544-5352b0902921 h1:iU7T1X1J6yxDr0rda54sWGkHgOp5XJrqm79gcNlC2VM= golang.org/x/crypto v0.0.0-20220408190544-5352b0902921/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= diff --git a/ui/commands.go b/ui/commands.go index 7466511..c4de657 100644 --- a/ui/commands.go +++ b/ui/commands.go @@ -867,7 +867,15 @@ func cmdToggle(cmd *Command) { case "showurls": val = &cmd.Config.Preferences.DisableShowURLs case "inlineurls": - val = &cmd.Config.Preferences.InlineURLs + switch cmd.Config.Preferences.InlineURLMode { + case "enable": + cmd.Config.Preferences.InlineURLMode = "disable" + cmd.Reply("Force-disabled using fancy terminal features to render URLs inside text. Restart gomuks to apply changes.") + default: + cmd.Config.Preferences.InlineURLMode = "enable" + cmd.Reply("Force-enabled using fancy terminal features to render URLs inside text. Restart gomuks to apply changes.") + } + continue case "newline": val = &cmd.Config.Preferences.AltEnterToSend default: diff --git a/ui/messages/filemessage.go b/ui/messages/filemessage.go index cbcc5be..f4bc3a4 100644 --- a/ui/messages/filemessage.go +++ b/ui/messages/filemessage.go @@ -148,7 +148,7 @@ func (msg *FileMessage) CalculateBuffer(prefs config.UserPreferences, width int, if prefs.BareMessageView || prefs.DisableImages || len(msg.imageData) == 0 { url := msg.matrix.GetDownloadURL(msg.URL) var urlTString tstring.TString - if prefs.InlineURLs { + if prefs.EnableInlineURLs() { urlTString = tstring.NewStyleTString(url, tcell.StyleDefault.Hyperlink(url, msg.eventID.String())) } else { urlTString = tstring.NewTString(url) diff --git a/ui/messages/html/parser.go b/ui/messages/html/parser.go index 7442ccf..7f955cd 100644 --- a/ui/messages/html/parser.go +++ b/ui/messages/html/parser.go @@ -234,7 +234,7 @@ func (parser *htmlParser) linkToEntity(node *html.Node) Entity { } else if matrixURI.Sigil1 == '#' { entity.Children = []Entity{text} } - } else if parser.prefs.InlineURLs { + } else if parser.prefs.EnableInlineURLs() { linkID := fmt.Sprintf("%s-%d", parser.evt.ID, parser.linkIDCounter) parser.linkIDCounter++ entity.AdjustStyle(AdjustStyleLink(href, linkID), AdjustStyleReasonNormal) @@ -426,7 +426,7 @@ func (parser *htmlParser) singleNodeToEntity(node *html.Node) Entity { node.Data = strings.ReplaceAll(node.Data, "\n", "") node.Data = spaces.ReplaceAllLiteralString(node.Data, " ") } - return TextToEntity(node.Data, parser.evt.ID, parser.prefs.InlineURLs) + return TextToEntity(node.Data, parser.evt.ID, parser.prefs.EnableInlineURLs()) case html.ElementNode: parsed := parser.tagNodeToEntity(node) if parsed != nil && !parsed.IsBlock() && parsed.IsEmpty() { diff --git a/ui/messages/parser.go b/ui/messages/parser.go index 07197db..79cc683 100644 --- a/ui/messages/parser.go +++ b/ui/messages/parser.go @@ -208,7 +208,7 @@ func ParseMessage(matrix ifc.MatrixContainer, room *rooms.Room, evt *muksevt.Eve return NewHTMLMessage(evt, displayname, html.Parse(matrix.Preferences(), room, content, evt, displayname)) } content.Body = strings.Replace(content.Body, "\t", " ", -1) - return NewHTMLMessage(evt, displayname, html.TextToEntity(content.Body, evt.ID, matrix.Preferences().InlineURLs)) + return NewHTMLMessage(evt, displayname, html.TextToEntity(content.Body, evt.ID, matrix.Preferences().EnableInlineURLs())) case event.MsgImage, event.MsgVideo, event.MsgAudio, event.MsgFile: msg := NewFileMessage(matrix, evt, displayname) if !matrix.Preferences().DisableDownloads {