xmtop/widgets/help.go

59 lines
1.0 KiB
Go
Raw Normal View History

2018-02-18 23:25:02 -08:00
package widgets
import (
2018-12-31 16:55:50 -08:00
"image"
2018-02-18 23:25:02 -08:00
"strings"
2019-03-07 23:15:41 -08:00
ui "github.com/gizak/termui/v3"
lingo "github.com/jdkeke142/lingo-toml"
2018-02-18 23:25:02 -08:00
)
2020-05-05 08:13:25 -05:00
var tr lingo.Translations
var keyBinds string
2018-02-18 23:25:02 -08:00
type HelpMenu struct {
2018-12-31 16:55:50 -08:00
ui.Block
2018-02-18 23:25:02 -08:00
}
2020-05-05 08:13:25 -05:00
func NewHelpMenu(tra lingo.Translations) *HelpMenu {
tr = tra
keyBinds = tr.Value("help.help")
2018-12-31 16:55:50 -08:00
return &HelpMenu{
Block: *ui.NewBlock(),
}
2018-02-18 23:25:02 -08:00
}
2020-04-27 20:33:41 -05:00
func (help *HelpMenu) Resize(termWidth, termHeight int) {
2018-12-31 16:55:50 -08:00
textWidth := 53
2020-05-05 08:13:25 -05:00
for _, line := range strings.Split(keyBinds, "\n") {
if textWidth < len(line) {
textWidth = len(line) + 2
}
}
2020-05-05 08:13:25 -05:00
textHeight := strings.Count(keyBinds, "\n") + 1
2018-12-31 16:55:50 -08:00
x := (termWidth - textWidth) / 2
y := (termHeight - textHeight) / 2
2020-04-27 20:33:41 -05:00
help.Block.SetRect(x, y, textWidth+x, textHeight+y)
2018-12-31 16:55:50 -08:00
}
2018-02-18 23:25:02 -08:00
2020-04-27 20:33:41 -05:00
func (help *HelpMenu) Draw(buf *ui.Buffer) {
help.Block.Draw(buf)
2018-04-12 19:48:50 -07:00
2020-05-05 08:13:25 -05:00
for y, line := range strings.Split(keyBinds, "\n") {
2019-02-28 16:29:52 -08:00
for x, rune := range line {
2018-12-31 16:55:50 -08:00
buf.SetCell(
ui.NewCell(rune, ui.Theme.Default),
2020-04-27 20:33:41 -05:00
image.Pt(help.Inner.Min.X+x, help.Inner.Min.Y+y-1),
2018-12-31 16:55:50 -08:00
)
2018-02-18 23:25:02 -08:00
}
}
}
2019-07-15 16:06:37 -07:00
func maxInt(a int, b int) int {
if a > b {
return a
}
return b
}