Skip to content

Commit

Permalink
fix: use colorize to ensure proper TTY detection
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Jul 9, 2024
1 parent e059411 commit 8a5d71f
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 13 deletions.
3 changes: 1 addition & 2 deletions src/command.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import ansis from 'ansis'
import {fileURLToPath} from 'node:url'
import {inspect} from 'node:util'

Expand Down Expand Up @@ -196,7 +195,7 @@ export abstract class Command {
} else {
if (!err.message) throw err
try {
ux.action.stop(ansis.bold.red('!'))
ux.action.stop(ux.colorize('bold', ux.colorize('red', '!')))
} catch {}

throw err
Expand Down
6 changes: 3 additions & 3 deletions src/errors/errors/cli.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import ansis from 'ansis'
import cs from 'clean-stack'
import indent from 'indent-string'
import wrap from 'wrap-ansi'
Expand All @@ -7,6 +6,7 @@ import Cache from '../../cache'
import {OclifError, PrettyPrintableError} from '../../interfaces/errors'
import {errtermwidth} from '../../screen'
import {settings} from '../../settings'
import {colorize} from '../../ux/theme'

/**
* properties specific to internal oclif error handling
Expand Down Expand Up @@ -39,7 +39,7 @@ export class CLIError extends Error implements OclifError {

get bang(): string | undefined {
try {
return ansis.red(process.platform === 'win32' ? '»' : '›')
return colorize('red', process.platform === 'win32' ? '»' : '›')
} catch {}
}

Expand Down Expand Up @@ -74,7 +74,7 @@ export namespace CLIError {

get bang(): string | undefined {
try {
return ansis.yellow(process.platform === 'win32' ? '»' : '›')
return colorize('yellow', process.platform === 'win32' ? '»' : '›')
} catch {}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/help/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export class CommandHelp extends HelpFormatter {
this.opts.showFlagNameInTitle ?? false,
this.opts.showFlagOptionsInTitle ?? showOptions,
)
if (!value.includes('|')) value = ansis.underline(value)
if (!value.includes('|')) value = colorize('underline', value)
label += `=${value}`
}

Expand Down
2 changes: 1 addition & 1 deletion src/help/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export class HelpFormatter {
}

const output = [
colorize(this.config?.theme?.sectionHeader, ansis.bold(header)),
colorize(this.config?.theme?.sectionHeader, colorize('bold', header)),
colorize(
this.config?.theme?.sectionDescription,
this.indent(
Expand Down
5 changes: 2 additions & 3 deletions src/parser/errors.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import ansis from 'ansis'

import Cache from '../cache'
import {CLIError} from '../errors'
import {Arg, ArgInput, CLIParseErrorOptions, OptionFlag} from '../interfaces/parser'
import {uniq} from '../util/util'
import renderList from '../ux/list'
import {colorize} from '../ux/theme'

export {CLIError} from '../errors'

Expand Down Expand Up @@ -124,7 +123,7 @@ export class FailedFlagValidationError extends CLIParseError {
const reasons = failed.map((r) => r.reason)
const deduped = uniq(reasons)
const errString = deduped.length === 1 ? 'error' : 'errors'
const message = `The following ${errString} occurred:\n ${ansis.dim(deduped.join('\n '))}`
const message = `The following ${errString} occurred:\n ${colorize('dim', deduped.join('\n '))}`
super({exit: Cache.getInstance().get('exitCodes')?.failedFlagValidation ?? exit, message, parse})
}
}
5 changes: 2 additions & 3 deletions src/parser/help.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import ansis from 'ansis'

import {Flag, FlagUsageOptions} from '../interfaces/parser'
import {sortBy} from '../util/util'
import {colorize} from '../ux'

export function flagUsage(flag: Flag<any>, options: FlagUsageOptions = {}): [string, string | undefined] {
const label = []
Expand All @@ -17,7 +16,7 @@ export function flagUsage(flag: Flag<any>, options: FlagUsageOptions = {}): [str

let description: string | undefined = flag.summary || flag.description || ''
if (options.displayRequired && flag.required) description = `(required) ${description}`
description = description ? ansis.dim(description) : undefined
description = description ? colorize('dim', description) : undefined

return [` ${label.join(',').trim()}${usage}`, description] as [string, string | undefined]
}
Expand Down

0 comments on commit 8a5d71f

Please sign in to comment.