From 5e0f04882612dd28756449f5100068b1a6975d5d Mon Sep 17 00:00:00 2001 From: "Sean E. Russell" Date: Tue, 16 Feb 2021 16:24:34 -0600 Subject: [PATCH] Fix candidate for #167 --- widgets/help.go | 42 ++++++++++++------------------------------ 1 file changed, 12 insertions(+), 30 deletions(-) diff --git a/widgets/help.go b/widgets/help.go index 73f67a0..1facf31 100644 --- a/widgets/help.go +++ b/widgets/help.go @@ -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) }