aboutsummaryrefslogtreecommitdiff
path: root/gui/internal/model/prefs.go
blob: 52ed012e68a5d8497d50135a3de5236e7be365c6 (plain) (blame)
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
// 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"`
	// PreviewHeight is how tall the preview under the explanation is, in
	// pixels; the divider between them sets it, and so does Settings.
	PreviewHeight int `json:"preview_height"`
}

// DefaultPreviewHeight is the preview's height until one is chosen, and the
// floor a smaller one is raised to.
const DefaultPreviewHeight = 280

// DefaultPrefs is what a window does before anything is chosen.
func DefaultPrefs() Prefs {
	return Prefs{FontSize: 0, Colours: true, Preview: true, SelectAll: true,
		PreviewHeight: DefaultPreviewHeight}
}

// 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()
	}
	if p.PreviewHeight < 80 {
		p.PreviewHeight = DefaultPreviewHeight
	}
	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)
}