github.com/hugelgupf/u-root@v0.0.0-20191023214958-4807c632154c/cmds/core/elvish/eval/vals/list.go (about)

     1  package vals
     2  
     3  import (
     4  	"bytes"
     5  	"strings"
     6  
     7  	"github.com/u-root/u-root/cmds/core/elvish/vector"
     8  )
     9  
    10  // EmptyList is an empty list.
    11  var EmptyList = vector.Empty
    12  
    13  // MakeList creates a new List from values.
    14  func MakeList(vs ...interface{}) vector.Vector {
    15  	vec := vector.Empty
    16  	for _, v := range vs {
    17  		vec = vec.Cons(v)
    18  	}
    19  	return vec
    20  }
    21  
    22  // MakeList creates a new List from strings.
    23  func MakeStringList(vs ...string) vector.Vector {
    24  	vec := vector.Empty
    25  	for _, v := range vs {
    26  		vec = vec.Cons(v)
    27  	}
    28  	return vec
    29  }
    30  
    31  // ListReprBuilder helps to build Repr of list-like Values.
    32  type ListReprBuilder struct {
    33  	Indent int
    34  	buf    bytes.Buffer
    35  }
    36  
    37  func (b *ListReprBuilder) WriteElem(v string) {
    38  	if b.buf.Len() == 0 {
    39  		b.buf.WriteByte('[')
    40  	}
    41  	if b.Indent >= 0 {
    42  		// Pretty printing.
    43  		//
    44  		// Add a newline and indent+1 spaces, so that the
    45  		// starting & lines up with the first pair.
    46  		b.buf.WriteString("\n" + strings.Repeat(" ", b.Indent+1))
    47  	} else if b.buf.Len() > 1 {
    48  		b.buf.WriteByte(' ')
    49  	}
    50  	b.buf.WriteString(v)
    51  }
    52  
    53  func (b *ListReprBuilder) String() string {
    54  	if b.buf.Len() == 0 {
    55  		return "[]"
    56  	}
    57  	if b.Indent >= 0 {
    58  		b.buf.WriteString("\n" + strings.Repeat(" ", b.Indent))
    59  	}
    60  	b.buf.WriteByte(']')
    61  	return b.buf.String()
    62  }