~taiite/senpai

ref: a532aae30446d7d76c74780bebaf0a40c775694e senpai/config.go -rw-r--r-- 2.0 KiB
a532aae3Hubert Hirtz Fix history not being loaded on channel re-JOIN 4 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
package senpai

import (
	"errors"
	"fmt"
	"io/ioutil"
	"strconv"
	"strings"

	"github.com/gdamore/tcell/v2"

	"gopkg.in/yaml.v2"
)

type Color tcell.Color

func (c *Color) UnmarshalText(data []byte) error {
	s := string(data)

	if strings.HasPrefix(s, "#") {
		hex, err := strconv.ParseInt(s[1:], 16, 32)
		if err != nil {
			return err
		}

		*c = Color(tcell.NewHexColor(int32(hex)))
		return nil
	}

	code, err := strconv.Atoi(s)
	if err != nil {
		return err
	}

	if code == -1 {
		*c = Color(tcell.ColorDefault)
		return nil
	}

	if code < 0 || code > 255 {
		return fmt.Errorf("color code must be between 0-255. If you meant to use true colors, use #aabbcc notation")
	}

	*c = Color(tcell.PaletteColor(code))

	return nil
}

type Config struct {
	Addr     string
	Nick     string
	Real     string
	User     string
	Password *string
	NoTLS    bool `yaml:"no-tls"`
	Channels []string

	NoTypings bool `yaml:"no-typings"`
	Mouse     *bool

	Highlights     []string
	OnHighlight    string `yaml:"on-highlight"`
	NickColWidth   int    `yaml:"nick-column-width"`
	ChanColWidth   int    `yaml:"chan-column-width"`
	MemberColWidth int    `yaml:"member-column-width"`

	Colors struct {
		Prompt Color
	}

	Debug bool
}

func ParseConfig(buf []byte) (cfg Config, err error) {
	err = yaml.Unmarshal(buf, &cfg)
	if err != nil {
		return cfg, err
	}
	if cfg.Addr == "" {
		return cfg, errors.New("addr is required")
	}
	if cfg.Nick == "" {
		return cfg, errors.New("nick is required")
	}
	if cfg.User == "" {
		cfg.User = cfg.Nick
	}
	if cfg.Real == "" {
		cfg.Real = cfg.Nick
	}
	if cfg.NickColWidth <= 0 {
		cfg.NickColWidth = 16
	}
	if cfg.ChanColWidth < 0 {
		cfg.ChanColWidth = 0
	}
	if cfg.MemberColWidth < 0 {
		cfg.MemberColWidth = 0
	}
	return
}

func LoadConfigFile(filename string) (cfg Config, err error) {
	var buf []byte

	buf, err = ioutil.ReadFile(filename)
	if err != nil {
		return cfg, fmt.Errorf("failed to read the file: %s", err)
	}

	cfg, err = ParseConfig(buf)
	if err != nil {
		return cfg, fmt.Errorf("invalid content found in the file: %s", err)
	}
	return
}