Adds telnet and http modules, updates visit method on client
This commit is contained in:
parent
da45f627e0
commit
84631a38da
62
bookmarks.go
62
bookmarks.go
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
//------------------------------------------------\\
|
//------------------------------------------------\\
|
||||||
|
@ -21,14 +22,24 @@ type Bookmarks struct {
|
||||||
// + + + R E C E I V E R S + + + \\
|
// + + + R E C E I V E R S + + + \\
|
||||||
//--------------------------------------------------\\
|
//--------------------------------------------------\\
|
||||||
|
|
||||||
func (b *Bookmarks) Add([]string) error {
|
func (b *Bookmarks) Add(v []string) (string, error) {
|
||||||
// TODO add a bookmark
|
if len(v) < 2 {
|
||||||
return fmt.Errorf("")
|
return "", fmt.Errorf("Received %d arguments, expected 2+", len(v))
|
||||||
|
}
|
||||||
|
b.Titles = append(b.Titles, strings.Join(v[1:], " "))
|
||||||
|
b.Links = append(b.Links, v[0])
|
||||||
|
b.Length = len(b.Titles)
|
||||||
|
return "Bookmark added successfully", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bookmarks) Delete(int) error {
|
func (b *Bookmarks) Delete(i int) (string, error) {
|
||||||
// TODO delete a bookmark
|
if i < len(b.Titles) && len(b.Titles) == len(b.Links) {
|
||||||
return fmt.Errorf("")
|
b.Titles = append(b.Titles[:i], b.Titles[i+1:]...)
|
||||||
|
b.Links = append(b.Links[:i], b.Links[i+1:]...)
|
||||||
|
b.Length = len(b.Titles)
|
||||||
|
return "Bookmark deleted successfully", nil
|
||||||
|
}
|
||||||
|
return "", fmt.Errorf("Bookmark %d does not exist", i)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bookmarks) ToggleOpen() {
|
func (b *Bookmarks) ToggleOpen() {
|
||||||
|
@ -46,17 +57,42 @@ func (b *Bookmarks) ToggleFocused() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bookmarks) IniDump() string {
|
func (b Bookmarks) IniDump() string {
|
||||||
// TODO create dump of values for INI file
|
if len(b.Titles) < 0 {
|
||||||
return ""
|
return ""
|
||||||
|
}
|
||||||
|
out := "[BOOKMARKS]\n"
|
||||||
|
for i := 0; i < len(b.Titles); i++ {
|
||||||
|
out += b.Titles[i]
|
||||||
|
out += "="
|
||||||
|
out += b.Links[i]
|
||||||
|
out += "\n"
|
||||||
|
}
|
||||||
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bookmarks) Render() ([]string, error) {
|
// Get a list, including link nums, of bookmarks
|
||||||
// TODO grab all of the bookmarks as a fixed
|
// as a string slice
|
||||||
// width string including border and spacing
|
func (b Bookmarks) List() []string {
|
||||||
return []string{}, fmt.Errorf("")
|
var out []string
|
||||||
|
for i, t := range b.Titles {
|
||||||
|
out = append(out, fmt.Sprintf("[%d] %s", i, t))
|
||||||
|
}
|
||||||
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b Bookmarks) Render() ([]string, error) {
|
||||||
|
// TODO Use b.List() to get the necessary
|
||||||
|
// text and add on the correct border for
|
||||||
|
// rendering the focus. Use sprintf, left
|
||||||
|
// aligned: "| %-36.36s |" of the like.
|
||||||
|
return []string{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO handle scrolling of the bookmarks list
|
||||||
|
// either here widh a scroll up/down or in the client
|
||||||
|
// code for scroll
|
||||||
|
|
||||||
|
|
||||||
//------------------------------------------------\\
|
//------------------------------------------------\\
|
||||||
// + + + F U N C T I O N S + + + \\
|
// + + + F U N C T I O N S + + + \\
|
||||||
|
|
38
client.go
38
client.go
|
@ -14,7 +14,10 @@ import (
|
||||||
|
|
||||||
"tildegit.org/sloum/bombadillo/cmdparse"
|
"tildegit.org/sloum/bombadillo/cmdparse"
|
||||||
"tildegit.org/sloum/bombadillo/cui"
|
"tildegit.org/sloum/bombadillo/cui"
|
||||||
"tildegit.org/sloum/bombadillo/gopher"
|
// "tildegit.org/sloum/bombadillo/gemini"
|
||||||
|
// "tildegit.org/sloum/bombadillo/gopher"
|
||||||
|
"tildegit.org/sloum/bombadillo/http"
|
||||||
|
"tildegit.org/sloum/bombadillo/telnet"
|
||||||
)
|
)
|
||||||
|
|
||||||
//------------------------------------------------\\
|
//------------------------------------------------\\
|
||||||
|
@ -231,11 +234,14 @@ func (c *client) doCommandAs(action string, values []string) {
|
||||||
|
|
||||||
switch action {
|
switch action {
|
||||||
case "ADD", "A":
|
case "ADD", "A":
|
||||||
err := c.BookMarks.Add(values)
|
msg, err := c.BookMarks.Add(values)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.SetMessage(err.Error(), true)
|
c.SetMessage(err.Error(), true)
|
||||||
c.DrawMessage()
|
c.DrawMessage()
|
||||||
return
|
return
|
||||||
|
} else {
|
||||||
|
c.SetMessage(msg, false)
|
||||||
|
c.DrawMessage()
|
||||||
}
|
}
|
||||||
|
|
||||||
err = saveConfig()
|
err = saveConfig()
|
||||||
|
@ -342,15 +348,7 @@ func (c *client) search() {
|
||||||
escapedEntry := entry
|
escapedEntry := entry
|
||||||
go c.Visit(fmt.Sprintf("%s?%s",u.Full,escapedEntry))
|
go c.Visit(fmt.Sprintf("%s?%s",u.Full,escapedEntry))
|
||||||
case "http", "https":
|
case "http", "https":
|
||||||
c.SetMessage("Attempting to open in web browser", false)
|
c.Visit(u.Full)
|
||||||
c.DrawMessage()
|
|
||||||
err := gopher.OpenBrowser(u.Full)
|
|
||||||
if err != nil {
|
|
||||||
c.SetMessage(err.Error(), true)
|
|
||||||
} else {
|
|
||||||
c.SetMessage("Opened in web browser", false)
|
|
||||||
}
|
|
||||||
c.DrawMessage()
|
|
||||||
default:
|
default:
|
||||||
c.SetMessage(fmt.Sprintf("%q is not a supported protocol", u.Scheme), true)
|
c.SetMessage(fmt.Sprintf("%q is not a supported protocol", u.Scheme), true)
|
||||||
c.DrawMessage()
|
c.DrawMessage()
|
||||||
|
@ -463,19 +461,31 @@ func (c *client) Visit(url string) {
|
||||||
// TODO send over to gopher request
|
// TODO send over to gopher request
|
||||||
case "gemini":
|
case "gemini":
|
||||||
// TODO send over to gemini request
|
// TODO send over to gemini request
|
||||||
|
case "telnet":
|
||||||
|
c.SetMessage("Attempting to start telnet session", false)
|
||||||
|
c.DrawMessage()
|
||||||
|
msg, err := telnet.StartSession(u.Host, u.Port)
|
||||||
|
if err != nil {
|
||||||
|
c.SetMessage(err.Error(), true)
|
||||||
|
c.DrawMessage()
|
||||||
|
} else {
|
||||||
|
c.SetMessage(msg, true)
|
||||||
|
c.DrawMessage()
|
||||||
|
}
|
||||||
|
c.Draw()
|
||||||
case "http", "https":
|
case "http", "https":
|
||||||
c.SetMessage("Attempting to open in web browser", false)
|
c.SetMessage("Attempting to open in web browser", false)
|
||||||
c.DrawMessage()
|
c.DrawMessage()
|
||||||
if strings.ToUpper(c.Options["openhttp"]) == "TRUE" {
|
if strings.ToUpper(c.Options["openhttp"]) == "TRUE" {
|
||||||
err := gopher.OpenBrowser(u.Full)
|
msg, err := http.OpenInBrowser(u.Full)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.SetMessage(err.Error(), true)
|
c.SetMessage(err.Error(), true)
|
||||||
} else {
|
} else {
|
||||||
c.SetMessage("Opened in web browser", false)
|
c.SetMessage(msg, false)
|
||||||
}
|
}
|
||||||
c.DrawMessage()
|
c.DrawMessage()
|
||||||
} else {
|
} else {
|
||||||
c.SetMessage("'openhttp' is not set to true, aborting opening web link", false)
|
c.SetMessage("'openhttp' is not set to true, cannot open web link", false)
|
||||||
c.DrawMessage()
|
c.DrawMessage()
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -5,7 +5,7 @@ package gopher
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
// "fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -86,16 +86,16 @@ func Visit(addr, openhttp string) (View, error) {
|
||||||
return View{}, err
|
return View{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if u.Gophertype == "h" {
|
// if u.Gophertype == "h" {
|
||||||
if res, tf := isWebLink(u.Resource); tf && strings.ToUpper(openhttp) == "TRUE" {
|
// if res, tf := isWebLink(u.Resource); tf && strings.ToUpper(openhttp) == "TRUE" {
|
||||||
err := OpenBrowser(res)
|
// err := OpenBrowser(res)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return View{}, err
|
// return View{}, err
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
return View{}, fmt.Errorf("")
|
// return View{}, fmt.Errorf("")
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
text, err := Retrieve(u)
|
text, err := Retrieve(u)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -1,9 +0,0 @@
|
||||||
// +build darwin
|
|
||||||
|
|
||||||
package gopher
|
|
||||||
|
|
||||||
import "os/exec"
|
|
||||||
|
|
||||||
func OpenBrowser(url string) error {
|
|
||||||
return exec.Command("open", url).Start()
|
|
||||||
}
|
|
|
@ -1,9 +0,0 @@
|
||||||
// +build linux
|
|
||||||
|
|
||||||
package gopher
|
|
||||||
|
|
||||||
import "os/exec"
|
|
||||||
|
|
||||||
func OpenBrowser(url string) error {
|
|
||||||
return exec.Command("xdg-open", url).Start()
|
|
||||||
}
|
|
|
@ -1,11 +0,0 @@
|
||||||
// +build !linux
|
|
||||||
// +build !darwin
|
|
||||||
// +build !windows
|
|
||||||
|
|
||||||
package gopher
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
func OpenBrowser(url string) error {
|
|
||||||
return fmt.Errorf("Unsupported os for browser detection")
|
|
||||||
}
|
|
|
@ -1,9 +0,0 @@
|
||||||
// +build windows
|
|
||||||
|
|
||||||
package gopher
|
|
||||||
|
|
||||||
import "os/exec"
|
|
||||||
|
|
||||||
func OpenBrowser(url string) error {
|
|
||||||
return exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
|
|
||||||
}
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
// +build darwin
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
// +build linux
|
||||||
|
|
||||||
|
package http
|
||||||
|
|
||||||
|
import "os/exec"
|
||||||
|
|
||||||
|
func OpenInBrowser(url string) (string, error) {
|
||||||
|
err := exec.Command("xdg-open", url).Start()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return "Opened in system default web browser", nil
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
// +build !linux
|
||||||
|
// +build !darwin
|
||||||
|
// +build !windows
|
||||||
|
|
||||||
|
package http
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func OpenInBrowser(url string) (string, error) {
|
||||||
|
return "", fmt.Errorf("Unsupported os for browser detection")
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
// +build windows
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
4
page.go
4
page.go
|
@ -6,7 +6,7 @@ package main
|
||||||
//--------------------------------------------------\\
|
//--------------------------------------------------\\
|
||||||
|
|
||||||
type Page struct {
|
type Page struct {
|
||||||
WrappedContent []string
|
WrappedContent string
|
||||||
RawContent string
|
RawContent string
|
||||||
Links []string
|
Links []string
|
||||||
Location Url
|
Location Url
|
||||||
|
@ -24,7 +24,7 @@ type Page struct {
|
||||||
//--------------------------------------------------\\
|
//--------------------------------------------------\\
|
||||||
|
|
||||||
func MakePage(url Url, content string) Page {
|
func MakePage(url Url, content string) Page {
|
||||||
p := Page{make([]string, 0), content, make([]string, 0), url, 0}
|
p := Page{"", content, make([]string, 0), url, 0}
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
package telnet
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
)
|
||||||
|
|
||||||
|
func StartSession(host string, port string) (string, error) {
|
||||||
|
// Case for telnet links
|
||||||
|
c := exec.Command("telnet", host, port)
|
||||||
|
c.Stdin = os.Stdin
|
||||||
|
c.Stdout = os.Stdout
|
||||||
|
c.Stderr = os.Stderr
|
||||||
|
// Clear the screen and position the cursor at the top left
|
||||||
|
fmt.Print("\033[2J\033[0;0H")
|
||||||
|
err := c.Run()
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("Telnet error response: %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return "Telnet session terminated", nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue