-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
162 lines (132 loc) · 4.38 KB
/
main.go
File metadata and controls
162 lines (132 loc) · 4.38 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"errors"
"fmt"
"log"
"os"
"github.com/slzatz/vimango/auth"
"github.com/slzatz/vimango/rawmode"
"github.com/slzatz/vimango/vim"
)
// Global app struct
var app *App
func main() {
// Check for help flag first, before any initialization
if CheckForHelp(os.Args) {
os.Exit(0)
}
// Check for --init flag for first-time setup
if CheckForInit(os.Args) {
os.Exit(0)
}
app = CreateApp()
// Load user preferences (before DetectKittyCapabilities so we can override imageScale)
prefs := app.LoadPreferences("preferences.json")
// Detect kitty terminal capabilities (sets default imageScale=45)
app.DetectKittyCapabilities()
// Override defaults with user preferences
app.imageScale = prefs.ImageScale
app.imageCacheMaxWidth = prefs.ImageCacheMaxWidth
// Google Drive is optional - initialize if credentials are available
srv, err := auth.GetDriveService()
if err != nil {
// Google Drive not configured - this is OK, the app will work without it
// Users with gdrive: images will see a message explaining how to set it up
app.Session.googleDrive = nil
} else {
app.Session.googleDrive = srv
}
// Initialize image cache
initImageCache()
// Configure Vim implementation selection
vimConfig := DetermineVimDriver(os.Args)
//LogVimDriverChoice(vimConfig) //debugging
useGoVim := vimConfig.ShouldUseGoVim()
// Set up logging if Go implementation is used
if useGoVim {
// Set up logging to file instead of console to avoid flashing messages
logFile, err := os.OpenFile("govim_debug.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err == nil {
log.SetOutput(logFile)
log.Println("Go Vim implementation initialized")
}
}
// Initialize Vim with the appropriate implementation
vim.InitializeVim(useGoVim, 0)
// Configure SQLite driver selection
// currently defaulting to modernc sqlite driver unless --cgo-sqlite3 is specified
sqliteConfig := DetermineSQLiteDriver(os.Args)
//LogSQLiteDriverChoice(sqliteConfig) //debugging
// Initialize database connections
err = app.InitDatabases("config.json", sqliteConfig)
if err != nil {
// Check if this is a missing config.json error
if os.IsNotExist(err) {
fmt.Println("Error: config.json not found.")
fmt.Println()
fmt.Println("If this is a new installation, run:")
fmt.Println(" ./vimango --init")
fmt.Println()
fmt.Println("This will create config.json and initialize the SQLite databases.")
os.Exit(1)
}
// Check if database files are missing
if errors.Is(err, ErrDatabaseNotFound) {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
log.Fatal(err)
}
// Validate database schema exists
if err := app.ValidateDatabaseSchema(); err != nil {
fmt.Printf("Error: Database schema is missing or incomplete.\n")
fmt.Printf("Details: %v\n", err)
fmt.Println()
fmt.Println("Run './vimango --init' to initialize the databases.")
fmt.Println("(You may need to remove existing .db files first if they are corrupted)")
os.Exit(1)
}
// Migrate existing databases to UUID-based containers if needed
if err := app.MigrateToUUID(); err != nil {
fmt.Printf("Error: Database migration failed.\n")
fmt.Printf("Details: %v\n", err)
os.Exit(1)
}
// Validate glamour style file exists
if err := validateGlamourStyle(); err != nil {
log.Fatalf("Error: %v", err)
}
// Initialize research manager if Claude API key is configured
if app.Config != nil && app.Config.Claude.ApiKey != "" && app.Config.Claude.ApiKey != "CLAUDE_API_KEY_HERE" {
app.InitResearchManager(app.Config.Claude.ApiKey)
}
// Initialize async render manager for non-blocking image loading
app.InitRenderManager()
// Set up platform-specific signal handling
setupSignalHandling(app)
// Configure Vim settings
vim.ExecuteCommand("set iskeyword+=*")
vim.ExecuteCommand("set iskeyword+=`")
// Enable raw mode
origCfg, err := rawmode.Enable()
if err != nil {
fmt.Fprintf(os.Stderr, "Error enabling raw mode: %v", err)
os.Exit(1)
}
app.origTermCfg = origCfg
app.Session.editorMode = false
// Get window size
err = app.Screen.GetWindowSize()
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting window size: %v", err)
os.Exit(1)
}
//os.Exit(0) //debugging
app.InitApp()
// Set edPct BEFORE LoadInitialData() so it uses the correct value
// LoadInitialData() will calculate divider and totaleditorcols based on this
app.Screen.edPct = prefs.EdPct
app.LoadInitialData()
app.Run = true
app.MainLoop()
}