all repos — bibel @ 24fb7ab0d94eef6e9a9dab5d2553b861096d5889

Unnamed repository; edit this file 'description' to name the repository.

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