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

     1  package cli
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/elves/elvish/pkg/cli/term"
     7  	"github.com/elves/elvish/pkg/ui"
     8  )
     9  
    10  // Selected is a special value in the argument to WriteListing, signalling that
    11  // the argument before it is the selected line.
    12  const Selected = "<- selected"
    13  
    14  // WriteListing is a unit test helper that emulates the rendering of a "listing"
    15  // type addon. Among the lines arguments, the value "<- selected" is special and
    16  // signals that the argument before it is the selected line.
    17  func WriteListing(b *term.BufferBuilder, name, filter string, lines ...string) {
    18  	b.WriteStyled(ModeLine(name, true)).
    19  		Write(filter).SetDotHere()
    20  	for i, line := range lines {
    21  		switch {
    22  		case line == Selected:
    23  			continue
    24  		case i < len(lines)-1 && lines[i+1] == Selected:
    25  			b.Newline()
    26  			padded := fmt.Sprintf("%-*s", b.Width, line)
    27  			b.Write(padded, ui.Inverse)
    28  		default:
    29  			b.Newline()
    30  			b.Write(line)
    31  		}
    32  	}
    33  }