forked from y3owk1n/neru
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfo.go
More file actions
314 lines (264 loc) · 9.28 KB
/
Copy pathinfo.go
File metadata and controls
314 lines (264 loc) · 9.28 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package ipcctrl
import (
"context"
"os"
"path/filepath"
"sync"
"go.uber.org/zap"
"github.com/y3owk1n/neru/internal/adapter/ipc"
"github.com/y3owk1n/neru/internal/adapter/platform"
"github.com/y3owk1n/neru/internal/app/modes"
"github.com/y3owk1n/neru/internal/app/services"
"github.com/y3owk1n/neru/internal/config"
"github.com/y3owk1n/neru/internal/config/loader"
"github.com/y3owk1n/neru/internal/domain"
"github.com/y3owk1n/neru/internal/domain/state"
"github.com/y3owk1n/neru/internal/ports"
)
// healthNotInitialized is the status string for components that were not initialized.
const healthNotInitialized = "not initialized"
// detailSuffix marks informational sibling keys in capabilitiesMap (e.g.
// "dark_mode_detection_detail"). Any key ending in this suffix carries
// free-form prose for the `neru doctor` metadata header and is deliberately
// excluded from the component health-row loop, which only understands the
// supported/stub/headless/ok vocabulary.
const detailSuffix = "_detail"
// minConfigSetArgs is the minimum number of arguments required by the
// config-set IPC handler (key and value).
const minConfigSetArgs = 2
// InfoHandler handles info and config-related IPC commands.
type InfoHandler struct {
configService *loader.Service
appState *state.AppState
config *config.Config
modes *modes.Handler
hintService *services.HintService
gridService *services.GridService
actionService *services.ActionService
scrollService *services.ScrollService
systemPort ports.SystemPort
eventTap ports.EventTapPort
ipcServer ports.IPCPort
reloadConfig func(ctx context.Context, configPath string) error
cursorSlots *state.CursorSlots
logger *zap.Logger
// configMu protects config from concurrent read/write.
configMu sync.RWMutex
// setConfigField is a callback invoked by handleConfigSet to apply a
// config field change at the app level (reconfigure components, re-register
// hotkeys, etc.). If nil only the in-memory config is updated.
setConfigField func(ctx context.Context, key, value string) error
}
// InfoHandlerDeps collects everything NewInfoHandler needs.
//
// The reasoning matches Deps: the positional list had reached fourteen
// arguments, several nil at any given call site. Zero values are valid — a nil
// service means the commands that need it report it as unavailable, and
// EventTap and IPCServer are nil until initialization phase 8.
type InfoHandlerDeps struct {
ConfigService *loader.Service
AppState *state.AppState
Config *config.Config
Modes *modes.Handler
HintService *services.HintService
GridService *services.GridService
ActionService *services.ActionService
ScrollService *services.ScrollService
System ports.SystemPort
EventTap ports.EventTapPort
IPCServer ports.IPCPort
// ReloadConfig performs a full app-level config reload.
ReloadConfig func(ctx context.Context, configPath string) error
// SetConfigField applies a runtime config field change.
SetConfigField func(ctx context.Context, key, value string) error
// CursorSlots is the store save_cursor_pos writes to, reported by status.
CursorSlots *state.CursorSlots
Logger *zap.Logger
}
// NewInfoHandler creates the info/config command handler.
func NewInfoHandler(deps InfoHandlerDeps) *InfoHandler {
return &InfoHandler{
configService: deps.ConfigService,
appState: deps.AppState,
config: deps.Config,
modes: deps.Modes,
hintService: deps.HintService,
gridService: deps.GridService,
actionService: deps.ActionService,
scrollService: deps.ScrollService,
systemPort: deps.System,
eventTap: deps.EventTap,
ipcServer: deps.IPCServer,
reloadConfig: deps.ReloadConfig,
setConfigField: deps.SetConfigField,
cursorSlots: deps.CursorSlots,
logger: deps.Logger,
}
}
// RegisterHandlers registers info/config command handlers.
func (h *InfoHandler) RegisterHandlers(
handlers map[string]func(context.Context, ipc.Command) ipc.Response,
) {
handlers[domain.CommandStatus] = h.handleStatus
handlers[domain.CommandConfig] = h.handleConfig
handlers[domain.CommandReloadConfig] = h.handleReloadConfig
handlers[domain.CommandHealth] = h.handleHealth
handlers[domain.CommandConfigSet] = h.handleConfigSet
}
// ResolveConfigPath determines the configuration file path for status reporting.
func (h *InfoHandler) ResolveConfigPath() string {
configPath := h.configService.GetConfigPath()
if configPath == "" {
return "using default config"
}
// Check if the config file actually exists
_, err := os.Stat(configPath)
if os.IsNotExist(err) {
return "using default config"
}
// Convert to absolute path for display
absPath, err := filepath.Abs(configPath)
if err == nil {
return absPath
}
return configPath
}
// UpdateConfig updates the stored config.
func (h *InfoHandler) UpdateConfig(cfg *config.Config) {
h.configMu.Lock()
defer h.configMu.Unlock()
h.config = cfg
}
// configSnapshot returns the current config pointer under a read lock.
func (h *InfoHandler) configSnapshot() *config.Config {
h.configMu.RLock()
defer h.configMu.RUnlock()
return h.config
}
func (h *InfoHandler) handleStatus(_ context.Context, _ ipc.Command) ipc.Response {
configPath := h.ResolveConfigPath()
cfg := h.configSnapshot()
if cfg == nil {
h.logger.Error("Config is nil in handleStatus")
return h.configNotAvailableResponse()
}
status := map[string]any{
"enabled": h.appState.IsEnabled(),
"mode": domain.ModeString(h.appState.CurrentMode()),
"config": configPath,
"hints_enabled": cfg.Hints.Enabled,
"grid_enabled": cfg.Grid.Enabled,
"recursive_grid_enabled": cfg.RecursiveGrid.Enabled,
"capabilities": capabilitiesMap(h.systemCapabilities()),
"profile": profileMap(platform.CurrentProfile()),
// The runtime toggles. They are reported under the names the
// corresponding toggle-* command reports, so a caller can set one with
// --state and read back the field it just named.
"scroll_inverted": h.appState.IsScrollInverted(),
"hidden_for_screen_share": h.appState.IsHiddenForScreenShare(),
"cursor_follow_selection": h.cursorFollowSelection(),
"saved_cursor_slots": h.savedCursorSlots(),
}
return ipc.Response{
Success: true,
Message: "status retrieved successfully",
Data: status,
Code: ipc.CodeOK,
}
}
// cursorFollowSelection reports the active mode's cursor-follow-selection
// preference, or nil when no mode carries one.
//
// It is null rather than false in that case because the two are different
// answers: false means a running mode is not following the selection, and null
// means there is nothing to follow it with. A caller that treated the absence
// as false would think it had read a state it can in fact only set once a mode
// is running.
func (h *InfoHandler) cursorFollowSelection() *bool {
if h.modes == nil {
return nil
}
enabled, ok := h.modes.CursorFollowSelection()
if !ok {
return nil
}
return &enabled
}
// savedCursorSlots reports the occupied cursor slots and the position each
// holds, as an object keyed by slot name.
//
// The points are re-keyed to lowercase x and y rather than encoded straight
// from image.Point, whose fields marshal as X and Y — the rest of this payload
// is snake_case, and the shape a script reads should not depend on how the
// position happens to be stored.
func (h *InfoHandler) savedCursorSlots() map[string]map[string]int {
slots := map[string]map[string]int{}
if h.cursorSlots == nil {
return slots
}
for name, pos := range h.cursorSlots.Snapshot() {
slots[name] = map[string]int{"x": pos.X, "y": pos.Y}
}
return slots
}
func (h *InfoHandler) handleConfig(ctx context.Context, cmd ipc.Command) ipc.Response {
// Support sub-commands like "config set ..." from hotkey bindings.
if len(cmd.Args) > 0 {
switch cmd.Args[0] {
case "set":
return h.handleConfigSet(ctx, ipc.Command{
Action: domain.CommandConfigSet,
Args: cmd.Args[1:],
})
case "reset":
return h.handleConfigReset(ctx, ipc.Command{
Action: "config-reset",
Args: cmd.Args[1:],
})
case "reload":
return h.handleReloadConfig(ctx, ipc.Command{})
default:
return ipc.Response{
Success: false,
Message: "unknown config subcommand: " + cmd.Args[0],
Code: ipc.CodeInvalidInput,
}
}
}
// Default: dump the full config.
cfg := h.configSnapshot()
if cfg == nil {
h.logger.Error("Config is nil in handleConfig")
return h.configNotAvailableResponse()
}
return ipc.Response{
Success: true,
Data: cfg,
Code: ipc.CodeOK,
}
}
func (h *InfoHandler) handleReloadConfig(ctx context.Context, _ ipc.Command) ipc.Response {
if h.reloadConfig == nil {
h.logger.Error("Reload config callback is not set")
return ipc.Response{
Success: false,
Message: "reload config not available",
Code: ipc.CodeActionFailed,
}
}
configPath := h.configService.GetConfigPath()
err := h.reloadConfig(ctx, configPath)
if err != nil {
h.logger.Error("Failed to reload config", zap.Error(err))
return ipc.Response{
Success: false,
Message: "failed to reload config: " + err.Error(),
Code: ipc.CodeActionFailed,
}
}
return ipc.Response{
Success: true,
Message: "config reloaded successfully",
Code: ipc.CodeOK,
}
}