/
keyhandler.go
84 lines (65 loc) 路 1.91 KB
/
keyhandler.go
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
package dashboard
import (
"strings"
"github.com/abdfnx/botway/constants"
"github.com/abdfnx/botway/internal/dashboard/components"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/tidwall/gjson"
)
// Handles all key press events
func (b *Bubble) handleKeys(msg tea.KeyMsg) tea.Cmd {
var cmds []tea.Cmd
if b.outOfBounds() {
if key.Matches(msg, b.keyMap.Quit) {
return tea.Quit
}
return tea.Batch(cmds...)
}
switch {
// Quit
case key.Matches(msg, b.keyMap.Quit):
return tea.Quit
// Down
case key.Matches(msg, b.keyMap.Down):
b.scrollView("down")
// Up
case key.Matches(msg, b.keyMap.Up):
b.scrollView("up")
// Left
case key.Matches(msg, b.keyMap.Left):
b.scrollView("left")
// Right
case key.Matches(msg, b.keyMap.Right):
b.scrollView("right")
// Escape
case key.Matches(msg, b.keyMap.Esc):
cmds = append(cmds, b.resetView())
// Swap view
case key.Matches(msg, b.keyMap.SwapView):
switch b.activeBox {
case components.BotListView:
b.switchActiveView(components.BotInfoView)
default:
b.switchActiveView(components.BotListView)
}
// Open bot project at Railway or Render
case key.Matches(msg, b.keyMap.Open):
if b.botInfo("host_service") == "railway.app" {
bot_project_id := gjson.Get(string(constants.RailwayConfig), "projects."+strings.ToLower(b.botInfo("path"))+".project").String()
OpenBrowser("https://railway.app/project/" + bot_project_id)
} else if b.botInfo("host_service") == "render.com" {
bot_project_id := gjson.Get(string(constants.RenderConfig), "projects."+b.botInfo("name")+".id").String()
OpenBrowser("https://dashboard.render.com/web/" + bot_project_id)
}
}
return tea.Batch(cmds...)
}
func (b *Bubble) resetView() tea.Cmd {
b.nav.boolCursor = false
b.nav.listCursor = 0
b.nav.listCursorHide = true
b.switchActiveView(components.BotListView)
b.lastActiveBox = components.BotListView
return nil
}