Skip to content

Commit

Permalink
Fix: colorize tasks names sequentially instead of by hash (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
lostfictions authored and mysticatea committed Dec 2, 2017
1 parent 21c0269 commit d56c268
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/run-task.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,23 @@ const spawn = require("./spawn")

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

let colorIndex = 0
const taskNamesToColors = new Map()

/**
* Select a color from given task name.
*
* @param {string} taskName - The task name.
* @returns {function} A colorize function that provided by `chalk`
*/
function selectColor(taskName) {
let hash = 0

for (const c of taskName) {
hash = ((hash << 5) - hash) + c
hash |= 0
let color = taskNamesToColors.get(taskName)
if (!color) {
color = colors[colorIndex]
colorIndex = (colorIndex + 1) % colors.length
taskNamesToColors.set(taskName, color)
}

return colors[Math.abs(hash) % colors.length]
return color
}

/**
Expand Down

0 comments on commit d56c268

Please sign in to comment.