File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ package writer
2
+
3
+ import (
4
+ "fmt"
5
+ "io"
6
+ "strings"
7
+ )
8
+
9
+ ///////////////////////////////////////////////////////////////////////////////
10
+ // TYPES
11
+
12
+ type TextWriter struct {
13
+ format string
14
+ }
15
+
16
+ ///////////////////////////////////////////////////////////////////////////////
17
+ // CONSTRUCTOR
18
+
19
+ // Write outputs the table to a writer
20
+ func NewTextWriter (columns []ColumnMeta , delim rune ) * TextWriter {
21
+ self := new (TextWriter )
22
+
23
+ // Create the format from the column metadata
24
+ f := new (strings.Builder )
25
+ f .WriteRune (delim )
26
+ for _ , column := range columns {
27
+ f .WriteRune ('%' )
28
+ if column .Flags & FormatAlignLeft != 0 {
29
+ f .WriteRune ('-' )
30
+ }
31
+ if column .Width > 0 {
32
+ f .WriteString (fmt .Sprint (column .Width ))
33
+ }
34
+ f .WriteRune ('s' )
35
+ f .WriteRune (delim )
36
+ }
37
+ self .format = f .String ()
38
+ return self
39
+ }
40
+
41
+ func (self * TextWriter ) Writeln (w io.Writer , elems []any ) error {
42
+ if _ , err := fmt .Fprintf (w , self .format , elems ... ); err != nil {
43
+ return err
44
+ }
45
+ if _ , err := fmt .Fprintln (w ); err != nil {
46
+ return err
47
+ }
48
+ return nil
49
+ }
You can’t perform that action at this time.
0 commit comments