1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  | 
// Copyright 2025 WIT.COM Inc Licensed GPL 3.0
package cobol
import (
	"fmt"
	"os"
	"go.wit.com/lib/protobuf/guipb"
	"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) {
	PrintTableLimit(pb, -1)
}
// limits the number of out lines
func PrintTableLimit(pb *guipb.Table, limit int) {
	// fmt.Info("print PB here")
	if pb.Grid == nil {
		// fmt.Info("grid = nil")
	} else {
		// fmt.Info("grid.Id =", pb.Grid.Id)
	}
	if cursize, ok := getTerminalWidth(); ok {
		TERMSIZE = cursize
	}
	var args []string
	var sizes []int
	// fmt.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)
	// fmt.Info(header)
	header, _ := StandardTableRow(sizes, args)
	fmt.Println(header)
	var counter int
	// now print the table rows
	for i := range len(pb.AnyCols[0].Vals) {
		var cells []string
		for _, col := range pb.AnyCols {
			val, ok := getAnyCell(col, i)
			cells = append(cells, val)
			if !ok {
				// probably remove this
				fmt.Println("cobol TABLE CELL FAILED", col.Header.Name, i, val, ok)
			}
		}
		line, fmtline := StandardTableRow(sizes, cells)
		if os.Getenv("TABLEPB_VERBOSE") == "true" {
			line += "FMT: " + fmtline
		}
		fmt.Println(line)
		if limit < 0 {
			continue
		}
		counter += 1
		if counter > limit {
			break
		}
	}
}
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_TIME:
		return Time(anyVal), true
	case guipb.ColAttr_DURATION:
		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 = Since(goTime)
			return sout, true
		}
		return "", false
	default:
		fmt.Println("cell unhandled type", col.Attr.Type)
	}
	// cellTime := r.Vals[row]
	// s := FormatDuration(time.Since(cellTime.AsTime()))
	return "fixme", true
}
  |