Skip to content

Commit

Permalink
Add HelpModal
Browse files Browse the repository at this point in the history
  • Loading branch information
LostbBlizzard committed Aug 10, 2024
1 parent 3394631 commit 6a35093
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
77 changes: 77 additions & 0 deletions components/HelpModal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package components

import (
"github.com/gdamore/tcell/v2"
app "github.com/jorgerojas26/lazysql/app"
"github.com/jorgerojas26/lazysql/commands"
. "github.com/jorgerojas26/lazysql/keymap"
"github.com/rivo/tview"
)

type HelpModal struct {
*tview.Flex
}

func NewHelpModal() *HelpModal {

// Colors
BorderColor := tcell.ColorGreen
SelectedColor := tcell.ColorBlue

list := tview.NewList().SetSelectedBackgroundColor(SelectedColor)
list.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {

command := app.Keymaps.Group("tree").Resolve(event)

if command == commands.MoveUp {
current := list.GetCurrentItem()

if current-1 >= 0 {
list.SetCurrentItem(current - 1)
}
} else if command == commands.MoveDown {

current := list.GetCurrentItem()

if current+1 < list.GetItemCount() {
list.SetCurrentItem(current + 1)
}
}
return event
})

flex := tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(list, 0, 5, true)

flex.SetBorder(true)
flex.SetTitleColor(BorderColor).SetTitle("Help")
flex.SetBorderColor(BorderColor)

//Magic Number gain from trial and error
screenWidth, screenHeight := 145, 30

modalWidth := 50
modalHeight := 20
x := (screenWidth - modalWidth) / 2
y := (screenHeight - modalHeight) / 2

flex.SetRect(x, y, modalWidth, modalHeight)

r := &HelpModal{flex}

r.drawgroup(list, "Global", app.Keymaps.Global)
for k, v := range app.Keymaps.Groups {
r.drawgroup(list, k, v)
}

list.SetCurrentItem(1)
return r
}
func (modal HelpModal) drawgroup(outtext *tview.List, groupname string, keys Map) {

outtext.AddItem("", "---"+groupname+"---", rune(0), nil)

for _, key := range keys {
outtext.AddItem(key.Key.String()+":"+key.Description, "", rune(0), nil)
}
}
17 changes: 17 additions & 0 deletions components/Home.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,23 @@ func (home *Home) homeInputCapture(event *tcell.EventKey) *tcell.EventKey {

MainPages.AddPage("Confirmation", confirmationModal, true, true)
}
} else if command == commands.HelpPopup {

if table == nil || !table.GetIsEditing() {
home.HelpModal = NewHelpModal()

home.HelpModal.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {

command := app.Keymaps.Resolve(event)
if command == commands.Quit {
App.Stop()
} else if event.Key() == tcell.KeyEsc {
MainPages.RemovePage("Help")
}
return event
})
MainPages.AddPage("Help", home.HelpModal, false, true)
}
}

return event
Expand Down

0 comments on commit 6a35093

Please sign in to comment.