~adnano/astronaut

ref: c2cbc6b7e5ba6ccdb1e473261a134a4e897e6d46 astronaut/command.go -rw-r--r-- 2.2 KiB
c2cbc6b7Adnan Maolood settings: Remove unused code 3 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
package main

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

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

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

var commands = map[string]Command{
	"open": func(b *Browser, args ...string) {
		tab := b.tabs[b.tab]

		var rawurl string
		if len(args) >= 1 {
			rawurl = args[0]
		} else {
			rawurl = "about://newtab"
		}

		u, err := url.Parse(rawurl)
		if err != nil {
			tab.Error(err)
			return
		}

		if len(u.Scheme) == 0 && len(u.Host) == 0 {
			u2, err := url.Parse("gemini://" + rawurl)
			if err == nil {
				u = u2
			}
		}
		b.tabs[b.tab].GetURL(u)
	},
	"close": func(b *Browser, args ...string) {
		b.CloseTab(b.tab)
	},
	"newtab": func(b *Browser, args ...string) {
		tab := b.NewTab()
		b.tab++
		b.InsertTab(b.tab, tab)
		tab.Do(&gemini.Request{
			URL: &url.URL{
				Scheme: "about",
				Host:   "newtab",
			},
		})
		b.makeTabVisible()
	},
	"clone": func(b *Browser, args ...string) {
		t := b.tabs[b.tab]
		clone := t.Clone()
		b.tab++
		b.InsertTab(b.tab, clone)
		clone.Reload()
		b.makeTabVisible()
	},
	"search": func(b *Browser, args ...string) {
		if len(args) >= 1 {
			b.tabs[b.tab].GetURL(&url.URL{
				Scheme:   "gemini",
				Host:     "geminispace.info",
				Path:     "/search",
				RawQuery: gemini.QueryEscape(strings.Join(args, " ")),
			})
		}
	},
	"back": func(b *Browser, args ...string) {
		b.tabs[b.tab].Back()
	},
	"forward": func(b *Browser, args ...string) {
		b.tabs[b.tab].Forward()
	},
	"reload": func(b *Browser, args ...string) {
		b.tabs[b.tab].Reload()
	},
	"cancel": func(b *Browser, args ...string) {
		b.tabs[b.tab].Cancel()
	},
	"quit": func(b *Browser, args ...string) {
		b.view.Exit()
	},
	"prev": func(b *Browser, args ...string) {
		b.Previous()
	},
	"next": func(b *Browser, args ...string) {
		b.Next()
	},
	"save": func(b *Browser, args ...string) {
		if len(args) >= 1 {
			tab := b.tabs[b.tab]
			f, err := os.Create(args[0])
			if err != nil {
				tab.Error(err)
				return
			}
			defer f.Close()
			io.Copy(f, &tab.buf)
		}
	},
	"bookmark": func(b *Browser, args ...string) {
		t := b.tabs[b.tab]
		if b.bookmarks.Add(gemini.LineLink{
			URL:  t.URL(),
			Name: t.Title(),
		}) {
			b.status.Clear()
			b.status.Info("Bookmarked")
		} else {
			b.status.Info("Already bookmarked")
		}
	},
}