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