Display fields with more than 1 value (p.e ip4_addr), line separator between jails
This commit is contained in:
85
cmd/list.go
85
cmd/list.go
@ -84,7 +84,13 @@ func displayStructFields(jails []Jail, valsToDisplay []string) {
|
||||
}
|
||||
if a.FieldByName(f).IsValid() {
|
||||
// For now this just contains this item length, will adjust later
|
||||
field.MaxLen = len(fmt.Sprintf("%v", a.FieldByName(f).Interface()))
|
||||
// We need to divide length by number of items in string fields (because we can have more than 1 ip4_addr)
|
||||
if reflect.TypeOf(a.FieldByName(f).Interface()).Kind() == reflect.String {
|
||||
itnr := len(strings.Split(string(a.FieldByName(f).Interface().(string)), ","))
|
||||
field.MaxLen = len(fmt.Sprintf("%v", a.FieldByName(f).Interface())) / itnr
|
||||
} else {
|
||||
field.MaxLen = len(fmt.Sprintf("%v", a.FieldByName(f).Interface()))
|
||||
}
|
||||
field.Value = fmt.Sprintf("%v", a.FieldByName(f).Interface())
|
||||
} else {
|
||||
fmt.Printf("Invalid field name: %s\n", f)
|
||||
@ -132,12 +138,16 @@ func displayStructFields(jails []Jail, valsToDisplay []string) {
|
||||
}
|
||||
|
||||
// Lets draw things on the screen!
|
||||
// First, headers
|
||||
for i := 0; i < totalLen ; i++ {
|
||||
fmt.Printf("-")
|
||||
// First, headers: 1st separator line
|
||||
for i, f := range out[0] {
|
||||
if i == 0 { fmt.Printf("+") }
|
||||
for i := 0 ; i < f.MaxLen+2 ; i++ { fmt.Printf("=") }
|
||||
fmt.Printf("+")
|
||||
}
|
||||
fmt.Printf("\n")
|
||||
|
||||
|
||||
// Column names
|
||||
for i, f := range out[0] {
|
||||
if i == 0 {
|
||||
fmt.Printf("|")
|
||||
@ -151,14 +161,21 @@ func displayStructFields(jails []Jail, valsToDisplay []string) {
|
||||
}
|
||||
fmt.Printf(" |")
|
||||
}
|
||||
// Finally separator line
|
||||
fmt.Printf("\n")
|
||||
for i := 0; i < totalLen ; i++ {
|
||||
fmt.Printf("-")
|
||||
for i, f := range out[0] {
|
||||
if i == 0 { fmt.Printf("+") }
|
||||
for i := 0 ; i < f.MaxLen+2 ; i++ { fmt.Printf("=") }
|
||||
fmt.Printf("+")
|
||||
}
|
||||
fmt.Printf("\n")
|
||||
|
||||
// Then display data
|
||||
// Loop through lines
|
||||
for _, l := range out {
|
||||
// Loop through fields
|
||||
// In case we need to add a line for a 2nd IP, or whatever object
|
||||
var supplines = make(map[string]string)
|
||||
for i, f := range l {
|
||||
if i == 0 {
|
||||
fmt.Printf("|")
|
||||
@ -166,22 +183,68 @@ func displayStructFields(jails []Jail, valsToDisplay []string) {
|
||||
// Special cases of value displaying
|
||||
if f.Name == "JID" && f.Value == "0" {
|
||||
fmt.Printf(" ")
|
||||
/* } else if f.Name == "Ip4_addr" {
|
||||
fmt.Printf(" %s", strings.Split(strings.Split(f.Value, "|")[1], "/")[0])
|
||||
*/
|
||||
} else if f.Name == "Ip4_addr" {
|
||||
ia := strings.Split(f.Value, ",")
|
||||
// If we have more than 1 value we need to finish this line, and store value for writing at the end of line loop
|
||||
for i, inter := range ia {
|
||||
if i > 0 {
|
||||
supplines[f.Name] = inter
|
||||
} else {
|
||||
fmt.Printf(" %s", inter)
|
||||
}
|
||||
}
|
||||
//fmt.Printf(" %s", strings.Split(strings.Split(f.Value, "|")[1], "/")[0])
|
||||
} else {
|
||||
fmt.Printf(" %s", f.Value)
|
||||
}
|
||||
// Complete with spaces to the max length
|
||||
for i := len(f.Value)+1 ; i < f.MaxLen+1 ; i++ {
|
||||
fmt.Printf(" ")
|
||||
}
|
||||
fmt.Printf(" |")
|
||||
}
|
||||
// Draw supplementary lines
|
||||
if len(supplines) > 0 {
|
||||
for i, f := range l {
|
||||
if i == 0 {
|
||||
fmt.Printf("\n|")
|
||||
}
|
||||
// Overwrite value in this scope
|
||||
v, exists := supplines[f.Name]
|
||||
if exists {
|
||||
fmt.Printf(" %s", v)
|
||||
} else {
|
||||
// 1st option : Do not redisplay precedent line values
|
||||
fmt.Printf(" ")
|
||||
// 2nd option : Redisplay precedenet line values
|
||||
//fmt.Printf(" %s", f.Value)
|
||||
}
|
||||
// Complete with spaces to the max length
|
||||
for i := len(v)+1 ; i < f.MaxLen+1 ; i++ {
|
||||
fmt.Printf(" ")
|
||||
}
|
||||
fmt.Printf(" |")
|
||||
}
|
||||
}
|
||||
// Draw line separator between jails
|
||||
if !gNoLineSep {
|
||||
fmt.Printf("\n")
|
||||
for i, f := range out[0] {
|
||||
if i == 0 { fmt.Printf("+") }
|
||||
for i := 0 ; i < f.MaxLen+2 ; i++ { fmt.Printf("-") }
|
||||
fmt.Printf("+")
|
||||
}
|
||||
}
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
for i := 0; i < totalLen ; i++ {
|
||||
fmt.Printf("-")
|
||||
if gNoLineSep {
|
||||
for i, f := range out[0] {
|
||||
if i == 0 { fmt.Printf("+") }
|
||||
for i := 0 ; i < f.MaxLen+2 ; i++ { fmt.Printf("-") }
|
||||
fmt.Printf("+")
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user