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 GenerateConfig bool `arg:"--generate-config" help:"generate a default configuration file and exit"`
20 ConfigPath string `arg:"-c,--config" help:"path to configuration file (default: $XDG_CONFIG_HOME/bibel/config.toml)"`
21 Numbered bool `arg:"-n,--numbered" help:"print each verse on a numbered line with verse number"`
22 Paragraphs bool `arg:"-g,--paragraphs" help:"render pilcrows (ΒΆ) as blank lines instead of ignoring them"`
23}
24
25// Description returns a description of the program
26func (Args) Description() string {
27 return "Display today's Bible reading based on selected reading mode.\n" +
28 "Reads 12 verses per day based on the current date.\n" +
29 "Reading modes: evangelion (four Gospels), new_testament, old_testament, bible\n" +
30 "Configuration file: $XDG_CONFIG_HOME/bibel/config.toml"
31}
32
33func main() {
34 var args Args
35 arg.MustParse(&args)
36
37 // Handle config generation
38 if args.GenerateConfig {
39 if err := bible.GenerateDefaultConfig(); err != nil {
40 fmt.Fprintf(os.Stderr, "Error generating config: %v\n", err)
41 os.Exit(1)
42 }
43 fmt.Printf("Default configuration generated at: %s\n", bible.GetConfigPath())
44 return
45 }
46
47 // Load configuration
48 cfg, err := bible.LoadConfig()
49 if err != nil {
50 fmt.Fprintf(os.Stderr, "Error loading configuration: %v\n", err)
51 os.Exit(1)
52 }
53
54 // Override config with command line arguments
55 if args.Reading != "" {
56 cfg.ReadingMode = args.Reading
57 }
58 if args.Plain {
59 cfg.OutputMode = "plain"
60 } else if args.Formatted {
61 cfg.OutputMode = "formatted"
62 }
63 if args.Numbered {
64 cfg.Formatter.Numbered = args.Numbered
65 }
66 if args.Paragraphs {
67 cfg.Formatter.Paragraphs = args.Paragraphs
68 }
69 if args.ConfigPath != "" {
70 // Note: This would require modifying LoadConfig to accept a path
71 // For now, we'll just use the default XDG location
72 fmt.Fprintf(os.Stderr, "Note: Custom config path not yet implemented, using default location\n")
73 }
74
75 // Load Bible data
76 biblePath := cfg.BiblePath
77 bibleData, err := bible.LoadBible(biblePath)
78 if err != nil {
79 fmt.Fprintf(os.Stderr, "Error loading Bible data: %v\n", err)
80 os.Exit(1)
81 }
82
83 // Initialise date progression with configured verses per day
84 dateProg := bible.NewDateProgressionWithReadingMode(bibleData, cfg.DateProgression.VersesPerDay, bible.ReadingMode(cfg.ReadingMode))
85
86 // Get current date
87 currentDate := time.Now()
88
89 // Calculate position for today
90 todayBookmark, err := dateProg.GetPositionForDate(currentDate)
91 if err != nil {
92 fmt.Fprintf(os.Stderr, "Error calculating date position: %v\n", err)
93 os.Exit(1)
94 }
95
96 // Determine output mode based on configuration and terminal
97 outputMode := cfg.OutputMode
98
99 // If terminal detection suggests different mode, adjust
100 if !term.IsTerminal(int(os.Stdout.Fd())) && outputMode == "tui" {
101 // Not a terminal, fall back to formatted
102 outputMode = "formatted"
103 }
104
105 // Handle different output modes
106 switch outputMode {
107 case "plain":
108 formatter := bible.NewFormatterWithFullConfig(bibleData, false, cfg.Formatter.HeaderFormat, cfg.Formatter.Numbered, cfg.Formatter.Paragraphs)
109 // In plain mode, we don't print the header
110 fmt.Println(formatter.ExtractAndFormat(bibleData, todayBookmark))
111
112 case "formatted":
113 formatter := bible.NewFormatterWithFullConfig(bibleData, cfg.Formatter.UseColours, cfg.Formatter.HeaderFormat, cfg.Formatter.Numbered, cfg.Formatter.Paragraphs)
114 fmt.Println(formatter.FormatHeader(todayBookmark))
115 fmt.Println(formatter.ExtractAndFormat(bibleData, todayBookmark))
116
117 case "tui":
118 // Create and run TUI program with configuration
119 program := tui.CreateTUIProgram(bibleData, todayBookmark, cfg)
120 if err := program.Start(); err != nil {
121 fmt.Fprintf(os.Stderr, "Error running TUI: %v\n", err)
122 os.Exit(1)
123 }
124
125 default:
126 fmt.Fprintf(os.Stderr, "Unknown output mode: %s\n", outputMode)
127 fmt.Fprintf(os.Stderr, "Valid modes: tui, formatted, plain\n")
128 os.Exit(1)
129 }
130}