2020-06-18 16:54:48 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-06-23 20:23:28 -04:00
|
|
|
"fmt"
|
2021-06-24 23:59:14 -04:00
|
|
|
"io"
|
2020-06-18 16:54:48 -04:00
|
|
|
"os"
|
2021-06-24 23:59:14 -04:00
|
|
|
"strings"
|
2020-06-18 16:54:48 -04:00
|
|
|
|
2021-02-27 00:13:11 -05:00
|
|
|
"github.com/makeworld-the-better-one/amfora/bookmarks"
|
2020-11-23 21:09:48 -05:00
|
|
|
"github.com/makeworld-the-better-one/amfora/client"
|
2020-06-18 16:54:48 -04:00
|
|
|
"github.com/makeworld-the-better-one/amfora/config"
|
|
|
|
"github.com/makeworld-the-better-one/amfora/display"
|
2020-11-27 17:01:29 -05:00
|
|
|
"github.com/makeworld-the-better-one/amfora/subscriptions"
|
2020-06-18 16:54:48 -04:00
|
|
|
)
|
|
|
|
|
2020-09-01 19:55:06 -04:00
|
|
|
var (
|
2021-02-17 15:27:13 -05:00
|
|
|
version = "v1.8.0"
|
2020-09-01 19:55:06 -04:00
|
|
|
commit = "unknown"
|
|
|
|
builtBy = "unknown"
|
|
|
|
)
|
2020-06-23 20:23:28 -04:00
|
|
|
|
2020-06-18 16:54:48 -04:00
|
|
|
func main() {
|
2020-12-06 21:02:41 -05:00
|
|
|
// err := logger.Init()
|
|
|
|
// if err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
2020-06-18 16:54:48 -04:00
|
|
|
|
2020-06-23 20:23:28 -04:00
|
|
|
if len(os.Args) > 1 {
|
|
|
|
if os.Args[1] == "--version" || os.Args[1] == "-v" {
|
2020-09-01 19:55:06 -04:00
|
|
|
fmt.Println("Amfora", version)
|
|
|
|
fmt.Println("Commit:", commit)
|
|
|
|
fmt.Println("Built by:", builtBy)
|
2020-06-23 20:23:28 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if os.Args[1] == "--help" || os.Args[1] == "-h" {
|
2020-07-09 19:28:39 -04:00
|
|
|
fmt.Println("Amfora is a fancy terminal browser for the Gemini protocol.")
|
|
|
|
fmt.Println()
|
|
|
|
fmt.Println("Usage:")
|
|
|
|
fmt.Println("amfora [URL]")
|
|
|
|
fmt.Println("amfora --version, -v")
|
2020-06-23 20:23:28 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-06 21:02:41 -05:00
|
|
|
err := config.Init()
|
2020-06-18 16:54:48 -04:00
|
|
|
if err != nil {
|
2020-08-10 18:50:40 -04:00
|
|
|
fmt.Fprintf(os.Stderr, "Config error: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2021-02-27 00:13:11 -05:00
|
|
|
client.Init()
|
|
|
|
|
2020-11-27 17:01:29 -05:00
|
|
|
err = subscriptions.Init()
|
2020-08-10 18:50:40 -04:00
|
|
|
if err != nil {
|
2020-11-27 17:01:29 -05:00
|
|
|
fmt.Fprintf(os.Stderr, "subscriptions.json error: %v\n", err)
|
2020-07-09 19:28:39 -04:00
|
|
|
os.Exit(1)
|
2020-06-18 16:54:48 -04:00
|
|
|
}
|
2021-02-27 00:13:11 -05:00
|
|
|
err = bookmarks.Init()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "bookmarks.xml error: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-11-23 21:09:48 -05:00
|
|
|
|
2021-02-17 14:17:13 -05:00
|
|
|
// Initialize lower-level cview app
|
|
|
|
if err = display.App.Init(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize Amfora's settings
|
2020-12-20 16:39:33 -05:00
|
|
|
display.Init(version, commit, builtBy)
|
2020-06-24 13:18:23 -04:00
|
|
|
display.NewTab()
|
|
|
|
if len(os.Args[1:]) > 0 {
|
2020-06-21 19:48:58 -04:00
|
|
|
display.URL(os.Args[1])
|
2021-06-24 23:59:14 -04:00
|
|
|
} else if !isStdinEmpty() {
|
|
|
|
renderFromStdin()
|
2020-06-18 16:54:48 -04:00
|
|
|
}
|
|
|
|
|
2021-02-17 14:17:13 -05:00
|
|
|
// Start
|
2020-06-18 16:54:48 -04:00
|
|
|
if err = display.App.Run(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2021-06-24 23:59:14 -04:00
|
|
|
|
|
|
|
func isStdinEmpty() bool {
|
|
|
|
stat, _ := os.Stdin.Stat()
|
|
|
|
return (stat.Mode() & os.ModeCharDevice) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func renderFromStdin() {
|
|
|
|
stdinTextBuilder := new(strings.Builder)
|
|
|
|
_, err := io.Copy(stdinTextBuilder, os.Stdin)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error reading from standard input: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
stdinText := stdinTextBuilder.String()
|
|
|
|
display.RenderFromString(stdinText)
|
|
|
|
}
|