~adnano/astronaut

ref: 72ba913688a9d2dafb0bfdf11a5cee16685ce98a astronaut/settings.go -rw-r--r-- 1.3 KiB
72ba9136Adnan Maolood Use scfg to configure settings 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
package main

import (
	"net/url"
	"strconv"
	"sync"

	"git.sr.ht/~emersion/go-scfg"
)

type Settings struct {
	sync.RWMutex
	Display struct {
		Graphics bool `json:"graphics"`
		AltText  bool `json:"alt-text"`
	} `json:"display"`
	SearchURL *url.URL
}

// Default applies the default settings.
func (s *Settings) Default() {
	s.Display.Graphics = true
	s.Display.AltText = false
	s.SearchURL = &url.URL{
		Scheme: "gemini",
		Host:   "geminispace.info",
		Path:   "/search",
	}
}

// Load loads the settings from the provided path.
func (s *Settings) Parse(block scfg.Block) {
	s.Default()
	for _, d := range block {
		switch d.Name {
		case "display":
			s.parseDisplay(d.Children)
		case "search":
			s.parseSearch(d)
		}
	}
}

func (s *Settings) parseDisplay(block scfg.Block) {
	for _, d := range block {
		switch d.Name {
		case "graphics":
			var enabled string
			d.ParseParams(&enabled)
			b, err := strconv.ParseBool(enabled)
			if err == nil {
				s.Display.Graphics = b
			}
		case "alt-text":
			var enabled string
			d.ParseParams(&enabled)
			b, err := strconv.ParseBool(enabled)
			if err == nil {
				s.Display.AltText = b
			}
		}
	}
}

func (s *Settings) parseSearch(d *scfg.Directive) {
	var searchURL string
	d.ParseParams(&searchURL)
	u, err := url.Parse(searchURL)
	if err == nil {
		s.SearchURL = u
	}
}