summaryrefslogtreecommitdiff
path: root/main.go
blob: 17fc65507918491b0b91160ac8f1297d60573ecf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main

// The entry point of the qwe editor. It handles command-line flags, initializes
// configuration, file types, terminal interface (termbox), and starts the main
// editor loop.

import (
	"flag"
	"fmt"
	"os"

	"github.com/nsf/termbox-go"
)

// Version of the editor, injected at build time.
var Version = "dev"

func main() {
	// Initialize configuration from flags and environment.
	InitConfig()

	// If -version flag is provided, print version and exit.
	if Config.ShowVersion {
		fmt.Println(Version)
		return
	}

	// Load supported file types for syntax highlighting.
	InitFileTypes()

	// Print available colors if -colors flag is provided.
	if Config.ShowColors {
		PrintColors()
		return
	}

	// Print system information if -info flag is provided.
	if Config.ShowInfo {
		PrintInfo()
		return
	}

	// Initialize termbox for TUI handling.
	err := termbox.Init()
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to init termbox: %v\n", err)
		os.Exit(1)
	}
	defer termbox.Close()

	// Enable mouse support and escape key handling.
	termbox.SetInputMode(termbox.InputEsc | termbox.InputMouse)
	// Use 256 color mode for better aesthetics.
	termbox.SetOutputMode(termbox.Output256)

	// Create a new editor instance.
	editor := NewEditor(Config.DevMode)
	// Start background checks for Ollama AI and file changes on disk.
	editor.ollamaClient.PeriodicStatusCheck()
	editor.PeriodicFileChangesCheck()

	// Check if filenames were provided as arguments and load them into buffers.
	if flag.NArg() > 0 {
		for _, filename := range flag.Args() {
			if err := editor.LoadFile(filename); err != nil {
				termbox.Close()
				fmt.Fprintf(os.Stderr, "failed to open file %s: %v\n", filename, err)
				os.Exit(1)
			}
		}
		// Start with the first file active
		editor.activeBufferIndex = 0
	}

	// Enter the main event loop (keyboard and mouse input).
	editor.HandleEvents()
}