Skip to content

Commit

Permalink
Improve table formatting.
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
infalmo committed Dec 13, 2020
1 parent 2081ad8 commit 587ced6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 34 deletions.
10 changes: 6 additions & 4 deletions cmd/codeforces/submit/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
36 changes: 18 additions & 18 deletions cmd/test/modes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func judgeMode(runScript, checkerTmplt string, timelimit time.Duration,
FailLog error
Stderr string
CheckerLog string
Input string
Compare string
}

Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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())
Expand All @@ -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.
Expand All @@ -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 ---- * ----"))
Expand Down
5 changes: 2 additions & 3 deletions cmd/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 0 additions & 9 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/cp-tools/cpt/packages/conf"

"github.com/fatih/color"
"github.com/gosuri/uilive"
)

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 587ced6

Please sign in to comment.