Dynamically load style for help from skin (#1113)

mine
Louis Garman 2021-05-04 05:26:28 +01:00 committed by GitHub
parent d5192a07a7
commit d49a06d851
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 51 additions and 20 deletions

View File

@ -50,11 +50,19 @@ func (h *Help) Init(ctx context.Context) error {
h.SetBorderPadding(0, 0, 1, 1)
h.bindKeys()
h.build()
h.SetBackgroundColor(h.App().Styles.BgColor())
h.app.Styles.AddListener(h)
h.StylesChanged(h.app.Styles)
return nil
}
// StylesChanged notifies skin changed.
func (h *Help) StylesChanged(s *config.Styles) {
h.app.Styles = s
h.SetBackgroundColor(s.BgColor())
h.updateStyle()
}
func (h *Help) bindKeys() {
h.Actions().Delete(ui.KeySpace, tcell.KeyCtrlSpace, tcell.KeyCtrlS, ui.KeySlash)
h.Actions().Set(ui.KeyActions{
@ -287,9 +295,9 @@ func (h *Help) addSection(c int, title string, hh model.MenuHints) {
for _, hint := range hh {
col := c
h.SetCell(row, col, keyCell(hint.Mnemonic, h.maxKey))
h.SetCell(row, col, padCellWithRef(toMnemonic(hint.Mnemonic), h.maxKey, hint.Mnemonic))
col++
h.SetCell(row, col, infoCell(hint.Description, h.maxDesc))
h.SetCell(row, col, padCell(hint.Description, h.maxDesc))
row++
}
@ -306,6 +314,35 @@ func (h *Help) addSection(c int, title string, hh model.MenuHints) {
}
}
func (h *Help) updateStyle() {
var key, numKey, info tcell.Style
key = key.Background(h.app.Styles.BgColor())
key = key.Foreground(h.app.Styles.K9s.Frame.Menu.KeyColor.Color())
key = key.Bold(true)
numKey = numKey.Background(h.app.Styles.BgColor())
numKey = numKey.Foreground(h.app.Styles.K9s.Frame.Menu.NumKeyColor.Color())
numKey = numKey.Bold(true)
info = info.Background(h.app.Styles.BgColor())
info = info.Foreground(h.app.Styles.K9s.Frame.Menu.FgColor.Color())
for col := 0; col < h.GetColumnCount(); col++ {
for row := 1; row < h.GetRowCount(); row++ {
c := h.GetCell(row, col)
if col%2 == 0 {
if _, err := strconv.Atoi(extractRef(c)); err != nil {
c.SetStyle(key)
} else {
c.SetStyle(numKey)
}
} else {
c.SetStyle(info)
}
}
}
}
// ----------------------------------------------------------------------------
// Helpers...
@ -317,6 +354,15 @@ func toMnemonic(s string) string {
return "<" + keyConv(strings.ToLower(s)) + ">"
}
func extractRef(c *tview.TableCell) string {
ref, ok := c.GetReference().(string)
if !ok {
return c.Text
}
return ref
}
func keyConv(s string) string {
if !strings.Contains(s, "alt") {
return s
@ -339,23 +385,8 @@ func titleCell(title string) *tview.TableCell {
return c
}
func keyCell(k string, width int) *tview.TableCell {
c := padCell(toMnemonic(k), width)
if _, err := strconv.Atoi(k); err != nil {
c.SetTextColor(tcell.ColorDodgerBlue)
} else {
c.SetTextColor(tcell.ColorFuchsia)
}
c.SetAttributes(tcell.AttrBold)
return c
}
func infoCell(info string, width int) *tview.TableCell {
c := padCell(info, width)
c.SetTextColor(tcell.ColorWhite)
return c
func padCellWithRef(s string, width int, ref interface{}) *tview.TableCell {
return padCell(s, width).SetReference(ref)
}
func padCell(s string, width int) *tview.TableCell {