github.com/elves/elvish@v0.15.0/pkg/cli/addons/stub/stub.go (about)

     1  // Package stub implements the stub addon, a general-purpose addon that shows a
     2  // modeline and supports pluggable binding.
     3  package stub
     4  
     5  import (
     6  	"github.com/elves/elvish/pkg/cli"
     7  	"github.com/elves/elvish/pkg/cli/term"
     8  )
     9  
    10  // Config keeps the configuration for the stub addon.
    11  type Config struct {
    12  	// Keybinding.
    13  	Binding cli.Handler
    14  	// Name to show in the modeline.
    15  	Name string
    16  	// Whether the addon widget gets the focus.
    17  	Focus bool
    18  }
    19  
    20  type widget struct {
    21  	Config
    22  }
    23  
    24  func (w *widget) Render(width, height int) *term.Buffer {
    25  	buf := term.NewBufferBuilder(width).
    26  		WriteStyled(cli.ModeLine(w.Name, false)).SetDotHere().Buffer()
    27  	buf.TrimToLines(0, height)
    28  	return buf
    29  }
    30  
    31  func (w *widget) Handle(event term.Event) bool {
    32  	return w.Binding.Handle(event)
    33  }
    34  
    35  func (w *widget) Focus() bool {
    36  	return w.Config.Focus
    37  }
    38  
    39  // Start starts the stub addon.
    40  func Start(app cli.App, cfg Config) {
    41  	if cfg.Binding == nil {
    42  		cfg.Binding = cli.DummyHandler{}
    43  	}
    44  	w := widget{cfg}
    45  	app.MutateState(func(s *cli.State) { s.Addon = &w })
    46  	app.Redraw()
    47  }