1- package util
1+ package base
22
33import (
44 "bufio"
@@ -13,10 +13,9 @@ import (
1313 "time"
1414 "unicode"
1515
16- "github.com/ucloud/ucloud-sdk-go/sdk"
17- uerr "github.com/ucloud/ucloud-sdk-go/sdk/error"
18- "github.com/ucloud/ucloud-sdk-go/sdk/response"
19- service "github.com/ucloud/ucloud-sdk-go/services"
16+ sdk "github.com/ucloud/ucloud-sdk-go/ucloud"
17+ uerr "github.com/ucloud/ucloud-sdk-go/ucloud/error"
18+ "github.com/ucloud/ucloud-sdk-go/ucloud/response"
2019
2120 "github.com/ucloud/ucloud-cli/model"
2221)
@@ -34,7 +33,7 @@ var Cxt = model.GetContext(os.Stdout)
3433var SdkClient * sdk.Client
3534
3635//BizClient 用于调用业务接口
37- var BizClient * service. Client
36+ var BizClient * Client
3837
3938//GetHomePath 获取家目录
4039func GetHomePath () string {
@@ -136,24 +135,32 @@ func PrintJSON(dataSet interface{}) error {
136135 return nil
137136}
138137
139- //PrintTable 以表格方式打印数据集合
140- func PrintTable (dataSet interface {}, fieldList [] string ) error {
138+ //PrintTableS 简化版表格打印,无需传表头,根据结构体反射解析
139+ func PrintTableS (dataSet interface {}) {
141140 dataSetVal := reflect .ValueOf (dataSet )
141+ fieldNameList := make ([]string , 0 )
142+ if dataSetVal .Len () > 0 {
143+ elemType := dataSetVal .Index (0 ).Type ()
144+ for i := 0 ; i < elemType .NumField (); i ++ {
145+ fieldNameList = append (fieldNameList , elemType .Field (i ).Name )
146+ }
147+ }
148+ if kind := dataSetVal .Kind (); kind == reflect .Slice || kind == reflect .Array {
149+ displaySlice (dataSetVal , fieldNameList )
150+ } else {
151+ panic (fmt .Sprintf ("Internal error, PrintTableS expect array or slice, accept %T" , dataSet ))
152+ }
153+ }
142154
155+ //PrintTable 以表格方式打印数据集合
156+ func PrintTable (dataSet interface {}, fieldList []string ) {
157+ dataSetVal := reflect .ValueOf (dataSet )
143158 switch dataSetVal .Kind () {
144159 case reflect .Slice , reflect .Array :
145160 displaySlice (dataSetVal , fieldList )
146- case reflect .Map :
147- displayMap (dataSetVal , fieldList )
148161 default :
149- return fmt .Errorf ("PrintTable expect array,slice or map, accept %T" , dataSet )
162+ panic ( fmt .Sprintf ("PrintTable expect array,slice or map, accept %T" , dataSet ) )
150163 }
151- return nil
152- }
153-
154- func displayMap (mapVal reflect.Value , fieldList []string ) {
155- fmt .Println (mapVal , fieldList )
156- //todo
157164}
158165
159166func displaySlice (listVal reflect.Value , fieldList []string ) {
@@ -180,39 +187,42 @@ func displaySlice(listVal reflect.Value, fieldList []string) {
180187 }
181188 rowList = append (rowList , row )
182189 }
190+ printTable (rowList , fieldList , showFieldMap )
191+ }
183192
193+ func printTable (rowList []map [string ]interface {}, fieldList []string , fieldWidthMap map [string ]int ) {
184194 for _ , field := range fieldList {
185- tmpl := "%-" + strconv .Itoa (showFieldMap [field ]+ GAP ) + "s"
195+ tmpl := "%-" + strconv .Itoa (fieldWidthMap [field ]+ GAP ) + "s"
186196 fmt .Printf (tmpl , field )
187197 }
188198 fmt .Printf ("\n " )
189199
190200 for _ , row := range rowList {
191201 for _ , field := range fieldList {
192202 cutWidth := calcCutWidth (fmt .Sprintf ("%v" , row [field ]))
193- tmpl := "%-" + strconv .Itoa (showFieldMap [field ]- cutWidth + GAP ) + "v"
203+ tmpl := "%-" + strconv .Itoa (fieldWidthMap [field ]- cutWidth + GAP ) + "v"
194204 fmt .Printf (tmpl , row [field ])
195205 }
196206 fmt .Printf ("\n " )
197207 }
198208}
199209
200210func calcCutWidth (text string ) int {
201- set := []* unicode.RangeTable {unicode .Han }
211+ set := []* unicode.RangeTable {unicode .Han , unicode . Punct }
202212 width := 0
203213 for _ , r := range text {
204- if unicode .IsOneOf (set , r ) {
214+ if unicode .IsOneOf (set , r ) && r > unicode . MaxLatin1 {
205215 width ++
206216 }
207217 }
208218 return width
209219}
210220
211221func calcWidth (text string ) int {
212- set := []* unicode.RangeTable {unicode .Han }
222+ set := []* unicode.RangeTable {unicode .Han , unicode . Punct }
213223 width := 0
214224 for _ , r := range text {
215- if unicode .IsOneOf (set , r ) {
225+ if unicode .IsOneOf (set , r ) && r > unicode . MaxLatin1 {
216226 width += 2
217227 } else {
218228 width ++
0 commit comments