-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmee.go
More file actions
234 lines (205 loc) · 5.41 KB
/
Copy pathmee.go
File metadata and controls
234 lines (205 loc) · 5.41 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package main
import (
"bufio"
"flag"
"fmt"
"os"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/halqme/mee/cmd/history"
"github.com/halqme/mee/cmd/plugin"
"github.com/halqme/mee/pkg/core"
"github.com/halqme/mee/pkg/database"
"github.com/halqme/mee/pkg/platform"
"github.com/halqme/mee/pkg/plugin"
"github.com/halqme/mee/pkg/setup"
"github.com/halqme/mee/pkg/tui"
)
func main() {
// Parse flags before checking subcommands
setupCmd := flag.NewFlagSet("setup", flag.ContinueOnError)
forceFlag := setupCmd.Bool("force", false, "overwrite existing config")
withPluginsFlag := setupCmd.Bool("with-plugins", false, "install default plugins")
pluginDirFlag := setupCmd.String("plugin-dir", "", "source plugin directory")
// Check for subcommands
if len(os.Args) > 1 {
switch os.Args[1] {
case "setup":
setupCmd.Parse(os.Args[2:])
opts := setup.Options{
Force: *forceFlag,
WithPlugins: *withPluginsFlag,
PluginDir: *pluginDirFlag,
}
if err := setup.Run(opts); err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1)
}
return
case "check":
if err := setup.Check(); err != nil {
os.Exit(1)
}
return
case "plugin", "plugins":
runPluginCommand()
return
case "history":
runHistoryCommand()
return
case "--help", "-h":
printHelp()
return
case "--version", "-v":
printVersion()
return
}
}
// Run main TUI or direct mode
runMain()
}
func runMain() {
config := core.Load()
ranker := core.NewRanker()
// Open database for history recording
db, err := database.Open(platform.DataDir() + "/mee.db")
if err == nil {
defer db.Close()
db.Migrate() // ignore error, DB might already exist
ranker = core.NewRankerWithHistory(db)
}
pm := plugin.NewManager()
for _, dir := range config.PluginDirs {
pm.LoadFromDirectory(dir)
}
for _, p := range pm.GetProviders() {
ranker.AddProvider(p)
}
for _, t := range pm.GetTriggers() {
ranker.AddTrigger(t)
}
stat, _ := os.Stdin.Stat()
piped := (stat.Mode() & os.ModeCharDevice) == 0
if piped || len(os.Args) > 1 {
var q string
if piped {
if s := bufio.NewScanner(os.Stdin); s.Scan() {
q = strings.TrimSpace(s.Text())
}
}
if len(os.Args) > 1 {
q = strings.TrimSpace(os.Args[1])
}
if q != "" {
if r := ranker.Search(q); len(r) > 0 {
fmt.Println(r[0].Payload)
execAction(r[0].Action, r[0].Payload)
// Record in history
if db != nil {
ranker.RecordSelection("", q, r[0].Payload)
}
}
}
return
}
m := tui.New(config, ranker)
p := tea.NewProgram(m, tea.WithAltScreen())
f, err := p.Run()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if m, ok := f.(tui.Model); ok {
if item := m.Selected(); item != nil {
fmt.Println(item.Payload)
execAction(item.Action, item.Payload)
}
}
}
func runPluginCommand() {
db, err := database.Open(platform.DataDir() + "/mee.db")
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
defer db.Close()
if err := db.Migrate(); err != nil {
fmt.Fprintf(os.Stderr, "Migration error: %v\n", err)
os.Exit(1)
}
pm := plugin.NewManager()
// Load existing plugins from config dirs
config := core.Load()
for _, dir := range config.PluginDirs {
pm.LoadFromDirectory(dir)
}
cmd := plugincmd.New(db, pm)
if err := cmd.Run(os.Args[2:]); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
func runHistoryCommand() {
db, err := database.Open(platform.DataDir() + "/mee.db")
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
defer db.Close()
if err := db.Migrate(); err != nil {
fmt.Fprintf(os.Stderr, "Migration error: %v\n", err)
os.Exit(1)
}
cmd := historycmd.New(db)
if err := cmd.Run(os.Args[2:]); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
func execAction(action, payload string) {
switch action {
case "open", "launch":
platform.Open(payload)
}
}
func printHelp() {
fmt.Println(`mee - Fast, extensible launcher
Usage:
mee [query] Launch TUI or search directly
mee setup [options] Initialize mee
mee check Check setup status
mee plugin <command> Manage plugins
mee history <command> Manage search history
Setup Options:
--force Overwrite existing config
--with-plugins Install default plugins
--plugin-dir <dir> Source plugin directory
Plugin Commands:
add <url|path> Install a plugin from URL or local path
list List installed plugins
remove <id|name> Remove a plugin
enable <id|name> Enable a plugin
disable <id|name> Disable a plugin
info <id|name> Show plugin details
update [id|name] Update plugin(s)
History Commands:
list Show recent history
search <query> Search history
top, popular Show most used queries
clear Clear all history
prune [days] Remove old entries
export [file] Export to JSON
import <file> Import from JSON
Examples:
mee setup --with-plugins # Initial setup with plugins
mee check # Check setup status
mee "calc" # Direct search
mee plugin add https://github.com/... # Install plugin
mee history top # Show popular queries
Options:
-h, --help Show this help
-v, --version Show version`)
}
func printVersion() {
fmt.Println("mee version 0.2.0")
}