From 8a22a34853d1a14420e07da0af01faf984ac4d33 Mon Sep 17 00:00:00 2001 From: sloum Date: Sat, 8 May 2021 14:53:33 -0700 Subject: [PATCH] Adds url opening --- main.go | 5 +++++ open_browser_darwin.go | 13 +++++++++++++ open_browser_other.go | 30 ++++++++++++++++++++++++++++++ open_browser_windows.go | 13 +++++++++++++ workbook.go | 6 ++++++ 5 files changed, 67 insertions(+) create mode 100644 open_browser_darwin.go create mode 100644 open_browser_other.go create mode 100644 open_browser_windows.go diff --git a/main.go b/main.go index 5b72697..cf091f9 100644 --- a/main.go +++ b/main.go @@ -25,6 +25,7 @@ const ( var wb workbook var reAddr *regexp.Regexp = regexp.MustCompile(`^\$?[A-Z]\$?[0-9]+$`) var reAddrRange *regexp.Regexp = regexp.MustCompile(`^\$?[A-Z]\$?[0-9]+:\$?[A-Z]\$?[0-9]+\+$`) +var reURL *regexp.Regexp = regexp.MustCompile(`^.*://.*$`) var modified bool = false type point struct { @@ -178,6 +179,10 @@ func IsRange(addr string) bool { return reAddrRange.MatchString(addr) } +func IsURL(u string) bool { + return reURL.MatchString(u) +} + func IsFunc(val string) bool { switch strings.ToUpper(val) { case "+", "-", "/", "*", "MIN", "MAX", diff --git a/open_browser_darwin.go b/open_browser_darwin.go new file mode 100644 index 0000000..d57a290 --- /dev/null +++ b/open_browser_darwin.go @@ -0,0 +1,13 @@ +// This will build for osx without a build tag based on the filename + +package http + +import "os/exec" + +func OpenInBrowser(url string) (string, error) { + err := exec.Command("open", url).Start() + if err != nil { + return "", err + } + return "Opened in system default web browser", nil +} diff --git a/open_browser_other.go b/open_browser_other.go new file mode 100644 index 0000000..2f74d22 --- /dev/null +++ b/open_browser_other.go @@ -0,0 +1,30 @@ +// +build !darwin,!windows + +package http + +import ( + "fmt" + "os" + "os/exec" +) + +// OpenInBrowser checks for the presence of a display server +// and environment variables indicating a gui is present. If found +// then xdg-open is called on a url to open said url in the default +// gui web browser for the system +func OpenInBrowser(url string) (string, error) { + disp := os.Getenv("DISPLAY") + wayland := os.Getenv("WAYLAND_DISPLAY") + _, err := exec.LookPath("Xorg") + if disp == "" && wayland == "" && err != nil { + return "", fmt.Errorf("No gui is available, check 'webmode' setting") + } + + // Use start rather than run or output in order + // to release the process and not block + err = exec.Command("xdg-open", url).Start() + if err != nil { + return "", err + } + return "Opened in system default web browser", nil +} diff --git a/open_browser_windows.go b/open_browser_windows.go new file mode 100644 index 0000000..496d00b --- /dev/null +++ b/open_browser_windows.go @@ -0,0 +1,13 @@ +// This will only build for windows based on the filename +// no build tag required +package http + +import "os/exec" + +func OpenInBrowser(url string) (string, error) { + err := exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start() + if err != nil { + return "", err + } + return "Opened in system default web browser", nil +} diff --git a/workbook.go b/workbook.go index af24ea9..b7be7a3 100644 --- a/workbook.go +++ b/workbook.go @@ -34,6 +34,7 @@ const ( ModFaint rune = 'f' ModItalic rune = 'i' ModUnderline rune = 'u' + OpenLink rune = 'O' ) type workbook struct { @@ -133,6 +134,11 @@ func (w *workbook) Run() { w.sheets[w.sheet].cells[w.sheets[w.sheet].selection.row-1][w.sheets[w.sheet].selection.col-1].ToggleMod(Italic) case ModUnderline: w.sheets[w.sheet].cells[w.sheets[w.sheet].selection.row-1][w.sheets[w.sheet].selection.col-1].ToggleMod(Underline) + case OpenLink: + url := w.sheets[w.sheet].cells[w.sheets[w.sheet].selection.row-1][w.sheets[w.sheet].selection.col-1].mask + if IsURL(url) { + OpenInBrowser(url) + } } } }