Fixes errors introduced by a distracted commit earlier, also handles linting
This commit is contained in:
parent
e6f1ecd41c
commit
ef27e54d0e
|
@ -8,12 +8,16 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type page struct {
|
// Page represents the contents and links or an http/https document
|
||||||
|
type Page struct {
|
||||||
Content string
|
Content string
|
||||||
Links []string
|
Links []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func Visit(webmode, url string, width int) (page, error) {
|
// Visit is the main entry to viewing a web document in bombadillo.
|
||||||
|
// It takes a url, a terminal width, and which web backend the user
|
||||||
|
// currently has set. Visit returns a Page and an error
|
||||||
|
func Visit(webmode, url string, width int) (Page, error) {
|
||||||
if width > 80 {
|
if width > 80 {
|
||||||
width = 80
|
width = 80
|
||||||
}
|
}
|
||||||
|
@ -26,17 +30,18 @@ func Visit(webmode, url string, width int) (page, error) {
|
||||||
case "elinks":
|
case "elinks":
|
||||||
w = "-dump-width"
|
w = "-dump-width"
|
||||||
default:
|
default:
|
||||||
return page{}, fmt.Errorf("Invalid webmode setting")
|
return Page{}, fmt.Errorf("Invalid webmode setting")
|
||||||
}
|
}
|
||||||
c, err := exec.Command(webmode, "-dump", w, fmt.Sprintf("%d", width), url).Output()
|
c, err := exec.Command(webmode, "-dump", w, fmt.Sprintf("%d", width), url).Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return page{}, err
|
return Page{}, err
|
||||||
}
|
}
|
||||||
return parseLinks(string(c)), nil
|
return parseLinks(string(c)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns false on err or non-text type
|
// IsTextFile makes an http(s) head request to a given URL
|
||||||
// Else returns true
|
// and determines if the content-type is text based. It then
|
||||||
|
// returns a bool
|
||||||
func IsTextFile(url string) bool {
|
func IsTextFile(url string) bool {
|
||||||
resp, err := http.Head(url)
|
resp, err := http.Head(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -50,8 +55,8 @@ func IsTextFile(url string) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseLinks(c string) page {
|
func parseLinks(c string) Page {
|
||||||
var out page
|
var out Page
|
||||||
contentUntil := strings.LastIndex(c, "References")
|
contentUntil := strings.LastIndex(c, "References")
|
||||||
if contentUntil >= 1 {
|
if contentUntil >= 1 {
|
||||||
out.Content = c[:contentUntil]
|
out.Content = c[:contentUntil]
|
||||||
|
@ -74,6 +79,9 @@ func parseLinks(c string) page {
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fetch makes an http(s) request and returns the []bytes
|
||||||
|
// for the response and an error. Fetch is used for saving
|
||||||
|
// the source file of an http(s) document
|
||||||
func Fetch(url string) ([]byte, error) {
|
func Fetch(url string) ([]byte, error) {
|
||||||
resp, err := http.Get(url)
|
resp, err := http.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -2,21 +2,27 @@
|
||||||
|
|
||||||
package http
|
package http
|
||||||
|
|
||||||
import "os/exec"
|
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) {
|
func OpenInBrowser(url string) (string, error) {
|
||||||
// Check for a local display server, this is
|
disp := os.Getenv("DISPLAY")
|
||||||
// not a silver bullet but should help ssh
|
wayland := os.Getenv("WAYLAND_DISPLAY")
|
||||||
// connected users on many systems get accurate
|
_, err := exec.LookPath("Xorg")
|
||||||
// messaging and not spin off processes needlessly
|
if disp == "" && wayland == "" && err != nil {
|
||||||
err := exec.Command("type", "Xorg").Run()
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("No gui is available, check 'webmode' setting")
|
return "", fmt.Errorf("No gui is available, check 'webmode' setting")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use start rather than run or output in order
|
// Use start rather than run or output in order
|
||||||
// to release the process and not block
|
// to release the process and not block
|
||||||
err := exec.Command("xdg-open", url).Start()
|
err = exec.Command("xdg-open", url).Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue