Skip to content

Commit d56c268

Browse files
lostfictionsmysticatea
authored andcommitted
Fix: colorize tasks names sequentially instead of by hash (#115)
1 parent 21c0269 commit d56c268

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

lib/run-task.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,23 @@ const spawn = require("./spawn")
2424

2525
const colors = [chalk.cyan, chalk.green, chalk.magenta, chalk.yellow, chalk.red]
2626

27+
let colorIndex = 0
28+
const taskNamesToColors = new Map()
29+
2730
/**
2831
* Select a color from given task name.
2932
*
3033
* @param {string} taskName - The task name.
3134
* @returns {function} A colorize function that provided by `chalk`
3235
*/
3336
function selectColor(taskName) {
34-
let hash = 0
35-
36-
for (const c of taskName) {
37-
hash = ((hash << 5) - hash) + c
38-
hash |= 0
37+
let color = taskNamesToColors.get(taskName)
38+
if (!color) {
39+
color = colors[colorIndex]
40+
colorIndex = (colorIndex + 1) % colors.length
41+
taskNamesToColors.set(taskName, color)
3942
}
40-
41-
return colors[Math.abs(hash) % colors.length]
43+
return color
4244
}
4345

4446
/**

0 commit comments

Comments
 (0)