Skip to content

Commit 7d0c8f0

Browse files
authored
Merge pull request #49 from mkantor/no-color
Add `--no-color` command-line option
2 parents b04919e + 022d909 commit 7d0c8f0

File tree

7 files changed

+81
-60
lines changed

7 files changed

+81
-60
lines changed

src/language/cli/output.ts

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import either, { type Either } from '@matt.kantor/either'
2+
import kleur from 'kleur'
23
import { parseArgs } from 'node:util'
34
import { type SyntaxTree } from '../parsing/syntax-tree.js'
45
import {
@@ -17,35 +18,42 @@ export const handleOutput = async (
1718
args: process.argv.slice(2), // remove `execPath` and `filename`
1819
strict: false,
1920
options: {
21+
'no-color': { type: 'boolean' },
2022
'output-format': { type: 'string' },
2123
},
2224
})
23-
const outputFormat = args.values['output-format']
24-
if (outputFormat === undefined) {
25+
26+
const noColorArg = args.values['no-color'] ?? false
27+
if (typeof noColorArg !== 'boolean') {
28+
throw new Error('Unsupported value for --no-color')
29+
} else if (noColorArg === true) {
30+
kleur.enabled = false
31+
}
32+
33+
const outputFormatArg = args.values['output-format']
34+
let notation: Notation
35+
if (outputFormatArg === undefined) {
2536
throw new Error('Missing required option: --output-format')
37+
} else if (outputFormatArg === 'json') {
38+
notation = prettyJson
39+
} else if (outputFormatArg === 'plz') {
40+
notation = prettyPlz
41+
} else if (outputFormatArg === 'sugar-free-plz') {
42+
notation = sugarFreePrettyPlz
2643
} else {
27-
let notation: Notation
28-
if (outputFormat === 'json') {
29-
notation = prettyJson
30-
} else if (outputFormat === 'plz') {
31-
notation = prettyPlz
32-
} else if (outputFormat === 'sugar-free-plz') {
33-
notation = sugarFreePrettyPlz
34-
} else {
35-
throw new Error(`Unsupported output format: "${outputFormat}"`)
36-
}
37-
38-
const result = await command()
39-
return either.match(result, {
40-
left: error => {
41-
throw new Error(error.message) // TODO: Improve error reporting.
42-
},
43-
right: output => {
44-
writeOutput(process.stdout, notation, output)
45-
return undefined
46-
},
47-
})
44+
throw new Error(`Unsupported output format: "${outputFormatArg}"`)
4845
}
46+
47+
const result = await command()
48+
return either.match(result, {
49+
left: error => {
50+
throw new Error(error.message) // TODO: Improve error reporting.
51+
},
52+
right: output => {
53+
writeOutput(process.stdout, notation, output)
54+
return undefined
55+
},
56+
})
4957
}
5058

