-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.go
115 lines (98 loc) · 2.12 KB
/
settings.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
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
type Settings struct {
APIKey string
Temperature float32
Model string
Context int
UserColor string
BotColor string
BoldColor string
CodeBlock string
TextBlock string
Comments string
References string
ClearCmd string
SubmitCmd string
ResetCmd string
PermCmd string
ExitCmd string
}
func readSettings(filePath string) (Settings, error) {
file, err := os.Open(filePath)
if err != nil {
return Settings{}, err
}
defer file.Close()
settings := Settings{}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "#") || strings.TrimSpace(line) == "" {
continue
}
parts := strings.SplitN(line, "=", 2)
if len(parts) != 2 {
continue
}
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
value = strings.Trim(value, "\"")
switch key {
case "API_KEY":
settings.APIKey = value
case "Temperature":
settings.Temperature = parseFloat32(value)
case "Model":
settings.Model = value
case "Context":
settings.Context = parseInt(value)
case "UserColor":
settings.UserColor = value
case "BotColor":
settings.BotColor = value
case "BoldColor":
settings.BoldColor = value
case "CodeBlock":
settings.CodeBlock = value
case "TextBlock":
settings.TextBlock = value
case "Comments":
settings.Comments = value
case "References":
settings.References = value
case "Clear":
settings.ClearCmd = value
case "Submit":
settings.SubmitCmd = value
case "Reset":
settings.ResetCmd = value
case "Permanent":
settings.PermCmd = value
case "Exit":
settings.ExitCmd = value
}
}
if err := scanner.Err(); err != nil {
return Settings{}, err
}
return settings, nil
}
func parseFloat32(str string) float32 {
var value float32
fmt.Sscanf(str, "%f", &value)
return value
}
func parseBool(str string) bool {
return strings.ToLower(str) == "true"
}
func parseInt(str string) int {
var value int
fmt.Sscanf(str, "%d", &value)
return value
}