github.com/click2cloud/libcompose@v0.4.1-0.20170816121048-7c20f79ac6b9/project/info.go (about)

     1  package project
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"text/tabwriter"
     7  )
     8  
     9  // InfoSet holds a list of Info.
    10  type InfoSet []Info
    11  
    12  // Info holds a list of InfoPart.
    13  type Info map[string]string
    14  
    15  func (infos InfoSet) String(columns []string, titleFlag bool) string {
    16  	//no error checking, none of this should fail
    17  	buffer := bytes.NewBuffer(make([]byte, 0, 1024))
    18  	tabwriter := tabwriter.NewWriter(buffer, 4, 4, 2, ' ', 0)
    19  
    20  	first := true
    21  	for _, info := range infos {
    22  		if first && titleFlag {
    23  			writeLine(tabwriter, columns, true, info)
    24  		}
    25  		first = false
    26  		writeLine(tabwriter, columns, false, info)
    27  	}
    28  
    29  	tabwriter.Flush()
    30  	return buffer.String()
    31  }
    32  
    33  func writeLine(writer io.Writer, columns []string, key bool, info Info) {
    34  	first := true
    35  	for _, column := range columns {
    36  		if !first {
    37  			writer.Write([]byte{'\t'})
    38  		}
    39  		first = false
    40  		if key {
    41  			writer.Write([]byte(column))
    42  		} else {
    43  			writer.Write([]byte(info[column]))
    44  		}
    45  	}
    46  
    47  	writer.Write([]byte{'\n'})
    48  }