forked from makew0rld/amfora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amfora.go
129 lines (113 loc) · 2.84 KB
/
amfora.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/makeworld-the-better-one/amfora/bookmarks"
"github.com/makeworld-the-better-one/amfora/client"
"github.com/makeworld-the-better-one/amfora/config"
"github.com/makeworld-the-better-one/amfora/display"
"github.com/makeworld-the-better-one/amfora/logger"
"github.com/makeworld-the-better-one/amfora/subscriptions"
)
var (
version = "v1.9.2"
commit = "unknown"
builtBy = "unknown"
)
func main() {
log, err := logger.GetLogger()
if err != nil {
panic(err)
}
debugModeEnabled := os.Getenv("AMFORA_DEBUG") == "1"
if debugModeEnabled {
log.Println("Debug mode enabled")
}
if len(os.Args) > 1 {
if os.Args[1] == "--version" || os.Args[1] == "-v" {
fmt.Println("Amfora", version)
fmt.Println("Commit:", commit)
fmt.Println("Built by:", builtBy)
return
}
if os.Args[1] == "--help" || os.Args[1] == "-h" {
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")
return
}
}
err = config.Init()
if err != nil {
fmt.Fprintf(os.Stderr, "Config error: %v\n", err)
os.Exit(1)
}
err = client.Init()
if err != nil {
fmt.Fprintf(os.Stderr, "Client error: %v\n", err)
os.Exit(1)
}
err = subscriptions.Init()
if err != nil {
fmt.Fprintf(os.Stderr, "subscriptions.json error: %v\n", err)
os.Exit(1)
}
err = bookmarks.Init()
if err != nil {
fmt.Fprintf(os.Stderr, "bookmarks.xml error: %v\n", err)
os.Exit(1)
}
// Initialize lower-level cview app
if err = display.App.Init(); err != nil {
panic(err)
}
// Initialize Amfora's settings
display.Init(version, commit, builtBy)
// Load a URL, file, or render from stdin
if len(os.Args[1:]) > 0 {
url := os.Args[1]
if !strings.Contains(url, "://") || strings.HasPrefix(url, "../") || strings.HasPrefix(url, "./") {
fileName := url
if _, err := os.Stat(fileName); err == nil {
if !strings.HasPrefix(fileName, "/") {
cwd, err := os.Getwd()
if err != nil {
fmt.Fprintf(os.Stderr, "error getting working directory path: %v\n", err)
os.Exit(1)
}
fileName = filepath.Join(cwd, fileName)
}
url = "file://" + fileName
}
}
display.NewTabWithURL(url)
} else if !isStdinEmpty() {
display.NewTab()
renderFromStdin()
} else {
display.NewTab()
}
// Start
if err = display.App.Run(); err != nil {
panic(err)
}
}
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)
}