~adnano/astronaut

ref: 716e8b7c3fcd0c825d1e480eaaae92bc21f82ec7 astronaut/command.go -rw-r--r-- 3.7 KiB
716e8b7cAdnan Maolood tab: Implement certificate creation page 2 months ago
                                                                                
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main

import (
	"errors"
	"io"
	"net/url"
	"os"
	"strconv"
	"strings"

	"git.sr.ht/~adnano/go-gemini"
)

type Command func(b *Browser, args ...string) error

var commands = map[string]Command{
	"prompt": func(b *Browser, args ...string) error {
		b.CommandMode()
		prompt := strings.Join(args, " ")
		b.input.SetInput(prompt)
		return nil
	},
	"follow": func(b *Browser, args ...string) error {
		return b.FollowMode()
	},
	"jump": func(b *Browser, args ...string) error {
		b.Jump()
		return nil
	},
	"open": func(b *Browser, args ...string) error {
		if len(args) == 0 {
			return errors.New("usage: open <url>")
		}

		rawurl := args[0]
		u, err := url.Parse(rawurl)
		if err != nil {
			return err
		}

		if len(u.Scheme) == 0 && len(u.Host) == 0 {
			u2, err := url.Parse("gemini://" + rawurl)
			if err == nil {
				u = u2
			}
		}
		b.tabs[b.tab].DoBackground(&gemini.Request{URL: u})
		return nil
	},
	"close": func(b *Browser, args ...string) error {
		b.CloseTab(b.tab)
		return nil
	},
	"newtab": func(b *Browser, args ...string) error {
		tab := b.NewTab()
		b.tab++
		b.InsertTab(b.tab, tab)
		tab.Do(&gemini.Request{
			URL: &url.URL{
				Scheme: "about",
				Host:   "newtab",
			},
		})
		b.makeTabVisible()
		return nil
	},
	"clone": func(b *Browser, args ...string) error {
		t := b.tabs[b.tab]
		clone := t.Clone()
		b.tab++
		b.InsertTab(b.tab, clone)
		clone.Reload()
		b.makeTabVisible()
		return nil
	},
	"reload": func(b *Browser, args ...string) error {
		b.tabs[b.tab].Reload()
		return nil
	},
	"cancel": func(b *Browser, args ...string) error {
		b.tabs[b.tab].Cancel()
		return nil
	},
	"back": func(b *Browser, args ...string) error {
		tab := b.tabs[b.tab]
		if !tab.Back() {
			b.Message("Already at beginning of history")
		}
		return nil
	},
	"forward": func(b *Browser, args ...string) error {
		tab := b.tabs[b.tab]
		if !tab.Forward() {
			b.Message("Already at end of history")
		}
		return nil
	},
	"previous": func(b *Browser, args ...string) error {
		b.Previous()
		return nil
	},
	"next": func(b *Browser, args ...string) error {
		b.Next()
		return nil
	},
	"quit": func(b *Browser, args ...string) error {
		b.view.Exit()
		return nil
	},
	"scroll": func(b *Browser, args ...string) error {
		const usage = "usage: scroll <n>[%]"
		if len(args) == 0 {
			return errors.New(usage)
		}
		amount := args[0]
		switch amount {
		case "top":
			b.tabView.Top()
			b.tabView.Invalidate()
		case "bottom":
			b.tabView.Bottom()
			b.tabView.Invalidate()
		default:
			var scroll int
			if strings.HasSuffix(amount, "%") {
				percent, err := strconv.Atoi(amount[:len(amount)-1])
				if err != nil {
					return errors.New(usage)
				}
				_, h := b.tabView.Size()
				scroll = h * percent / 100
			} else {
				var err error
				scroll, err = strconv.Atoi(amount)
				if err != nil {
					return errors.New(usage)
				}
			}
			b.tabView.ScrollDown(scroll)
			b.tabView.Invalidate()
		}
		return nil
	},
	"save": func(b *Browser, args ...string) error {
		if len(args) == 0 {
			return errors.New("usage: save <path>")
		}
		tab := b.tabs[b.tab]
		f, err := os.Create(args[0])
		if err != nil {
			return err
		}
		defer f.Close()
		io.Copy(f, &tab.buf)
		return nil
	},
	"bookmark": func(b *Browser, args ...string) error {
		tab := b.tabs[b.tab]
		if b.bookmarks.Add(gemini.LineLink{
			URL:  tab.URL(),
			Name: tab.Title(),
		}) {
			b.Message("Bookmarked")
		} else {
			b.Message("Already bookmarked")
		}
		return nil
	},
	"search": func(b *Browser, args ...string) error {
		if len(args) == 0 {
			return errors.New("usage: search <query...>")
		}
		u := b.config.Search.URL.ResolveReference(&url.URL{
			RawQuery: gemini.QueryEscape(strings.Join(args, " ")),
		})
		b.tabs[b.tab].DoBackground(&gemini.Request{URL: u})
		return nil
	},
}