Skip to content

Commit

Permalink
misc: Fix format for article (#130)
Browse files Browse the repository at this point in the history
* misc: Refine format for article

* Fix test

* misc: Fix example output in README.md
  • Loading branch information
apstndb authored Feb 9, 2025
1 parent b43e616 commit a41cb95
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1412,22 +1412,22 @@ spanner> GRAPH FinGraph
```sql
GRAPH FinGraph
MATCH (n:Account)
RETURN LABELS(n) AS labels, PROPERTY_NAMES(n) AS props, n.id
/*--------------+------------------------------------------+-------+
| labels | props | id |
| ARRAY<STRING> | ARRAY<STRING> | INT64 |
+---------------+------------------------------------------+-------+
| [Account] | [create_time, id, is_blocked, nick_name] | 7 |
| [Account] | [create_time, id, is_blocked, nick_name] | 16 |
| [Account] | [create_time, id, is_blocked, nick_name] | 20 |
+---------------+------------------------------------------+-------+
3 rows in set (6.47 msecs)
timestamp: 2025-02-02T10:22:04.867235+09:00
cpu time: 4.66 msecs
RETURN LABELS(n) AS labels, PROPERTY_NAMES(n) AS props, n.id;
/*---------------+------------------------------------------+-------+
| labels | props | id |
| ARRAY<STRING> | ARRAY<STRING> | INT64 |
+---------------+------------------------------------------+-------+
| [Account] | [create_time, id, is_blocked, nick_name] | 7 |
| [Account] | [create_time, id, is_blocked, nick_name] | 16 |
| [Account] | [create_time, id, is_blocked, nick_name] | 20 |
+---------------+------------------------------------------+-------+
3 rows in set (6.26 msecs)
timestamp: 2025-02-09T23:28:26.088524+09:00
cpu time: 4.45 msecs
rows scanned: 3 rows
deleted rows scanned: 0 rows
optimizer version: 7
optimizer statistics: auto_20250201_06_22_44UTC
optimizer statistics: auto_20250207_09_19_31UTC
*/
```
````
Expand Down
10 changes: 7 additions & 3 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,11 @@ func adjustByHeader(headers []string, availableWidth int) []int {
return adjustWidths
}

var (
topLeftRe = regexp.MustCompile(`^\+`)
bottomRightRe = regexp.MustCompile(`\+$`)
)

func printResult(sysVars *systemVariables, screenWidth int, out io.Writer, result *Result, interactive bool, input string) {
mode := sysVars.CLIFormat

Expand All @@ -911,7 +916,7 @@ func printResult(sysVars *systemVariables, screenWidth int, out io.Writer, resul
}

if sysVars.EchoInput && input != "" {
fmt.Fprintln(out, input)
fmt.Fprintln(out, input+";")
}

// screenWidth <= means no limit.
Expand Down Expand Up @@ -968,12 +973,11 @@ func printResult(sysVars *systemVariables, screenWidth int, out io.Writer, resul

s := strings.TrimSpace(tableBuf.String())
if mode == DisplayModeTableComment || mode == DisplayModeTableDetailComment {
topLeftRe := regexp.MustCompile(`^\+-`)
s = strings.ReplaceAll(s, "\n", "\n ")
s = topLeftRe.ReplaceAllLiteralString(s, "/*")
}

if mode == DisplayModeTableComment {
bottomRightRe := regexp.MustCompile(`-\+$`)
s = bottomRightRe.ReplaceAllLiteralString(s, "*/")
}

Expand Down
26 changes: 13 additions & 13 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@ func TestPrintResult(t *testing.T) {
IsMutation: false,
},
want: strings.TrimPrefix(`
/*----+-----+
| foo | bar |
+-----+-----+
| 1 | 2 |
| 3 | 4 |
+-----+----*/
/*-----+-----+
| foo | bar |
+-----+-----+
| 1 | 2 |
| 3 | 4 |
+-----+-----*/
`, "\n"),
},
{
Expand All @@ -198,13 +198,13 @@ func TestPrintResult(t *testing.T) {
},
want: "```sql" + `
SELECT foo, bar
FROM input
/*----+-----+
| foo | bar |
+-----+-----+
| 1 | 2 |
| 3 | 4 |
+-----+-----+
FROM input;
/*-----+-----+
| foo | bar |
+-----+-----+
| 1 | 2 |
| 3 | 4 |
+-----+-----+
Empty set
*/
` + "```\n",
Expand Down

0 comments on commit a41cb95

Please sign in to comment.