src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/cli/modes/stub.go (about)

     1  package modes
     2  
     3  import (
     4  	"src.elv.sh/pkg/cli/term"
     5  	"src.elv.sh/pkg/cli/tk"
     6  )
     7  
     8  // Stub is a mode that just shows a modeline and keeps the focus on the code
     9  // area. It is mainly useful to apply some special non-default bindings.
    10  type Stub interface {
    11  	tk.Widget
    12  }
    13  
    14  // StubSpec specifies the configuration for the stub mode.
    15  type StubSpec struct {
    16  	// Key bindings.
    17  	Bindings tk.Bindings
    18  	// Name to show in the modeline.
    19  	Name string
    20  }
    21  
    22  type stub struct {
    23  	StubSpec
    24  }
    25  
    26  func (w stub) Render(width, height int) *term.Buffer {
    27  	buf := w.render(width)
    28  	buf.TrimToLines(0, height)
    29  	return buf
    30  }
    31  
    32  func (w stub) MaxHeight(width, height int) int {
    33  	return len(w.render(width).Lines)
    34  }
    35  
    36  func (w stub) render(width int) *term.Buffer {
    37  	return term.NewBufferBuilder(width).
    38  		WriteStyled(modeLine(w.Name, false)).SetDotHere().Buffer()
    39  }
    40  
    41  func (w stub) Handle(event term.Event) bool {
    42  	return w.Bindings.Handle(w, event)
    43  }
    44  
    45  func (w stub) Focus() bool {
    46  	return false
    47  }
    48  
    49  // NewStub creates a new Stub mode.
    50  func NewStub(cfg StubSpec) Stub {
    51  	if cfg.Bindings == nil {
    52  		cfg.Bindings = tk.DummyBindings{}
    53  	}
    54  	return stub{cfg}
    55  }