Skip to content

Commit

Permalink
docs: create example with external output
Browse files Browse the repository at this point in the history
  • Loading branch information
keindev committed Nov 26, 2022
1 parent e74c9fa commit af34490
Showing 1 changed file with 50 additions and 35 deletions.
85 changes: 50 additions & 35 deletions example/app.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,60 @@
/* eslint-disable */
import TaskTree from '../src/TaskTree.js';
import TaskTree from '../src/index.js';
import Enquirer from 'enquirer';

const tree = TaskTree.tree();

// start task tree log update in terminal
tree.start();

// create tasks
const task1 = tree.add('{underline Task {bold #1}}');
const task2 = tree.add('Task {bold #2}');
const task3 = task2.add('Subtask...');
const tpl = ':bar :rate/bps {cyan.bold :percent} :etas';
// create progress bars
const bars = [task3.bar(tpl), task3.bar(tpl), task3.bar(tpl)];

// ... whatever
let once = false;
const promises = [50, 75, 200].map((ms, i) => {
return new Promise(resolve => {
const handle = setInterval(() => {
const bar = bars[i];

if (bar) {
if (once) {
if (bar.percent >= 50) {
bar.skip();
try {
// start task tree log update in terminal
tree.start();

// create tasks
const task1 = tree.add('{underline Task {bold #1}}');
const task2 = tree.add('Task {bold #2}');
const task3 = task2.add('Subtask...');
const tpl = ':bar :rate/bps {cyan.bold :percent} :etas';
// create progress bars
const bars = [task3.bar(tpl), task3.bar(tpl), task3.bar(tpl)];

// ... whatever
let once = false;
const promises = [50, 75, 200].map((ms, i) => {
return new Promise(resolve => {
const handle = setInterval(() => {
const bar = bars[i];

if (bar) {
if (once) {
if (bar.percent >= 50) {
bar.skip();
} else {
bar.fail();
}
} else {
bar.fail();
once = bar.tick(Math.random() * 10).isCompleted;
}
} else {
once = bar.tick(Math.random() * 10).isCompleted;
}
}

if (once) {
clearInterval(handle);
resolve('');
}
}, ms);
if (once) {
clearInterval(handle);
resolve();
}
}, ms);
});
});

await Promise.all(promises);

await tree.pause(async () => {
const prompt = new Enquirer.Input({ name: 'username', message: 'What is your username?' });

await prompt.run().then(answer => console.log('Username:', answer));

// Return count of output rows if necessary to remove its from output.
// TaskTree can't automatically detect external output rows count
return 2;
});
});

Promise.all(promises).then(() => {
// skip task
task3.skip('Subtask skipped');
// log info message in Task #2, complete task
Expand All @@ -50,4 +63,6 @@ Promise.all(promises).then(() => {
task1.warn('Warning message').error(new Error('Something bad happened'), true);
// stop task tree log update
tree.stop();
});
} catch (error) {
tree.fail(error);
}

0 comments on commit af34490

Please sign in to comment.