initial commit
This commit is contained in:
112
internal/ui/help/help.go
Normal file
112
internal/ui/help/help.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package help
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.brettb.xyz/goinv/client/internal/ui/style"
|
||||
"git.brettb.xyz/goinv/client/internal/ui/utils"
|
||||
"github.com/gdamore/tcell/v2"
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
||||
type Help struct {
|
||||
*tview.Box
|
||||
title string
|
||||
layout *tview.Flex
|
||||
}
|
||||
|
||||
func NewHelp(appName string, appVersion string) *Help {
|
||||
help := &Help{
|
||||
Box: tview.NewBox(),
|
||||
title: "help",
|
||||
}
|
||||
|
||||
headerColor := style.HelpHeaderFgColor
|
||||
fgColor := style.FgColor
|
||||
bgColor := style.BgColor
|
||||
borderColor := style.BorderColor
|
||||
|
||||
keyinfo := tview.NewTable()
|
||||
keyinfo.SetBackgroundColor(bgColor)
|
||||
keyinfo.SetFixed(1, 1)
|
||||
keyinfo.SetSelectable(false, false)
|
||||
|
||||
appinfo := tview.NewTextView().
|
||||
SetDynamicColors(true).
|
||||
SetWrap(true).
|
||||
SetTextAlign(tview.AlignLeft)
|
||||
appinfo.SetBackgroundColor(bgColor)
|
||||
|
||||
appInfoText := fmt.Sprintf("%s %s - (C) 2024 Brett Bender", appName, appVersion)
|
||||
appinfo.SetText(appInfoText)
|
||||
appinfo.SetTextColor(headerColor)
|
||||
|
||||
rowIndex := 0
|
||||
colIndex := 0
|
||||
needInit := true
|
||||
maxRowIndex := len(utils.UIKeyBindings) / 2
|
||||
|
||||
for i := 0; i < len(utils.UIKeyBindings); i++ {
|
||||
if i >= maxRowIndex {
|
||||
if needInit {
|
||||
colIndex = 2
|
||||
rowIndex = 0
|
||||
needInit = false
|
||||
}
|
||||
}
|
||||
|
||||
keyinfo.SetCell(rowIndex, colIndex,
|
||||
tview.NewTableCell(fmt.Sprintf("%s:", utils.UIKeyBindings[i].Label())).
|
||||
SetAlign(tview.AlignRight).
|
||||
SetBackgroundColor(bgColor).
|
||||
SetSelectable(true).SetTextColor(headerColor))
|
||||
|
||||
keyinfo.SetCell(rowIndex, colIndex+1,
|
||||
tview.NewTableCell(utils.UIKeyBindings[i].Description()).
|
||||
SetAlign(tview.AlignLeft).
|
||||
SetBackgroundColor(bgColor).
|
||||
SetSelectable(true).
|
||||
SetTextColor(fgColor))
|
||||
|
||||
rowIndex++
|
||||
}
|
||||
|
||||
mlayout := tview.NewFlex().SetDirection(tview.FlexRow)
|
||||
mlayout.AddItem(appinfo, 1, 0, false)
|
||||
mlayout.AddItem(utils.EmptyBoxSpace(bgColor), 1, 0, false)
|
||||
mlayout.AddItem(keyinfo, 0, 1, false)
|
||||
mlayout.AddItem(utils.EmptyBoxSpace(bgColor), 1, 0, false)
|
||||
|
||||
help.layout = tview.NewFlex().SetDirection(tview.FlexColumn)
|
||||
help.layout.AddItem(utils.EmptyBoxSpace(bgColor), 1, 0, false)
|
||||
help.layout.AddItem(mlayout, 0, 1, false)
|
||||
help.layout.AddItem(utils.EmptyBoxSpace(bgColor), 1, 0, false)
|
||||
help.layout.SetBorder(true)
|
||||
help.layout.SetBackgroundColor(bgColor)
|
||||
help.layout.SetBorderColor(borderColor)
|
||||
|
||||
return help
|
||||
}
|
||||
|
||||
func (help *Help) GetTitle() string {
|
||||
return help.title
|
||||
}
|
||||
|
||||
func (help *Help) HasFocus() bool {
|
||||
return utils.CheckFocus(help.Box, help.layout)
|
||||
}
|
||||
|
||||
func (help *Help) Focus(delegate func(tview.Primitive)) {
|
||||
delegate(help.layout)
|
||||
}
|
||||
|
||||
func (help *Help) Draw(screen tcell.Screen) {
|
||||
x, y, width, height := help.Box.GetInnerRect()
|
||||
if height <= 3 {
|
||||
return
|
||||
}
|
||||
|
||||
help.Box.DrawForSubclass(screen, help)
|
||||
help.layout.SetRect(x, y, width, height)
|
||||
help.layout.Draw(screen)
|
||||
}
|
||||
Reference in New Issue
Block a user