2019-02-25 01:43:10 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-03-17 16:58:39 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2019-04-28 19:46:47 +00:00
|
|
|
"strings"
|
2019-05-01 22:25:45 +00:00
|
|
|
|
2019-04-28 19:46:47 +00:00
|
|
|
"tildegit.org/sloum/bombadillo/config"
|
|
|
|
"tildegit.org/sloum/bombadillo/cui"
|
2019-02-25 01:43:10 +00:00
|
|
|
)
|
|
|
|
|
2019-09-10 02:35:16 +00:00
|
|
|
var bombadillo *client
|
2019-04-28 19:46:47 +00:00
|
|
|
var helplocation string = "gopher://colorfield.space:70/1/bombadillo-info"
|
2019-03-17 16:58:39 +00:00
|
|
|
var settings config.Config
|
2019-02-25 01:43:10 +00:00
|
|
|
|
2019-05-01 00:18:28 +00:00
|
|
|
|
2019-09-10 02:35:16 +00:00
|
|
|
// func saveFileFromData(v gopher.View) error {
|
|
|
|
// quickMessage("Saving file...", false)
|
|
|
|
// urlsplit := strings.Split(v.Address.Full, "/")
|
|
|
|
// filename := urlsplit[len(urlsplit)-1]
|
|
|
|
// saveMsg := fmt.Sprintf("Saved file as %q", options["savelocation"]+filename)
|
|
|
|
// err := ioutil.WriteFile(options["savelocation"]+filename, []byte(strings.Join(v.Content, "")), 0644)
|
|
|
|
// if err != nil {
|
|
|
|
// quickMessage("Saving file...", true)
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
|
|
|
|
// quickMessage(saveMsg, false)
|
|
|
|
// return nil
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
2019-05-01 00:18:28 +00:00
|
|
|
func saveConfig() error {
|
2019-09-10 02:35:16 +00:00
|
|
|
bkmrks := bombadillo.BookMarks.IniDump()
|
|
|
|
// TODO opts becomes a string builder rather than concat
|
2019-03-17 16:58:39 +00:00
|
|
|
opts := "\n[SETTINGS]\n"
|
2019-09-10 02:35:16 +00:00
|
|
|
for k, v := range bombadillo.Options {
|
2019-03-17 16:58:39 +00:00
|
|
|
opts += k
|
|
|
|
opts += "="
|
|
|
|
opts += v
|
|
|
|
opts += "\n"
|
|
|
|
}
|
2019-05-01 00:18:28 +00:00
|
|
|
|
2019-09-10 02:35:16 +00:00
|
|
|
return ioutil.WriteFile(bombadillo.Options["configlocation"] + "/.bombadillo.ini", []byte(bkmrks+opts), 0644)
|
2019-03-17 16:58:39 +00:00
|
|
|
}
|
|
|
|
|
2019-05-01 00:18:28 +00:00
|
|
|
func loadConfig() error {
|
2019-09-10 02:35:16 +00:00
|
|
|
file, err := os.Open(bombadillo.Options["configlocation"] + "/.bombadillo.ini")
|
2019-03-19 03:11:02 +00:00
|
|
|
if err != nil {
|
2019-05-01 00:18:28 +00:00
|
|
|
err = saveConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-19 03:11:02 +00:00
|
|
|
}
|
2019-05-01 00:18:28 +00:00
|
|
|
|
2019-03-17 16:58:39 +00:00
|
|
|
confparser := config.NewParser(file)
|
|
|
|
settings, _ = confparser.Parse()
|
|
|
|
file.Close()
|
|
|
|
for _, v := range settings.Settings {
|
|
|
|
lowerkey := strings.ToLower(v.Key)
|
2019-09-10 02:35:16 +00:00
|
|
|
if lowerkey == "configlocation" {
|
|
|
|
// The config should always be stored in home
|
|
|
|
// folder. Users cannot really edit this value.
|
|
|
|
// It is still stored in the ini and as a part
|
|
|
|
// of the options map.
|
|
|
|
continue
|
2019-03-17 16:58:39 +00:00
|
|
|
}
|
2019-05-01 00:18:28 +00:00
|
|
|
|
2019-09-10 02:35:16 +00:00
|
|
|
if _, ok := bombadillo.Options[lowerkey]; ok {
|
|
|
|
bombadillo.Options[lowerkey] = v.Value
|
2019-04-11 05:29:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-13 03:57:48 +00:00
|
|
|
for i, v := range settings.Bookmarks.Titles {
|
|
|
|
bombadillo.BookMarks.Add([]string{v, settings.Bookmarks.Links[i]})
|
|
|
|
}
|
|
|
|
|
2019-09-10 02:35:16 +00:00
|
|
|
return nil
|
2019-03-19 03:11:02 +00:00
|
|
|
}
|
|
|
|
|
2019-05-01 00:18:28 +00:00
|
|
|
func initClient() error {
|
2019-09-10 02:35:16 +00:00
|
|
|
bombadillo = MakeClient(" ((( Bombadillo ))) ")
|
2019-03-21 05:29:09 +00:00
|
|
|
cui.SetCharMode()
|
2019-09-10 02:35:16 +00:00
|
|
|
err := loadConfig()
|
|
|
|
return err
|
2019-05-28 21:23:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-17 16:58:39 +00:00
|
|
|
func main() {
|
2019-09-13 03:57:48 +00:00
|
|
|
cui.HandleAlternateScreen("smcup")
|
|
|
|
defer cui.Exit()
|
2019-05-01 00:18:28 +00:00
|
|
|
err := initClient()
|
|
|
|
if err != nil {
|
2019-09-10 02:35:16 +00:00
|
|
|
// if we can't initialize we should bail out
|
2019-05-01 00:18:28 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-09-14 05:56:38 +00:00
|
|
|
// TODO find out why the loading message
|
|
|
|
// has disappeared on initial load...
|
|
|
|
|
2019-09-10 02:35:16 +00:00
|
|
|
// Start polling for terminal size changes
|
|
|
|
go bombadillo.GetSize()
|
2019-02-25 01:43:10 +00:00
|
|
|
|
2019-05-09 00:16:59 +00:00
|
|
|
if len(os.Args) > 1 {
|
2019-09-10 02:35:16 +00:00
|
|
|
// If a url was passed, move it down the line
|
|
|
|
// Goroutine so keypresses can be made during
|
|
|
|
// page load
|
2019-09-14 05:56:38 +00:00
|
|
|
go bombadillo.Visit(os.Args[1])
|
2019-05-09 00:16:59 +00:00
|
|
|
} else {
|
2019-09-10 02:35:16 +00:00
|
|
|
// Otherwise, load the homeurl
|
|
|
|
// Goroutine so keypresses can be made during
|
|
|
|
// page load
|
2019-09-14 05:56:38 +00:00
|
|
|
go bombadillo.Visit(bombadillo.Options["homeurl"])
|
2019-05-09 00:16:59 +00:00
|
|
|
}
|
2019-03-17 16:58:39 +00:00
|
|
|
|
2019-09-10 02:35:16 +00:00
|
|
|
// Loop indefinitely on user input
|
2019-05-09 00:16:59 +00:00
|
|
|
for {
|
2019-09-10 02:35:16 +00:00
|
|
|
bombadillo.TakeControlInput()
|
2019-02-25 01:43:10 +00:00
|
|
|
}
|
|
|
|
}
|