Fix candidate for #167

This commit is contained in:
Sean E. Russell 2021-02-16 16:24:34 -06:00
parent a59ad35ed5
commit 5e0f048826

View File

@ -1,58 +1,40 @@
package widgets package widgets
import ( import (
"image"
"strings" "strings"
ui "github.com/gizak/termui/v3" "github.com/gizak/termui/v3/widgets"
lingo "github.com/xxxserxxx/lingo" lingo "github.com/xxxserxxx/lingo"
) )
// Used by all widgets
var tr lingo.Translations var tr lingo.Translations
var keyBinds string
type HelpMenu struct { type HelpMenu struct {
ui.Block widgets.Paragraph
} }
func NewHelpMenu(tra lingo.Translations) *HelpMenu { func NewHelpMenu(tra lingo.Translations) *HelpMenu {
tr = tra tr = tra
keyBinds = tr.Value("help.help") help := &HelpMenu{
return &HelpMenu{ Paragraph: *widgets.NewParagraph(),
Block: *ui.NewBlock(),
} }
help.Paragraph.Text = tra.Value("help.help")
return help
} }
func (help *HelpMenu) Resize(termWidth, termHeight int) { func (help *HelpMenu) Resize(termWidth, termHeight int) {
textWidth := 53 textWidth := 53
for _, line := range strings.Split(keyBinds, "\n") { var nlines int
var line string
for nlines, line = range strings.Split(help.Text, "\n") {
if textWidth < len(line) { if textWidth < len(line) {
textWidth = len(line) + 2 textWidth = len(line) + 2
} }
} }
textHeight := strings.Count(keyBinds, "\n") + 1 textHeight := nlines + 2
x := (termWidth - textWidth) / 2 x := (termWidth - textWidth) / 2
y := (termHeight - textHeight) / 2 y := (termHeight - textHeight) / 2
help.Block.SetRect(x, y, textWidth+x, textHeight+y) help.Paragraph.SetRect(x, y, textWidth+x, textHeight+y)
}
func (help *HelpMenu) Draw(buf *ui.Buffer) {
help.Block.Draw(buf)
for y, line := range strings.Split(keyBinds, "\n") {
for x, rune := range line {
buf.SetCell(
ui.NewCell(rune, ui.Theme.Default),
image.Pt(help.Inner.Min.X+x, help.Inner.Min.Y+y-1),
)
}
}
}
func maxInt(a int, b int) int {
if a > b {
return a
}
return b
} }