Skip to content

Commit c6e0066

Browse files
committed
added sorted output, total number of functions in category and 4 missing function descriptions
1 parent 236b595 commit c6e0066

File tree

2 files changed

+81
-24
lines changed

2 files changed

+81
-24
lines changed

pkg/cmd/functionList.go

+40-23
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/ugol/jr/pkg/functions"
2727
"os"
2828
"os/exec"
29+
"slices"
2930
"strings"
3031
)
3132

@@ -57,17 +58,21 @@ func doList(cmd *cobra.Command, args []string) {
5758
noColor, _ := cmd.Flags().GetBool("nocolor")
5859

5960
if category && len(args) > 0 {
61+
var functionNames []string
6062
for k, v := range functions.DescriptionMap() {
6163
if strings.Contains(v.Category, args[0]) {
62-
printFunction(k, isMarkdown, noColor)
64+
functionNames = append(functionNames, k)
6365
}
6466
}
67+
sortAndPrint(functionNames, isMarkdown, noColor)
6568
} else if find && len(args) > 0 {
69+
var functionNames []string
6670
for k, v := range functions.DescriptionMap() {
6771
if strings.Contains(v.Description, args[0]) || strings.Contains(v.Name, args[0]) {
68-
printFunction(k, isMarkdown, noColor)
72+
functionNames = append(functionNames, k)
6973
}
7074
}
75+
sortAndPrint(functionNames, isMarkdown, noColor)
7176
} else if len(args) == 1 {
7277

7378
if run {
@@ -83,15 +88,30 @@ func doList(cmd *cobra.Command, args []string) {
8388
printFunction(args[0], isMarkdown, noColor)
8489
}
8590
} else {
86-
count := 0
87-
for k := range functions.FunctionsMap() {
88-
count++
89-
printFunction(k, isMarkdown, noColor)
91+
92+
//l := len(functions.FunctionsMap())
93+
l := len(functions.DescriptionMap())
94+
functionNames := make([]string, l)
95+
96+
i := 0
97+
for k := range functions.DescriptionMap() {
98+
functionNames[i] = k
99+
i++
90100
}
91-
fmt.Println()
92-
fmt.Printf("Total functions: %d\n", count)
101+
sortAndPrint(functionNames, isMarkdown, noColor)
102+
103+
}
104+
fmt.Println()
105+
}
106+
107+
func sortAndPrint(functionNames []string, isMarkdown bool, noColor bool) {
108+
slices.Sort(functionNames)
109+
for _, k := range functionNames {
110+
printFunction(k, isMarkdown, noColor)
111+
//fmt.Println(k)
93112
}
94113
fmt.Println()
114+
fmt.Printf("Total functions: %d\n", len(functionNames))
95115
}
96116

97117
func printFunction(name string, isMarkdown bool, noColor bool) (functions.FunctionDescription, bool) {
@@ -104,21 +124,8 @@ func printFunction(name string, isMarkdown bool, noColor bool) (functions.Functi
104124
Reset = "\033[0m"
105125
}
106126

107-
if !isMarkdown {
108-
109-
if found {
110-
fmt.Println()
111-
fmt.Printf("%sName: %s%s\n", Cyan, Reset, f.Name)
112-
fmt.Printf("%sCategory: %s%s\n", Cyan, Reset, f.Category)
113-
fmt.Printf("%sDescription: %s%s\n", Cyan, Reset, f.Description)
114-
fmt.Printf("%sParameters: %s%s\n", Cyan, Reset, f.Parameters)
115-
fmt.Printf("%sLocalizable: %s%v\n", Cyan, Reset, f.Localizable)
116-
fmt.Printf("%sReturn: %s%s\n", Cyan, Reset, f.Return)
117-
fmt.Printf("%sExample: %s%s\n", Cyan, Reset, f.Example)
118-
fmt.Printf("%sOutput: %s%s\n", Cyan, Reset, f.Output)
119-
}
120-
} else {
121-
if found {
127+
if found {
128+
if isMarkdown {
122129
fmt.Println()
123130
fmt.Printf("## Name: %s \n", f.Name)
124131
fmt.Printf("**Category:** %s\\\n", f.Category)
@@ -133,6 +140,16 @@ func printFunction(name string, isMarkdown bool, noColor bool) (functions.Functi
133140
fmt.Printf("**Return:** `%s`\\\n", f.Return)
134141
fmt.Printf("**Example:** `%s`\\\n", f.Example)
135142
fmt.Printf("**Output:** `%s`\n", f.Output)
143+
} else {
144+
fmt.Println()
145+
fmt.Printf("%sName: %s%s\n", Cyan, Reset, f.Name)
146+
fmt.Printf("%sCategory: %s%s\n", Cyan, Reset, f.Category)
147+
fmt.Printf("%sDescription: %s%s\n", Cyan, Reset, f.Description)
148+
fmt.Printf("%sParameters: %s%s\n", Cyan, Reset, f.Parameters)
149+
fmt.Printf("%sLocalizable: %s%v\n", Cyan, Reset, f.Localizable)
150+
fmt.Printf("%sReturn: %s%s\n", Cyan, Reset, f.Return)
151+
fmt.Printf("%sExample: %s%s\n", Cyan, Reset, f.Example)
152+
fmt.Printf("%sOutput: %s%s\n", Cyan, Reset, f.Output)
136153
}
137154
}
138155
return f, found

pkg/functions/functionsDescription.go

+41-1
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,16 @@ var funcDesc = map[string]FunctionDescription{
512512
Example: "jr template run --embedded '{{set_v \"id\" \"12770\"}}{{get_v \"id\"}}'",
513513
Output: "12770",
514514
},
515+
"get_v_from_list_at_index": {
516+
Name: "get_v_from_list_at_index",
517+
Category: "context",
518+
Description: "returns a specific value from a list. The list must be set with 'add_v_to_list', usually in an other template",
519+
Parameters: "name string index int",
520+
Localizable: false,
521+
Return: "string",
522+
Example: "jr template run --embedded '{{add_v_to_list \"ids\" \"12770\"}}{{get_v_from_list_at_index \"ids\" 0}}'",
523+
Output: "12770",
524+
},
515525
"http_method": {
516526
Name: "http_method",
517527
Category: "network",
@@ -728,6 +738,16 @@ var funcDesc = map[string]FunctionDescription{
728738
Name: "max",
729739
Category: "math",
730740
Description: "returns the maximum of two numbers",
741+
Parameters: "first int|float, second int|float",
742+
Localizable: false,
743+
Return: "int",
744+
Example: "jr template run --embedded '{{max 10.3 2.4}}'",
745+
Output: "10.3",
746+
},
747+
"maxint": {
748+
Name: "max",
749+
Category: "math",
750+
Description: "returns the maximum of two ints",
731751
Parameters: "first int, second int",
732752
Localizable: false,
733753
Return: "int",
@@ -748,7 +768,17 @@ var funcDesc = map[string]FunctionDescription{
748768
Name: "min",
749769
Category: "math",
750770
Description: "returns the minimum of two numbers",
751-
Parameters: "min int, max int",
771+
Parameters: "first int|float, second int|float",
772+
Localizable: false,
773+
Return: "int",
774+
Example: "jr template run --embedded '{{min 10.1 2.3}}'",
775+
Output: "2.3",
776+
},
777+
"minint": {
778+
Name: "min",
779+
Category: "math",
780+
Description: "returns the minimum of two ints",
781+
Parameters: "first int, second int",
752782
Localizable: false,
753783
Return: "int",
754784
Example: "jr template run --embedded '{{min 10 2}}'",
@@ -994,6 +1024,16 @@ var funcDesc = map[string]FunctionDescription{
9941024
Example: "jr template run --embedded '{{replaceall \"hello world\" \"hello\" \"goodbye\"}}'",
9951025
Output: "goodbye world",
9961026
},
1027+
"seed": {
1028+
Name: "seed",
1029+
Category: "utilities",
1030+
Description: "set seed directly in a template",
1031+
Parameters: "rndSeed int64",
1032+
Localizable: false,
1033+
Return: "",
1034+
Example: "jr template run --embedded '{{seed 12345}}'",
1035+
Output: "",
1036+
},
9971037
"sedol": {
9981038
Name: "sedol",
9991039
Category: "finance",

0 commit comments

Comments
 (0)