|
@@ -4,6 +4,7 @@ import (
|
|
|
"bytes"
|
|
|
"fmt"
|
|
|
"strings"
|
|
|
+ "time"
|
|
|
"unicode/utf8"
|
|
|
)
|
|
|
|
|
@@ -47,6 +48,17 @@ func calcCharsetWidth(s string) int {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func (table *Table) toString(v interface{}) string {
|
|
|
+ switch t := v.(type) {
|
|
|
+ case float32, float64:
|
|
|
+ return fmt.Sprintf("%.2f", t)
|
|
|
+ case time.Time:
|
|
|
+ return t.Format("2006-01-02 15:04:05")
|
|
|
+ default:
|
|
|
+ return fmt.Sprint(v)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
func (table *Table) Marshal() ([]byte, error) {
|
|
|
columns := make([][]string, 0)
|
|
|
var widths []int
|
|
@@ -64,7 +76,7 @@ func (table *Table) Marshal() ([]byte, error) {
|
|
|
vl := len(vs)
|
|
|
column := make([]string, vl)
|
|
|
for i, val := range vs {
|
|
|
- str := fmt.Sprint(val)
|
|
|
+ str := table.toString(val)
|
|
|
if vl > 1 {
|
|
|
width = calcWidth(str)
|
|
|
if width > widths[i] {
|