cmd/bibel.go (view raw)
1package main
2
3import (
4 "fmt"
5 "os"
6 "time"
7
8 "github.com/alexflint/go-arg"
9 "golang.org/x/term"
10 "maxwelljensen/bibel/internal"
11 "maxwelljensen/bibel/internal/tui"
12)
13
14// Args defines the command line arguments
15type Args struct {
16 Reading string `arg:"-r,--reading" help:"reading mode: evangelion (Gospels), new_testament, old_testament, bible (default: evangelion)"`
17 Plain bool `arg:"-p,--plain" help:"output plain text without formatting or TUI"`
18 Formatted bool `arg:"-f,--formatted" help:"output formatted text with ANSI colours (no TUI)"`
19 ConfigPath string `arg:"-c,--config" help:"path to configuration file (default: $XDG_CONFIG_HOME/bibel/config.toml)"`
20 Numbered bool `arg:"-n,--numbered" help:"print each verse on a numbered line with verse number"`
21 Paragraphs bool `arg:"-g,--paragraphs" help:"render pilcrows (ΒΆ) as blank lines instead of ignoring them"`
22 Latin bool `arg:"-l,--latin" help:"show time until Roman Catholic Easter"`
23 GenerateConfig bool `arg:"--generate-config" help:"generate a default configuration file and exit"`
24 Verbose bool `arg:"-v,--verbose" help:"print additional runtime information to STDOUT"`
25}
26
27// Description returns a description of the program
28func (Args) Description() string {
29 return "Display today's Bible reading based on selected reading mode.\n" +
30 "Reads 12 verses per day based on the current date.\n" +
31 "Reading modes: evangelion (four Gospels), new_testament, old_testament, bible\n" +
32 "Configuration file: $XDG_CONFIG_HOME/bibel/config.toml"
33}
34
35func main() {
36 var args Args
37 arg.MustParse(&args)
38
39 // Handle config generation
40 if args.GenerateConfig {
41 if err := bible.GenerateDefaultConfig(); err != nil {
42 fmt.Fprintf(os.Stderr, "Error generating config: %v\n", err)
43 os.Exit(1)
44 }
45 fmt.Printf("Default configuration generated at: %s\n", bible.GetConfigPath())
46 return
47 }
48
49 // Load configuration
50 config, err := bible.LoadConfig(args.ConfigPath, args.Verbose)
51 if err != nil {
52 fmt.Fprintf(os.Stderr, "Error loading config: %v\n", err)
53 os.Exit(1)
54 }
55
56 // Override Easter type from command line flag if specified
57 if args.Latin {
58 config.EasterType = "latin"
59 }
60
61 // Override reading mode from command line if specified
62 if args.Reading != "" {
63 config.ReadingMode = args.Reading
64 }
65
66 // Load Bible data
67 bibleData, err := bible.LoadBible(config.BiblePath, args.Verbose)
68 if err != nil {
69 fmt.Fprintf(os.Stderr, "Error loading Bible data: %v\n", err)
70 os.Exit(1)
71 }
72
73 // Create date progression calculator
74 // We need to convert reading mode string to ReadingMode type
75 // Based on verse.go, ReadingMode is a string type with constants
76 dateProg := bible.NewDateProgression(bibleData)
77
78 // Get today's bookmark
79 bookmark, err := dateProg.GetPositionForDate(time.Now())
80 if err != nil {
81 fmt.Fprintf(os.Stderr, "Error calculating today's reading position: %v\n", err)
82 os.Exit(1)
83 }
84
85 // Determine output mode: plain, formatted, or TUI
86 // Plain mode forces no colours, formatted respects config
87 usePlain := args.Plain
88 useFormatted := args.Formatted
89
90 // If no explicit mode but numbered/paragraphs flags are set, use formatted mode
91 if !usePlain && !useFormatted && (args.Numbered || args.Paragraphs) {
92 useFormatted = true
93 }
94
95 // If we're outputting text (plain or formatted)
96 if usePlain || useFormatted {
97 // Determine colours: plain mode = false, formatted mode = from config
98 useColours := false
99 if useFormatted {
100 useColours = config.Formatter.UseColours
101 }
102
103 // Create formatter with appropriate settings
104 formatter := bible.NewFormatterWithFullConfig(bibleData, useColours, config.Formatter.HeaderFormat, args.Numbered, args.Paragraphs)
105 header := formatter.FormatHeader(bookmark)
106 content := formatter.ExtractAndFormat(bibleData, bookmark)
107 fmt.Println(header)
108 fmt.Println(content)
109 return
110 }
111
112 // Default: TUI mode (no plain or formatted flags)
113 // Check if we're in a terminal
114 if !term.IsTerminal(int(os.Stdin.Fd())) {
115 fmt.Fprintln(os.Stderr, "Error: Standard input is not a terminal. Use --plain or --formatted for non-interactive output.")
116 os.Exit(1)
117 }
118
119 // Create and run TUI program
120 program := tui.CreateTUIProgram(bibleData, bookmark, config)
121 if _, err := program.Run(); err != nil {
122 fmt.Fprintf(os.Stderr, "Error running TUI: %v\n", err)
123 os.Exit(1)
124 }
125}