2
0
mirror of https://github.com/miguelmota/cointop synced 2024-11-10 13:10:26 +00:00
cointop/cointop/help.go
2021-02-05 01:04:09 -08:00

90 lines
2.2 KiB
Go

package cointop
import (
"fmt"
"sort"
"github.com/miguelmota/cointop/pkg/pad"
)
// UpdateHelp updates the help views
func (ct *Cointop) UpdateHelp() {
ct.debuglog("updateHelp()")
keys := make([]string, 0, len(ct.State.shortcutKeys))
for k := range ct.State.shortcutKeys {
keys = append(keys, k)
}
sort.Strings(keys)
header := ct.colorscheme.MenuHeader(fmt.Sprintf(" Help %s\n\n", pad.Left("[q] close ", ct.width()-9, " ")))
cnt := 0
h := ct.Views.Menu.Height()
percol := h - 11
cols := make([][]string, percol)
for i := range cols {
cols[i] = make([]string, 20)
}
for _, k := range keys {
v := ct.State.shortcutKeys[k]
if cnt%percol == 0 {
cnt = 0
}
item := fmt.Sprintf("%10s %-45s", k, ct.colorscheme.MenuLabel(v))
cols[cnt] = append(cols[cnt], item)
cnt = cnt + 1
}
var body string
for i := 0; i < percol; i++ {
var row string
for j := 0; j < len(cols[i]); j++ {
item := cols[i][j]
row = fmt.Sprintf("%s%s", row, item)
}
body = fmt.Sprintf("%s%s\n", body, row)
}
versionLine := fmt.Sprintf("cointop %s - (C) 2017-2021 Miguel Mota", ct.Version())
licenseLine := "Released under the Apache 2.0 License."
instructionsLine := "List of keyboard shortcuts"
infoLine := "See git.io/cointop for more info.\n Press ESC to return."
content := fmt.Sprintf("%s %s\n %s\n\n %s\n\n%s\n %s", header, versionLine, licenseLine, instructionsLine, body, infoLine)
ct.UpdateUI(func() error {
ct.Views.Menu.SetFrame(true)
return ct.Views.Menu.Update(content)
})
}
// ShowHelp shows the help view
func (ct *Cointop) ShowHelp() error {
ct.debuglog("showHelp()")
ct.State.helpVisible = true
ct.UpdateHelp()
ct.SetActiveView(ct.Views.Menu.Name())
return nil
}
// HideHelp hides the help view
func (ct *Cointop) HideHelp() error {
ct.debuglog("hideHelp()")
ct.State.helpVisible = false
ct.ui.SetViewOnBottom(ct.Views.Menu)
ct.SetActiveView(ct.Views.Table.Name())
ct.UpdateUI(func() error {
ct.Views.Menu.SetFrame(false)
return ct.Views.Menu.Update("")
})
return nil
}
// ToggleHelp toggles the help view
func (ct *Cointop) ToggleHelp() error {
ct.debuglog("toggleHelp()")
ct.State.helpVisible = !ct.State.helpVisible
if ct.State.helpVisible {
return ct.ShowHelp()
}
return ct.HideHelp()
}