diff --git a/cmd/list.go b/cmd/list.go index e7d1b31..c7c2532 100644 --- a/cmd/list.go +++ b/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") } diff --git a/cmd/root.go b/cmd/root.go index 6cd6629..7f43557 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -20,6 +20,7 @@ var ( gConfigFile string gDisplayColumns string + gNoLineSep bool rootCmd = & cobra.Command{ @@ -76,6 +77,7 @@ func init() { // Command dependant switches listCmd.PersistentFlags().StringVarP(&gDisplayColumns, "outcol", "o", "JID,Name,Config.Release,Config.Ip4_addr,Running", "Show these columns in output") + listCmd.PersistentFlags().BoolVarP(&gNoLineSep, "nolinesep", "l", false, "Do not display line separator between jails") /* searchComputerCmd.PersistentFlags().BoolVarP(&gShowAccount, "show-account", "c", false, "Show account when listing computer") searchComputerCmd.PersistentFlags().BoolVarP(&gDisplayAsCSV, "csv-format", "v", false, "Show results in CSV (separator = ';')") */ @@ -112,6 +114,9 @@ func initConfig() { if false == listCmd.Flags().Lookup("outcol").Changed { gDisplayColumns = viper.GetString("outcol") } + if false == listCmd.Flags().Lookup("nolinesep").Changed { + gNoLineSep = viper.GetBool("nolinesep") + } } func Execute() {