Permalink
Browse files

allow html to be passed to openurl

revert from using webbrowser, it cannot handle file:// urls yet
  • Loading branch information...
justone committed Oct 10, 2016
1 parent 6cb1e2c commit 530d16fc8a38194750958a5c0cac79a189f2d6e0
Showing with 65 additions and 9 deletions.
  1. +8 −1 introducer.go
  2. +6 −5 openurl.go
  3. +51 −3 util.go
@@ -91,7 +91,14 @@ func runIntroducer(bus *pmb.PMB, conn *pmb.Connection) error {
}
conn.Out <- pmb.Message{Contents: data}
} else if message.Contents["type"].(string) == "OpenURL" {
err := openURL(message.Contents["data"].(string))
var isHTML bool
if isHTMLRaw, ok := message.Contents["is_html"]; ok {
isHTML = isHTMLRaw.(bool)
} else {
isHTML = false
}
err := openURL(message.Contents["data"].(string), isHTML)
if err != nil {
displayNotice(fmt.Sprintf("Unable to open url: %v", err), false)
continue
@@ -12,7 +12,7 @@ import (
)
type OpenURLCommand struct {
// no options yet
IsHTML bool `short:"H" long:"html" description:"Instead of a URL, HTML is being provided."`
}
var openURLCommand OpenURLCommand
@@ -40,7 +40,7 @@ func (x *OpenURLCommand) Execute(args []string) error {
return err
}
return runOpenURL(conn, id, strings.TrimSpace(data))
return runOpenURL(conn, id, strings.TrimSpace(data), openURLCommand.IsHTML)
}
func init() {
@@ -50,11 +50,12 @@ func init() {
&openURLCommand)
}
func runOpenURL(conn *pmb.Connection, id string, data string) error {
func runOpenURL(conn *pmb.Connection, id string, data string, isHTML bool) error {
copyData := map[string]interface{}{
"type": "OpenURL",
"data": data,
"type": "OpenURL",
"data": data,
"is_html": isHTML,
}
conn.Out <- pmb.Message{Contents: copyData}
54 util.go
@@ -2,12 +2,13 @@ package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"runtime"
"strings"
"github.com/Sirupsen/logrus"
"github.com/toqueteos/webbrowser"
)
func copyToClipboard(data string) error {
@@ -31,8 +32,55 @@ func copyToClipboard(data string) error {
return nil
}
func openURL(url string) error {
return webbrowser.Open(url)
func openURL(data string, isHTML bool) error {
var url string
if isHTML {
tmpfile, err := ioutil.TempFile("", "pmbopenurl")
if err != nil {
return err
}
_, err = tmpfile.Write([]byte(data))
if err != nil {
return err
}
err = tmpfile.Close()
if err != nil {
return err
}
nameWithSuffix := fmt.Sprintf("%s.html", tmpfile.Name())
err = os.Rename(tmpfile.Name(), nameWithSuffix)
if err != nil {
return err
}
// url = fmt.Sprintf("file://%s", nameWithSuffix)
url = nameWithSuffix
} else {
url = data
}
logrus.Infof("opening url: %s", url)
// TODO switch to using webbrowser when it can handle file urls
// return webbrowser.Open(url)
var cmd *exec.Cmd
// only supports OSX
if runtime.GOOS == "darwin" {
cmd = exec.Command("open", url)
} else {
return fmt.Errorf("unable to open URL on this platform")
}
cmd.Stdin = strings.NewReader(data)
err := cmd.Run()
if err != nil {
return err
}
return nil
}
func truncate(data string, length int) string {

0 comments on commit 530d16f

Please sign in to comment.