From 84631a38da4fe79b673c1f0d1c4b14f290992685 Mon Sep 17 00:00:00 2001 From: sloumdrone Date: Tue, 10 Sep 2019 20:13:30 -0700 Subject: [PATCH] Adds telnet and http modules, updates visit method on client --- bookmarks.go | 62 +++++++++++++++++++++++++++------- client.go | 38 +++++++++++++-------- gopher/gopher.go | 22 ++++++------ gopher/open_browser_darwin.go | 9 ----- gopher/open_browser_linux.go | 9 ----- gopher/open_browser_other.go | 11 ------ gopher/open_browser_windows.go | 9 ----- http/open_browser_darwin.go | 13 +++++++ http/open_browser_linux.go | 13 +++++++ http/open_browser_other.go | 11 ++++++ http/open_browser_windows.go | 13 +++++++ page.go | 4 +-- telnet/telnet.go | 24 +++++++++++++ 13 files changed, 160 insertions(+), 78 deletions(-) delete mode 100644 gopher/open_browser_darwin.go delete mode 100644 gopher/open_browser_linux.go delete mode 100644 gopher/open_browser_other.go delete mode 100644 gopher/open_browser_windows.go create mode 100644 http/open_browser_darwin.go create mode 100644 http/open_browser_linux.go create mode 100644 http/open_browser_other.go create mode 100644 http/open_browser_windows.go create mode 100644 telnet/telnet.go diff --git a/bookmarks.go b/bookmarks.go index 3b044fd..a4c0c84 100644 --- a/bookmarks.go +++ b/bookmarks.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "strings" ) //------------------------------------------------\\ @@ -21,14 +22,24 @@ type Bookmarks struct { // + + + R E C E I V E R S + + + \\ //--------------------------------------------------\\ -func (b *Bookmarks) Add([]string) error { - // TODO add a bookmark - return fmt.Errorf("") +func (b *Bookmarks) Add(v []string) (string, error) { + if len(v) < 2 { + 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 { - // TODO delete a bookmark - return fmt.Errorf("") +func (b *Bookmarks) Delete(i int) (string, error) { + if i < len(b.Titles) && len(b.Titles) == len(b.Links) { + 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() { @@ -46,17 +57,42 @@ func (b *Bookmarks) ToggleFocused() { } } -func (b *Bookmarks) IniDump() string { - // TODO create dump of values for INI file - return "" +func (b Bookmarks) IniDump() string { + if len(b.Titles) < 0 { + 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) { - // TODO grab all of the bookmarks as a fixed - // width string including border and spacing - return []string{}, fmt.Errorf("") +// Get a list, including link nums, of bookmarks +// as a string slice +func (b Bookmarks) List() []string { + 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 + + + \\ diff --git a/client.go b/client.go index 8c2bdc0..bd4719e 100644 --- a/client.go +++ b/client.go @@ -14,7 +14,10 @@ import ( "tildegit.org/sloum/bombadillo/cmdparse" "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 { case "ADD", "A": - err := c.BookMarks.Add(values) + msg, err := c.BookMarks.Add(values) if err != nil { c.SetMessage(err.Error(), true) c.DrawMessage() return + } else { + c.SetMessage(msg, false) + c.DrawMessage() } err = saveConfig() @@ -342,15 +348,7 @@ func (c *client) search() { escapedEntry := entry go c.Visit(fmt.Sprintf("%s?%s",u.Full,escapedEntry)) case "http", "https": - c.SetMessage("Attempting to open in web browser", false) - 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() + c.Visit(u.Full) default: c.SetMessage(fmt.Sprintf("%q is not a supported protocol", u.Scheme), true) c.DrawMessage() @@ -463,19 +461,31 @@ func (c *client) Visit(url string) { // TODO send over to gopher request case "gemini": // 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": c.SetMessage("Attempting to open in web browser", false) c.DrawMessage() if strings.ToUpper(c.Options["openhttp"]) == "TRUE" { - err := gopher.OpenBrowser(u.Full) + msg, err := http.OpenInBrowser(u.Full) if err != nil { c.SetMessage(err.Error(), true) } else { - c.SetMessage("Opened in web browser", false) + c.SetMessage(msg, false) } c.DrawMessage() } 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() } default: diff --git a/gopher/gopher.go b/gopher/gopher.go index 5f488fe..d2c97a4 100644 --- a/gopher/gopher.go +++ b/gopher/gopher.go @@ -5,7 +5,7 @@ package gopher import ( "errors" - "fmt" + // "fmt" "io/ioutil" "net" "strings" @@ -86,16 +86,16 @@ func Visit(addr, openhttp string) (View, error) { return View{}, err } - if u.Gophertype == "h" { - if res, tf := isWebLink(u.Resource); tf && strings.ToUpper(openhttp) == "TRUE" { - err := OpenBrowser(res) - if err != nil { - return View{}, err - } - - return View{}, fmt.Errorf("") - } - } + // if u.Gophertype == "h" { + // if res, tf := isWebLink(u.Resource); tf && strings.ToUpper(openhttp) == "TRUE" { + // err := OpenBrowser(res) + // if err != nil { + // return View{}, err + // } +// + // return View{}, fmt.Errorf("") + // } + // } text, err := Retrieve(u) if err != nil { diff --git a/gopher/open_browser_darwin.go b/gopher/open_browser_darwin.go deleted file mode 100644 index 33db791..0000000 --- a/gopher/open_browser_darwin.go +++ /dev/null @@ -1,9 +0,0 @@ -// +build darwin - -package gopher - -import "os/exec" - -func OpenBrowser(url string) error { - return exec.Command("open", url).Start() -} diff --git a/gopher/open_browser_linux.go b/gopher/open_browser_linux.go deleted file mode 100644 index bea56c3..0000000 --- a/gopher/open_browser_linux.go +++ /dev/null @@ -1,9 +0,0 @@ -// +build linux - -package gopher - -import "os/exec" - -func OpenBrowser(url string) error { - return exec.Command("xdg-open", url).Start() -} diff --git a/gopher/open_browser_other.go b/gopher/open_browser_other.go deleted file mode 100644 index f452ed4..0000000 --- a/gopher/open_browser_other.go +++ /dev/null @@ -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") -} diff --git a/gopher/open_browser_windows.go b/gopher/open_browser_windows.go deleted file mode 100644 index 2912217..0000000 --- a/gopher/open_browser_windows.go +++ /dev/null @@ -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() -} diff --git a/http/open_browser_darwin.go b/http/open_browser_darwin.go new file mode 100644 index 0000000..dd7da7a --- /dev/null +++ b/http/open_browser_darwin.go @@ -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 +} diff --git a/http/open_browser_linux.go b/http/open_browser_linux.go new file mode 100644 index 0000000..dc99845 --- /dev/null +++ b/http/open_browser_linux.go @@ -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 +} diff --git a/http/open_browser_other.go b/http/open_browser_other.go new file mode 100644 index 0000000..c6e5342 --- /dev/null +++ b/http/open_browser_other.go @@ -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") +} diff --git a/http/open_browser_windows.go b/http/open_browser_windows.go new file mode 100644 index 0000000..0ddf6c7 --- /dev/null +++ b/http/open_browser_windows.go @@ -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 +} diff --git a/page.go b/page.go index 2e4b85e..9fa0a0e 100644 --- a/page.go +++ b/page.go @@ -6,7 +6,7 @@ package main //--------------------------------------------------\\ type Page struct { - WrappedContent []string + WrappedContent string RawContent string Links []string Location Url @@ -24,7 +24,7 @@ type Page struct { //--------------------------------------------------\\ 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 } diff --git a/telnet/telnet.go b/telnet/telnet.go new file mode 100644 index 0000000..609f13d --- /dev/null +++ b/telnet/telnet.go @@ -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 +} +