Commit 896e1d61 authored by Eric's avatar Eric Committed by Eric_Lee

fix bugs: 截断乱码问题

parent dff669eb
package common
import (
"fmt"
"strings"
"unicode/utf8"
"github.com/olekukonko/tablewriter"
)
......@@ -145,12 +147,15 @@ func (t *WrapperTable) convertDataToSlice() [][]string {
} else {
switch t.TruncPolicy {
case TruncSuffix:
row[m] = j[n][:columSize-3] + "..."
row[m] = fmt.Sprintf("%s...",
GetValidString(j[n],columSize-3, true))
case TruncPrefix:
row[m] = "..." + j[n][len(j[n])-columSize-3:]
row[m] = fmt.Sprintf("...%s",
GetValidString(j[n],len(j[n])-columSize-3, false))
case TruncMiddle:
midValue := (columSize - 3) / 2
row[m] = j[n][:midValue] + "..." + j[n][len(j[n])-midValue:]
row[m] = fmt.Sprintf("%s...%s",GetValidString(j[n],midValue, true),
GetValidString(j[n],len(j[n])-midValue, false))
}
}
......@@ -187,3 +192,24 @@ func (t *WrapperTable) Display() string {
table.Render()
return tableString.String()
}
func GetValidString(s string, position int, positive bool) string{
step := 1
if positive {
step = -1
}
for position >=0 && position <= len(s) {
switch positive {
case true:
if utf8.ValidString(s[:position]){
return s[:position]
}
case false:
if utf8.ValidString(s[position:]){
return s[position:]
}
}
position += step
}
return ""
}
\ No newline at end of file
......@@ -29,14 +29,11 @@ func TestNewTable_CalculateColumnsSize(t *testing.T) {
data := table.Display()
fmt.Println(data)
fmt.Println(table.fieldsSize)
//if table.fieldsSize["comment"] != 6 {
// t.Error("comment需要为6")
//}
//
//table.TotalSize = 188
//table.CalculateColumnsSize()
//if table.fieldsSize["comment"] != 136 {
// t.Error("comment长度需要为136")
//}
//fmt.Println(table.fieldsSize)
}
func TestGetCorrectString(t *testing.T) {
foo := "主2erert机名"
a:=GetValidString(foo,2,false)
t.Log(a == "2erert机名")
}
\ No newline at end of file
......@@ -168,7 +168,7 @@ func (p *AssetPagination) displayPageAssets() {
"hostname": {0, 8, 0},
"IP": {0, 15, 40},
"systemUsers": {0, 12, 0},
"comment": {0, 0, 40},
"comment": {0, 0, 0},
},
Data: data,
TotalSize: w,
......
......@@ -120,6 +120,8 @@ func (h *interactiveHandler) watchWinSizeChange() {
winChan := sessChan
for {
select {
case <-h.sess.Context().Done():
return
case sig, ok := <-h.winWatchChan:
if !ok {
return
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment