Commit 596eea80 authored by Eric's avatar Eric

[Update] modify pagination

parent 23def7bc
...@@ -14,6 +14,7 @@ const ( ...@@ -14,6 +14,7 @@ const (
) )
type WrapperTable struct { type WrapperTable struct {
Labels []string
Fields []string Fields []string
FieldsSize map[string][3]int // 列宽,列最小宽,列最大宽 FieldsSize map[string][3]int // 列宽,列最小宽,列最大宽
Data []map[string]string Data []map[string]string
...@@ -30,10 +31,16 @@ type WrapperTable struct { ...@@ -30,10 +31,16 @@ type WrapperTable struct {
func (t *WrapperTable) Initial() { func (t *WrapperTable) Initial() {
// 如果设置的宽度小于title的size, 重写 // 如果设置的宽度小于title的size, 重写
if t.Labels == nil {
t.Labels = t.Fields
}
if len(t.Fields) != len(t.Labels) {
panic("Labels should be equal with Fields")
}
t.paddingSize = 1 t.paddingSize = 1
t.bolderSize = 1 t.bolderSize = 1
for _, k := range t.Fields { for i, k := range t.Fields {
titleSize := len(k) titleSize := len(t.Labels[i])
sizeDefine := t.FieldsSize[k] sizeDefine := t.FieldsSize[k]
if titleSize > sizeDefine[1] { if titleSize > sizeDefine[1] {
sizeDefine[1] = titleSize sizeDefine[1] = titleSize
...@@ -121,7 +128,12 @@ func (t *WrapperTable) CalculateColumnsSize() { ...@@ -121,7 +128,12 @@ func (t *WrapperTable) CalculateColumnsSize() {
for _, k := range canChangeCols { for _, k := range canChangeCols {
t.fieldsSize[k] += step t.fieldsSize[k] += step
delta-- delta--
if delta == 0 {
break
}
fmt.Println(t.fieldsSize)
} }
fmt.Println(canChangeCols)
} }
} }
...@@ -130,7 +142,21 @@ func (t *WrapperTable) convertDataToSlice() [][]string { ...@@ -130,7 +142,21 @@ func (t *WrapperTable) convertDataToSlice() [][]string {
for i, j := range t.Data { for i, j := range t.Data {
row := make([]string, len(t.Fields)) row := make([]string, len(t.Fields))
for m, n := range t.Fields { for m, n := range t.Fields {
row[m] = j[n] columSize := t.fieldsSize[n]
if len(j[n]) <= columSize {
row[m] = j[n]
} else {
switch t.TruncPolicy {
case TruncSuffix:
row[m] = j[n][:columSize-3] + "..."
case TruncPrefix:
row[m] = "..." + j[n][len(j[n])-columSize-3:]
case TruncMiddle:
midValue := (columSize - 3) / 2
row[m] = j[n][:midValue] + "..." + j[n][len(j[n])-midValue:]
}
}
} }
data[i] = row data[i] = row
} }
...@@ -144,7 +170,7 @@ func (t *WrapperTable) Display() string { ...@@ -144,7 +170,7 @@ func (t *WrapperTable) Display() string {
tableString := &strings.Builder{} tableString := &strings.Builder{}
table := tablewriter.NewWriter(tableString) table := tablewriter.NewWriter(tableString)
table.SetBorder(false) table.SetBorder(false)
table.SetHeader(t.Fields) table.SetHeader(t.Labels)
colors := make([]tablewriter.Colors, len(t.Fields)) colors := make([]tablewriter.Colors, len(t.Fields))
for i := 0; i < len(t.Fields); i++ { for i := 0; i < len(t.Fields); i++ {
colors[i] = tablewriter.Colors{tablewriter.Bold, tablewriter.FgGreenColor} colors[i] = tablewriter.Colors{tablewriter.Bold, tablewriter.FgGreenColor}
......
...@@ -130,15 +130,23 @@ func (p *AssetPagination) Start() []model.Asset { ...@@ -130,15 +130,23 @@ func (p *AssetPagination) Start() []model.Asset {
} }
func (p *AssetPagination) displayPageAssets() { func (p *AssetPagination) displayPageAssets() {
fields := []string{"ID", "主机名", "IP", "系统用户", "Comment"} Labels := []string{i18n.T("ID"), i18n.T("主机名"), i18n.T("IP"), i18n.T("系统用户"), i18n.T("Comment")}
fields := []string{"ID", "hostname", "IP", "systemUsers", "comment"}
data := make([]map[string]string, len(p.currentData)) data := make([]map[string]string, len(p.currentData))
for i, j := range p.currentData { for i, j := range p.currentData {
row := make(map[string]string) row := make(map[string]string)
row["ID"] = strconv.Itoa(i + 1) row["ID"] = strconv.Itoa(i + 1)
row["主机名"] = j.Hostname row["hostname"] = j.Hostname
row["IP"] = j.Ip row["IP"] = j.Ip
row["系统用户"] = ""
row["Comment"] = "你好" systemUser := selectHighestPrioritySystemUsers(j.SystemUsers)
names := make([]string, len(systemUser))
for i := range systemUser {
names[i] = systemUser[i].Name
}
row["systemUsers"] = strings.Join(names, ",")
fmt.Println(row["系统用户"], len(row["系统用户"]))
row["comment"] = j.Comment
data[i] = row data[i] = row
} }
w, _ := p.term.GetSize() w, _ := p.term.GetSize()
...@@ -148,16 +156,18 @@ func (p *AssetPagination) displayPageAssets() { ...@@ -148,16 +156,18 @@ func (p *AssetPagination) displayPageAssets() {
caption = utils.WrapperString(caption, utils.Green) caption = utils.WrapperString(caption, utils.Green)
table := common.WrapperTable{ table := common.WrapperTable{
Fields: fields, Fields: fields,
Labels: Labels,
FieldsSize: map[string][3]int{ FieldsSize: map[string][3]int{
"ID": {0, 0, 5}, "ID": {0, 0, 0},
"主机名": {0, 8, 25}, "hostname": {0, 8, 0},
"IP": {15, 0, 0}, "IP": {15, 0, 0},
"系统用户": {0, 12, 20}, "systemUsers": {0, 12, 0},
"Comment": {0, 0, 0}, "comment": {0, 0, 0},
}, },
Data: data, Data: data,
TotalSize: w, TotalSize: w,
Caption: caption, Caption: caption,
TruncPolicy: common.TruncMiddle,
} }
table.Initial() table.Initial()
......
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