Skip to content

Commit 4a58fb6

Browse files
committed
Adding vertical format and template
1 parent dc79591 commit 4a58fb6

File tree

5 files changed

+47
-16
lines changed

5 files changed

+47
-16
lines changed

encode.go

+6
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,12 @@ func NewAsciiDocEncoder(resultSet ResultSet, opts ...Option) (Encoder, error) {
12031203
return NewTemplateEncoder(resultSet, append([]Option{WithTemplate("asciidoc")}, opts...)...)
12041204
}
12051205

1206+
// NewVerticalEncoder creates a new vertical template encoder using the
1207+
// provided options.
1208+
func NewVerticalEncoder(resultSet ResultSet, opts ...Option) (Encoder, error) {
1209+
return NewTemplateEncoder(resultSet, append([]Option{WithTemplate("vertical")}, opts...)...)
1210+
}
1211+
12061212
// Encode encodes a single result set to the writer using the formatting
12071213
// options specified in the encoder.
12081214
func (enc *TemplateEncoder) Encode(w io.Writer) error {

encode_test.go

-1
This file was deleted.

opts.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func FromMap(opts map[string]string) (Builder, []Option) {
114114
WithTitle(opts["title"]),
115115
WithEmpty(opts["null"]),
116116
}
117-
case "html", "asciidoc", "latex", "latex-longtable", "troff-ms":
117+
case "html", "asciidoc", "latex", "latex-longtable", "troff-ms", "vertical":
118118
return NewTemplateEncoder, []Option{
119119
WithTemplate(format),
120120
WithTableAttributes(opts["tableattr"]),
@@ -491,19 +491,22 @@ func WithRawTemplate(text, typ string) Option {
491491
template: func(enc *TemplateEncoder) error {
492492
switch typ {
493493
case "html":
494-
tpl, err := htmltemplate.New("").Funcs(htmltemplate.FuncMap{
494+
tpl, err := htmltemplate.New(typ).Funcs(htmltemplate.FuncMap{
495495
"attr": func(s string) htmltemplate.HTMLAttr { return htmltemplate.HTMLAttr(s) },
496496
"safe": func(s string) htmltemplate.HTML { return htmltemplate.HTML(s) },
497497
"toLower": func(s string) htmltemplate.HTML { return htmltemplate.HTML(strings.ToLower(s)) },
498498
"toUpper": func(s string) htmltemplate.HTML { return htmltemplate.HTML(strings.ToUpper(s)) },
499+
"inc": func(i int) int { return i + 1 },
499500
}).Parse(text)
500501
if err != nil {
501502
return err
502503
}
503504
enc.executor = tpl.Execute
504505
return nil
505506
case "text":
506-
tpl, err := texttemplate.New("").Parse(text)
507+
tpl, err := texttemplate.New(typ).Funcs(texttemplate.FuncMap{
508+
"inc": func(i int) int { return i + 1 },
509+
}).Parse(text)
507510
if err != nil {
508511
return err
509512
}

tblfmt.go

+32-12
Original file line numberDiff line numberDiff line change
@@ -163,42 +163,62 @@ func EncodeTemplateAll(w io.Writer, resultSet ResultSet, opts ...Option) error {
163163
return enc.EncodeAll(w)
164164
}
165165

166-
// EncodeHTMLTemplate encodes the result set to the writer using the html
167-
// template and the supplied encoding options.
168-
func EncodeHTMLTemplate(w io.Writer, resultSet ResultSet, opts ...Option) error {
166+
// EncodeHTML encodes the result set to the writer using the html template and
167+
// the supplied encoding options.
168+
func EncodeHTML(w io.Writer, resultSet ResultSet, opts ...Option) error {
169169
enc, err := NewHTMLEncoder(resultSet, opts...)
170170
if err != nil {
171171
return err
172172
}
173173
return enc.Encode(w)
174174
}
175175

176-
// EncodeHTMLTemplate encodes the result set to the writer using the html
177-
// template and the supplied encoding options.
178-
func EncodeHTMLTemplateAll(w io.Writer, resultSet ResultSet, opts ...Option) error {
176+
// EncodeHTML encodes the result set to the writer using the html template and
177+
// the supplied encoding options.
178+
func EncodeHTMLAll(w io.Writer, resultSet ResultSet, opts ...Option) error {
179179
enc, err := NewHTMLEncoder(resultSet, opts...)
180180
if err != nil {
181181
return err
182182
}
183183
return enc.EncodeAll(w)
184184
}
185185

186-
// EncodeAsciiDocTemplate encodes the result set to the writer using the
187-
// asciidoc template and the supplied encoding options.
188-
func EncodeAsciiDocTemplate(w io.Writer, resultSet ResultSet, opts ...Option) error {
186+
// EncodeAsciiDoc encodes the result set to the writer using the asciidoc
187+
// template and the supplied encoding options.
188+
func EncodeAsciiDoc(w io.Writer, resultSet ResultSet, opts ...Option) error {
189189
enc, err := NewAsciiDocEncoder(resultSet, opts...)
190190
if err != nil {
191191
return err
192192
}
193193
return enc.Encode(w)
194194
}
195195

196-
// EncodeAsciiDocTemplate encodes the result set to the writer using the
197-
// asciidoc template and the supplied encoding options.
198-
func EncodeAsciiDocTemplateAll(w io.Writer, resultSet ResultSet, opts ...Option) error {
196+
// EncodeAsciiDoc encodes the result set to the writer using the asciidoc
197+
// template and the supplied encoding options.
198+
func EncodeAsciiDocAll(w io.Writer, resultSet ResultSet, opts ...Option) error {
199199
enc, err := NewAsciiDocEncoder(resultSet, opts...)
200200
if err != nil {
201201
return err
202202
}
203203
return enc.EncodeAll(w)
204204
}
205+
206+
// EncodeVertical encodes the result set to the writer using the vertical
207+
// template and the supplied encoding options.
208+
func EncodeVertical(w io.Writer, resultSet ResultSet, opts ...Option) error {
209+
enc, err := NewVerticalEncoder(resultSet, opts...)
210+
if err != nil {
211+
return err
212+
}
213+
return enc.Encode(w)
214+
}
215+
216+
// EncodeVertical encodes the result set to the writer using the vertical
217+
// template and the supplied encoding options.
218+
func EncodeVerticalAll(w io.Writer, resultSet ResultSet, opts ...Option) error {
219+
enc, err := NewVerticalEncoder(resultSet, opts...)
220+
if err != nil {
221+
return err
222+
}
223+
return enc.EncodeAll(w)
224+
}

templates/vertical.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{{ $headers := .Headers }}{{ range $i, $r := .Rows }}*************************** {{ inc $i }}. row ***************************{{ range $j, $c := $r }}
2+
{{ index $headers $j }}: {{ $c }}{{ end }}
3+
{{ end -}}

0 commit comments

Comments
 (0)