Gofmt all code, switch to canonical function naming, add a go.mod file for use with the latest go version

This commit is contained in:
Justin Overfelt 2019-04-28 15:46:47 -04:00
parent 3a173d89e0
commit 701753b890
16 changed files with 380 additions and 436 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
bombadillo

View File

@ -2,13 +2,11 @@ package cmdparse
import (
"bufio"
"strings"
"io"
"bytes"
"io"
"strings"
)
//------------------------------------------------\\
// + + + T Y P E S + + + \\
//--------------------------------------------------\\
@ -24,12 +22,12 @@ type scanner struct {
type tok int
//------------------------------------------------\\
// + + + V A R I A B L E S + + + \\
//--------------------------------------------------\\
var eof rune = rune(0)
const (
Word tok = iota
Action
@ -43,7 +41,6 @@ const (
illegal
)
//------------------------------------------------\\
// + + + R E C E I V E R S + + + \\
//--------------------------------------------------\\
@ -94,7 +91,7 @@ func (s *scanner) scanWhitespace() Token {
s.unread()
break
} else {
_,_ = buf.WriteRune(ch)
_, _ = buf.WriteRune(ch)
}
}
@ -113,7 +110,7 @@ func (s *scanner) scanNumber() Token {
s.unread()
break
} else {
_,_ = buf.WriteRune(ch)
_, _ = buf.WriteRune(ch)
}
}
@ -143,7 +140,6 @@ func (s *scanner) scan() Token {
return Token{illegal, string(char)}
}
//------------------------------------------------\\
// + + + F U N C T I O N S + + + \\
//--------------------------------------------------\\
@ -167,4 +163,3 @@ func isDigit(ch rune) bool {
func isEOF(ch rune) bool {
return ch == rune(0)
}

View File

@ -1,11 +1,10 @@
package cmdparse
import (
"io"
"fmt"
"io"
)
//------------------------------------------------\\
// + + + T Y P E S + + + \\
//--------------------------------------------------\\
@ -26,6 +25,7 @@ type Command struct {
}
type Comtype int
const (
GOURL Comtype = iota
GOLINK
@ -35,7 +35,6 @@ const (
DOAS
)
//------------------------------------------------\\
// + + + R E C E I V E R S + + + \\
//--------------------------------------------------\\
@ -132,7 +131,6 @@ func (p *Parser) Parse() (*Command, error) {
}
}
//------------------------------------------------\\
// + + + F U N C T I O N S + + + \\
//--------------------------------------------------\\

View File

@ -2,13 +2,11 @@ package config
import (
"bufio"
"io"
"bytes"
"fmt"
"io"
)
//------------------------------------------------\\
// + + + T Y P E S + + + \\
//--------------------------------------------------\\
@ -24,7 +22,6 @@ type scanner struct {
type TokenType int
//------------------------------------------------\\
// + + + V A R I A B L E S + + + \\
//--------------------------------------------------\\
@ -35,7 +32,6 @@ var r_brace rune = ']'
var newline rune = '\n'
var equal rune = '='
const (
TOK_SECTION TokenType = iota
TOK_KEY
@ -47,7 +43,6 @@ const (
TOK_WHITESPACE
)
//------------------------------------------------\\
// + + + R E C E I V E R S + + + \\
//--------------------------------------------------\\
@ -103,7 +98,7 @@ func (s *scanner) scanSection() Token {
s.skipToEndOfLine()
return Token{TOK_ERROR, "Second left brace encountered before closing right brace in section"}
} else {
_,_ = buf.WriteRune(ch)
_, _ = buf.WriteRune(ch)
}
}
@ -127,7 +122,7 @@ func (s *scanner) scanKey() Token {
s.skipToEndOfLine()
return Token{TOK_ERROR, "Illegal brace character in key"}
} else {
_,_ = buf.WriteRune(ch)
_, _ = buf.WriteRune(ch)
}
}
@ -142,7 +137,7 @@ func (s *scanner) scanValue() Token {
s.unread()
break
} else if ch == equal {
_,_ = buf.WriteRune(ch)
_, _ = buf.WriteRune(ch)
} else if ch == newline {
s.unread()
break
@ -150,7 +145,7 @@ func (s *scanner) scanValue() Token {
s.skipToEndOfLine()
return Token{TOK_ERROR, "Illegal brace character in key"}
} else {
_,_ = buf.WriteRune(ch)
_, _ = buf.WriteRune(ch)
}
}
@ -184,7 +179,6 @@ func (s *scanner) scan() Token {
return Token{TOK_ERROR, fmt.Sprintf("Error on character %q", char)}
}
//------------------------------------------------\\
// + + + F U N C T I O N S + + + \\
//--------------------------------------------------\\

View File

@ -1,13 +1,12 @@
package config
import (
"io"
"fmt"
"io"
"strings"
"bombadillo/gopher"
"tildegit.org/sloum/bombadillo/gopher"
)
//------------------------------------------------\\
// + + + T Y P E S + + + \\
//--------------------------------------------------\\
@ -32,7 +31,6 @@ type KeyValue struct {
Value string
}
//------------------------------------------------\\
// + + + R E C E I V E R S + + + \\
//--------------------------------------------------\\
@ -68,7 +66,6 @@ func (p *Parser) parseKeyValue() (KeyValue, error) {
func (p *Parser) unscan() { p.buffer.size = 1 }
func (p *Parser) Parse() (Config, error) {
p.row = 1
section := ""
@ -103,7 +100,6 @@ func (p *Parser) Parse() (Config, error) {
return c, nil
}
//------------------------------------------------\\
// + + + F U N C T I O N S + + + \\
//--------------------------------------------------\\

View File

@ -1,12 +1,12 @@
package cui
import (
"strings"
"bufio"
"bytes"
"fmt"
"bufio"
"os"
"os/exec"
"strings"
)
var shapes = map[string]string{
@ -24,7 +24,6 @@ var shapes = map[string]string{
"abr": "╝",
}
func drawShape(shape string) {
if val, ok := shapes[shape]; ok {
fmt.Printf("%s", val)
@ -90,19 +89,19 @@ func WrapLines(s []string, length int) []string {
var subout bytes.Buffer
for i, wd := range words {
sublen := subout.Len()
if sublen + len(wd) + 1 <= length {
if sublen+len(wd)+1 <= length {
if sublen > 0 {
subout.WriteString(" ")
}
subout.WriteString(wd)
if i == len(words) - 1 {
if i == len(words)-1 {
out = append(out, subout.String())
}
} else {
out = append(out, subout.String())
subout.Reset()
subout.WriteString(wd)
if i == len(words) - 1 {
if i == len(words)-1 {
out = append(out, subout.String())
subout.Reset()
}

View File

@ -9,7 +9,6 @@ type MsgBar struct {
showTitle bool
}
// SetTitle sets the title for the MsgBar in question
func (m *MsgBar) SetTitle(s string) {
m.title = s
@ -28,6 +27,6 @@ func (m MsgBar) ClearAll() {
// ClearMessage clears all message text while leaving the title in place
func (m *MsgBar) ClearMessage() {
MoveCursorTo(m.row, len(m.title) + 1)
MoveCursorTo(m.row, len(m.title)+1)
Clear("right")
}

View File

@ -1,11 +1,11 @@
package cui
import (
"strings"
"bytes"
"fmt"
"os"
"os/exec"
"bytes"
"strings"
)
// screenInit records whether or not the screen has been initialized
@ -24,7 +24,6 @@ type Screen struct {
Bars []*MsgBar
}
// AddWindow adds a new window to the Screen struct in question
func (s *Screen) AddWindow(r1, c1, r2, c2 int, scroll, border, show bool) {
w := Window{box{r1, c1, r2, c2}, scroll, 0, []string{}, border, false, show}
@ -46,7 +45,7 @@ func (s Screen) DrawAllWindows() {
w.DrawWindow()
}
}
MoveCursorTo(s.Height - 1, 1)
MoveCursorTo(s.Height-1, 1)
}
// Clear removes all content from the interior of the screen
@ -57,7 +56,6 @@ func (s Screen) Clear() {
}
}
// ReflashScreen checks for a screen resize and resizes windows if
// needed then redraws the screen. It takes a bool to decide whether
// to redraw the full screen or just the content. On a resize
@ -108,12 +106,12 @@ func (s *Screen) DrawMsgBars() {
title := bar.title
fmt.Print(title)
if len(bar.title) > s.Width {
title = string(bar.title[:s.Width - 3]) + "..."
title = string(bar.title[:s.Width-3]) + "..."
}
_, _ = buf.WriteString(title)
msg := bar.message
if len(bar.message) > s.Width - len(title) {
msg = string(bar.message[:s.Width - len(title) - 3]) + "..."
if len(bar.message) > s.Width-len(title) {
msg = string(bar.message[:s.Width-len(title)-3]) + "..."
}
_, _ = buf.WriteString(msg)
@ -124,7 +122,6 @@ func (s *Screen) DrawMsgBars() {
}
}
// GetSize retrieves the terminal size and sets the Screen
// width and height to that size
func (s *Screen) GetSize() {
@ -141,7 +138,6 @@ func (s *Screen) GetSize() {
s.Width = w
}
// - - - - - - - - - - - - - - - - - - - - - - - - - -
// NewScreen is a constructor function that returns a pointer

View File

@ -5,7 +5,6 @@ import (
"strings"
)
type box struct {
row1 int
col1 int
@ -32,42 +31,42 @@ func (w *Window) DrawWindow() {
}
}
func (w *Window) DrawBox(){
func (w *Window) DrawBox() {
lead := ""
if w.Active {
lead = "a"
}
moveThenDrawShape(w.Box.row1, w.Box.col1, lead + "tl")
moveThenDrawShape(w.Box.row1, w.Box.col2, lead + "tr")
moveThenDrawShape(w.Box.row2, w.Box.col1, lead + "bl")
moveThenDrawShape(w.Box.row2, w.Box.col2, lead + "br")
moveThenDrawShape(w.Box.row1, w.Box.col1, lead+"tl")
moveThenDrawShape(w.Box.row1, w.Box.col2, lead+"tr")
moveThenDrawShape(w.Box.row2, w.Box.col1, lead+"bl")
moveThenDrawShape(w.Box.row2, w.Box.col2, lead+"br")
for i := w.Box.col1 + 1; i < w.Box.col2; i++ {
moveThenDrawShape(w.Box.row1, i, lead + "ceiling")
moveThenDrawShape(w.Box.row2, i, lead + "ceiling")
moveThenDrawShape(w.Box.row1, i, lead+"ceiling")
moveThenDrawShape(w.Box.row2, i, lead+"ceiling")
}
for i:= w.Box.row1 + 1; i < w.Box.row2; i++ {
moveThenDrawShape(i, w.Box.col1, lead + "wall")
moveThenDrawShape(i, w.Box.col2, lead + "wall")
for i := w.Box.row1 + 1; i < w.Box.row2; i++ {
moveThenDrawShape(i, w.Box.col1, lead+"wall")
moveThenDrawShape(i, w.Box.col2, lead+"wall")
}
}
func (w *Window) DrawContent(){
var maxlines, border_thickness, contenth int
func (w *Window) DrawContent() {
var maxlines, borderThickness, contenth int
var short_content bool = false
if w.drawBox {
border_thickness, contenth = -1, 1
borderThickness, contenth = -1, 1
} else {
border_thickness, contenth = 1, 0
borderThickness, contenth = 1, 0
}
height := w.Box.row2 - w.Box.row1 + border_thickness
width := w.Box.col2 - w.Box.col1 + border_thickness
height := w.Box.row2 - w.Box.row1 + borderThickness
width := w.Box.col2 - w.Box.col1 + borderThickness
content := WrapLines(w.Content, width)
if len(content) < w.Scrollposition + height {
if len(content) < w.Scrollposition+height {
maxlines = len(content)
short_content = true
} else {
@ -75,30 +74,30 @@ func (w *Window) DrawContent(){
}
for i := w.Scrollposition; i < maxlines; i++ {
MoveCursorTo(w.Box.row1 + contenth + i - w.Scrollposition, w.Box.col1 + contenth)
fmt.Print( strings.Repeat(" ", width) )
MoveCursorTo(w.Box.row1 + contenth + i - w.Scrollposition, w.Box.col1 + contenth)
MoveCursorTo(w.Box.row1+contenth+i-w.Scrollposition, w.Box.col1+contenth)
fmt.Print(strings.Repeat(" ", width))
MoveCursorTo(w.Box.row1+contenth+i-w.Scrollposition, w.Box.col1+contenth)
fmt.Print(content[i])
}
if short_content {
for i := len(content); i <= height; i++ {
MoveCursorTo(w.Box.row1 + contenth + i - w.Scrollposition, w.Box.col1 + contenth)
fmt.Print( strings.Repeat(" ", width) )
MoveCursorTo(w.Box.row1+contenth+i-w.Scrollposition, w.Box.col1+contenth)
fmt.Print(strings.Repeat(" ", width))
}
}
}
func (w *Window) ScrollDown() {
var border_thickness int
var borderThickness int
if w.drawBox {
border_thickness = -1
borderThickness = -1
} else {
border_thickness = 1
borderThickness = 1
}
height := w.Box.row2 - w.Box.row1 + border_thickness
height := w.Box.row2 - w.Box.row1 + borderThickness
contentLength := len(w.Content)
if w.Scrollposition < contentLength - height {
if w.Scrollposition < contentLength-height {
w.Scrollposition++
} else {
fmt.Print("\a")
@ -112,4 +111,3 @@ func (w *Window) ScrollUp() {
fmt.Print("\a")
}
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module tildegit.org/sloum/bombadillo
go 1.10

View File

@ -9,7 +9,6 @@ import (
// + + + T Y P E S + + + \\
//--------------------------------------------------\\
//Bookmarks is a holder for titles and links that
//can be retrieved by index
type Bookmarks struct {
@ -17,14 +16,10 @@ type Bookmarks struct {
Links []string
}
//------------------------------------------------\\
// + + + R E C E I V E R S + + + \\
//--------------------------------------------------\\
// Add adds a new title and link combination to the bookmarks
// struct. It takes as input a string slice in which the first
// element represents the link and all following items represent
@ -40,8 +35,8 @@ func (b *Bookmarks) Add(v []string) error {
func (b *Bookmarks) Del(i int) error {
if i < len(b.Titles) && i < len(b.Links) {
b.Titles = append(b.Titles[:i], b.Titles[i + 1:]...)
b.Links = append(b.Links[:i], b.Links[i + 1:]...)
b.Titles = append(b.Titles[:i], b.Titles[i+1:]...)
b.Links = append(b.Links[:i], b.Links[i+1:]...)
return nil
}
return fmt.Errorf("Bookmark %d does not exist", i)
@ -55,7 +50,6 @@ func (b Bookmarks) List() []string {
return out
}
func (b Bookmarks) IniDump() string {
if len(b.Titles) < 0 {
return ""
@ -70,7 +64,6 @@ func (b Bookmarks) IniDump() string {
return out
}
func MakeBookmarks() Bookmarks {
return Bookmarks{[]string{}, []string{}}
}

View File

@ -4,17 +4,16 @@
package gopher
import (
"strings"
"errors"
"net"
"fmt"
"io/ioutil"
"time"
"net"
"os/exec"
"runtime"
"fmt"
"strings"
"time"
)
//------------------------------------------------\\
// + + + V A R I A B L E S + + + \\
//--------------------------------------------------\\
@ -37,13 +36,10 @@ var types = map[string]string{
"p": "PNG",
}
//------------------------------------------------\\
// + + + F U N C T I O N S + + + \\
//--------------------------------------------------\\
// Retrieve makes a request to a Url and resturns
// the response as []byte/error. This function is
// available to use directly, but in most implementations
@ -82,7 +78,6 @@ func Retrieve(u Url) ([]byte, error) {
return result, err
}
// Visit is a high level combination of a few different
// types that makes it easy to create a Url, make a request
// to that Url, and add the response and Url to a View.

View File

@ -1,8 +1,8 @@
package gopher
import (
"fmt"
"errors"
"fmt"
)
//------------------------------------------------\\
@ -22,7 +22,6 @@ type History struct {
Collection [20]View
}
//------------------------------------------------\\
// + + + R E C E I V E R S + + + \\
//--------------------------------------------------\\
@ -34,11 +33,11 @@ type History struct {
// history length if something is added in the middle.
func (h *History) Add(v View) {
v.ParseMap()
if h.Position == h.Length - 1 && h.Length < len(h.Collection) {
if h.Position == h.Length-1 && h.Length < len(h.Collection) {
h.Collection[h.Length] = v
h.Length++
h.Position++
} else if h.Position == h.Length - 1 && h.Length == 20 {
} else if h.Position == h.Length-1 && h.Length == 20 {
for x := 1; x < len(h.Collection); x++ {
h.Collection[x-1] = h.Collection[x]
}
@ -76,13 +75,12 @@ func (h *History) GoBack() bool {
return false
}
// The "GoForward" receiver is called by a history struct.
// When called it increments the current position and
// displays the content for the View in that position.
// If history is at position len - 1, no action is taken.
func (h *History) GoForward() bool {
if h.Position + 1 < h.Length {
if h.Position+1 < h.Length {
h.Position++
return true
}
@ -100,12 +98,10 @@ func (h *History) DisplayCurrentView() {
h.Collection[h.Position].Display()
}
//------------------------------------------------\\
// + + + F U N C T I O N S + + + \\
//--------------------------------------------------\\
// Constructor function for History struct.
// This is used to initialize history position
// as -1, which is needed. Returns a copy of

View File

@ -1,15 +1,14 @@
package gopher
import (
"regexp"
"errors"
"regexp"
)
//------------------------------------------------\\
// + + + T Y P E S + + + \\
//--------------------------------------------------\\
// The url struct represents a URL for the rest of the system.
// It includes component parts as well as a full URL string.
type Url struct {
@ -22,7 +21,6 @@ type Url struct {
IsBinary bool
}
//------------------------------------------------\\
// + + + F U N C T I O N S + + + \\
//--------------------------------------------------\\
@ -87,4 +85,3 @@ func MakeUrl(u string) (Url, error) {
return out, nil
}

View File

@ -1,17 +1,14 @@
package gopher
import (
"strings"
"fmt"
"strings"
)
//------------------------------------------------\\
// + + + T Y P E S + + + \\
//--------------------------------------------------\\
// View is a struct representing a gopher page. It contains
// the page content as a string slice, a list of link URLs
// as string slices, and the Url struct representing the page.
@ -21,12 +18,10 @@ type View struct {
Address Url
}
//------------------------------------------------\\
// + + + R E C E I V E R S + + + \\
//--------------------------------------------------\\
// ParseMap is called by a view struct to parse a gophermap.
// It checks if the view is for a gophermap. If not,it does
// nothing. If so, it parses the gophermap into comment lines
@ -44,7 +39,7 @@ func (v *View) ParseMap() {
continue
}
line := strings.Split(e,"\t")
line := strings.Split(e, "\t")
var title string
if len(line[0]) > 1 {
title = line[0][1:]
@ -73,12 +68,10 @@ func (v View) Display() {
}
}
//------------------------------------------------\\
// + + + F U N C T I O N S + + + \\
//--------------------------------------------------\\
// MakeView creates and returns a new View struct from
// a Url and a string splice of content. This is used to
// initialize a View with a Url struct, links, and content.
@ -89,5 +82,3 @@ func MakeView(url Url, content []string) View {
v.ParseMap()
return v
}

View File

@ -1,20 +1,20 @@
package main
import (
"bombadillo/gopher"
"bombadillo/cmdparse"
"bombadillo/config"
"bombadillo/cui"
"os/user"
"fmt"
"io/ioutil"
"os"
"fmt"
"strings"
"os/user"
"regexp"
"strconv"
"strings"
"tildegit.org/sloum/bombadillo/cmdparse"
"tildegit.org/sloum/bombadillo/config"
"tildegit.org/sloum/bombadillo/cui"
"tildegit.org/sloum/bombadillo/gopher"
)
var helplocation string ="gopher://colorfield.space:70/1/bombadillo-info"
var helplocation string = "gopher://colorfield.space:70/1/bombadillo-info"
var history gopher.History = gopher.MakeHistory()
var screen *cui.Screen
var userinfo, _ = user.Current()
@ -27,12 +27,12 @@ var options = map[string]string{
"httpbrowser": "lynx",
}
func err_exit(err string, code int) {
func errExit(err string, code int) {
fmt.Println(err)
os.Exit(code)
}
func save_file(address, name string) error {
func saveFile(address, name string) error {
quickMessage("Saving file...", false)
defer quickMessage("Saving file...", true)
@ -46,7 +46,7 @@ func save_file(address, name string) error {
return err
}
err = ioutil.WriteFile(options["savelocation"] + name, data, 0644)
err = ioutil.WriteFile(options["savelocation"]+name, data, 0644)
if err != nil {
return err
}
@ -54,25 +54,25 @@ func save_file(address, name string) error {
return fmt.Errorf("Saved file to " + options["savelocation"] + name)
}
func save_file_from_data(v gopher.View) error {
func saveFileFromData(v gopher.View) error {
urlsplit := strings.Split(v.Address.Full, "/")
filename := urlsplit[len(urlsplit) - 1]
save_msg := fmt.Sprintf("Saved file as %q", options["savelocation"] + filename)
quickMessage(save_msg, false)
defer quickMessage(save_msg, true)
err := ioutil.WriteFile(options["savelocation"] + filename, []byte(strings.Join(v.Content, "")), 0644)
filename := urlsplit[len(urlsplit)-1]
saveMsg := fmt.Sprintf("Saved file as %q", options["savelocation"]+filename)
quickMessage(saveMsg, false)
defer quickMessage(saveMsg, true)
err := ioutil.WriteFile(options["savelocation"]+filename, []byte(strings.Join(v.Content, "")), 0644)
if err != nil {
return err
}
return fmt.Errorf(save_msg)
return fmt.Errorf(saveMsg)
}
func search(u string) error {
cui.MoveCursorTo(screen.Height - 1, 0)
cui.MoveCursorTo(screen.Height-1, 0)
cui.Clear("line")
fmt.Print("Enter form input: ")
cui.MoveCursorTo(screen.Height - 1, 17)
cui.MoveCursorTo(screen.Height-1, 17)
entry := cui.GetLine()
quickMessage("Searching...", false)
searchurl := fmt.Sprintf("%s\t%s", u, entry)
@ -89,22 +89,21 @@ func search(u string) error {
return nil
}
func route_input(com *cmdparse.Command) error {
func routeInput(com *cmdparse.Command) error {
var err error
switch com.Type {
case cmdparse.SIMPLE:
err = simple_command(com.Action)
err = simpleCommand(com.Action)
case cmdparse.GOURL:
err = go_to_url(com.Target)
err = goToURL(com.Target)
case cmdparse.GOLINK:
err = go_to_link(com.Target)
err = goToLink(com.Target)
case cmdparse.DOLINK:
err = do_link_command(com.Action, com.Target)
err = doLinkCommand(com.Action, com.Target)
case cmdparse.DOAS:
err = do_command_as(com.Action, com.Value)
err = doCommandAs(com.Action, com.Value)
case cmdparse.DOLINKAS:
err = do_link_command_as(com.Action, com.Target, com.Value)
err = doLinkCommandAs(com.Action, com.Target, com.Value)
default:
return fmt.Errorf("Unknown command entry!")
}
@ -112,7 +111,7 @@ func route_input(com *cmdparse.Command) error {
return err
}
func toggle_bookmarks() {
func toggleBookmarks() {
bookmarks := screen.Windows[1]
main := screen.Windows[0]
if bookmarks.Show {
@ -133,19 +132,19 @@ func toggle_bookmarks() {
screen.ReflashScreen(false)
}
func simple_command(a string) error {
func simpleCommand(a string) error {
a = strings.ToUpper(a)
switch a {
case "Q", "QUIT":
cui.Exit()
case "H", "HOME":
return go_home()
return goHome()
case "B", "BOOKMARKS":
toggle_bookmarks()
toggleBookmarks()
case "SEARCH":
return search(options["searchengine"])
case "HELP":
return go_to_url(helplocation)
return goToURL(helplocation)
default:
return fmt.Errorf("Unknown action %q", a)
@ -153,7 +152,7 @@ func simple_command(a string) error {
return nil
}
func go_to_url(u string) error {
func goToURL(u string) error {
quickMessage("Loading...", false)
v, err := gopher.Visit(u, options["openhttp"])
if err != nil {
@ -169,7 +168,7 @@ func go_to_url(u string) error {
}
} else if v.Address.IsBinary {
// TO DO: run this into the write to file method
return save_file_from_data(v)
return saveFileFromData(v)
} else {
history.Add(v)
}
@ -179,12 +178,12 @@ func go_to_url(u string) error {
return nil
}
func go_to_link(l string) error {
func goToLink(l string) error {
if num, _ := regexp.MatchString(`^\d+$`, l); num && history.Length > 0 {
linkcount := len(history.Collection[history.Position].Links)
item, _ := strconv.Atoi(l)
if item <= linkcount {
linkurl := history.Collection[history.Position].Links[item - 1]
linkurl := history.Collection[history.Position].Links[item-1]
quickMessage("Loading...", false)
v, err := gopher.Visit(linkurl, options["openhttp"])
if err != nil {
@ -199,7 +198,7 @@ func go_to_link(l string) error {
return err
}
} else if v.Address.IsBinary {
return save_file_from_data(v)
return saveFileFromData(v)
} else {
history.Add(v)
}
@ -215,14 +214,14 @@ func go_to_link(l string) error {
return nil
}
func go_home() error {
func goHome() error {
if options["homeurl"] != "unset" {
return go_to_url(options["homeurl"])
return goToURL(options["homeurl"])
}
return fmt.Errorf("No home address has been set")
}
func do_link_command(action, target string) error {
func doLinkCommand(action, target string) error {
num, err := strconv.Atoi(target)
if err != nil {
return fmt.Errorf("Expected number, got %q", target)
@ -232,21 +231,21 @@ func do_link_command(action, target string) error {
case "DELETE", "D":
err := settings.Bookmarks.Del(num)
screen.Windows[1].Content = settings.Bookmarks.List()
save_config()
saveConfig()
screen.ReflashScreen(false)
return err
case "BOOKMARKS", "B":
if num > len(settings.Bookmarks.Links) - 1 {
if num > len(settings.Bookmarks.Links)-1 {
return fmt.Errorf("There is no bookmark with ID %d", num)
}
err := go_to_url(settings.Bookmarks.Links[num])
err := goToURL(settings.Bookmarks.Links[num])
return err
}
return fmt.Errorf("This method has not been built")
}
func do_command_as(action string, values []string) error {
func doCommandAs(action string, values []string) error {
if len(values) < 2 {
return fmt.Errorf("%q", values)
}
@ -262,23 +261,23 @@ func do_command_as(action string, values []string) error {
return err
}
screen.Windows[1].Content = settings.Bookmarks.List()
save_config()
saveConfig()
screen.ReflashScreen(false)
return nil
case "WRITE", "W":
return save_file(values[0], strings.Join(values[1:], " "))
return saveFile(values[0], strings.Join(values[1:], " "))
case "SET", "S":
if _, ok := options[values[0]]; ok {
options[values[0]] = strings.Join(values[1:], " ")
save_config()
saveConfig()
return nil
}
return fmt.Errorf("Unable to set %s, it does not exist",values[0])
return fmt.Errorf("Unable to set %s, it does not exist", values[0])
}
return fmt.Errorf("Unknown command structure")
}
func do_link_command_as(action, target string, values []string) error {
func doLinkCommandAs(action, target string, values []string) error {
num, err := strconv.Atoi(target)
if err != nil {
return fmt.Errorf("Expected number, got %q", target)
@ -291,30 +290,29 @@ func do_link_command_as(action, target string, values []string) error {
switch action {
case "ADD", "A":
newBookmark := append([]string{links[num - 1]}, values...)
newBookmark := append([]string{links[num-1]}, values...)
err := settings.Bookmarks.Add(newBookmark)
if err != nil {
return err
}
screen.Windows[1].Content = settings.Bookmarks.List()
save_config()
saveConfig()
screen.ReflashScreen(false)
return nil
case "WRITE", "W":
return save_file(links[num - 1], strings.Join(values, " "))
return saveFile(links[num-1], strings.Join(values, " "))
}
return fmt.Errorf("This method has not been built")
}
func updateMainContent() {
screen.Windows[0].Content = history.Collection[history.Position].Content
screen.Bars[0].SetMessage(history.Collection[history.Position].Address.Full)
}
func clearInput(incError bool) {
cui.MoveCursorTo(screen.Height - 1, 0)
cui.MoveCursorTo(screen.Height-1, 0)
cui.Clear("line")
if incError {
cui.MoveCursorTo(screen.Height, 0)
@ -323,7 +321,7 @@ func clearInput(incError bool) {
}
func quickMessage(msg string, clearMsg bool) {
cui.MoveCursorTo(screen.Height, screen.Width - 2 - len(msg))
cui.MoveCursorTo(screen.Height, screen.Width-2-len(msg))
if clearMsg {
cui.Clear("right")
} else {
@ -331,8 +329,7 @@ func quickMessage(msg string, clearMsg bool) {
}
}
func save_config() {
func saveConfig() {
bkmrks := settings.Bookmarks.IniDump()
opts := "\n[SETTINGS]\n"
for k, v := range options {
@ -341,14 +338,13 @@ func save_config() {
opts += v
opts += "\n"
}
ioutil.WriteFile(userinfo.HomeDir + "/.bombadillo.ini", []byte(bkmrks+opts), 0644)
ioutil.WriteFile(userinfo.HomeDir+"/.bombadillo.ini", []byte(bkmrks+opts), 0644)
}
func load_config() {
func loadConfig() {
file, err := os.Open(userinfo.HomeDir + "/.bombadillo.ini")
if err != nil {
save_config()
saveConfig()
}
confparser := config.NewParser(file)
settings, _ = confparser.Parse()
@ -362,7 +358,7 @@ func load_config() {
}
}
func toggleActiveWindow(){
func toggleActiveWindow() {
if screen.Windows[1].Show {
if screen.Windows[0].Active {
screen.Windows[0].Active = false
@ -377,39 +373,36 @@ func toggleActiveWindow(){
}
}
func display_error(err error) {
func displayError(err error) {
cui.MoveCursorTo(screen.Height, 0)
fmt.Print("\033[41m\033[37m", err, "\033[0m")
}
func initClient() {
history.Position = -1
screen = cui.NewScreen()
cui.SetCharMode()
screen.AddWindow(2, 1, screen.Height - 2, screen.Width, false, false, true)
screen.AddWindow(2, 1, screen.Height-2, screen.Width, false, false, true)
screen.Windows[0].Active = true
screen.AddMsgBar(1, " ((( Bombadillo ))) ", " A fun gopher client!", true)
bookmarksWidth := 40
if screen.Width < 40 {
bookmarksWidth = screen.Width
}
screen.AddWindow(2, screen.Width - bookmarksWidth, screen.Height - 2, screen.Width, false, true, false)
load_config()
screen.AddWindow(2, screen.Width-bookmarksWidth, screen.Height-2, screen.Width, false, true, false)
loadConfig()
}
func main() {
defer cui.Exit()
initClient()
mainWindow := screen.Windows[0]
first_load := true
firstLoad := true
for {
if first_load {
first_load = false
err := go_home()
if firstLoad {
firstLoad = false
err := goHome()
if err == nil {
updateMainContent()
@ -435,7 +428,7 @@ func main() {
screen.ReflashScreen(true)
}
case 'B':
toggle_bookmarks()
toggleBookmarks()
case 'f', 'F':
success := history.GoForward()
if success {
@ -445,8 +438,8 @@ func main() {
}
case '\t':
toggleActiveWindow()
case ':',' ':
cui.MoveCursorTo(screen.Height - 1, 0)
case ':', ' ':
cui.MoveCursorTo(screen.Height-1, 0)
entry := cui.GetLine()
// Clear entry line and error line
clearInput(true)
@ -456,11 +449,11 @@ func main() {
parser := cmdparse.NewParser(strings.NewReader(entry))
p, err := parser.Parse()
if err != nil {
display_error(err)
displayError(err)
} else {
err := route_input(p)
err := routeInput(p)
if err != nil {
display_error(err)
displayError(err)
}
}
}