1- package util
1+ package base
22
33import (
44 "bufio"
@@ -136,24 +136,32 @@ func PrintJSON(dataSet interface{}) error {
136136 return nil
137137}
138138
139- //PrintTable 以表格方式打印数据集合
140- func PrintTable (dataSet interface {}, fieldList [] string ) error {
139+ //PrintTableS 简化版表格打印,无需传表头,根据结构体反射解析
140+ func PrintTableS (dataSet interface {}) {
141141 dataSetVal := reflect .ValueOf (dataSet )
142+ fieldNameList := make ([]string , 0 )
143+ if dataSetVal .Len () > 0 {
144+ elemType := dataSetVal .Index (0 ).Type ()
145+ for i := 0 ; i < elemType .NumField (); i ++ {
146+ fieldNameList = append (fieldNameList , elemType .Field (i ).Name )
147+ }
148+ }
149+ if kind := dataSetVal .Kind (); kind == reflect .Slice || kind == reflect .Array {
150+ displaySlice (dataSetVal , fieldNameList )
151+ } else {
152+ panic (fmt .Sprintf ("Internal error, PrintTableS expect array or slice, accept %T" , dataSet ))
153+ }
154+ }
142155
156+ //PrintTable 以表格方式打印数据集合
157+ func PrintTable (dataSet interface {}, fieldList []string ) {
158+ dataSetVal := reflect .ValueOf (dataSet )
143159 switch dataSetVal .Kind () {
144160 case reflect .Slice , reflect .Array :
145161 displaySlice (dataSetVal , fieldList )
146- case reflect .Map :
147- displayMap (dataSetVal , fieldList )
148162 default :
149- return fmt .Errorf ("PrintTable expect array,slice or map, accept %T" , dataSet )
163+ panic ( fmt .Sprintf ("PrintTable expect array,slice or map, accept %T" , dataSet ) )
150164 }
151- return nil
152- }
153-
154- func displayMap (mapVal reflect.Value , fieldList []string ) {
155- fmt .Println (mapVal , fieldList )
156- //todo
157165}
158166
159167func displaySlice (listVal reflect.Value , fieldList []string ) {
@@ -180,39 +188,42 @@ func displaySlice(listVal reflect.Value, fieldList []string) {
180188 }
181189 rowList = append (rowList , row )
182190 }
191+ printTable (rowList , fieldList , showFieldMap )
192+ }
183193
194+ func printTable (rowList []map [string ]interface {}, fieldList []string , fieldWidthMap map [string ]int ) {
184195 for _ , field := range fieldList {
185- tmpl := "%-" + strconv .Itoa (showFieldMap [field ]+ GAP ) + "s"
196+ tmpl := "%-" + strconv .Itoa (fieldWidthMap [field ]+ GAP ) + "s"
186197 fmt .Printf (tmpl , field )
187198 }
188199 fmt .Printf ("\n " )
189200
190201 for _ , row := range rowList {
191202 for _ , field := range fieldList {
192203 cutWidth := calcCutWidth (fmt .Sprintf ("%v" , row [field ]))
193- tmpl := "%-" + strconv .Itoa (showFieldMap [field ]- cutWidth + GAP ) + "v"
204+ tmpl := "%-" + strconv .Itoa (fieldWidthMap [field ]- cutWidth + GAP ) + "v"
194205 fmt .Printf (tmpl , row [field ])
195206 }
196207 fmt .Printf ("\n " )
197208 }
198209}
199210
200211func calcCutWidth (text string ) int {
201- set := []* unicode.RangeTable {unicode .Han }
212+ set := []* unicode.RangeTable {unicode .Han , unicode . Punct }
202213 width := 0
203214 for _ , r := range text {
204- if unicode .IsOneOf (set , r ) {
215+ if unicode .IsOneOf (set , r ) && r > unicode . MaxLatin1 {
205216 width ++
206217 }
207218 }
208219 return width
209220}
210221
211222func calcWidth (text string ) int {
212- set := []* unicode.RangeTable {unicode .Han }
223+ set := []* unicode.RangeTable {unicode .Han , unicode . Punct }
213224 width := 0
214225 for _ , r := range text {
215- if unicode .IsOneOf (set , r ) {
226+ if unicode .IsOneOf (set , r ) && r > unicode . MaxLatin1 {
216227 width += 2
217228 } else {
218229 width ++
0 commit comments