diff options
| -rw-r--r-- | tablePB.go | 110 |
1 files changed, 48 insertions, 62 deletions
@@ -27,24 +27,16 @@ func PrintTable(pb *guipb.Table) { TERMSIZE = cursize } - var HEIGHT int - if pb.Height == 0 { - HEIGHT = 4 - } - // log.Info("pb.Height", pb.Height, HEIGHT) var args []string var sizes []int + + // first print the table header for _, col := range pb.AnyCols { - arg, attr, tmp := getColAttr(pb, col.Header.Name) - if attr == nil { - continue - } - HEIGHT = tmp - args = append(args, arg) - if attr.Width == 0 { + args = append(args, col.Header.Name) + if col.Attr.Width == 0 { sizes = append(sizes, 8) } else { - sizes = append(sizes, int(attr.Width)) + sizes = append(sizes, int(col.Attr.Width)) } } // header, _ := StandardTableRowDebug(sizes, args) @@ -52,10 +44,11 @@ func PrintTable(pb *guipb.Table) { header, _ := StandardTableRow(sizes, args) log.Info(header) - for i := range HEIGHT { + // now print the table rows + for i := range len(pb.AnyCols[0].Vals) { var cells []string for _, col := range pb.AnyCols { - if val, ok := getAnyCell(pb, col.Header.Name, int(i)); ok { + if val, ok := getAnyCell(col, i); ok { cells = append(cells, val) } } @@ -79,58 +72,51 @@ func extractInt64(anyVal *anypb.Any) (int64, error) { return val.Value, nil } -func getAnyCell(t *guipb.Table, name string, row int) (string, bool) { - for _, col := range t.AnyCols { - if name != col.Header.Name { - // log.Info("skip sint row:", r.Header.Name, "!=", name) - continue - } - anyVal := col.Vals[row] +func getAnyCell(col *guipb.AnyCol, row int) (string, bool) { + anyVal := col.Vals[row] - switch col.Attr.Type { - case guipb.ColAttr_STRING: - // return col.Vals[row] true - // Assume 'anyVal' is an element from your r.Vals slice - var stringValue wrapperspb.StringValue - if err := anyVal.UnmarshalTo(&stringValue); err == nil { - // It's a string, now convert it back to a native Go string - goString := stringValue.GetValue() - // fmt.Printf("Successfully unpacked string: %s\n", goString) - return goString, true - } - case guipb.ColAttr_INT: - var finalInt int32 - // 1. Check if the Any contains an Int32Value - if anyVal.MessageIs(&wrapperspb.Int32Value{}) { - var intValue wrapperspb.Int32Value - // 2. Unmarshal into the wrapper - if err := anyVal.UnmarshalTo(&intValue); err == nil { - // 3. Get the native Go int32 from the wrapper - finalInt = intValue.GetValue() - } + switch col.Attr.Type { + case guipb.ColAttr_STRING: + // return col.Vals[row] true + // Assume 'anyVal' is an element from your r.Vals slice + var stringValue wrapperspb.StringValue + if err := anyVal.UnmarshalTo(&stringValue); err == nil { + // It's a string, now convert it back to a native Go string + goString := stringValue.GetValue() + // fmt.Printf("Successfully unpacked string: %s\n", goString) + return goString, true + } + case guipb.ColAttr_INT: + var finalInt int32 + // 1. Check if the Any contains an Int32Value + if anyVal.MessageIs(&wrapperspb.Int32Value{}) { + var intValue wrapperspb.Int32Value + // 2. Unmarshal into the wrapper + if err := anyVal.UnmarshalTo(&intValue); err == nil { + // 3. Get the native Go int32 from the wrapper + finalInt = intValue.GetValue() } + } - return fmt.Sprintf("%d", finalInt), true - case guipb.ColAttr_DURATION: - case guipb.ColAttr_TIME: - var sout string - var tsProto timestamppb.Timestamp - if err := anyVal.UnmarshalTo(&tsProto); err == nil { - // It's a timestamp, now convert it back to a Go time.Time - goTime := tsProto.AsTime() - // fmt.Printf("Successfully unpacked timestamp: %v\n", goTime) - sout = shell.FormatDuration(time.Since(goTime)) - return sout, true - } - return "", false - default: - log.Info("cell unhandled type", col.Attr.Type) + return fmt.Sprintf("%d", finalInt), true + case guipb.ColAttr_DURATION: + case guipb.ColAttr_TIME: + var sout string + var tsProto timestamppb.Timestamp + if err := anyVal.UnmarshalTo(&tsProto); err == nil { + // It's a timestamp, now convert it back to a Go time.Time + goTime := tsProto.AsTime() + // fmt.Printf("Successfully unpacked timestamp: %v\n", goTime) + sout = shell.FormatDuration(time.Since(goTime)) + return sout, true } - // cellTime := r.Vals[row] - // s := shell.FormatDuration(time.Since(cellTime.AsTime())) - return "fixme", true + return "", false + default: + log.Info("cell unhandled type", col.Attr.Type) } - return "", false + // cellTime := r.Vals[row] + // s := shell.FormatDuration(time.Since(cellTime.AsTime())) + return "fixme", true } func getColAttr(t *guipb.Table, name string) (string, *guipb.ColAttr, int) { |
