-
Notifications
You must be signed in to change notification settings - Fork 0
/
fmt.go
166 lines (142 loc) · 4.16 KB
/
fmt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* The Cookoo `fmt` package provides utility wrappers for formatting text.
The commands in this package provide simple string formatting and printing
at the command level.
For convenience, a basic "text/template" wrapper is included in this library,
though a more robust "html/template" set of commands are provided in
"github.com/Masterminds/cookoo/web".
Example usage:
reg.Route("test", "Test").
Does(Template, "out").
Using("template").WithDefault("{{.Hello}} {{.one}}").
Using("Hello").WithDefault("Hello").
Using("one").WithDefault(1)
Or
reg.Route("test", "Test").
Does(Sprintf, "out").
Using("format").WithDefault("%s %d").
Using("0").WithDefault("Hello").
Using("1").WithDefault(1)
*/
package fmt
import (
"github.com/Masterminds/cookoo"
"text/template"
"crypto/md5"
"bytes"
"fmt"
)
// Template is a template-based text formatter.
//
// This uses the core `text/template` to process a given string template.
//
// Params
// - template (string): A template string.
// - template.Context (bool): If true, the context will be placed into the
// template renderer as 'Cxt', and can be used as `{{.Cxt.Foo}}`. False
// by default.
// - ... (interface{}): Values passed into the template.
//
// Conventionally, template variables should start with an initial capital.
//
// Returns a formatted string.
func Template(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
format := cookoo.GetString("template", "", p)
withCxt := cookoo.GetBool("template.Context", false, p)
name := fmt.Sprintf("%x", md5.Sum([]byte(format)))
//c.Logf("debug", "Template %s is '%s'\n", name, format)
tpl, err := template.New(name).Parse(format)
if err != nil {
return "", err
}
data := p.AsMap()
if withCxt {
//c.Logf("debug", "Adding context.")
data["Cxt"] = c.AsMap()
}
var out bytes.Buffer
if err := tpl.Execute(&out, data); err != nil {
return "", err
}
return out.String(), nil
}
// Println prints a string to standard output, and appends a newline.
//
// Also see web.Flush.
//
// Params:
// - content (string): The string to print.
func Println(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
msg := cookoo.GetString("content", "", p)
fmt.Println(msg)
return msg, nil
}
// Printf formats a string and then sends it to standard out.
//
// This is a command wrapper for the core `fmt.Printf` function, but tooled
// to work the Cookoo way.
//
// The following prints 'Hello' to standard out.
//
// //...
// Does(Printf, "out").
// Using("format").WithDefault("%s\n")
// Using("0").WithDefault("Hello")
//
// Params:
// - format (string): The format string
// - "0"... (string): String representation of an integer ascending from 0.
// These are treated as positional.
func Printf(c cookoo.Context, params *cookoo.Params) (interface{}, cookoo.Interrupt) {
msg := params.Get("format", "").(string)
maxP := len(params.AsMap())
vals := make([]interface{}, 0, maxP - 1)
var istr string
var i = 0
for i < maxP {
istr = fmt.Sprintf("%d", i) // FIXME
if v, ok := params.Has(istr); ok {
//fmt.Printf("%d: Found %v\n", i, v)
vals = append(vals, v)
i++
} else {
break
}
}
fmt.Printf(msg, vals...)
return true, nil
}
// Sprintf formats a string and then returns the result to the context.
//
// This is a command wrapper for the core `fmt.Sprintf` function, but tooled
// to work the Cookoo way.
//
// The following returns 'Hello World' to the context.
//
// //...
// Does(Sprintf, "out").
// Using("format").WithDefault("%s %s\n")
// Using("0").WithDefault("Hello")
// Using("1").WithDefault("World")
//
// Params:
// - format (string): The format string
// - "0"... (string): String representation of an integer ascending from 0.
// These are treated as positional.
func Sprintf(c cookoo.Context, params *cookoo.Params) (interface{}, cookoo.Interrupt) {
msg := params.Get("format", "").(string)
maxP := len(params.AsMap())
vals := make([]interface{}, 0, maxP - 1)
var istr string
var i = 0
for i < maxP {
istr = fmt.Sprintf("%d", i) // FIXME
if v, ok := params.Has(istr); ok {
//fmt.Printf("%d: Found %v\n", i, v)
vals = append(vals, v)
i++
} else {
break
}
}
return fmt.Sprintf(msg, vals...), nil
}