From 587ced65b88359c03cda314077db9efa3f9935e9 Mon Sep 17 00:00:00 2001 From: Adithya Joshua Dsilva Date: Sun, 13 Dec 2020 17:32:27 +0530 Subject: [PATCH] Improve table formatting. Update test verdict to: - Print box around each verdict (asthetics) - Print input, expected and output in one table. - Minor formatting fixes. Update submit table to: - Revert back to previous table format. - Improved formatting. Remove obselete function (util.go) --- cmd/codeforces/submit/submit.go | 10 +++++---- cmd/test/modes.go | 36 ++++++++++++++++----------------- cmd/test/test.go | 5 ++--- util/util.go | 9 --------- 4 files changed, 26 insertions(+), 34 deletions(-) diff --git a/cmd/codeforces/submit/submit.go b/cmd/codeforces/submit/submit.go index ec7b70f..e9ed356 100644 --- a/cmd/codeforces/submit/submit.go +++ b/cmd/codeforces/submit/submit.go @@ -32,20 +32,22 @@ func Submit(arg codeforces.Args, filePath string, cnf *conf.Conf) { t := table.NewWriter() t.SetStyle(table.StyleLight) t.Style().Options.DrawBorder = false - t.Style().Box.PaddingRight = "\t" + t.Style().Box.PaddingLeft = "" + t.Style().Box.MiddleVertical = "\t" // Run live verdict till judging completed. writer := uilive.New() writer.Start() + headerColor := color.New(color.FgBlue, color.Bold).SprintFunc() for sub := range submission { t.ResetRows() - t.AppendRow(table.Row{color.BlueString("Verdict:"), list.ColorVerdict(sub)}) + t.AppendRow(table.Row{headerColor("Verdict:"), list.ColorVerdict(sub)}) if sub.IsJudging == false { // Judging done; add resource data. - t.AppendRow(table.Row{color.BlueString("Memory:"), sub.Memory}) - t.AppendRow(table.Row{color.BlueString("Time:"), sub.Time}) + t.AppendRow(table.Row{headerColor("Memory:"), sub.Memory}) + t.AppendRow(table.Row{headerColor("Time:"), sub.Time}) } fmt.Fprintln(writer, t.Render()) diff --git a/cmd/test/modes.go b/cmd/test/modes.go index de5ee28..86c201a 100644 --- a/cmd/test/modes.go +++ b/cmd/test/modes.go @@ -28,7 +28,6 @@ func judgeMode(runScript, checkerTmplt string, timelimit time.Duration, FailLog error Stderr string CheckerLog string - Input string Compare string } @@ -55,18 +54,18 @@ func judgeMode(runScript, checkerTmplt string, timelimit time.Duration, "{{- if .Stderr}}\n" + c("Stderr:") + "\n{{.Stderr}}" + "{{end}}", // Checker Log: Wrong answer, expected 3, found 4. "{{- if .CheckerLog}}\n" + c("Checker Log:") + " {{.CheckerLog}}" + "{{end}}", - // Input: - // 5 3 - // 1 2 3 4 5 - // - // OUTPUT | EXPECTED - // 4 | 3 - // 1 | 1 - "{{- if .Compare}}\n" + c("Input:") + "\n{{.Input}}" + "\n{{.Compare}}" + "{{end}}", + // INPUT | EXPECTED | OUTPUT + // 5 3 | 3 | 4 + // 1 2 3 4 5 | 1 | 1 + "{{- if .Compare}}\n" + "{{.Compare}}" + "{{end}}", }, "\n"), verdictData) - fmt.Println() - fmt.Println(strings.TrimSpace(out)) + // Print box around verdict + t := table.NewWriter() + t.SetStyle(table.StyleBold) + t.AppendRow(table.Row{strings.TrimSpace(out)}) + + fmt.Println(t.Render()) }() // Read input from file. @@ -136,6 +135,7 @@ func judgeMode(runScript, checkerTmplt string, timelimit time.Duration, if n, _ := input.Read(inputBuf); n == len(inputBuf) { inputBuf = append(inputBuf[:n-3], []byte("...")...) } + inputBuf = bytes.Trim(inputBuf, "\x00") // Read expected from file. expected, err := os.Open(expectedFile) @@ -148,6 +148,7 @@ func judgeMode(runScript, checkerTmplt string, timelimit time.Duration, if n, _ := expected.Read(expectedBuf); n == len(expectedBuf) { expectedBuf = append(expectedBuf[:n-3], []byte("...")...) } + expectedBuf = bytes.Trim(expectedBuf, "\x00") // Read output from created output file. output, err := os.Open(outputFile.Name()) @@ -160,24 +161,24 @@ func judgeMode(runScript, checkerTmplt string, timelimit time.Duration, if n, _ := output.Read(outputBuf); n == len(outputBuf) { outputBuf = append(outputBuf[:n-3], []byte("...")...) } + outputBuf = bytes.Trim(outputBuf, "\x00") // Table to display output difference. t := table.NewWriter() t.SetStyle(table.StyleLight) t.Style().Options.DrawBorder = false - t.Style().Box.PaddingRight = "\t" headerColor := text.Colors{text.FgBlue, text.Bold} t.SetColumnConfigs([]table.ColumnConfig{ - {Number: 1, AlignHeader: text.AlignCenter, ColorsHeader: headerColor, Align: text.AlignLeft, WidthMax: 50}, - {Number: 2, AlignHeader: text.AlignCenter, ColorsHeader: headerColor, Align: text.AlignLeft, WidthMax: 50}, + {Number: 1, AlignHeader: text.AlignCenter, ColorsHeader: headerColor, WidthMax: 40}, + {Number: 2, AlignHeader: text.AlignCenter, ColorsHeader: headerColor, WidthMax: 40}, + {Number: 3, AlignHeader: text.AlignCenter, ColorsHeader: headerColor, WidthMax: 40}, }) - t.AppendHeader(table.Row{"OUTPUT", "EXPECTED"}) - t.AppendRow(table.Row{string(outputBuf), string(expectedBuf)}) + t.AppendHeader(table.Row{"INPUT", "EXPECTED", "OUTPUT"}) + t.AppendRow(table.Row{string(inputBuf), string(expectedBuf), string(outputBuf)}) verdictData.Compare = t.Render() - verdictData.Input = string(inputBuf) } else if err != nil { // Unknown error; Panic. @@ -191,7 +192,6 @@ func judgeMode(runScript, checkerTmplt string, timelimit time.Duration, func interactiveMode(script string) { // It doesn't get any simpler, does it? - fmt.Println() // Newline for asthetics. fmt.Println(color.GreenString("---- * ---- launched ---- * ----")) runShellScript(script, time.Hour, os.Stdin, os.Stdout, os.Stderr) fmt.Println(color.GreenString("---- * ---- finished ---- * ----")) diff --git a/cmd/test/test.go b/cmd/test/test.go index d7f36f6..68b8fbb 100644 --- a/cmd/test/test.go +++ b/cmd/test/test.go @@ -23,7 +23,7 @@ func Test(checker, filePath, mode string, timelimit time.Duration, cnf *conf.Con // Run preScript. if preScript := cnf.GetString("template." + alias + ".preScript"); preScript != "" { script, _ := util.CleanTemplate(preScript, tmpltData) - fmt.Println(color.BlueString("prescript:"), script) + fmt.Println(color.BlueString("prescript:"), script, "\n") if _, err := runShellScript(script, time.Minute, os.Stdin, os.Stdout, os.Stderr); err != nil { fmt.Println(err) @@ -52,8 +52,7 @@ func Test(checker, filePath, mode string, timelimit time.Duration, cnf *conf.Con // Run postScript. if postScript := cnf.GetString("template." + alias + ".postScript"); postScript != "" { script, _ := util.CleanTemplate(postScript, tmpltData) - fmt.Println() - fmt.Println(color.BlueString("postscript:"), script) + fmt.Println("\n", color.BlueString("postscript:"), script) if _, err := runShellScript(script, time.Minute, os.Stdin, os.Stdout, os.Stderr); err != nil { fmt.Println(err) diff --git a/util/util.go b/util/util.go index 1c57276..ba3162c 100644 --- a/util/util.go +++ b/util/util.go @@ -9,7 +9,6 @@ import ( "github.com/cp-tools/cpt/packages/conf" - "github.com/fatih/color" "github.com/gosuri/uilive" ) @@ -42,14 +41,6 @@ func LoadLocalConf(cnf *conf.Conf) *conf.Conf { return cnf } -// ColorHeaderFormat sets color (for headers mostly). -func ColorHeaderFormat(str ...string) []string { - for i := range str { - str[i] = color.New(color.FgBlue, color.Bold, color.Underline).Sprint(str[i]) - } - return str -} - // CleanTemplate creates and runs template on passed string, with given params. func CleanTemplate(str string, data interface{}) (string, error) { tmplt, err := template.New("").Parse(str)