Simple fix applied, expanded error message, gofmt
This commit is contained in:
parent
66acf6102f
commit
1045829076
82
client.go
82
client.go
|
@ -23,19 +23,18 @@ import (
|
|||
//--------------------------------------------------\\
|
||||
|
||||
type client struct {
|
||||
Height int
|
||||
Width int
|
||||
Options map[string]string
|
||||
Message string
|
||||
Height int
|
||||
Width int
|
||||
Options map[string]string
|
||||
Message string
|
||||
MessageIsErr bool
|
||||
PageState Pages
|
||||
BookMarks Bookmarks
|
||||
TopBar Headbar
|
||||
FootBar Footbar
|
||||
Certs gemini.TofuDigest
|
||||
PageState Pages
|
||||
BookMarks Bookmarks
|
||||
TopBar Headbar
|
||||
FootBar Footbar
|
||||
Certs gemini.TofuDigest
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------\\
|
||||
// + + + R E C E I V E R S + + + \\
|
||||
//--------------------------------------------------\\
|
||||
|
@ -82,21 +81,21 @@ func (c *client) GetSize() {
|
|||
|
||||
func (c *client) Draw() {
|
||||
var screen strings.Builder
|
||||
screen.Grow(c.Height * c.Width + c.Width)
|
||||
screen.Grow(c.Height*c.Width + c.Width)
|
||||
screen.WriteString("\033[0m")
|
||||
screen.WriteString(c.TopBar.Render(c.Width, c.Options["theme"]))
|
||||
screen.WriteString("\n")
|
||||
pageContent := c.PageState.Render(c.Height, c.Width - 1)
|
||||
if c.Options["theme"] == "inverse" {
|
||||
pageContent := c.PageState.Render(c.Height, c.Width-1)
|
||||
if c.Options["theme"] == "inverse" {
|
||||
screen.WriteString("\033[7m")
|
||||
}
|
||||
if c.BookMarks.IsOpen {
|
||||
bm := c.BookMarks.Render(c.Width, c.Height)
|
||||
bmWidth := len([]rune(bm[0]))
|
||||
for i := 0; i < c.Height - 3; i++ {
|
||||
for i := 0; i < c.Height-3; i++ {
|
||||
if c.Width > bmWidth {
|
||||
contentWidth := c.Width - bmWidth
|
||||
if i < len(pageContent) {
|
||||
if i < len(pageContent) {
|
||||
screen.WriteString(fmt.Sprintf("%-*.*s", contentWidth, contentWidth, pageContent[i]))
|
||||
} else {
|
||||
screen.WriteString(fmt.Sprintf("%-*.*s", contentWidth, contentWidth, " "))
|
||||
|
@ -109,7 +108,7 @@ func (c *client) Draw() {
|
|||
} else if !c.BookMarks.IsFocused {
|
||||
screen.WriteString("\033[2m")
|
||||
}
|
||||
|
||||
|
||||
screen.WriteString(bm[i])
|
||||
|
||||
if c.Options["theme"] == "inverse" && !c.BookMarks.IsFocused {
|
||||
|
@ -121,9 +120,9 @@ func (c *client) Draw() {
|
|||
screen.WriteString("\n")
|
||||
}
|
||||
} else {
|
||||
for i := 0; i < c.Height - 3; i++ {
|
||||
for i := 0; i < c.Height-3; i++ {
|
||||
if i < len(pageContent) {
|
||||
screen.WriteString(fmt.Sprintf("%-*.*s", c.Width - 1, c.Width - 1, pageContent[i]))
|
||||
screen.WriteString(fmt.Sprintf("%-*.*s", c.Width-1, c.Width-1, pageContent[i]))
|
||||
screen.WriteString("\n")
|
||||
} else {
|
||||
screen.WriteString(fmt.Sprintf("%-*.*s", c.Width, c.Width, " "))
|
||||
|
@ -137,7 +136,7 @@ func (c *client) Draw() {
|
|||
screen.WriteString("\n") // for the input line
|
||||
screen.WriteString(c.FootBar.Render(c.Width, c.PageState.Position, c.Options["theme"]))
|
||||
// cui.Clear("screen")
|
||||
cui.MoveCursorTo(0,0)
|
||||
cui.MoveCursorTo(0, 0)
|
||||
fmt.Print(screen.String())
|
||||
}
|
||||
|
||||
|
@ -167,12 +166,12 @@ func (c *client) TakeControlInput() {
|
|||
case 'd':
|
||||
// scroll down 75%
|
||||
c.ClearMessage()
|
||||
distance := c.Height - c.Height / 4
|
||||
distance := c.Height - c.Height/4
|
||||
c.Scroll(distance)
|
||||
case 'u':
|
||||
// scroll up 75%
|
||||
c.ClearMessage()
|
||||
distance := c.Height - c.Height / 4
|
||||
distance := c.Height - c.Height/4
|
||||
c.Scroll(-distance)
|
||||
case 'b':
|
||||
// go back
|
||||
|
@ -239,7 +238,6 @@ func (c *client) TakeControlInput() {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
func (c *client) routeCommandInput(com *cmdparse.Command) error {
|
||||
var err error
|
||||
switch com.Type {
|
||||
|
@ -286,7 +284,7 @@ func (c *client) simpleCommand(action string) {
|
|||
case "HELP", "?":
|
||||
go c.Visit(helplocation)
|
||||
default:
|
||||
c.SetMessage(fmt.Sprintf("Unknown action %q", action), true)
|
||||
c.SetMessage(fmt.Sprintf("Unknown action %q", action), true)
|
||||
c.DrawMessage()
|
||||
}
|
||||
}
|
||||
|
@ -335,7 +333,7 @@ func (c *client) doCommand(action string, values []string) {
|
|||
fns := strings.Split(u.Resource, "/")
|
||||
var fn string
|
||||
if len(fns) > 0 {
|
||||
fn = strings.Trim(fns[len(fns) - 1], "\t\r\n \a\f\v")
|
||||
fn = strings.Trim(fns[len(fns)-1], "\t\r\n \a\f\v")
|
||||
} else {
|
||||
fn = "index"
|
||||
}
|
||||
|
@ -345,7 +343,7 @@ func (c *client) doCommand(action string, values []string) {
|
|||
c.saveFile(u, fn)
|
||||
|
||||
default:
|
||||
c.SetMessage(fmt.Sprintf("Unknown action %q", action), true)
|
||||
c.SetMessage(fmt.Sprintf("Unknown action %q", action), true)
|
||||
c.DrawMessage()
|
||||
}
|
||||
}
|
||||
|
@ -461,7 +459,7 @@ func (c *client) doLinkCommandAs(action, target string, values []string) {
|
|||
c.Draw()
|
||||
}
|
||||
case "WRITE", "W":
|
||||
out := make([]string, 0, len(values) + 1)
|
||||
out := make([]string, 0, len(values)+1)
|
||||
out = append(out, links[num])
|
||||
out = append(out, values...)
|
||||
c.doCommandAs(action, out)
|
||||
|
@ -470,7 +468,6 @@ func (c *client) doLinkCommandAs(action, target string, values []string) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
func (c *client) getCurrentPageUrl() (string, error) {
|
||||
if c.PageState.Length < 1 {
|
||||
return "", fmt.Errorf("There are no pages in history")
|
||||
|
@ -506,10 +503,10 @@ func (c *client) saveFile(u Url, name string) {
|
|||
c.DrawMessage()
|
||||
return
|
||||
}
|
||||
savePath := c.Options["savelocation"] + name
|
||||
savePath := c.Options["savelocation"] + "/" + name
|
||||
err = ioutil.WriteFile(savePath, file, 0644)
|
||||
if err != nil {
|
||||
c.SetMessage("Error writing file to disk", true)
|
||||
c.SetMessage("Error writing file: "+err.Error(), true)
|
||||
c.DrawMessage()
|
||||
return
|
||||
}
|
||||
|
@ -541,7 +538,6 @@ func (c *client) doLinkCommand(action, target string) {
|
|||
c.DrawMessage()
|
||||
}
|
||||
|
||||
|
||||
switch action {
|
||||
case "DELETE", "D":
|
||||
msg, err := c.BookMarks.Delete(num)
|
||||
|
@ -579,7 +575,7 @@ func (c *client) doLinkCommand(action, target string) {
|
|||
return
|
||||
}
|
||||
link := links[num]
|
||||
c.SetMessage(fmt.Sprintf("[%d] %s", num + 1, link), false)
|
||||
c.SetMessage(fmt.Sprintf("[%d] %s", num+1, link), false)
|
||||
c.DrawMessage()
|
||||
case "WRITE", "W":
|
||||
links := c.PageState.History[c.PageState.Position].Links
|
||||
|
@ -597,7 +593,7 @@ func (c *client) doLinkCommand(action, target string) {
|
|||
fns := strings.Split(u.Resource, "/")
|
||||
var fn string
|
||||
if len(fns) > 0 {
|
||||
fn = strings.Trim(fns[len(fns) - 1], "\t\r\n \a\f\v")
|
||||
fn = strings.Trim(fns[len(fns)-1], "\t\r\n \a\f\v")
|
||||
} else {
|
||||
fn = "index"
|
||||
}
|
||||
|
@ -606,7 +602,7 @@ func (c *client) doLinkCommand(action, target string) {
|
|||
}
|
||||
c.saveFile(u, fn)
|
||||
default:
|
||||
c.SetMessage(fmt.Sprintf("Action %q does not exist for target %q", action, target), true)
|
||||
c.SetMessage(fmt.Sprintf("Action %q does not exist for target %q", action, target), true)
|
||||
c.DrawMessage()
|
||||
}
|
||||
|
||||
|
@ -645,11 +641,11 @@ func (c *client) search(query, url, question string) {
|
|||
}
|
||||
switch u.Scheme {
|
||||
case "gopher":
|
||||
go c.Visit(fmt.Sprintf("%s\t%s",u.Full,entry))
|
||||
go c.Visit(fmt.Sprintf("%s\t%s", u.Full, entry))
|
||||
case "gemini":
|
||||
// TODO url escape the entry variable
|
||||
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":
|
||||
c.Visit(u.Full)
|
||||
default:
|
||||
|
@ -707,10 +703,10 @@ func (c *client) Scroll(amount int) {
|
|||
|
||||
c.PageState.History[c.PageState.Position].ScrollPosition = newScrollPosition
|
||||
|
||||
if len(page.WrappedContent) < c.Height - 3 {
|
||||
if len(page.WrappedContent) < c.Height-3 {
|
||||
percentRead = 100
|
||||
} else {
|
||||
percentRead = int(float32(newScrollPosition + c.Height - 3) / float32(len(page.WrappedContent)) * 100.0)
|
||||
percentRead = int(float32(newScrollPosition+c.Height-3) / float32(len(page.WrappedContent)) * 100.0)
|
||||
}
|
||||
c.FootBar.SetPercentRead(percentRead)
|
||||
}
|
||||
|
@ -720,10 +716,10 @@ func (c *client) Scroll(amount int) {
|
|||
func (c *client) SetPercentRead() {
|
||||
page := c.PageState.History[c.PageState.Position]
|
||||
var percentRead int
|
||||
if len(page.WrappedContent) < c.Height - 3 {
|
||||
if len(page.WrappedContent) < c.Height-3 {
|
||||
percentRead = 100
|
||||
} else {
|
||||
percentRead = int(float32(page.ScrollPosition + c.Height - 3) / float32(len(page.WrappedContent)) * 100.0)
|
||||
percentRead = int(float32(page.ScrollPosition+c.Height-3) / float32(len(page.WrappedContent)) * 100.0)
|
||||
}
|
||||
c.FootBar.SetPercentRead(percentRead)
|
||||
}
|
||||
|
@ -830,7 +826,7 @@ func (c *client) Visit(url string) {
|
|||
case "gopher":
|
||||
if u.DownloadOnly {
|
||||
nameSplit := strings.Split(u.Resource, "/")
|
||||
filename := nameSplit[len(nameSplit) - 1]
|
||||
filename := nameSplit[len(nameSplit)-1]
|
||||
filename = strings.Trim(filename, " \t\r\n\v\f\a")
|
||||
if filename == "" {
|
||||
filename = "gopherfile"
|
||||
|
@ -924,7 +920,7 @@ func (c *client) Visit(url string) {
|
|||
c.Draw()
|
||||
case 'w':
|
||||
nameSplit := strings.Split(u.Resource, "/")
|
||||
filename := nameSplit[len(nameSplit) - 1]
|
||||
filename := nameSplit[len(nameSplit)-1]
|
||||
c.saveFileFromData(capsule.Content, filename)
|
||||
}
|
||||
}
|
||||
|
@ -954,7 +950,7 @@ func (c *client) Visit(url string) {
|
|||
case "http", "https":
|
||||
c.SetMessage("Attempting to open in web browser", false)
|
||||
c.DrawMessage()
|
||||
if strings.ToUpper(c.Options["openhttp"]) == "TRUE" {
|
||||
if strings.ToUpper(c.Options["openhttp"]) == "TRUE" {
|
||||
msg, err := http.OpenInBrowser(u.Full)
|
||||
if err != nil {
|
||||
c.SetMessage(err.Error(), true)
|
||||
|
@ -972,7 +968,6 @@ func (c *client) Visit(url string) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------\\
|
||||
// + + + F U N C T I O N S + + + \\
|
||||
//--------------------------------------------------\\
|
||||
|
@ -981,4 +976,3 @@ func MakeClient(name string) *client {
|
|||
c := client{0, 0, defaultOptions, "", false, MakePages(), MakeBookmarks(), MakeHeadbar(name), MakeFootbar(), gemini.MakeTofuDigest()}
|
||||
return &c
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue