-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
131 lines (123 loc) · 2.78 KB
/
config.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
130
131
package main
import (
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/BurntSushi/toml"
)
// Config is the configuration structure for gelim
type Config struct {
Prompt string
MaxRedirects int
ShowRedirectHistory bool
StartURL string
LessOpts string
SearchURL string
Index0Shortcut int
MaxWidth int
ClipboardCopyCmd string
UseCertificate []string
}
// LoadConfig opens the specified configuration file if exists and returns a
// parsed configuration structure
func LoadConfig(path string) (*Config, error) {
var err error
var conf Config
// Defaults
conf.Prompt = "%U\n>"
conf.MaxRedirects = 5
conf.ShowRedirectHistory = true
conf.StartURL = ""
// XXX: -R is supposedly better than -r, but -R resets ansi formats on
// newlines :/
conf.LessOpts = "-FSXr~ -P pager (q to quit)"
conf.SearchURL = "gemini://kennedy.gemi.dev/search"
conf.MaxWidth = 70
conf.ClipboardCopyCmd = ""
_, err = os.Stat(path)
if os.IsNotExist(err) {
return &conf, err
}
f, err := os.Open(path)
if err == nil {
defer f.Close()
contents, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
if _, err = toml.Decode(string(contents), &conf); err != nil {
return nil, err
}
}
return &conf, nil
}
func (c *Client) parsePrompt() string {
var u *url.URL
if len(c.history) != 0 {
u = c.history[len(c.history)-1]
}
return BuildPrompt(u, c.conf.Prompt)
}
func BuildPrompt(u *url.URL, promptConf string) (prompt string) {
fullURL := u.String()
if u.Scheme == "gopher" && strings.Contains(fullURL, "%09") {
parts := strings.Split(fullURL, "%09")
fullURL = strings.Join(parts[:len(parts)-1], "%09")
}
path := u.Path
if u.Scheme == "gopher" {
if strings.Contains(path, "\t") {
parts := strings.Split(path, "\t")
path = strings.Join(parts[:len(parts)-1], "%09")
}
parts := strings.SplitN(strings.TrimPrefix(path, "/"), "/", 2)
if len(parts) == 2 {
path = "/" + parts[1]
} else {
path = "/" + parts[0]
}
}
percent := false
for _, char := range promptConf {
if char == '%' {
if percent {
prompt += "%"
percent = false
continue
}
percent = true
continue
}
if percent {
if u == nil {
percent = false
continue
}
switch char {
case 'U':
prompt += strings.TrimSuffix(u.String(), "?"+u.RawQuery)
case 'u':
prompt += strings.TrimSuffix(strings.TrimPrefix(fullURL, u.Scheme+"://"), "?"+u.RawQuery)
case 'P':
if !strings.HasPrefix(path, "/") {
prompt += "/"
}
prompt += path
case 'p':
prompt += filepath.Base(path)
case 'H':
prompt += u.Host
case 'h':
prompt += u.Hostname()
default:
prompt += "%" + string(char)
}
percent = false
continue
}
prompt += string(char)
}
return
}