github.com/pwn-term/docker@v0.0.0-20210616085119-6e977cce2565/cli/templates/templates.go (about)

     1  package templates
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"strings"
     7  	"text/template"
     8  )
     9  
    10  // basicFunctions are the set of initial
    11  // functions provided to every template.
    12  var basicFunctions = template.FuncMap{
    13  	"json": func(v interface{}) string {
    14  		buf := &bytes.Buffer{}
    15  		enc := json.NewEncoder(buf)
    16  		enc.SetEscapeHTML(false)
    17  		enc.Encode(v)
    18  		// Remove the trailing new line added by the encoder
    19  		return strings.TrimSpace(buf.String())
    20  	},
    21  	"split":    strings.Split,
    22  	"join":     strings.Join,
    23  	"title":    strings.Title,
    24  	"lower":    strings.ToLower,
    25  	"upper":    strings.ToUpper,
    26  	"pad":      padWithSpace,
    27  	"truncate": truncateWithLength,
    28  }
    29  
    30  // HeaderFunctions are used to created headers of a table.
    31  // This is a replacement of basicFunctions for header generation
    32  // because we want the header to remain intact.
    33  // Some functions like `pad` are not overridden (to preserve alignment
    34  // with the columns).
    35  var HeaderFunctions = template.FuncMap{
    36  	"json": func(v string) string {
    37  		return v
    38  	},
    39  	"split": func(v string, _ string) string {
    40  		// we want the table header to show the name of the column, and not
    41  		// split the table header itself. Using a different signature
    42  		// here, and return a string instead of []string
    43  		return v
    44  	},
    45  	"join": func(v string, _ string) string {
    46  		// table headers are always a string, so use a different signature
    47  		// for the "join" function (string instead of []string)
    48  		return v
    49  	},
    50  	"title": func(v string) string {
    51  		return v
    52  	},
    53  	"lower": func(v string) string {
    54  		return v
    55  	},
    56  	"upper": func(v string) string {
    57  		return v
    58  	},
    59  	"truncate": func(v string, _ int) string {
    60  		return v
    61  	},
    62  }
    63  
    64  // Parse creates a new anonymous template with the basic functions
    65  // and parses the given format.
    66  func Parse(format string) (*template.Template, error) {
    67  	return NewParse("", format)
    68  }
    69  
    70  // New creates a new empty template with the provided tag and built-in
    71  // template functions.
    72  func New(tag string) *template.Template {
    73  	return template.New(tag).Funcs(basicFunctions)
    74  }
    75  
    76  // NewParse creates a new tagged template with the basic functions
    77  // and parses the given format.
    78  func NewParse(tag, format string) (*template.Template, error) {
    79  	return New(tag).Parse(format)
    80  }
    81  
    82  // padWithSpace adds whitespace to the input if the input is non-empty
    83  func padWithSpace(source string, prefix, suffix int) string {
    84  	if source == "" {
    85  		return source
    86  	}
    87  	return strings.Repeat(" ", prefix) + source + strings.Repeat(" ", suffix)
    88  }
    89  
    90  // truncateWithLength truncates the source string up to the length provided by the input
    91  func truncateWithLength(source string, length int) string {
    92  	if len(source) < length {
    93  		return source
    94  	}
    95  	return source[:length]
    96  }