forked from cjbassi/gotop
-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
12 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |