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()
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)
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 // Handle plain output mode
86 if args.Plain {
87 formatter := bible.NewFormatter(bibleData)
88 header := formatter.FormatHeader(bookmark)
89 content := formatter.ExtractAndFormat(bibleData, bookmark)
90 fmt.Println(header)
91 fmt.Println(content)
92 return
93 }
94
95 // Handle formatted output mode
96 if args.Formatted {
97 formatter := bible.NewFormatterWithConfig(bibleData, config.Formatter.UseColours, config.Formatter.HeaderFormat)
98 header := formatter.FormatHeader(bookmark)
99 content := formatter.ExtractAndFormat(bibleData, bookmark)
100 fmt.Println(header)
101 fmt.Println(content)
102 return
103 }
104
105 // Handle numbered/paragraph options
106 if args.Numbered || args.Paragraphs {
107 formatter := bible.NewFormatterWithFullConfig(bibleData, config.Formatter.UseColours, config.Formatter.HeaderFormat, args.Numbered, args.Paragraphs)
108 header := formatter.FormatHeader(bookmark)
109 content := formatter.ExtractAndFormat(bibleData, bookmark)
110 fmt.Println(header)
111 fmt.Println(content)
112 return
113 }
114
115 // Default: TUI mode
116 // Check if we're in a terminal
117 if !term.IsTerminal(int(os.Stdin.Fd())) {
118 fmt.Fprintln(os.Stderr, "Error: Standard input is not a terminal. Use --plain or --formatted for non-interactive output.")
119 os.Exit(1)
120 }
121
122 // Create and run TUI program
123 program := tui.CreateTUIProgram(bibleData, bookmark, config)
124 if _, err := program.Run(); err != nil {
125 fmt.Fprintf(os.Stderr, "Error running TUI: %v\n", err)
126 os.Exit(1)
127 }
128}