diff options
| author | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-07-24 16:07:14 +0200 |
|---|---|---|
| committer | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-07-24 16:07:14 +0200 |
| commit | 0321ea57f5c561215a8dea111ed7f3ea7b05594e (patch) | |
| tree | 75ce332f8985907fcd0118aa2c3f4aafb8587ad7 /internal/bookmarks/place.go | |
| parent | f48b79d8860e4c4616821ceeeea091ab4b838197 (diff) | |
| download | lectio-0321ea57f5c561215a8dea111ed7f3ea7b05594e.tar.gz lectio-0321ea57f5c561215a8dea111ed7f3ea7b05594e.zip | |
tui reader: bookmarks (m=mark w/ note, b=list/jump/delete, per-verse via top-visible verse) + remember last place; v0.18.0
Diffstat (limited to 'internal/bookmarks/place.go')
| -rw-r--r-- | internal/bookmarks/place.go | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/internal/bookmarks/place.go b/internal/bookmarks/place.go new file mode 100644 index 0000000..2aae058 --- /dev/null +++ b/internal/bookmarks/place.go @@ -0,0 +1,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 +} |
