summaryrefslogtreecommitdiff
path: root/tablePB.go
blob: 4d42c32aca404a1594afd1a60c506493e2df1003 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// 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 HEIGHT int
	if pb.Height == 0 {
		HEIGHT = 4
	}
	// log.Info("pb.Height", pb.Height, HEIGHT)
	var args []string
	var sizes []int
	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 {
			sizes = append(sizes, 8)
		} else {
			sizes = append(sizes, int(attr.Width))
		}
	}
	// header, _ := StandardTableRowDebug(sizes, args)
	// log.Info(header)
	header, _ := StandardTableRow(sizes, args)
	log.Info(header)

	for i := range HEIGHT {
		var cells []string
		for _, col := range pb.AnyCols {
			if val, ok := getAnyCell(pb, col.Header.Name, int(i)); ok {
				cells = append(cells, val)
			}
		}
		line, fmtline := StandardTableRow(sizes, cells)
		if os.Getenv("TABLEPB_VERBOSE") == "true" {
			line += "FMT: " + fmtline
		}
		log.Info(line)
	}
}

func makeGridLabel(pb *guipb.Widget, w int, h int) {
	log.Info("MAKE GRID LABEL", w, h, pb.Name)
}

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(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]

		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
	}
	return "", false
}

func getColAttr(t *guipb.Table, name string) (string, *guipb.ColAttr, int) {
	// find the Column that matches the "name"
	for _, r := range t.AnyCols {
		if name != r.Header.Name {
			// log.Info("skip sint row:", r.Header.Name, "!=", name)
			continue
		}
		return r.Header.Name, r.Attr, len(r.Vals)
	}
	return "", nil, 0
}