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
|
package bookmarks
import (
"encoding/json"
"os"
"path/filepath"
)
// dataFile resolves ${XDG_DATA_HOME:-~/.local/share}/lectio/<name> -- the same
// data-dir rule as the sigla store and the bookmarks file.
func dataFile(name string) string {
base := os.Getenv("XDG_DATA_HOME")
if base == "" {
home, err := os.UserHomeDir()
if err != nil {
home = "."
}
base = filepath.Join(home, ".local", "share")
}
return filepath.Join(base, "lectio", name)
}
// Place is the reader's last position, so the TUI reader can reopen where it
// left off (book + chapter + the top-visible verse).
type Place struct {
Book string `json:"book"` // canonical English name (Bible-corpus key)
Chapter int `json:"chapter"`
Verse int `json:"verse"`
}
// SavePlace persists the reader's last position (atomic temp+rename).
func SavePlace(p Place) error {
path := dataFile("reader-place.json")
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return err
}
data, err := json.MarshalIndent(p, "", " ")
if err != nil {
return err
}
tmp := path + ".tmp"
if err := os.WriteFile(tmp, data, 0o644); err != nil {
return err
}
return os.Rename(tmp, path)
}
// LoadPlace reads the reader's last position; ok is false when none is saved
// (or the file names no book).
func LoadPlace() (Place, bool, error) {
data, err := os.ReadFile(dataFile("reader-place.json"))
if err != nil {
if os.IsNotExist(err) {
return Place{}, false, nil
}
return Place{}, false, err
}
var p Place
if err := json.Unmarshal(data, &p); err != nil {
return Place{}, false, err
}
if p.Book == "" {
return Place{}, false, nil
}
return p, true, nil
}
|