diff --git a/cli/error.go b/cli/error.go index 8e9537a1..64174f19 100644 --- a/cli/error.go +++ b/cli/error.go @@ -84,8 +84,8 @@ func (err *queryParseError) Error() string { return fmt.Sprintf("invalid query: %s:%d\n%s %s", err.fname, line, formatLineInfo(linestr, line, column), err.err) } - return fmt.Sprintf("invalid query: %s\n %s\n%s ^ %s", - err.contents, linestr, strings.Repeat(" ", column), err.err) + return fmt.Sprintf("invalid query: %s\n %s\n %*c %s", + err.contents, linestr, column+1, '^', err.err) } func (err *queryParseError) ExitCode() int { @@ -110,8 +110,8 @@ func (err *jsonParseError) Error() string { return fmt.Sprintf("invalid json: %s:%d\n%s %s", err.fname, line, formatLineInfo(linestr, line, column), err.err) } - return fmt.Sprintf("invalid json: %s\n %s\n%s ^ %s", - err.fname, linestr, strings.Repeat(" ", column), err.err) + return fmt.Sprintf("invalid json: %s\n %s\n %*c %s", + err.fname, linestr, column+1, '^', err.err) } type yamlParseError struct { @@ -210,8 +210,7 @@ func trimLastInvalidRune(s string) string { func formatLineInfo(linestr string, line, column int) string { l := strconv.Itoa(line) - return " " + l + " | " + linestr + "\n" + - strings.Repeat(" ", len(l)+column) + " ^" + return fmt.Sprintf(" %s | %s\n %*c", l, linestr, column+len(l)+4, '^') } type stringScanner struct { diff --git a/debug.go b/debug.go index ad3d7216..23698280 100644 --- a/debug.go +++ b/debug.go @@ -103,7 +103,7 @@ func (env *env) debugCodes() { s = "\t## " + name } } - fmt.Fprintf(debugOut, "\t%d\t%s%s%s\n", i, formatOp(c.op, false), debugOperand(c), s) + fmt.Fprintf(debugOut, "\t%d\t%-*s%s%s\n", i, 25, c.op, debugOperand(c), s) } fmt.Fprintln(debugOut, "\t"+strings.Repeat("-", 40)+"+") } @@ -114,7 +114,11 @@ func (env *env) debugState(pc int, backtrack bool) { } var sb strings.Builder c := env.codes[pc] - fmt.Fprintf(&sb, "\t%d\t%s%s\t|", pc, formatOp(c.op, backtrack), debugOperand(c)) + op := c.op.String() + if backtrack { + op += " " + } + fmt.Fprintf(&sb, "\t%d\t%-*s%s\t|", pc, 25, op, debugOperand(c)) var xs []int for i := env.stack.index; i >= 0; i = env.stack.data[i].next { xs = append(xs, i) @@ -149,13 +153,6 @@ func (env *env) debugState(pc int, backtrack bool) { fmt.Fprintln(debugOut, sb.String()) } -func formatOp(c opcode, backtrack bool) string { - if backtrack { - return c.String() + " " + strings.Repeat(" ", 13-len(c.String())) - } - return c.String() + strings.Repeat(" ", 25-len(c.String())) -} - func (env *env) debugForks(pc int, op string) { if !debug { return @@ -173,7 +170,7 @@ func (env *env) debugForks(pc int, op string) { sb.WriteByte('>') } } - fmt.Fprintf(debugOut, "\t-\t%s%s%d\t|\t%s\n", op, strings.Repeat(" ", 22), pc, sb.String()) + fmt.Fprintf(debugOut, "\t-\t%-*s%d\t|\t%s\n", 25, op, pc, sb.String()) } func debugOperand(c *code) string {