1package main
 2
 3// The entry point of the qwe editor. It handles command-line flags, initializes
 4// configuration, file types, terminal interface (termbox), and starts the main
 5// editor loop.
 6
 7import (
 8	"flag"
 9	"fmt"
10	"os"
11
12	"github.com/nsf/termbox-go"
13)
14
15// Version of the editor, injected at build time.
16var Version = "dev"
17
18func main() {
19	// Initialize configuration from flags and environment.
20	InitConfig()
21
22	// If -version flag is provided, print version and exit.
23	if Config.ShowVersion {
24		fmt.Println(Version)
25		return
26	}
27
28	// Load supported file types for syntax highlighting.
29	InitFileTypes()
30
31	// Print available colors if -colors flag is provided.
32	if Config.ShowColors {
33		PrintColors()
34		return
35	}
36
37	// Print system information if -info flag is provided.
38	if Config.ShowInfo {
39		PrintInfo()
40		return
41	}
42
43	// Initialize termbox for TUI handling.
44	err := termbox.Init()
45	if err != nil {
46		fmt.Fprintf(os.Stderr, "failed to init termbox: %v\n", err)
47		os.Exit(1)
48	}
49	defer termbox.Close()
50
51	// Enable mouse support and escape key handling.
52	termbox.SetInputMode(termbox.InputEsc | termbox.InputMouse)
53	// Use 256 color mode for better aesthetics.
54	termbox.SetOutputMode(termbox.Output256)
55
56	// Create a new editor instance.
57	editor := NewEditor(Config.DevMode)
58	// Start background checks for Ollama AI and file changes on disk.
59	editor.ollamaClient.PeriodicStatusCheck()
60	editor.PeriodicFileChangesCheck()
61
62	// Check if filenames were provided as arguments and load them into buffers.
63	if flag.NArg() > 0 {
64		for _, filename := range flag.Args() {
65			if err := editor.LoadFile(filename); err != nil {
66				termbox.Close()
67				fmt.Fprintf(os.Stderr, "failed to open file %s: %v\n", filename, err)
68				os.Exit(1)
69			}
70		}
71		// Start with the first file active
72		editor.activeBufferIndex = 0
73	}
74
75	// Enter the main event loop (keyboard and mouse input).
76	editor.HandleEvents()
77}