-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsimple.go
79 lines (69 loc) · 1.93 KB
/
simple.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
/***************************************************************
*
* Copyright (c) 2015, Menglong TAN <[email protected]>
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the BSD licence
*
**************************************************************/
/**
*
*
* @file simple.go
* @author Menglong TAN <[email protected]>
* @date Sat Oct 10 15:57:57 2015
*
**/
package gotabulate
import (
"fmt"
"strings"
)
//===================================================================
// Public APIs
//===================================================================
type SimpleFormatter struct{}
func NewSimpleFormatter() *SimpleFormatter {
return &SimpleFormatter{}
}
func (this *SimpleFormatter) Format(info *TableInfo) string {
str := ""
var header []string
if info.FirstRowHeader && len(info.Data) > 0 {
header = info.Data[0]
} else {
header = info.Headers
}
str += this.formatLine(header, info)
str += this.formatHeaderSep(info)
rowStart := 0
if info.FirstRowHeader {
rowStart = 1
}
for i := rowStart; i < len(info.Data); i++ {
str += this.formatLine(info.Data[i], info)
}
return str
}
//===================================================================
// Private
//===================================================================
func (this *SimpleFormatter) formatLine(row []string, info *TableInfo) string {
str := ""
l := len(row)
str += fmt.Sprintf("%s%s ", row[0], strings.Repeat(" ", info.CellWidth[0]-len(row[0])))
for i := 1; i < l && i < info.ColumnSize; i++ {
str += fmt.Sprintf("%s%s ", strings.Repeat(" ", info.CellWidth[i]-len(row[i])), row[i])
}
str += "\n"
return str
}
func (this *SimpleFormatter) formatHeaderSep(info *TableInfo) string {
str := ""
headerSep := make([]string, info.ColumnSize)
for i, _ := range headerSep {
headerSep[i] = strings.Repeat("-", info.CellWidth[i])
}
str += this.formatLine(headerSep, info)
return str
}