Skip to content

Commit

Permalink
chore: Move task list helper functions into the logTasks closure (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Mar 10, 2024
1 parent cb03b9a commit c70ce34
Showing 1 changed file with 80 additions and 80 deletions.
160 changes: 80 additions & 80 deletions lib/shared/log/tasks.js
Expand Up @@ -25,105 +25,105 @@ function logTasks(tree, opts, getTask) {
};

printTaskTree(tree, treeOpts);
}

function printTaskTree(tree, opts) {
var lines = [];
lines.push({ label: tree.label });
var maxLabelWidth = 0;

tree.nodes.forEach(function(node, idx, arr) {
var isLast = idx === arr.length - 1;
var w = createTreeLines(node, lines, opts, 1, '', isLast);
maxLabelWidth = Math.max(maxLabelWidth, w || 0);
});

lines.forEach(function(line) {
var s = line.label;
if (line.desc) {
var spaces = ' '.repeat(maxLabelWidth - line.label.length) + ' ';
s += spaces + line.desc;
}
log.info(s);
});
}

function createTreeLines(node, lines, opts, depth, bars, isLast) {
var task = { label: node.label, bars: bars, depth: depth };
if (depth === 1) {
var t = opts.getTask(node.label);
task.desc = t.description;
task.flags = t.flags;
}

var isLeaf = isLeafNode(node, depth, opts);

var maxLabelWidth = addTaskToLines(task, lines, isLast, isLeaf);
function printTaskTree(tree, opts) {
var lines = [];
lines.push({ label: tree.label });
var maxLabelWidth = 0;

if (!isLeaf) {
bars += (isLast ? ' ' : '│ ');
node.nodes.forEach(function(node, idx, arr) {
tree.nodes.forEach(function(node, idx, arr) {
var isLast = idx === arr.length - 1;
createTreeLines(node, lines, opts, depth + 1, bars, isLast);
var w = createTreeLines(node, lines, opts, 1, '', isLast);
maxLabelWidth = Math.max(maxLabelWidth, w || 0);
});
}

return maxLabelWidth;
}

function addTaskToLines(task, lines, isLast, isLeaf) {
var taskBars = task.bars + (isLast ? '└' : '├') + '─';
if (isLeaf) {
taskBars += '─ ';
} else {
taskBars += '┬ ';
lines.forEach(function(line) {
var s = line.label;
if (line.desc) {
var spaces = ' '.repeat(maxLabelWidth - line.label.length) + ' ';
s += spaces + line.desc;
}
log.info(s);
});
}

var line = {};
if (task.depth === 1) {
line.label = chalk.white(taskBars) + chalk.white(task.label);
} else {
line.label = chalk.white(taskBars) + chalk.cyan(task.label);
}
if (typeof task.desc === 'string' && task.desc) {
line.desc = chalk.white(task.desc);
}
lines.push(line);
function createTreeLines(node, lines, opts, depth, bars, isLast) {
var task = { label: node.label, bars: bars, depth: depth };
if (depth === 1) {
var t = opts.getTask(node.label);
task.desc = t.description;
task.flags = t.flags;
}

var maxLabelWidth = line.label.length
var isLeaf = isLeafNode(node, depth, opts);

if (!isObject(task.flags)) {
return maxLabelWidth;
}
var maxLabelWidth = addTaskToLines(task, lines, isLast, isLeaf);

var flagBars = task.bars;
if (isLast) {
flagBars += ' ';
} else {
flagBars += '│ ';
}
if (!isLeaf) {
bars += (isLast ? ' ' : '│ ');
node.nodes.forEach(function(node, idx, arr) {
var isLast = idx === arr.length - 1;
createTreeLines(node, lines, opts, depth + 1, bars, isLast);
});
}

if (isLeaf) {
flagBars += ' ';
} else {
flagBars += '│ ';
return maxLabelWidth;
}

Object.entries(task.flags).sort(flagSorter).forEach(addFlagsToLines);
function addTaskToLines(task, lines, isLast, isLeaf) {
var taskBars = task.bars + (isLast ? '└' : '├') + '─';
if (isLeaf) {
taskBars += '─ ';
} else {
taskBars += '┬ ';
}

function addFlagsToLines(ent) {
if (typeof ent[0] !== 'string' || !ent[0]) return;
var line = {};
if (task.depth === 1) {
line.label = chalk.white(taskBars) + chalk.white(task.label);
} else {
line.label = chalk.white(taskBars) + chalk.cyan(task.label);
}
if (typeof task.desc === 'string' && task.desc) {
line.desc = chalk.white(task.desc);
}
lines.push(line);
line.label = chalk.white(flagBars) + chalk.magenta(ent[0]);

maxLabelWidth = Math.max(maxLabelWidth, line.label.length);
var maxLabelWidth = line.label.length

if (typeof ent[1] !== 'string' || !ent[1]) return;
line.desc = chalk.white('…' + ent[1]);
}
if (!isObject(task.flags)) {
return maxLabelWidth;
}

var flagBars = task.bars;
if (isLast) {
flagBars += ' ';
} else {
flagBars += '│ ';
}

return maxLabelWidth;
if (isLeaf) {
flagBars += ' ';
} else {
flagBars += '│ ';
}

Object.entries(task.flags).sort(flagSorter).forEach(addFlagsToLines);

function addFlagsToLines(ent) {
if (typeof ent[0] !== 'string' || !ent[0]) return;
var line = {};
lines.push(line);
line.label = chalk.white(flagBars) + chalk.magenta(ent[0]);

maxLabelWidth = Math.max(maxLabelWidth, line.label.length);

if (typeof ent[1] !== 'string' || !ent[1]) return;
line.desc = chalk.white('…' + ent[1]);
}

return maxLabelWidth;
}
}

function isLeafNode(node, depth, opts) {
Expand Down

0 comments on commit c70ce34

Please sign in to comment.