// Copyright 2025 WIT.COM Inc Licensed GPL 3.0 package cobol import ( "fmt" "os" "time" "go.wit.com/lib/gui/shell" "go.wit.com/lib/protobuf/guipb" "go.wit.com/log" "google.golang.org/protobuf/types/known/anypb" "google.golang.org/protobuf/types/known/timestamppb" "google.golang.org/protobuf/types/known/wrapperspb" ) func PrintTable(pb *guipb.Table) { // log.Info("print PB here") if pb.Grid == nil { // log.Info("grid = nil") } else { // log.Info("grid.Id =", pb.Grid.Id) } if cursize, ok := getTerminalWidth(); ok { TERMSIZE = cursize } var args []string var sizes []int log.Info("INFO: table len=", len(pb.AnyCols)) // first print the table header for _, col := range pb.AnyCols { args = append(args, col.Header.Name) if col.Attr.Width == 0 { sizes = append(sizes, 8) } else { sizes = append(sizes, int(col.Attr.Width)) } } // header, _ := StandardTableRowDebug(sizes, args) // log.Info(header) header, _ := StandardTableRow(sizes, args) log.Info(header) // 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(col, i); ok { cells = append(cells, val) } else { log.Info("cobol TABLE CELL FAILED", col.Header.Name, i, val, ok) } } line, fmtline := StandardTableRow(sizes, cells) if os.Getenv("TABLEPB_VERBOSE") == "true" { line += "FMT: " + fmtline } log.Info(line) } } func extractInt64(anyVal *anypb.Any) (int64, error) { val := &wrapperspb.Int64Value{} if err := anyVal.UnmarshalTo(val); err != nil { return 0, err } return val.Value, nil } 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() } } 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) } // cellTime := r.Vals[row] // s := shell.FormatDuration(time.Since(cellTime.AsTime())) return "fixme", true }