github.com/coveo/gotemplate@v2.7.7+incompatible/template/template_help.go (about)

     1  package template
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  	"sort"
     7  	"strings"
     8  
     9  	"github.com/coveo/gotemplate/collections"
    10  	"github.com/coveo/gotemplate/utils"
    11  	"github.com/fatih/color"
    12  )
    13  
    14  // PrintTemplates output the list of templates available.
    15  func (t Template) PrintTemplates(all, long bool) {
    16  	templates := t.getTemplateNames()
    17  	var maxLen int
    18  	for _, template := range templates {
    19  		t := t.Lookup(template)
    20  		if len(template) > maxLen && template != t.ParseName {
    21  			maxLen = len(template)
    22  		}
    23  	}
    24  
    25  	faint := color.New(color.Faint).SprintfFunc()
    26  
    27  	for _, template := range templates {
    28  		tpl := t.Lookup(template)
    29  		if all || tpl.Name() != tpl.ParseName {
    30  			name := tpl.Name()
    31  			if tpl.Name() == tpl.ParseName {
    32  				name = ""
    33  			}
    34  			folder := utils.Relative(t.folder, tpl.ParseName)
    35  			if folder+name != "." {
    36  				ErrPrintf("%-[3]*[1]s %[2]s\n", name, faint(folder), maxLen)
    37  			}
    38  		}
    39  	}
    40  	ErrPrintln()
    41  }
    42  
    43  // PrintFunctions outputs the list of functions available.
    44  func (t Template) PrintFunctions(all, long, groupByCategory bool, filters ...string) {
    45  	functions := t.filterFunctions(all, groupByCategory, long, filters...)
    46  
    47  	maxLength := 0
    48  	categories := make(map[string][]string)
    49  	for i := range functions {
    50  		var group string
    51  		if groupByCategory {
    52  			funcInfo := t.functions[functions[i]]
    53  			if funcInfo.alias != nil {
    54  				funcInfo = *funcInfo.alias
    55  			}
    56  			group = funcInfo.group
    57  		}
    58  		categories[group] = append(categories[group], functions[i])
    59  		maxLength = int(math.Max(float64(len(functions[i])), float64(maxLength)))
    60  	}
    61  
    62  	keys := make([]string, 0, len(categories))
    63  	for key := range categories {
    64  		keys = append(keys, key)
    65  	}
    66  	sort.Strings(keys)
    67  
    68  	print := t.printFunctionsShort
    69  	if long {
    70  		print = t.printFunctionsDetailed
    71  	}
    72  
    73  	for _, key := range keys {
    74  		if key != "" {
    75  			title, link := collections.Split2(key, ", http")
    76  			title = color.New(color.Underline, color.FgYellow).Sprint(title)
    77  			if link != "" {
    78  				link = color.BlackString(fmt.Sprintf(" http%s", link))
    79  			}
    80  			Printf("%s%s\n\n", title, link)
    81  		}
    82  		print(categories[key], maxLength, all)
    83  		Println()
    84  	}
    85  }
    86  
    87  func (t Template) filterFunctions(all, category, detailed bool, filters ...string) []string {
    88  	functions := t.getAllFunctions()
    89  	if all && len(filters) == 0 {
    90  		return functions
    91  	}
    92  
    93  	for i := range filters {
    94  		filters[i] = strings.ToLower(filters[i])
    95  	}
    96  
    97  	filtered := make([]string, 0, len(functions))
    98  	for i := range functions {
    99  		funcInfo := t.functions[functions[i]]
   100  		if funcInfo.alias != nil {
   101  			if !all {
   102  				continue
   103  			}
   104  			funcInfo = *funcInfo.alias
   105  		}
   106  
   107  		if len(filters) == 0 {
   108  			filtered = append(filtered, functions[i])
   109  			continue
   110  		}
   111  
   112  		search := strings.ToLower(functions[i] + " " + strings.Join(funcInfo.aliases, " "))
   113  		if category {
   114  			search += " " + strings.ToLower(funcInfo.group)
   115  		}
   116  		if detailed {
   117  			search += " " + strings.ToLower(funcInfo.description)
   118  		}
   119  
   120  		for f := range filters {
   121  			if strings.Contains(search, filters[f]) {
   122  				filtered = append(filtered, functions[i])
   123  				break
   124  			}
   125  		}
   126  	}
   127  	return filtered
   128  }
   129  
   130  func (t Template) printFunctionsShort(functions []string, maxLength int, alias bool) {
   131  	const nbColumn = 5
   132  	l := len(functions)
   133  	colLength := int(math.Ceil(float64(l) / float64(nbColumn)))
   134  	for i := 0; i < colLength*nbColumn; i += nbColumn {
   135  		for j := 0; j < nbColumn; j++ {
   136  			pos := j*colLength + i/nbColumn
   137  			if pos >= l {
   138  				continue
   139  			}
   140  			item, extraLen := functions[pos], 0
   141  
   142  			if t.functions[item].alias != nil {
   143  				ex := len(color.HiBlackString(""))
   144  				Printf("%-[1]*[2]s", maxLength+2+ex, color.HiBlackString(item))
   145  			} else {
   146  				Printf("%-[1]*[2]s", maxLength+2+extraLen, item)
   147  			}
   148  		}
   149  		Println()
   150  	}
   151  }
   152  
   153  func (t Template) printFunctionsDetailed(functions []string, maxLength int, alias bool) {
   154  	// We only print entries that are not alias
   155  	allFunc := make(map[string]int)
   156  	for i := range functions {
   157  		funcInfo := t.functions[functions[i]]
   158  		if funcInfo.alias == nil {
   159  			allFunc[functions[i]]++
   160  		} else {
   161  			allFunc[funcInfo.alias.name]++
   162  		}
   163  	}
   164  	functions = make([]string, 0, len(functions))
   165  	for f := range allFunc {
   166  		functions = append(functions, f)
   167  	}
   168  	sort.Strings(functions)
   169  
   170  	for i := range functions {
   171  		fi := t.functions[functions[i]]
   172  		if fi.description != "" {
   173  			Printf(color.GreenString("%s\n"), fi.description)
   174  		}
   175  		Println(fi.Signature())
   176  
   177  		if alias {
   178  			sort.Strings(fi.aliases)
   179  			for j := range fi.aliases {
   180  				aliasFunc := t.functions[fi.aliases[j]]
   181  				if !aliasFunc.IsAlias() || aliasFunc.Arguments() != fi.Arguments() || aliasFunc.Result() != fi.Result() {
   182  					// The alias has been replaced
   183  					continue
   184  				}
   185  				Println(aliasFunc.Signature())
   186  			}
   187  		}
   188  		Println()
   189  	}
   190  }