-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathlua.go
More file actions
744 lines (703 loc) · 19.4 KB
/
Copy pathlua.go
File metadata and controls
744 lines (703 loc) · 19.4 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
package scripting
import (
stdcontext "context"
"fmt"
"strconv"
"strings"
"sync/atomic"
tea "charm.land/bubbletea/v2"
"github.com/atotto/clipboard"
"github.com/idursun/jjui/internal/ui/actionmeta"
"github.com/idursun/jjui/internal/ui/choose"
"github.com/idursun/jjui/internal/ui/common"
uicontext "github.com/idursun/jjui/internal/ui/context"
"github.com/idursun/jjui/internal/ui/exec_process"
"github.com/idursun/jjui/internal/ui/input"
"github.com/idursun/jjui/internal/ui/intents"
"github.com/idursun/jjui/internal/ui/revisions"
lua "github.com/yuin/gopher-lua"
)
type step struct {
cmd tea.Cmd
matcher func(tea.Msg) (bool, []lua.LValue)
}
type Runner struct {
ctx *uicontext.MainContext
main *lua.LState
thread *lua.LState
cancel stdcontext.CancelFunc
fn *lua.LFunction
started bool
await func(tea.Msg) (bool, []lua.LValue)
resumeArgs []lua.LValue
done bool
}
var nextActionCompletionID atomic.Uint64
func RunScript(ctx *uicontext.MainContext, src string) (*Runner, tea.Cmd, error) {
L, err := vmFromContext(ctx)
if err != nil {
return nil, nil, err
}
r := &Runner{ctx: ctx, main: L}
fn, err := L.LoadString(src)
if err != nil {
return nil, nil, fmt.Errorf("lua: %w", err)
}
r.fn = fn
r.thread, r.cancel = L.NewThread()
cmd := r.resume()
if r.done {
r.close()
}
return r, cmd, nil
}
func (r *Runner) close() {
if r.cancel != nil {
r.cancel()
r.cancel = nil
}
}
func (r *Runner) resume() tea.Cmd {
if r.done {
return nil
}
var cmds []tea.Cmd
for {
var fn *lua.LFunction
if !r.started {
fn = r.fn
}
args := r.resumeArgs
r.resumeArgs = nil
state, err, values := r.main.Resume(r.thread, fn, args...)
r.started = true
if err != nil {
r.done = true
cmds = append(cmds, intents.Invoke(intents.AddMessage{Text: err.Error(), Err: err}))
break
}
for _, v := range values {
if ud, ok := v.(*lua.LUserData); ok {
if st, ok := ud.Value.(step); ok {
if st.matcher != nil {
r.await = st.matcher
if st.cmd != nil {
cmds = append(cmds, st.cmd)
}
return tea.Sequence(cmds...)
}
if st.cmd != nil {
cmds = append(cmds, st.cmd)
}
}
}
}
if state == lua.ResumeOK {
r.done = true
break
}
// continue to resume to collect subsequent steps until an await or completion
if len(cmds) > 0 {
continue
}
}
if len(cmds) == 0 {
return nil
}
return tea.Sequence(cmds...)
}
// HandleMsg resumes the script if waiting for a matching message.
func (r *Runner) HandleMsg(msg tea.Msg) tea.Cmd {
if r.await == nil {
return nil
}
ok, resume := r.await(msg)
if !ok {
return nil
}
r.await = nil
r.resumeArgs = resume
cmd := r.resume()
if r.done {
r.close()
}
return cmd
}
func (r *Runner) Done() bool {
return r.done && r.await == nil
}
func registerAPI(L *lua.LState, ctx *uicontext.MainContext) {
revisionsTable := L.NewTable()
revisionsTable.RawSetString("current", L.NewFunction(func(L *lua.LState) int {
if rev, ok := ctx.SelectedItem.(uicontext.SelectedRevision); ok {
L.Push(lua.LString(rev.ChangeId))
return 1
}
return 0
}))
revisionsTable.RawSetString("checked", L.NewFunction(func(L *lua.LState) int {
tbl := L.NewTable()
for _, item := range ctx.CheckedItems {
if rev, ok := item.(uicontext.SelectedRevision); ok {
tbl.Append(lua.LString(rev.ChangeId))
}
}
L.Push(tbl)
return 1
}))
revisionsTable.RawSetString("refresh", L.NewFunction(func(L *lua.LState) int {
payload := payloadFromTop(L)
intent := intents.Refresh{
KeepSelections: boolVal(payload, "keep_selections"),
SelectedRevision: stringVal(payload, "selected_revision"),
}
return yieldStep(L, step{cmd: revisions.RevisionsCmd(intent), matcher: matchUpdateRevisionsSuccess})
}))
revisionsTable.RawSetString("navigate", L.NewFunction(func(L *lua.LState) int {
payload := payloadFromTop(L)
target := parseNavigateTarget(stringVal(payload, "target"))
intent := intents.Navigate{
Delta: intVal(payload, "by"),
IsPage: boolVal(payload, "page"),
Target: target,
ChangeID: stringVal(payload, "to"),
FallbackID: stringVal(payload, "fallback"),
}
if v, ok := payload["ensureView"]; ok {
if b, ok := v.(bool); ok {
intent.EnsureView = boolPtr(b)
}
}
if v, ok := payload["allowStream"]; ok {
if b, ok := v.(bool); ok {
intent.AllowStream = boolPtr(b)
}
}
return yieldStep(L, step{cmd: revisions.RevisionsCmd(intent)})
}))
revsetTable := L.NewTable()
revsetTable.RawSetString("current", L.NewFunction(func(L *lua.LState) int {
L.Push(lua.LString(ctx.CurrentRevset))
return 1
}))
revsetTable.RawSetString("default", L.NewFunction(func(L *lua.LState) int {
L.Push(lua.LString(ctx.DefaultRevset))
return 1
}))
contextTable := L.NewTable()
contextTable.RawSetString("change_id", L.NewFunction(func(L *lua.LState) int {
switch item := ctx.SelectedItem.(type) {
case uicontext.SelectedRevision:
L.Push(lua.LString(item.ChangeId))
return 1
case uicontext.SelectedFile:
L.Push(lua.LString(item.ChangeId))
return 1
}
return 0
}))
contextTable.RawSetString("commit_id", L.NewFunction(func(L *lua.LState) int {
switch item := ctx.SelectedItem.(type) {
case uicontext.SelectedRevision:
L.Push(lua.LString(item.CommitId))
return 1
case uicontext.SelectedFile:
L.Push(lua.LString(item.CommitId))
return 1
case uicontext.SelectedCommit:
L.Push(lua.LString(item.CommitId))
return 1
}
return 0
}))
contextTable.RawSetString("file", L.NewFunction(func(L *lua.LState) int {
if item, ok := ctx.SelectedItem.(uicontext.SelectedFile); ok {
L.Push(lua.LString(item.File))
return 1
}
return 0
}))
contextTable.RawSetString("operation_id", L.NewFunction(func(L *lua.LState) int {
if item, ok := ctx.SelectedItem.(uicontext.SelectedOperation); ok {
L.Push(lua.LString(item.OperationId))
return 1
}
return 0
}))
contextTable.RawSetString("checked_files", L.NewFunction(func(L *lua.LState) int {
tbl := L.NewTable()
for _, item := range ctx.CheckedItems {
if f, ok := item.(uicontext.SelectedFile); ok {
tbl.Append(lua.LString(f.File))
}
}
L.Push(tbl)
return 1
}))
contextTable.RawSetString("checked_change_ids", L.NewFunction(func(L *lua.LState) int {
tbl := L.NewTable()
for _, item := range ctx.CheckedItems {
switch i := item.(type) {
case uicontext.SelectedRevision:
tbl.Append(lua.LString(i.ChangeId))
case uicontext.SelectedFile:
tbl.Append(lua.LString(i.ChangeId))
}
}
L.Push(tbl)
return 1
}))
contextTable.RawSetString("checked_commit_ids", L.NewFunction(func(L *lua.LState) int {
tbl := L.NewTable()
for _, item := range ctx.CheckedItems {
switch i := item.(type) {
case uicontext.SelectedRevision:
tbl.Append(lua.LString(i.CommitId))
case uicontext.SelectedFile:
tbl.Append(lua.LString(i.CommitId))
case uicontext.SelectedCommit:
tbl.Append(lua.LString(i.CommitId))
}
}
L.Push(tbl)
return 1
}))
jjAsyncFn := L.NewFunction(func(L *lua.LState) int {
args := argsFromLua(L)
return yieldStep(L, step{cmd: ctx.RunCommand(args)})
})
jjInteractiveFn := L.NewFunction(func(L *lua.LState) int {
args := argsFromLua(L)
return yieldStep(L, step{cmd: ctx.RunInteractiveCommand(args, nil)})
})
jjFn := L.NewFunction(func(L *lua.LState) int {
args := argsFromLua(L)
out, err := ctx.RunCommandImmediate(args)
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
L.Push(lua.LString(out))
L.Push(lua.LNil)
return 2
})
flashFn := L.NewFunction(func(L *lua.LState) int {
intent := intents.AddMessage{}
switch v := L.Get(1).(type) {
case *lua.LTable:
payload := luaTableToMap(v)
intent.Text = stringVal(payload, "text")
if boolVal(payload, "error") {
intent.Err = fmt.Errorf("%s", intent.Text)
}
intent.Sticky = boolVal(payload, "sticky")
default:
intent.Text = L.CheckString(1)
}
return yieldStep(L, step{cmd: intents.Invoke(intent)})
})
setThemeFn := L.NewFunction(func(L *lua.LState) int {
name := L.CheckString(1)
return yieldStep(L, step{cmd: intents.Invoke(intents.ChangeTheme{Name: name})})
})
copyToClipboardFn := L.NewFunction(func(L *lua.LState) int {
text := L.CheckString(1)
if err := clipboard.WriteAll(text); err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
L.Push(lua.LBool(true))
L.Push(lua.LNil)
return 2
})
execShellFn := L.NewFunction(func(L *lua.LState) int {
command := L.CheckString(1)
msg := common.ExecMsg{
Line: command,
Mode: common.ExecShell,
}
return yieldStep(L, step{cmd: exec_process.ExecLine(ctx, msg)})
})
splitLinesFn := L.NewFunction(func(L *lua.LState) int {
text := L.CheckString(1)
keepEmpty := false
if L.GetTop() >= 2 {
keepEmpty = L.CheckBool(2)
}
tbl := L.NewTable()
for line := range strings.SplitSeq(text, "\n") {
line = strings.TrimSuffix(line, "\r")
if line == "" && !keepEmpty {
continue
}
tbl.Append(lua.LString(line))
}
L.Push(tbl)
return 1
})
chooseFn := L.NewFunction(func(L *lua.LState) int {
var (
options []string
title string
filter bool
ordered bool
)
if L.GetTop() == 1 {
if tbl, ok := L.Get(1).(*lua.LTable); ok {
if optVal := tbl.RawGetString("options"); optVal != lua.LNil {
if optTbl, ok := optVal.(*lua.LTable); ok {
options = stringSliceFromTable(optTbl)
} else if s, ok := optVal.(lua.LString); ok {
options = []string{s.String()}
}
}
if titleVal := tbl.RawGetString("title"); titleVal != lua.LNil {
title = titleVal.String()
}
if filterVal := tbl.RawGetString("filter"); filterVal != lua.LNil {
filter = bool(filterVal.(lua.LBool))
}
if orderedVal := tbl.RawGetString("ordered"); orderedVal != lua.LNil {
ordered = bool(orderedVal.(lua.LBool))
}
if options == nil {
options = stringSliceFromTable(tbl)
}
return yieldStep(L, step{cmd: choose.ShowOrdered(options, title, filter, ordered), matcher: matchChoose})
}
}
options = argsFromLua(L)
return yieldStep(L, step{cmd: choose.ShowWithTitle(options, "", false), matcher: matchChoose})
})
inputFn := L.NewFunction(func(L *lua.LState) int {
var title, prompt, value string
if L.GetTop() == 1 {
if tbl, ok := L.Get(1).(*lua.LTable); ok {
if titleVal := tbl.RawGetString("title"); titleVal != lua.LNil {
title = titleVal.String()
}
if promptVal := tbl.RawGetString("prompt"); promptVal != lua.LNil {
prompt = promptVal.String()
}
if valueVal := tbl.RawGetString("value"); valueVal != lua.LNil {
value = valueVal.String()
}
return yieldStep(L, step{cmd: input.ShowWithTitle(title, prompt, value), matcher: matchInput})
}
}
return yieldStep(L, step{cmd: input.ShowWithTitle("", "", ""), matcher: matchInput})
})
waitCloseFn := L.NewFunction(func(L *lua.LState) int {
return yieldStep(L, step{matcher: matchCloseViewMsg})
})
waitRefreshFn := L.NewFunction(func(L *lua.LState) int {
return yieldStep(L, step{matcher: matchUpdateRevisionsSuccess})
})
changeWsFn := L.NewFunction(func(L *lua.LState) int {
path := L.CheckString(1)
prev := ctx.Location
ctx.ChangeWorkspace(path)
out, err := ctx.RunCommandImmediate([]string{"root"})
if err != nil {
ctx.ChangeWorkspace(prev)
L.Push(lua.LNil)
L.Push(lua.LString("not a jj repo: " + path))
return 2
}
ctx.ChangeWorkspace(strings.TrimSpace(string(out)))
L.Push(lua.LBool(true))
L.Push(lua.LNil)
return 2
})
// make sure we have a `jjui` namespace
root := L.NewTable()
root.RawSetString("revisions", revisionsTable)
root.RawSetString("revset", revsetTable)
root.RawSetString("context", contextTable)
root.RawSetString("jj_async", jjAsyncFn)
root.RawSetString("jj_interactive", jjInteractiveFn)
root.RawSetString("jj", jjFn)
root.RawSetString("flash", flashFn)
root.RawSetString("set_theme", setThemeFn)
root.RawSetString("copy_to_clipboard", copyToClipboardFn)
root.RawSetString("exec_shell", execShellFn)
root.RawSetString("split_lines", splitLinesFn)
root.RawSetString("choose", chooseFn)
root.RawSetString("input", inputFn)
root.RawSetString("wait_close", waitCloseFn)
root.RawSetString("wait_refresh", waitRefreshFn)
root.RawSetString("change_workspace", changeWsFn)
builtinRoot := L.NewTable()
root.RawSetString("builtin", builtinRoot)
registerGeneratedActionAPI(L, root, false)
registerGeneratedActionAPI(L, builtinRoot, true)
L.SetGlobal("jjui", root)
// but also expose at the top level for convenience
L.SetGlobal("revisions", revisionsTable)
L.SetGlobal("revset", revsetTable)
if diffTable, ok := root.RawGetString("diff").(*lua.LTable); ok {
L.SetGlobal("diff", diffTable)
}
if uiTable, ok := root.RawGetString("ui").(*lua.LTable); ok {
L.SetGlobal("ui", uiTable)
}
L.SetGlobal("context", contextTable)
L.SetGlobal("jj_async", jjAsyncFn)
L.SetGlobal("jj_interactive", jjInteractiveFn)
L.SetGlobal("jj", jjFn)
L.SetGlobal("flash", flashFn)
L.SetGlobal("set_theme", setThemeFn)
L.SetGlobal("copy_to_clipboard", copyToClipboardFn)
L.SetGlobal("exec_shell", execShellFn)
L.SetGlobal("split_lines", splitLinesFn)
L.SetGlobal("choose", chooseFn)
L.SetGlobal("input", inputFn)
L.SetGlobal("wait_close", waitCloseFn)
L.SetGlobal("wait_refresh", waitRefreshFn)
L.SetGlobal("change_workspace", changeWsFn)
}
func registerGeneratedActionAPI(L *lua.LState, root *lua.LTable, builtIn bool) {
actions := actionmeta.BuiltInActions()
for _, actionID := range actions {
scopes := actionmeta.ActionScopes(actionID)
for _, scope := range scopes {
scopeTable := ensureScopeTable(L, root, scope)
token := actionTokenFromCanonical(actionID)
if token == "" {
continue
}
// Keep existing utility helpers (e.g. jjui.revisions.refresh) intact.
if scopeTable.RawGetString(token) != lua.LNil {
continue
}
scopeTable.RawSetString(token, generatedActionFn(L, actionID, builtIn))
if token == "cancel" && scopeTable.RawGetString("close") == lua.LNil {
scopeTable.RawSetString("close", generatedActionFn(L, actionID, builtIn))
}
}
}
}
func ensureScopeTable(L *lua.LState, root *lua.LTable, scope string) *lua.LTable {
current := root
for segment := range strings.SplitSeq(scope, ".") {
existing := current.RawGetString(segment)
if tbl, ok := existing.(*lua.LTable); ok {
current = tbl
continue
}
next := L.NewTable()
current.RawSetString(segment, next)
current = next
}
return current
}
func generatedActionFn(L *lua.LState, canonical string, builtIn bool) *lua.LFunction {
var positionalKey string
if required := actionmeta.ActionRequiredArgs(canonical); len(required) == 1 && actionmeta.ActionArgSchema(canonical)[required[0]] == "string" {
positionalKey = required[0]
}
return L.NewFunction(func(L *lua.LState) int {
var args map[string]any
if positionalKey != "" && L.GetTop() >= 1 {
if s, ok := L.Get(1).(lua.LString); ok {
args = map[string]any{positionalKey: s.String()}
} else {
args = optionalLuaMapArg(L, 1)
}
} else {
args = optionalLuaMapArg(L, 1)
}
completionID := strconv.FormatUint(nextActionCompletionID.Add(1), 10)
return yieldStep(L, step{cmd: func() tea.Msg {
return common.DispatchActionMsg{
Action: canonical,
Args: args,
BuiltIn: builtIn,
CompletionID: completionID,
}
}, matcher: matchActionCompleted(completionID)})
})
}
func optionalLuaMapArg(L *lua.LState, pos int) map[string]any {
if L.GetTop() < pos || L.Get(pos) == lua.LNil {
return nil
}
tbl, ok := L.Get(pos).(*lua.LTable)
if !ok {
L.ArgError(pos, "expected table or nil")
return nil
}
return luaTableToMap(tbl)
}
func payloadFromTop(L *lua.LState) map[string]any {
if L.GetTop() >= 1 && L.CheckAny(1) != lua.LNil {
if tbl, ok := L.Get(1).(*lua.LTable); ok {
return luaTableToMap(tbl)
}
}
return map[string]any{}
}
func boolVal(payload map[string]any, key string) bool {
if v, ok := payload[key]; ok {
if b, ok := v.(bool); ok {
return b
}
}
return false
}
func intVal(payload map[string]any, key string) int {
if v, ok := payload[key]; ok {
switch n := v.(type) {
case int:
return n
case int64:
return int(n)
case float64:
return int(n)
case float32:
return int(n)
}
}
return 0
}
func stringVal(payload map[string]any, key string) string {
if v, ok := payload[key]; ok {
if s, ok := v.(string); ok {
return s
}
}
return ""
}
func argsFromLua(L *lua.LState) []string {
if L.GetTop() == 0 {
return nil
}
if tbl, ok := L.Get(1).(*lua.LTable); ok {
return stringSliceFromTable(tbl)
}
var out []string
top := L.GetTop()
for i := 1; i <= top; i++ {
out = append(out, L.CheckString(i))
}
return out
}
func stringSliceFromTable(tbl *lua.LTable) []string {
var out []string
tbl.ForEach(func(_, value lua.LValue) {
if s, ok := value.(lua.LString); ok {
out = append(out, s.String())
}
})
return out
}
func luaTableToMap(tbl *lua.LTable) map[string]any {
result := map[string]any{}
tbl.ForEach(func(key, value lua.LValue) {
if key.Type() != lua.LTString {
return
}
result[key.String()] = luaValueToGo(value)
})
return result
}
func luaTableToSlice(tbl *lua.LTable) []any {
var result []any
tbl.ForEach(func(_, value lua.LValue) {
result = append(result, luaValueToGo(value))
})
return result
}
func luaValueToGo(value lua.LValue) any {
switch value.Type() {
case lua.LTBool:
return bool(value.(lua.LBool))
case lua.LTNumber:
return float64(value.(lua.LNumber))
case lua.LTString:
return value.String()
case lua.LTTable:
t := value.(*lua.LTable)
// Heuristic: if keys are string, convert to map; otherwise, slice.
isMap := false
t.ForEach(func(key, _ lua.LValue) {
if key.Type() == lua.LTString {
isMap = true
}
})
if isMap {
return luaTableToMap(t)
}
return luaTableToSlice(t)
default:
return nil
}
}
func yieldStep(L *lua.LState, st step) int {
ud := L.NewUserData()
ud.Value = st
return L.Yield(ud)
}
func boolPtr(v bool) *bool {
return &v
}
func parseNavigateTarget(val string) intents.NavigationTarget {
switch strings.ToLower(val) {
case "parent":
return intents.TargetParent
case "child", "children":
return intents.TargetChild
case "working", "working_copy", "work":
return intents.TargetWorkingCopy
default:
return intents.TargetNone
}
}
func matchUpdateRevisionsSuccess(msg tea.Msg) (bool, []lua.LValue) {
switch msg.(type) {
case common.UpdateRevisionsSuccessMsg, common.UpdateRevisionsFailedMsg:
return true, nil
default:
return false, nil
}
}
func matchCloseViewMsg(msg tea.Msg) (bool, []lua.LValue) {
if closeMsg, ok := msg.(common.CloseViewMsg); ok {
return true, []lua.LValue{lua.LBool(closeMsg.Applied)}
}
return false, nil
}
func matchActionCompleted(id string) func(tea.Msg) (bool, []lua.LValue) {
return func(msg tea.Msg) (bool, []lua.LValue) {
completed, ok := msg.(common.ActionCompletedMsg)
return ok && completed.ID == id, nil
}
}
func matchChoose(msg tea.Msg) (bool, []lua.LValue) {
switch msg := msg.(type) {
case choose.SelectedMsg:
return true, []lua.LValue{lua.LString(msg.Value)}
case choose.CancelledMsg:
return true, []lua.LValue{lua.LNil}
default:
return false, nil
}
}
func matchInput(msg tea.Msg) (bool, []lua.LValue) {
switch msg := msg.(type) {
case input.SelectedMsg:
return true, []lua.LValue{lua.LString(msg.Value)}
case input.CancelledMsg:
return true, []lua.LValue{lua.LNil}
default:
return false, nil
}
}
func actionTokenFromCanonical(actionID string) string {
if idx := strings.LastIndexByte(actionID, '.'); idx >= 0 && idx < len(actionID)-1 {
return actionID[idx+1:]
}
return actionID
}