summaryrefslogtreecommitdiff
path: root/argv.Print.go
blob: 68c2391e110839bda44ab449fe1122dd65e6b925 (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
package argvpb

// debug argv protobuf functions
// enable with --autodebug or Setenv( "AUTOCOMPLETE_VERBOSE" )

import (
	"fmt"
	"os"
	"strings"

	"go.wit.com/lib/cobol"
)

func (pb *Argv) Debugf(fmts string, parts ...any) {
	Debugf(fmts, parts...)
}

// decides here to print to STDERR or not
func Debugf(fmts string, parts ...any) {
	fmts = strings.TrimSpace(fmts)
	fmts += "\n"
	// me.pb.Stderr += fmt.Sprintf(fmts, parts...)
	fmt.Fprintf(Stddbg, fmts, parts...)
}

// print out auto complete debugging info
func (pb *Argv) PrintDebug(last string) {
	pb.PrintDebugNew("ARGV", me.last)
}

func (pb *Argv) PrintDebugNew(msg string, last *Argv) {
	// var arglast string
	// arglast = fmt.Sprintf("last='%s'", last.GetCmd())
	partial := fmt.Sprintf("p='%s'", pb.Partial)
	age := cobol.Duration(pb.Ctime)
	pdur := cobol.Duration(pb.Duration)
	argvdur := cobol.Duration(pb.ArgvDuration)

	var dbg string
	if me.debug {
		dbg = "dbg=1"
	} else {
		dbg = "dbg=0"
	}
	lens := fmt.Sprintf("len=%-2d,%-2d,%-2d", me.all.Len(), len(strings.Fields(pb.Stdout)), len(strings.Split(pb.Stderr, "\n")))
	fast := fmt.Sprintf("F%-2dout%-2derr%-2d %s", pb.Fast, pb.OutCounter, pb.ErrCounter, lens)
	top := fmt.Sprintf("%-4.4s age=(%s)(%s)dur(%s) %s %s cmd=%-12.12s", pb.Uuid, age, pdur, argvdur, dbg, fast, pb.GetCmd())
	Debugf("%s: %s %-12.12s real='%v'", msg, top, partial, pb.Real)
}

func (all *Argvs) PrintHistory(msg string) {
	counter := 0
	var last *Argv
	// for pb := range all.Argv[0:10] {
	for pb := range all.IterAll() {
		counter += 1
		pb.PrintDebugNew(msg, last)
		last = pb
		if counter > 30 {
			// sometimes I'm dumb
			break
		}
	}
}

func PrintStddbg() {
	if !me.debug {
		return
	}
	f, err := os.OpenFile("/tmp/argv.debug", os.O_APPEND|os.O_RDWR|os.O_CREATE, 0644)
	if err != nil {
		panic("WTF openfile")
	}
	defer f.Close()
	lines := strings.Split(strings.TrimSpace(PB.Stddbg), "\n")
	if len(lines) == 0 {
		return
	}
	fmt.Fprintf(f, "\n")
	for _, line := range lines {
		fmt.Fprintf(f, "%s\n", line)
	}
	fmt.Fprintf(f, "\n")
}

func PrintStderr() {
	var lines []string
	for _, line := range strings.Split(strings.TrimSpace(PB.Stderr), "\n") {
		if strings.TrimSpace(line) == "" {
			continue
		}
		lines = append(lines, line)
	}
	if len(lines) == 0 {
		return
	}
	fmt.Fprintf(os.Stderr, "\n")
	for _, line := range lines {
		// line = strings.TrimSpace(line)
		// if line == "" {
		// 	continue
		// }
		fmt.Fprintf(os.Stderr, "%s\n", line)
	}
	fmt.Fprintf(os.Stderr, "\n")
}

func PrintStdout() {
	line := strings.TrimSpace(PB.Stdout)
	if line == "" {
		return
	}
	fmt.Fprintf(os.Stdout, " %s\n", line)
}