Fix up the summary to log as a table
This commit is contained in:
71
main.go
71
main.go
@@ -8,7 +8,9 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
|
||||
utils "git.site.quack-lab.dev/dave/cyutils"
|
||||
@@ -185,14 +187,77 @@ func processInstructions(instructions chan *LinkInstruction) int32 {
|
||||
}
|
||||
|
||||
func printSummary(fs FileSystem) {
|
||||
lines := fs.SummaryLines()
|
||||
if len(lines) == 0 {
|
||||
records := fs.SummaryRecords()
|
||||
if len(records) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
header := []string{"RESULT", "OPERATION", "SOURCE", "TARGET", "DETAIL"}
|
||||
widths := make([]int, len(header))
|
||||
for i, h := range header {
|
||||
widths[i] = len(h)
|
||||
}
|
||||
|
||||
type row struct {
|
||||
values []string
|
||||
err error
|
||||
}
|
||||
rows := make([]row, len(records))
|
||||
|
||||
for i, record := range records {
|
||||
values := []string{
|
||||
record.resultLabel(),
|
||||
record.kindLabel(),
|
||||
FormatSourcePath(record.source),
|
||||
FormatTargetPath(record.target),
|
||||
record.detail(),
|
||||
}
|
||||
rows[i] = row{values: values, err: record.err}
|
||||
|
||||
for j, val := range values {
|
||||
if l := visibleLength(val); l > widths[j] {
|
||||
widths[j] = l
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LogInfo("Summary:")
|
||||
for _, line := range lines {
|
||||
var lineBuilder strings.Builder
|
||||
|
||||
writeSummaryRow(&lineBuilder, header, widths)
|
||||
for _, line := range strings.Split(strings.TrimRight(lineBuilder.String(), "\n"), "\n") {
|
||||
LogInfo("%s", line)
|
||||
}
|
||||
|
||||
for _, r := range rows {
|
||||
lineBuilder.Reset()
|
||||
writeSummaryRow(&lineBuilder, r.values, widths)
|
||||
line := strings.TrimRight(lineBuilder.String(), "\n")
|
||||
LogInfo("%s", line)
|
||||
}
|
||||
}
|
||||
|
||||
func writeSummaryRow(b *strings.Builder, cols []string, widths []int) {
|
||||
for i, val := range cols {
|
||||
if val == "" {
|
||||
val = "-"
|
||||
}
|
||||
b.WriteString(val)
|
||||
pad := widths[i] - visibleLength(val)
|
||||
if pad < 0 {
|
||||
pad = 0
|
||||
}
|
||||
if i < len(cols)-1 {
|
||||
b.WriteString(strings.Repeat(" ", pad+2))
|
||||
}
|
||||
}
|
||||
b.WriteByte('\n')
|
||||
}
|
||||
|
||||
var ansiRegexp = regexp.MustCompile(`\x1b\[[0-9;]*m`)
|
||||
|
||||
func visibleLength(s string) int {
|
||||
return len(ansiRegexp.ReplaceAllString(s, ""))
|
||||
}
|
||||
|
||||
func IsPipeInput() bool {
|
||||
|
||||
Reference in New Issue
Block a user