Skip to content

Commit d7821df

Browse files
committed
Change labels to be displayed in individual colors for each task
1 parent 2ec9889 commit d7821df

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/lib/run-task.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,25 @@ const spawn = require("./spawn")
2222
// Helpers
2323
//------------------------------------------------------------------------------
2424

25+
const colors = [chalk.cyan, chalk.green, chalk.magenta, chalk.yellow, chalk.red]
26+
27+
/**
28+
* Select a color from given task name.
29+
*
30+
* @param {string} taskName - The task name.
31+
* @returns {function} A colorize function that provided by `chalk`
32+
*/
33+
function selectColor(taskName) {
34+
let hash = 0
35+
36+
for (const i in taskName) {
37+
hash = ((hash << 5) - hash) + taskName.charCodeAt(i)
38+
hash |= 0
39+
}
40+
41+
return colors[Math.abs(hash) % colors.length]
42+
}
43+
2544
/**
2645
* Wraps stdout/stderr with a transform stream to add the task name as prefix.
2746
*
@@ -36,7 +55,7 @@ function wrapLabeling(taskName, source, labelState) {
3655
}
3756

3857
const label = padEnd(taskName, labelState.width)
39-
const color = source.isTTY ? chalk.gray : (x) => x
58+
const color = source.isTTY ? selectColor(taskName) : (x) => x
4059
const prefix = color(`[${label}] `)
4160
const stream = createPrefixTransform(prefix, labelState)
4261

0 commit comments

Comments
 (0)