From 7830ae321749e613e875931274130ed7051de117 Mon Sep 17 00:00:00 2001
From: Andrew-71
Date: Wed, 8 May 2024 15:56:11 +0300
Subject: [PATCH] Improve config and add a theme
---
CHANGELOG.md | 10 ++++++++
README.md | 4 +++-
config.go | 47 +++++++++++++++++++++++---------------
info.go | 6 ++---
pages/base.html | 6 ++---
pages/info.html | 2 +-
public/themes/lavender.css | 26 +++++++++++++++++++++
routes.go | 8 +++----
8 files changed, 79 insertions(+), 30 deletions(-)
create mode 100644 public/themes/lavender.css
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1302467..f04fa32 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,16 @@
# Changelog
This file keeps track of changes in more human-readable fashion
+## v0.4.0
+* Customisation changes
+ * Added `title` option to config
+ * Controls the text in the header, "🌺 Hibiscus.txt" by default
+ * Added a nice `lavender` theme :)
+ * No longer ensuring config.Theme ends up inside `/public`, unsure what to do in that regard
+* Technical changes to config
+ * Now only *some* default values are saved to file when creating initial config.txt
+ * Spaces in config options are now supported (basically just for `title`)
+
## v0.3.0
* Added themes
* Picked theme is set by `theme` key in config. Default is ...`default`
diff --git a/README.md b/README.md
index 8abbffb..d0d99e5 100644
--- a/README.md
+++ b/README.md
@@ -39,7 +39,9 @@ config
Deleting notes is done by clearing contents and clicking "Save" - the app deletes empty files when saving.
### Config options:
-Below are defaults of config.txt. The settings are defined in newline separated key=value pairs.
+Below are available configuration options and their defaults.
+The settings are defined as newline separated key=value pairs in config.txt.
+If you do not provide an option in your config, it will be using the default.
Please don't include the bash-style "comments" in your actual config,
they are provided purely for demonstration only and **will break the config if present**.
```
diff --git a/config.go b/config.go
index 983434f..0de2fc8 100644
--- a/config.go
+++ b/config.go
@@ -16,13 +16,14 @@ import (
var ConfigFile = "config/config.txt"
type Config struct {
- Username string `config:"username" type:"string"`
- Password string `config:"password" type:"string"`
- Port int `config:"port" type:"int"`
- Timezone *time.Location `config:"timezone" type:"location"`
+ Username string `config:"username" type:"string" mandatory:"true"`
+ Password string `config:"password" type:"string" mandatory:"true"`
+ Port int `config:"port" type:"int" mandatory:"true"`
+ Timezone *time.Location `config:"timezone" type:"location" mandatory:"true"`
GraceTime time.Duration `config:"grace_period" type:"duration"`
- Language string `config:"language" type:"string"`
+ Language string `config:"language" type:"string" mandatory:"true"`
Theme string `config:"theme" type:"string"`
+ Title string `config:"title" type:"string"`
LogToFile bool `config:"log_to_file" type:"bool"`
LogFile string `config:"log_file" type:"string"`
Scram bool `config:"enable_scram" type:"bool"`
@@ -31,15 +32,35 @@ type Config struct {
TelegramChat string `config:"tg_chat" type:"string"`
}
+var DefaultConfig = Config{
+ Username: "admin",
+ Password: "admin",
+ Port: 7101,
+ Timezone: time.Local,
+ GraceTime: 0,
+ Language: "en",
+ Theme: "default",
+ Title: "🌺 Hibiscus.txt",
+ LogToFile: false,
+ LogFile: "config/log.txt",
+ Scram: false,
+
+ TelegramToken: "",
+ TelegramChat: "",
+}
+
+// Save puts modified and mandatory config options into the config.txt file
func (c *Config) Save() error {
output := ""
v := reflect.ValueOf(*c)
+ vDefault := reflect.ValueOf(DefaultConfig)
typeOfS := v.Type()
for i := 0; i < v.NumField(); i++ {
key := typeOfS.Field(i).Tag.Get("config")
value := v.Field(i).Interface()
- if value != "" {
+ mandatory := typeOfS.Field(i).Tag.Get("mandatory")
+ if (mandatory == "true") || (value != vDefault.Field(i).Interface()) { // Only save non-default values
output += fmt.Sprintf("%s=%v\n", key, value)
}
}
@@ -70,7 +91,7 @@ func (c *Config) Reload() error {
options := map[string]string{}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
- entry := strings.Split(strings.Replace(scanner.Text(), " ", "", -1), "=")
+ entry := strings.Split(strings.Trim(scanner.Text(), " \t"), "=")
if len(entry) != 2 {
continue
}
@@ -134,18 +155,8 @@ func (c *Config) Reload() error {
}
// ConfigInit loads config on startup
-// Some defaults are declared here
func ConfigInit() Config {
- cfg := Config{
- Port: 7101,
- Username: "admin",
- Password: "admin",
- Timezone: time.Local,
- Language: "en",
- Theme: "default",
- LogFile: "config/log.txt",
- GraceTime: 0,
- }
+ cfg := DefaultConfig
err := cfg.Reload()
if err != nil {
log.Fatal(err)
diff --git a/info.go b/info.go
index 976b521..0185d27 100644
--- a/info.go
+++ b/info.go
@@ -8,14 +8,14 @@ import (
var infoTemplate = template.Must(template.New("").Funcs(templateFuncs).ParseFiles("./pages/base.html", "./pages/info.html"))
-type HibiscusInfo struct {
+type AppInfo struct {
Version string
SourceLink string
}
// Info contains app information
-var Info = HibiscusInfo{
- Version: "0.3.0",
+var Info = AppInfo{
+ Version: "0.4.0",
SourceLink: "https://git.a71.su/Andrew71/hibiscus",
}
diff --git a/pages/base.html b/pages/base.html
index c6bfb1b..c53fcad 100644
--- a/pages/base.html
+++ b/pages/base.html
@@ -1,6 +1,6 @@
{{define "header"}}
{{end}}
@@ -14,7 +14,7 @@
-
+
Hibiscus.txt
@@ -35,6 +35,6 @@
{{define "footer"}}
{{end}}
\ No newline at end of file
diff --git a/pages/info.html b/pages/info.html
index 9e949f3..12feaa1 100644
--- a/pages/info.html
+++ b/pages/info.html
@@ -1,7 +1,7 @@
{{define "main"}}
{{ translatableText "title.info" }}
- {{ translatableText "info.version" }} - {{ .Version }} ({{ translatableText "info.version.link" }} )
+ {{ translatableText "info.version" }} - {{ info.Version }} ({{ translatableText "info.version.link" }} )
{{ translatableText "info.export" }}
{{ translatableText "info.readme" }}
{{ translatableText "info.reload" }}
diff --git a/public/themes/lavender.css b/public/themes/lavender.css
new file mode 100644
index 0000000..b773d3c
--- /dev/null
+++ b/public/themes/lavender.css
@@ -0,0 +1,26 @@
+/* Tell me a secret. */
+:root {
+ /* Light theme */
+ --text-light: #3c3836;
+ --bg-light: #e6dffa; /* d4c7fb*/
+
+ --clickable-light: #9975f5;
+ --clickable-hover-light: #765bef;
+ --clickable-label-light: #e2d8ff;
+ --text-hover-light: #665c54;
+
+ --textarea-bg-light: #f3ecff;
+ --textarea-border-light: #282828;
+
+ /* Dark theme */
+ --text-dark: #e6dffa;
+ --bg-dark: #25252a;
+
+ --clickable-dark: #9975f5;
+ --clickable-hover-dark: #765bef;
+ --clickable-label-dark: #e2d8ff;
+ --text-hover-dark: #a5a5b9;
+
+ --textarea-bg-dark: #27272f;
+ --textarea-border-dark: #3c3836;
+}
\ No newline at end of file
diff --git a/routes.go b/routes.go
index 85ffe0f..5d72ff8 100644
--- a/routes.go
+++ b/routes.go
@@ -7,7 +7,6 @@ import (
"log/slog"
"net/http"
"os"
- "path"
"strings"
"time"
)
@@ -28,8 +27,9 @@ type formatEntries func([]string) []Entry
var templateFuncs = map[string]interface{}{
"translatableText": TranslatableText,
- "hibiscusVersion": func() string { return "v" + Info.Version },
- "hibiscusTheme": func() string { return path.Clean(Cfg.Theme) }}
+ "info": func() AppInfo { return Info },
+ "config": func() Config { return Cfg },
+}
var editTemplate = template.Must(template.New("").Funcs(templateFuncs).ParseFiles("./pages/base.html", "./pages/edit.html"))
var viewTemplate = template.Must(template.New("").Funcs(templateFuncs).ParseFiles("./pages/base.html", "./pages/entry.html"))
var listTemplate = template.Must(template.New("").Funcs(templateFuncs).ParseFiles("./pages/base.html", "./pages/list.html"))
@@ -216,7 +216,7 @@ func PostNote(w http.ResponseWriter, r *http.Request) {
// GetReadme calls GetEntry for readme.txt
func GetReadme(w http.ResponseWriter, r *http.Request) {
- GetEntry(w, r, "readme", "readme", true)
+ GetEntry(w, r, "readme.txt", "readme", true)
}
// PostReadme saves contents of readme.txt file