summaryrefslogtreecommitdiff
path: root/argv.Print.go
blob: 2d7ea80ae03ac0f69963e5222110939150fb505b (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
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(Stderr, fmts, parts...)
}

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

func (pb *Argv) PrintDebugNew(msg string, last string) {
	cmd := fmt.Sprintf("cmd='%s'", pb.GetCmd())
	var arglast string
	arglast = fmt.Sprintf("last='%s'", last)
	partial := fmt.Sprintf("p='%s'", pb.Partial)
	age := cobol.Duration(pb.Ctime)
	dur := cobol.Duration(pb.Duration)
	var fast string
	if pb.Fast {
		fast = "fast=1," + pb.GetCmd()
	} else {
		fast = "fast=0," + pb.GetCmd()
	}

	var dbg string
	if me.debug {
		dbg = "dbg=1"
	} else {
		dbg = "dbg=0"
	}
	sargv := fmt.Sprintf("argv(%v)", pb.Real)
	top := fmt.Sprintf("%-4.4s age=(%s)dur(%s) %s h%2d %-12.12s %-12.12s", pb.Uuid, age, dur, dbg, pb.HelpCounter, cmd, arglast)
	Debugf("%s: %s %-12.12s %s %s real='%v' len(%d)", msg, top, partial, fast, sargv, pb.Real, me.all.Len())
}

func (all *Argvs) PrintHistory(last string) {
	counter := 0
	for pb := range all.IterAll() {
		counter += 1
		// hist := fmt.Sprintf("HIST(%d)", counter)
		hist := fmt.Sprintf("HIST")
		pb.PrintDebugNew(hist, last)
		if counter > 100 {
			// sometimes I'm dumb
			break
		}
	}
}

func PrintStderr() {
	if !me.debug {
		return
	}
	lines := strings.Split(strings.TrimSpace(PB.Stderr), "\n")
	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)
}