Skip to content

Commit

Permalink
Fix coloring for diagnostics in summaries (#548)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecooper authored Dec 19, 2024
1 parent 10e8902 commit 40894cd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/lib/fauna-client.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
//@ts-check

import stripAnsi from "strip-ansi";

import { container } from "../cli.mjs";
import { isUnknownError } from "./errors.mjs";
import { faunaToCommandError } from "./fauna.mjs";
import { faunadbToCommandError } from "./faunadb.mjs";
import { colorize, Format } from "./formatting/colorize.mjs";

const SUMMARY_FQL_REGEX = /^(\s\s\|)|(\d\s\|)/;
/**
* Regex to match the FQL diagnostic line.
* @type {RegExp}
*/
export const FQL_DIAGNOSTIC_REGEX = /^(\s{2,}\|)|(\s*\d{1,}\s\|)/;

/**
* Gets a secret for the current credentials.
Expand Down Expand Up @@ -168,7 +174,7 @@ export const formatQuerySummary = (summary) => {

try {
const lines = summary.split("\n").map((line) => {
if (!line.match(SUMMARY_FQL_REGEX)) {
if (!line.match(FQL_DIAGNOSTIC_REGEX)) {
return line;
}
return colorize(line, { format: Format.FQL });
Expand Down Expand Up @@ -228,14 +234,16 @@ export const formatQueryInfo = (response, { apiVersion, color, include }) => {

if (Object.keys(queryInfoToDisplay).length === 0) return "";

const SUMMARY_IN_QUERY_INFO_FQL_REGEX = /^(\s\s\s\s\|)|(\d\s\|)/;
// We colorize the entire query info object as YAML, but then need to
// colorize the diagnostic lines individually. To simplify this, we
// strip the ansi when we're checking if the line is a diagnostic line.
const colorized = colorize(queryInfoToDisplay, {
color,
format: Format.YAML,
})
.split("\n")
.map((line) => {
if (!line.match(SUMMARY_IN_QUERY_INFO_FQL_REGEX)) {
if (!stripAnsi(line).match(FQL_DIAGNOSTIC_REGEX)) {
return line;
}
return colorize(line, { format: Format.FQL });
Expand Down
30 changes: 30 additions & 0 deletions test/lib/fauna-client.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expect } from "chai";

import { FQL_DIAGNOSTIC_REGEX } from "../../src/lib/fauna-client.mjs";

describe("FQL_DIAGNOSTIC_REGEX", () => {
const validLines = ["1 |", "12 |", "123 |", " |", " |", " |", " 1 |"];

const invalidLines = [
"normal text",
"1 |",
"| invalid",
"abc |",
"|",
"1|",
" | ",
"text | more",
];

validLines.forEach((line) => {
it(`should match diagnostic line: "${line}"`, () => {
expect(line).to.match(FQL_DIAGNOSTIC_REGEX);
});
});

invalidLines.forEach((line) => {
it(`should not match non-diagnostic line: "${line}"`, () => {
expect(line).to.not.match(FQL_DIAGNOSTIC_REGEX);
});
});
});

0 comments on commit 40894cd

Please sign in to comment.