Skip to content

Commit

Permalink
fix: Honor the --color option
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed Nov 26, 2024
1 parent 84a8302 commit 65cb527
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
8 changes: 4 additions & 4 deletions packages/cspell/src/app/__snapshots__/app.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -665,8 +665,8 @@ exports[`Validate cli > app 'lint --help --issue-template' Expect Error: 'output
" --gitignore-root <path> 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",
Expand Down Expand Up @@ -809,8 +809,8 @@ exports[`Validate cli > app 'lint --help --verbose' Expect Error: 'outputHelp' 1
" --gitignore-root <path> 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",
Expand Down Expand Up @@ -946,8 +946,8 @@ exports[`Validate cli > app 'lint --help' Expect Error: 'outputHelp' 1`] = `
" --gitignore-root <path> 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",
Expand Down Expand Up @@ -1081,8 +1081,8 @@ exports[`Validate cli > app 'no-args' Expect Error: 'outputHelp' 1`] = `
" --gitignore-root <path> 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",
Expand Down
9 changes: 7 additions & 2 deletions packages/cspell/src/app/cli-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ function isSlow(elapsedTmeMs: number | undefined): number | undefined {
export interface ReporterOptions
extends Pick<
LinterCliOptions,
| 'color'
| 'debug'
| 'issues'
| 'issuesSummaryReport'
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions packages/cspell/src/app/commandLint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ export function commandLint(prog: Command): Command {
.option('--gitignore-root <path>', '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.')
Expand Down
7 changes: 7 additions & 0 deletions packages/cspell/src/app/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 65cb527

Please sign in to comment.