summaryrefslogtreecommitdiff
path: root/tablePB.go
blob: c30baf6b159ed8f4d8255e73037d4186e0b393df (plain)
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
// 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

	// 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
}