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

     1  package tk
     2  
     3  import (
     4  	"src.elv.sh/pkg/cli/term"
     5  	"src.elv.sh/pkg/ui"
     6  )
     7  
     8  // Label is a Renderer that writes out a text.
     9  type Label struct {
    10  	Content ui.Text
    11  }
    12  
    13  // Render shows the content. If the given box is too small, the text is cropped.
    14  func (l Label) Render(width, height int) *term.Buffer {
    15  	// TODO: Optimize by stopping as soon as $height rows are written.
    16  	b := l.render(width)
    17  	b.TrimToLines(0, height)
    18  	return b
    19  }
    20  
    21  // MaxHeight returns the maximum height the Label can take when rendering within
    22  // a bound box.
    23  func (l Label) MaxHeight(width, height int) int {
    24  	return len(l.render(width).Lines)
    25  }
    26  
    27  func (l Label) render(width int) *term.Buffer {
    28  	return term.NewBufferBuilder(width).WriteStyled(l.Content).Buffer()
    29  }
    30  
    31  // Handle always returns false.
    32  func (l Label) Handle(event term.Event) bool {
    33  	return false
    34  }