-
-
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
Co-authored-by: Pascal Jufer <pascal-jufer@bluewin.ch>
- Loading branch information
Showing
7 changed files
with
176 additions
and
4 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,71 @@ | ||
import { PrefixColorSelector } from './prefix-color-selector'; | ||
|
||
it('does not produce a color if it should not', () => { | ||
const prefixColorSelector = new PrefixColorSelector([], false); | ||
|
||
let selectedColor = prefixColorSelector.getNextColor(0); | ||
expect(selectedColor).toBe(''); | ||
selectedColor = prefixColorSelector.getNextColor(1); | ||
expect(selectedColor).toBe(''); | ||
selectedColor = prefixColorSelector.getNextColor(2); | ||
expect(selectedColor).toBe(''); | ||
}); | ||
|
||
it('uses user defined prefix colors only if not allowed to use auto colors', () => { | ||
const prefixColorSelector = new PrefixColorSelector(['red', 'green', 'blue'], false); | ||
|
||
let selectedColor = prefixColorSelector.getNextColor(0); | ||
expect(selectedColor).toBe('red'); | ||
selectedColor = prefixColorSelector.getNextColor(1); | ||
expect(selectedColor).toBe('green'); | ||
selectedColor = prefixColorSelector.getNextColor(2); | ||
expect(selectedColor).toBe('blue'); | ||
|
||
// uses last color if no more user defined colors | ||
selectedColor = prefixColorSelector.getNextColor(3); | ||
expect(selectedColor).toBe('blue'); | ||
selectedColor = prefixColorSelector.getNextColor(4); | ||
expect(selectedColor).toBe('blue'); | ||
}); | ||
|
||
it('uses user defined colors then recurring auto colors without repeating consecutive colors', () => { | ||
const prefixColorSelector = new PrefixColorSelector(['red', 'green'], true); | ||
|
||
jest.spyOn(prefixColorSelector, 'ACCEPTABLE_CONSOLE_COLORS', 'get').mockReturnValue([ | ||
'green', | ||
'blue', | ||
]); | ||
|
||
let selectedColor = prefixColorSelector.getNextColor(0); | ||
expect(selectedColor).toBe('red'); | ||
selectedColor = prefixColorSelector.getNextColor(1); | ||
expect(selectedColor).toBe('green'); | ||
|
||
// auto colors now, does not repeat last user color of green | ||
selectedColor = prefixColorSelector.getNextColor(2); | ||
expect(selectedColor).toBe('blue'); | ||
|
||
selectedColor = prefixColorSelector.getNextColor(3); | ||
expect(selectedColor).toBe('green'); | ||
|
||
selectedColor = prefixColorSelector.getNextColor(4); | ||
expect(selectedColor).toBe('blue'); | ||
}); | ||
|
||
it('has more than 1 auto color defined', () => { | ||
const prefixColorSelector = new PrefixColorSelector([], true); | ||
// ! code assumes this always has more than one entry, so make sure | ||
expect(prefixColorSelector.ACCEPTABLE_CONSOLE_COLORS.length).toBeGreaterThan(1); | ||
}); | ||
|
||
it('can use only auto colors and does not repeat consecutive colors', () => { | ||
const prefixColorSelector = new PrefixColorSelector([], true); | ||
|
||
let previousColor; | ||
let selectedColor: string; | ||
Array(prefixColorSelector.ACCEPTABLE_CONSOLE_COLORS.length * 2).forEach((_, index) => { | ||
previousColor = selectedColor; | ||
selectedColor = prefixColorSelector.getNextColor(index); | ||
expect(selectedColor).not.toBe(previousColor); | ||
}); | ||
}); |
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,77 @@ | ||
import chalk from 'chalk'; | ||
|
||
export class PrefixColorSelector { | ||
lastColor: string; | ||
autoColors: string[]; | ||
|
||
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.toString()) | ||
); | ||
} | ||
|
||
constructor(private readonly prefixColors?: string[], private readonly color?: boolean) {} | ||
|
||
getNextColor(index?: number) { | ||
const cannotSelectColor = !this.prefixColors?.length && !this.color; | ||
if (cannotSelectColor) { | ||
return ''; | ||
} | ||
|
||
const userDefinedColorForCurrentCommand = | ||
this.prefixColors && typeof index === 'number' && this.prefixColors[index]; | ||
|
||
if (!this.color) { | ||
// 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 | ||
// (i.e. 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]; | ||
} | ||
} |