5159
export const writeOutput = (

src/language/unparsing/inline-plz.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import either from '@matt.kantor/either'
2+
import kleur from 'kleur'
23
import type { Atom, Molecule } from '../parsing.js'
34
import {
4-
closeBrace,
5-
comma,
65
moleculeAsKeyValuePairStrings,
76
moleculeUnparser,
8-
openBrace,
97
unparseAtom,
108
} from './plz-utilities.js'
11-
import type { Notation } from './unparsing-utilities.js'
9+
import { punctuation, type Notation } from './unparsing-utilities.js'
1210

1311
const unparseSugarFreeMolecule = (value: Molecule) => {
12+
const { comma, closeBrace, openBrace } = punctuation(kleur)
1413
if (Object.keys(value).length === 0) {
1514
return either.makeRight(openBrace + closeBrace)
1615
} else {

src/language/unparsing/plz-utilities.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,7 @@ import {
1818
type LookupExpression,
1919
type SemanticGraph,
2020
} from '../semantics.js'
21-
22-
export const dot = kleur.dim('.')
23-
export const quote = kleur.dim('"')
24-
export const colon = kleur.dim(':')
25-
export const comma = kleur.dim(',')
26-
export const openBrace = kleur.dim('{')
27-
export const closeBrace = kleur.dim('}')
28-
export const openParenthesis = kleur.dim('(')
29-
export const closeParenthesis = kleur.dim(')')
30-
export const arrow = kleur.dim('=>')
21+
import { punctuation } from './unparsing-utilities.js'
3122

3223
export const moleculeUnparser =
3324
(
@@ -73,6 +64,7 @@ export const moleculeAsKeyValuePairStrings = (
7364
unparseAtomOrMolecule: UnparseAtomOrMolecule,
7465
options: { readonly ordinalKeys: 'omit' | 'preserve' },
7566
): Either<UnserializableValueError, readonly string[]> => {
67+
const { colon } = punctuation(kleur)
7668
const entries = Object.entries(value)
7769

7870
const keyValuePairsAsStrings: string[] = []
@@ -110,6 +102,7 @@ export const unparseAtom = (atom: string): Right<string> =>
110102
)
111103

112104
const quoteAtomIfNecessary = (value: string): string => {
105+
const { quote } = punctuation(kleur)
113106
const unquotedAtomResult = parsing.parse(unquotedAtomParser, value)
114107
if (either.isLeft(unquotedAtomResult)) {
115108
return quote.concat(escapeStringContents(value)).concat(quote)
@@ -119,6 +112,7 @@ const quoteAtomIfNecessary = (value: string): string => {
119112
}
120113

121114
const quoteKeyPathComponentIfNecessary = (value: string): string => {
115+
const { quote } = punctuation(kleur)
122116
const unquotedAtomResult = parsing.parse(unquotedAtomParser, value)
123117
if (either.isLeft(unquotedAtomResult) || value.includes('.')) {
124118
return quote.concat(escapeStringContents(value)).concat(quote)
@@ -145,6 +139,7 @@ const unparseSugaredApply = (
145139
expression: ApplyExpression,
146140
unparseAtomOrMolecule: UnparseAtomOrMolecule,
147141
) => {
142+
const { closeParenthesis, openParenthesis } = punctuation(kleur)
148143
const functionUnparseResult = either.map(
149144
either.flatMap(
150145
serializeIfNeeded(expression.function),
@@ -182,7 +177,11 @@ const unparseSugaredFunction = (
182177
) =>
183178
either.flatMap(serializeIfNeeded(expression.body), serializedBody =>
184179
either.map(unparseAtomOrMolecule(serializedBody), bodyAsString =>
185-
[kleur.cyan(expression.parameter), arrow, bodyAsString].join(' '),
180+
[
181+
kleur.cyan(expression.parameter),
182+
punctuation(kleur).arrow,
183+
bodyAsString,
184+
].join(' '),
186185
),
187186
)
188187

@@ -223,6 +222,8 @@ const unparseSugaredIndex = (
223222
})
224223
}
225224

225+
const { dot } = punctuation(kleur)
226+
226227
return either.makeRight(
227228
objectUnparseResult.value
228229
.concat(dot)
@@ -235,5 +236,9 @@ const unparseSugaredLookup = (
235236
_unparseAtomOrMolecule: UnparseAtomOrMolecule,
236237
) =>
237238
either.makeRight(
238-
kleur.cyan(colon.concat(quoteKeyPathComponentIfNecessary(expression.key))),
239+
kleur.cyan(
240+
punctuation(kleur).colon.concat(
241+
quoteKeyPathComponentIfNecessary(expression.key),
242+
),
243+
),
239244
)

src/language/unparsing/pretty-json.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,30 @@ import type { Right } from '@matt.kantor/either'
22
import either from '@matt.kantor/either'
33
import kleur from 'kleur'
44
import type { Atom, Molecule } from '../parsing.js'
5-
import { indent, type Notation } from './unparsing-utilities.js'
6-
7-
const quote = kleur.dim('"')
8-
const colon = kleur.dim(':')
9-
const comma = kleur.dim(',')
10-
const openBrace = kleur.dim('{')
11-
const closeBrace = kleur.dim('}')
5+
import { indent, punctuation, type Notation } from './unparsing-utilities.js'
126

137
const escapeStringContents = (value: string) =>
148
value.replace('\\', '\\\\').replace('"', '\\"')
159

16-
const key = (value: Atom): string =>
17-
quote.concat(kleur.bold(escapeStringContents(value))).concat(quote)
10+
const key = (value: Atom): string => {
11+
const { quote } = punctuation(kleur)
12+
return quote.concat(kleur.bold(escapeStringContents(value))).concat(quote)
13+
}
1814

19-
const unparseAtom = (value: Atom): Right<string> =>
20-
either.makeRight(
15+
const unparseAtom = (value: Atom): Right<string> => {
16+
const { quote } = punctuation(kleur)
17+
return either.makeRight(
2118
quote.concat(
2219
escapeStringContents(
2320
/^@[^@]/.test(value) ? kleur.bold(kleur.underline(value)) : value,
2421
),
2522
quote,
2623
),
2724
)
25+
}
2826

2927
const unparseMolecule = (value: Molecule): Right<string> => {
28+
const { closeBrace, colon, comma, openBrace } = punctuation(kleur)
3029
const entries = Object.entries(value)
3130
if (entries.length === 0) {
3231
return either.makeRight(openBrace.concat(closeBrace))

src/language/unparsing/pretty-plz.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import either from '@matt.kantor/either'
2+
import kleur from 'kleur'
23
import type { Atom, Molecule } from '../parsing.js'
34
import {
4-
closeBrace,
55
moleculeAsKeyValuePairStrings,
66
moleculeUnparser,
7-
openBrace,
87
unparseAtom,
98
} from './plz-utilities.js'
10-
import { indent, type Notation } from './unparsing-utilities.js'
9+
import { indent, punctuation, type Notation } from './unparsing-utilities.js'
1110

1211
const unparseSugarFreeMolecule = (value: Molecule) => {
12+
const { closeBrace, openBrace } = punctuation(kleur)
1313
if (Object.keys(value).length === 0) {
1414
return either.makeRight(openBrace + closeBrace)
1515
} else {

src/language/unparsing/sugar-free-pretty-plz.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import either from '@matt.kantor/either'
2+
import kleur from 'kleur'
23
import type { Atom, Molecule } from '../parsing.js'
3-
import {
4-
closeBrace,
5-
moleculeAsKeyValuePairStrings,
6-
openBrace,
7-
unparseAtom,
8-
} from './plz-utilities.js'
9-
import { indent, type Notation } from './unparsing-utilities.js'
4+
import { moleculeAsKeyValuePairStrings, unparseAtom } from './plz-utilities.js'
5+
import { indent, punctuation, type Notation } from './unparsing-utilities.js'
106

117
const unparseMolecule = (value: Molecule) => {
8+
const { closeBrace, openBrace } = punctuation(kleur)
129
if (Object.keys(value).length === 0) {
1310
return either.makeRight(openBrace + closeBrace)
1411
} else {

src/language/unparsing/unparsing-utilities.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { type Either } from '@matt.kantor/either'
2+
import type { Kleur } from 'kleur'
23
import type { UnserializableValueError } from '../errors.js'
34
import type { Atom, Molecule } from '../parsing.js'
45

@@ -16,3 +17,15 @@ export const indent = (spaces: number, textToIndent: string) => {
1617
.concat(textToIndent)
1718
.replace(/(\r?\n)/g, `$1${indentation}`)
1819
}
20+
21+
export const punctuation = (kleur: Kleur) => ({
22+
dot: kleur.dim('.'),
23+
quote: kleur.dim('"'),
24+
colon: kleur.dim(':'),
25+
comma: kleur.dim(','),
26+
openBrace: kleur.dim('{'),
27+
closeBrace: kleur.dim('}'),
28+
openParenthesis: kleur.dim('('),
29+
closeParenthesis: kleur.dim(')'),
30+
arrow: kleur.dim('=>'),
31+
})

0 commit comments

Comments
 (0)