Skip to content

Commit

Permalink
Fix candidate for #167
Browse files Browse the repository at this point in the history
  • Loading branch information
xxxserxxx committed Feb 16, 2021
1 parent a59ad35 commit 5e0f048
Showing 1 changed file with 12 additions and 30 deletions.
42 changes: 12 additions & 30 deletions widgets/help.go
Original file line number Diff line number Diff line change
@@ -1,58 +1,40 @@
package widgets

import (
"image"
"strings"

ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
lingo "github.com/xxxserxxx/lingo"
)

// Used by all widgets
var tr lingo.Translations
var keyBinds string

type HelpMenu struct {
ui.Block
widgets.Paragraph
}

func NewHelpMenu(tra lingo.Translations) *HelpMenu {
tr = tra
keyBinds = tr.Value("help.help")
return &HelpMenu{
Block: *ui.NewBlock(),
help := &HelpMenu{
Paragraph: *widgets.NewParagraph(),
}
help.Paragraph.Text = tra.Value("help.help")
return help
}

func (help *HelpMenu) Resize(termWidth, termHeight int) {
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) {
textWidth = len(line) + 2
}
}
textHeight := strings.Count(keyBinds, "\n") + 1
textHeight := nlines + 2
x := (termWidth - textWidth) / 2
y := (termHeight - textHeight) / 2

help.Block.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
help.Paragraph.SetRect(x, y, textWidth+x, textHeight+y)
}

0 comments on commit 5e0f048

Please sign in to comment.