diff --git a/packages/cspell/src/app/__snapshots__/app.test.ts.snap b/packages/cspell/src/app/__snapshots__/app.test.ts.snap index 947cf1be069..96087fe4e67 100644 --- a/packages/cspell/src/app/__snapshots__/app.test.ts.snap +++ b/packages/cspell/src/app/__snapshots__/app.test.ts.snap @@ -665,8 +665,8 @@ exports[`Validate cli > app 'lint --help --issue-template' Expect Error: 'output " --gitignore-root Prevent searching for .gitignore files past", " root.", " --validate-directives Validate in-document CSpell directives.", - " --no-color Turn off color.", " --color Force color.", + " --no-color Turn off color.", " --no-default-configuration Do not load the default configuration and", " dictionaries.", " --debug Output information useful for debugging", @@ -809,8 +809,8 @@ exports[`Validate cli > app 'lint --help --verbose' Expect Error: 'outputHelp' 1 " --gitignore-root Prevent searching for .gitignore files past", " root.", " --validate-directives Validate in-document CSpell directives.", - " --no-color Turn off color.", " --color Force color.", + " --no-color Turn off color.", " --no-default-configuration Do not load the default configuration and", " dictionaries.", " --debug Output information useful for debugging", @@ -946,8 +946,8 @@ exports[`Validate cli > app 'lint --help' Expect Error: 'outputHelp' 1`] = ` " --gitignore-root Prevent searching for .gitignore files past", " root.", " --validate-directives Validate in-document CSpell directives.", - " --no-color Turn off color.", " --color Force color.", + " --no-color Turn off color.", " --no-default-configuration Do not load the default configuration and", " dictionaries.", " --debug Output information useful for debugging", @@ -1081,8 +1081,8 @@ exports[`Validate cli > app 'no-args' Expect Error: 'outputHelp' 1`] = ` " --gitignore-root Prevent searching for .gitignore files past", " root.", " --validate-directives Validate in-document CSpell directives.", - " --no-color Turn off color.", " --color Force color.", + " --no-color Turn off color.", " --no-default-configuration Do not load the default configuration and", " dictionaries.", " --debug Output information useful for debugging", diff --git a/packages/cspell/src/app/cli-reporter.ts b/packages/cspell/src/app/cli-reporter.ts index 37d64b39992..16da8a332e8 100644 --- a/packages/cspell/src/app/cli-reporter.ts +++ b/packages/cspell/src/app/cli-reporter.ts @@ -165,6 +165,7 @@ function isSlow(elapsedTmeMs: number | undefined): number | undefined { export interface ReporterOptions extends Pick< LinterCliOptions, + | 'color' | 'debug' | 'issues' | 'issuesSummaryReport' @@ -195,6 +196,8 @@ export function getReporter(options: ReporterOptions, config?: CSpellReporterCon elapsedTimeMs: 0, perf: Object.create(null) as SpellCheckFilePerf, }; + const noColor = options.color === false; + const forceColor = options.color === true; const uniqueIssues = config?.unique || false; const defaultIssueTemplate = options.wordsOnly ? templateIssueWordsOnly @@ -217,13 +220,15 @@ export function getReporter(options: ReporterOptions, config?: CSpellReporterCon const console = config?.console || customConsole; + const colorLevel = noColor ? 0 : forceColor ? 2 : console.stdoutChannel.getColorLevel(); + const stdio: IO = { ...console.stdoutChannel, - chalk: new Chalk({ level: console.stdoutChannel.getColorLevel() }), + chalk: new Chalk({ level: colorLevel }), }; const stderr: IO = { ...console.stderrChannel, - chalk: new Chalk({ level: console.stderrChannel.getColorLevel() }), + chalk: new Chalk({ level: colorLevel }), }; const consoleError = (msg: string) => stderr.writeLine(msg); diff --git a/packages/cspell/src/app/commandLint.ts b/packages/cspell/src/app/commandLint.ts index 7a843c14bed..ace1c0c0943 100644 --- a/packages/cspell/src/app/commandLint.ts +++ b/packages/cspell/src/app/commandLint.ts @@ -149,8 +149,8 @@ export function commandLint(prog: Command): Command { .option('--gitignore-root ', 'Prevent searching for .gitignore files past root.', collect) .option('--validate-directives', 'Validate in-document CSpell directives.') .addOption(crOpt('--no-validate-directives', 'Do not validate in-document CSpell directives.').hideHelp()) - .option('--no-color', 'Turn off color.') - .option('--color', 'Force color.') + .addOption(crOpt('--color', 'Force color.').default(undefined)) + .addOption(crOpt('--no-color', 'Turn off color.').default(undefined)) .addOption(crOpt('--default-configuration', 'Load the default configuration and dictionaries.').hideHelp()) .addOption(crOpt('--no-default-configuration', 'Do not load the default configuration and dictionaries.')) .option('--debug', 'Output information useful for debugging cspell.json files.') diff --git a/packages/cspell/src/app/options.ts b/packages/cspell/src/app/options.ts index 8174e3b5ffe..6e5dc8dae49 100644 --- a/packages/cspell/src/app/options.ts +++ b/packages/cspell/src/app/options.ts @@ -201,6 +201,13 @@ export interface BaseOptions { * Flags are of the form key:value */ flag?: string[]; + + /** + * Use color in the output. + * `true` to force color, `false` to turn off color. + * `undefined` to use color if the output is a TTY. + */ + color?: boolean; } export interface LinterCliOptions extends LinterOptions {