Adds url opening

This commit is contained in:
sloum 2021-05-08 14:53:33 -07:00
parent 0b059c9a72
commit 8a22a34853
5 changed files with 67 additions and 0 deletions

View File

@ -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",

13
open_browser_darwin.go Normal file
View File

@ -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
}

30
open_browser_other.go Normal file
View File

@ -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
}

13
open_browser_windows.go Normal file
View File

@ -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
}

View File

@ -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)
}
}
}
}