k9s/internal/views/cmd.go

73 lines
1.2 KiB
Go

package views
import (
"fmt"
"github.com/derailed/tview"
"github.com/gdamore/tcell"
"github.com/rs/zerolog/log"
)
const defaultPrompt = "%c> %s"
type cmdView struct {
*tview.TextView
activated bool
icon rune
text string
}
func newCmdView(ic rune) *cmdView {
v := cmdView{icon: ic, TextView: tview.NewTextView()}
{
v.SetWordWrap(true)
v.SetWrap(true)
v.SetDynamicColors(true)
v.SetBorderPadding(0, 0, 1, 1)
v.SetTextColor(tcell.ColorAqua)
}
return &v
}
func (v *cmdView) inCmdMode() bool {
return v.activated
}
func (v *cmdView) activate() {
v.write(v.text)
}
func (v *cmdView) update(s string) {
v.text = s
v.Clear()
v.write(s)
}
func (v *cmdView) append(r rune) {
fmt.Fprintf(v, string(r))
}
func (v *cmdView) write(s string) {
fmt.Fprintf(v, defaultPrompt, v.icon, s)
}
// ----------------------------------------------------------------------------
// Event Listener protocol...
func (v *cmdView) changed(s string) {
v.update(s)
}
func (v *cmdView) active(f bool) {
v.activated = f
if f {
log.Debug().Msg("CmdView was activated...")
v.SetBackgroundColor(tcell.ColorDodgerBlue)
v.activate()
} else {
v.SetBackgroundColor(tcell.ColorDefault)
v.Clear()
}
}