aboutsummaryrefslogtreecommitdiff
path: root/gui/internal/model/prefs.go
diff options
context:
space:
mode:
authorLukasz Kasprzak <lukas@labunix.xyz>2026-09-17 00:23:51 +0200
committerLukasz Kasprzak <lukas@labunix.xyz>2026-09-17 00:23:51 +0200
commit04b4243ad31d144c4caf9be4c5096a0f27a2648e (patch)
tree056515d145ba8e4fba29a2150b2c58199133b0c1 /gui/internal/model/prefs.go
parent9db67b201b80e9b7f824989df8517cefc587036d (diff)
downloadkrino-04b4243ad31d144c4caf9be4c5096a0f27a2648e.tar.gz
krino-04b4243ad31d144c4caf9be4c5096a0f27a2648e.zip
gui: syntax colours, a file preview, and a settings window
Diffstat (limited to 'gui/internal/model/prefs.go')
-rw-r--r--gui/internal/model/prefs.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/gui/internal/model/prefs.go b/gui/internal/model/prefs.go
new file mode 100644
index 0000000..ce9ee42
--- /dev/null
+++ b/gui/internal/model/prefs.go
@@ -0,0 +1,62 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+package model
+
+import (
+ "encoding/json"
+ "os"
+ "path/filepath"
+
+ "krino/internal/xdg"
+)
+
+// Prefs is how the window behaves - nothing about what krino does to
+// files, which belongs in the configuration. It lives beside krino.conf as
+// gui.json, a file krino itself never reads (his request, 2026-09-16).
+type Prefs struct {
+ // FontSize is the editor's font in points; 0 keeps the theme's.
+ FontSize int `json:"font_size"`
+ // Colours paints the configuration in the Text tab.
+ Colours bool `json:"colours"`
+ // Preview shows the file behind the selected row in the Plan tab.
+ Preview bool `json:"preview"`
+ // SelectAll starts a plan's rows checked, as the terminal review does.
+ SelectAll bool `json:"select_all"`
+}
+
+// DefaultPrefs is what a window does before anything is chosen.
+func DefaultPrefs() Prefs {
+ return Prefs{FontSize: 0, Colours: true, Preview: true, SelectAll: true}
+}
+
+// PrefsFile is where they are kept.
+func PrefsFile() string {
+ return filepath.Join(xdg.ConfigHome(), "krino", "gui.json")
+}
+
+// LoadPrefs reads them. A missing or unreadable file is not an error: the
+// window opens with the defaults, as it does the first time.
+func LoadPrefs() Prefs {
+ p := DefaultPrefs()
+ data, err := os.ReadFile(PrefsFile())
+ if err != nil {
+ return p
+ }
+ if err := json.Unmarshal(data, &p); err != nil {
+ return DefaultPrefs()
+ }
+ return p
+}
+
+// Save writes them, creating the directory if it is not there yet.
+func (p Prefs) Save() error {
+ data, err := json.MarshalIndent(p, "", " ")
+ if err != nil {
+ return err
+ }
+ file := PrefsFile()
+ if err := os.MkdirAll(filepath.Dir(file), 0o755); err != nil {
+ return err
+ }
+ return os.WriteFile(file, append(data, '\n'), 0o644)
+}