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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
// Command lectio-ui is lectio's interactive Bubble Tea reader.
package main
import (
"flag"
"fmt"
"io"
"os"
"regexp"
tea "github.com/charmbracelet/bubbletea"
"github.com/lukaszkasprzak/lectio/internal/bible"
"github.com/lukaszkasprzak/lectio/internal/bookmarks"
"github.com/lukaszkasprzak/lectio/internal/caldata"
"github.com/lukaszkasprzak/lectio/internal/config"
"github.com/lukaszkasprzak/lectio/internal/i18n"
"github.com/lukaszkasprzak/lectio/internal/naming"
"github.com/lukaszkasprzak/lectio/internal/tui"
)
const helpText = `lectio-ui — interactive offline daily-readings reader (OF + EF)
Usage:
lectio-ui [DATE] [flags] DATE = YYYY-MM-DD (default: today), any position
Flags:
-b, --bible VER start on version (vul built in; see Versions below)
-R, --reader open the Bible reader (book picker) instead of the daily view
-a, --all show all parts (override config)
-l, --lectionary WHICH new|trad (trad -> traditional)
-v, --version print the version and exit
-h, --help this help
Versions: vul (Latin Vulgate) is built in. wuj (Wujek), grb (Greek) and drb
(Douay-Rheims) are optional: build with 'make build-full', or drop <code>.tsv +
<code>.ini into ~/.config/lectio/corpora/. ui_language accepts any code (day and
saint names from ~/.config/lectio/names/<code>.ini, labels from ui/<code>.ini).
Flags override config. Exit codes: 0 ok, 1 runtime error, 2 usage error.
`
var dateRe = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`)
func main() {
os.Exit(run(os.Args[1:], os.Stdout, os.Stderr))
}
// run is lectio-ui's testable entry point: parse args, validate, load
// config, apply overrides, and launch the TUI. Returns a process exit code.
func run(args []string, stdout, stderr io.Writer) int {
if wantsHelp(args) {
fmt.Fprint(stdout, helpText)
return 0
}
if wantsVersion(args) {
fmt.Fprintln(stdout, "lectio-ui "+config.Version)
return 0
}
date, rest, err := extractDate(args)
if err != nil {
fmt.Fprintln(stderr, "lectio-ui:", err)
return 2
}
cfg, err := config.Load()
if err != nil {
fmt.Fprintln(stderr, "lectio-ui:", err)
return 1
}
if dir, err := config.CorporaDir(); err == nil {
bible.SetUserCorporaDir(dir)
}
if dir, err := config.NamesDir(); err == nil {
naming.SetUserDir(dir)
}
if dir, err := config.UIDir(); err == nil {
i18n.SetUserDir(dir)
}
if dir, err := config.SanctoraleDir(); err == nil {
caldata.SetSanctoraleDir(dir)
}
var all, offline, reader bool
var bibleVer, lectionary string
fs := flag.NewFlagSet("lectio-ui", flag.ContinueOnError)
fs.SetOutput(stderr)
fs.Usage = func() { fmt.Fprint(stderr, helpText) }
fs.StringVar(&bibleVer, "b", "", "start on version: wuj,vul,grb,drb")
fs.StringVar(&bibleVer, "bible", "", "start on version: wuj,vul,grb,drb")
fs.BoolVar(&reader, "R", false, "open the Bible reader (fuzzy book picker) instead of the daily view")
fs.BoolVar(&reader, "reader", false, "open the Bible reader (fuzzy book picker) instead of the daily view")
fs.BoolVar(&all, "a", cfg.All, "show all parts (override config)")
fs.BoolVar(&all, "all", cfg.All, "show all parts (override config)")
fs.BoolVar(&offline, "o", cfg.Offline, "cache/sigla only, no network")
fs.BoolVar(&offline, "offline", cfg.Offline, "cache/sigla only, no network")
fs.StringVar(&lectionary, "l", "", "new|trad")
fs.StringVar(&lectionary, "lectionary", "", "new|trad")
if err := fs.Parse(rest); err != nil {
return 2
}
if fs.NArg() > 0 {
fmt.Fprintf(stderr, "lectio-ui: unexpected argument %q; see 'lectio-ui -h'\n", fs.Arg(0))
return 2
}
lectionary, err = normalizeLectionary(lectionary)
if err != nil {
fmt.Fprintln(stderr, "lectio-ui:", err)
return 2
}
if bibleVer != "" && !config.ValidVersion(bibleVer) {
fmt.Fprintf(stderr, "lectio-ui: unknown version %q (want one of wuj, vul, grb, drb)\n", bibleVer)
return 2
}
cfg.All = all
cfg.Offline = offline
if lectionary != "" {
cfg.Lectionary = lectionary
}
var mdl tea.Model = tui.New(cfg, date, bibleVer)
if reader {
tbl, _ := bible.LoadBookTable(config.UserBooksINI())
mdl = tui.NewReader(cfg, tbl, bookmarks.Open())
}
if _, err := tea.NewProgram(mdl, tea.WithAltScreen()).Run(); err != nil {
fmt.Fprintln(stderr, err)
return 1
}
return 0
}
// wantsHelp reports whether -h/--help appears anywhere in args.
func wantsHelp(args []string) bool {
for _, a := range args {
if a == "-h" || a == "--help" {
return true
}
}
return false
}
// wantsVersion reports whether -v/--version appears anywhere in args.
func wantsVersion(args []string) bool {
for _, a := range args {
if a == "-v" || a == "--version" {
return true
}
}
return false
}
// extractDate pulls the single positional DATE token (YYYY-MM-DD, matching
// dateRe) out of args, wherever it appears, and returns it along with the
// remaining tokens for flag.FlagSet to parse. Defaults to "" (New's
// today-fallback) when no date token is present; errors if more than one is
// found.
func extractDate(args []string) (date string, rest []string, err error) {
found := false
for _, a := range args {
if dateRe.MatchString(a) {
if found {
return "", nil, fmt.Errorf("multiple dates given (%q and %q)", date, a)
}
date = a
found = true
continue
}
rest = append(rest, a)
}
return date, rest, nil
}
// normalizeLectionary maps -l/--lectionary's accepted spellings via
// config.NormalizeLectionary; "" (flag not given) passes through unchanged.
func normalizeLectionary(lectionary string) (string, error) {
if lectionary == "" {
return "", nil
}
v, ok := config.NormalizeLectionary(lectionary)
if !ok {
return "", fmt.Errorf("invalid --lectionary %q (want new|trad)", lectionary)
}
return v, nil
}
|