Skip to content

Commit

Permalink
Merge pull request #75 from nulltask/feature/colorize (fixes #73)
Browse files Browse the repository at this point in the history
Change labels to be displayed in individual colors for each task
  • Loading branch information
mysticatea committed Dec 23, 2016
2 parents 2ec9889 + d7821df commit 910d78d
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/lib/run-task.js
Expand Up @@ -22,6 +22,25 @@ const spawn = require("./spawn")
// Helpers
//------------------------------------------------------------------------------

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

/**
* 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 i in taskName) {
hash = ((hash << 5) - hash) + taskName.charCodeAt(i)
hash |= 0
}

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

/**
* Wraps stdout/stderr with a transform stream to add the task name as prefix.
*
Expand All @@ -36,7 +55,7 @@ function wrapLabeling(taskName, source, labelState) {
}

const label = padEnd(taskName, labelState.width)
const color = source.isTTY ? chalk.gray : (x) => x
const color = source.isTTY ? selectColor(taskName) : (x) => x
const prefix = color(`[${label}] `)
const stream = createPrefixTransform(prefix, labelState)

Expand Down

0 comments on commit 910d78d

Please sign in to comment.