Improve format of command line output

This commit is contained in:
Kurt Jung
2022-06-23 10:34:46 -04:00
committed by Shawn Smith
parent 9eb5ac8a26
commit eacacaa1f5

View File

@@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"strings"
"github.com/gojp/goreportcard/check" "github.com/gojp/goreportcard/check"
) )
@@ -17,6 +18,19 @@ var (
jsn = flag.Bool("j", false, "JSON output. The binary will always exit with code 0") jsn = flag.Bool("j", false, "JSON output. The binary will always exit with code 0")
) )
// dotPrintf fills in the blank space between two strings with dots. The total
// length displayed is indicated by fullLen. The left string is specified by
// lfStr. The right string is formatted. At least two dots are shown, even if
// this means the total length exceeds fullLen.
func dotPrintf(fullLen int, lfStr, rtFmtStr string, args ...interface{}) {
rtStr := fmt.Sprintf(rtFmtStr, args...)
dotLen := fullLen - len(lfStr) - len(rtStr)
if dotLen < 2 {
dotLen = 2
}
fmt.Printf("%s %s %s\n", lfStr, strings.Repeat(".", dotLen), rtStr)
}
func main() { func main() {
flag.Parse() flag.Parse()
@@ -31,12 +45,12 @@ func main() {
os.Exit(0) os.Exit(0)
} }
fmt.Printf("Grade: %s (%.1f%%)\n", result.Grade, result.Average*100) dotPrintf(24, "Grade", "%s %.1f%%", result.Grade, result.Average*100)
fmt.Printf("Files: %d\n", result.Files) dotPrintf(24, "Files", "%d", result.Files)
fmt.Printf("Issues: %d\n", result.Issues) dotPrintf(24, "Issues", "%d", result.Issues)
for _, c := range result.Checks { for _, c := range result.Checks {
fmt.Printf("%s: %d%%\n", c.Name, int64(c.Percentage*100)) dotPrintf(24, c.Name, "%d%%", int64(c.Percentage*100))
if *verbose && len(c.FileSummaries) > 0 { if *verbose && len(c.FileSummaries) > 0 {
for _, f := range c.FileSummaries { for _, f := range c.FileSummaries {
fmt.Printf("\t%s\n", f.Filename) fmt.Printf("\t%s\n", f.Filename)