-
-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add auto colors functionality to resolve #279
- Loading branch information
Showing
7 changed files
with
233 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const chalk = require('chalk'); | ||
|
||
module.exports = class PrefixColorSelector { | ||
get ACCEPTABLE_CONSOLE_COLORS() { | ||
// colors picked randomly, can be amended if required | ||
return [ | ||
chalk.cyan, | ||
chalk.yellow, | ||
chalk.magenta, | ||
chalk.grey, | ||
chalk.bgBlueBright, | ||
chalk.bgMagenta, | ||
chalk.magentaBright, | ||
chalk.bgBlack, | ||
chalk.bgWhite, | ||
chalk.bgCyan, | ||
chalk.bgGreen, | ||
chalk.bgYellow, | ||
chalk.bgRed, | ||
chalk.bgGreenBright, | ||
chalk.bgGrey, | ||
chalk.blueBright, | ||
] | ||
// filter out duplicates | ||
.filter((chalkColor, index, arr) => { | ||
return arr.indexOf( chalkColor ) === index; | ||
}) | ||
.map(chalkColor => chalkColor.bold); | ||
} | ||
|
||
constructor(options) { | ||
this.userDefinedPrefixColors = options.prefixColors; | ||
this.canAutoSelectColors = options.color; | ||
} | ||
|
||
getNextColor(index) { | ||
const cannotSelectColor = !this.userDefinedPrefixColors && !this.canAutoSelectColors; | ||
if (cannotSelectColor) { return ''; } | ||
|
||
const userDefinedColorForCurrentCommand = this.userDefinedPrefixColors && typeof index === 'number' && this.userDefinedPrefixColors[index]; | ||
|
||
if (!this.canAutoSelectColors) { | ||
// original behaviour | ||
// Use documented behaviour of repeating last color when specifying more commands than colors | ||
this.lastColor = userDefinedColorForCurrentCommand || this.lastColor; | ||
return this.lastColor; | ||
} | ||
|
||
// user preference takes priority if defined | ||
if (userDefinedColorForCurrentCommand) { | ||
this.lastColor = userDefinedColorForCurrentCommand; | ||
return userDefinedColorForCurrentCommand; | ||
} | ||
|
||
// auto selection requested and no user preference defined, select next auto color | ||
if (!this.autoColors || !this.autoColors.length) { this.refillAutoColors(); } | ||
|
||
// prevent consecutive colors from being the same (ie when transitioning from user colours to auto colours) | ||
const nextColor = this.autoColors.shift(); | ||
|
||
this.lastColor = nextColor !== this.lastColor ? nextColor : this.getNextColor(); | ||
return this.lastColor; | ||
} | ||
|
||
refillAutoColors() { | ||
// make sure auto colors are not empty after refill | ||
this.autoColors = [...this.ACCEPTABLE_CONSOLE_COLORS]; | ||
} | ||
}; |
Oops, something went wrong.