From 3b3d64cf62e95e3a7251042720a2fc937ccadd17 Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Thu, 27 Oct 2022 00:36:32 -0600 Subject: [PATCH] spinny logs --- bot.js | 13 ++++++++++++- src/util/log/log.js | 5 +++-- src/util/log/ora.js | 12 ++++++++++++ src/util/misc/wait.js | 7 +++++++ 4 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 src/util/log/ora.js create mode 100644 src/util/misc/wait.js diff --git a/bot.js b/bot.js index b7dc006..70a8581 100644 --- a/bot.js +++ b/bot.js @@ -1,7 +1,13 @@ const logger = require('./src/util/log/log'); +const ora = require('./src/util/log/ora'); +const wait = require('./src/util/misc/wait'); +const chalk = require('chalk'); + const client = {test: 'e', config: {logLevel: 1}}; + const loggers = logger(client); const log = loggers.log; + log(); log("Hello World!") log("Hello in blue", {color: 'blue'}); @@ -15,4 +21,9 @@ log('regular line'); log('a custom fancy pink color', {color: '#ff00ff', sourceColor: '#660066'});*/ loggers.warn("A warning appears!"); loggers.error("Something bad happened!"); -loggers.success("Something good happened!"); \ No newline at end of file +loggers.success("Something good happened!"); +const r = async () => { + await ora(chalk.yellowBright("Waiting for a slow connection..."), wait(2000), () => loggers.success("Connected!", 0, true)); + log('after thingy'); +}; +r(); \ No newline at end of file diff --git a/src/util/log/log.js b/src/util/log/log.js index ebdc271..7345627 100644 --- a/src/util/log/log.js +++ b/src/util/log/log.js @@ -13,8 +13,9 @@ let defaultOptions = { suffix: " >> " }; -const tlog = (client) => (message = "Test Log", options = {}, newLine = false, spacer = false) => { +const tlog = (client) => (message = "Test Log", options = {}, prenl = false, postnl = false) => { let opt = {}; + if (typeof options !== 'object') {options = {};} opt.color = options.color || defaultOptions.color; opt.level = ['string', 'number'].includes(typeof options.level) ? options.level : defaultOptions.level; opt.suffix = typeof options.suffix === 'string' ? options.suffix : defaultOptions.suffix; @@ -25,7 +26,7 @@ const tlog = (client) => (message = "Test Log", options = {}, newLine = false, s client.config.logLevel = getLevel(client.config.logLevel); if (client.config.logLevel < opt.level) {return;} } - console.log(`${spacer ? '\n' : ''}${(opt.sourceColor.startsWith('#') ? chalk.hex(opt.sourceColor) : chalk[opt.sourceColor])(`[${opt.source.toUpperCase()}]`)}${opt.suffix}${options.nc || options.noColor ? message : (opt.color.startsWith('#') ? chalk.hex(opt.color) : chalk[opt.color])(message)}${newLine ? '\n' : ''}`); + console.log(`${prenl ? '\n' : ''}${(opt.sourceColor.startsWith('#') ? chalk.hex(opt.sourceColor) : chalk[opt.sourceColor])(`[${opt.source.toUpperCase()}]`)}${opt.suffix}${options.nc || options.noColor ? message : (opt.color.startsWith('#') ? chalk.hex(opt.color) : chalk[opt.color])(message)}${postnl ? '\n' : ''}`); }; module.exports = (client) => { diff --git a/src/util/log/ora.js b/src/util/log/ora.js new file mode 100644 index 0000000..7b0f6ca --- /dev/null +++ b/src/util/log/ora.js @@ -0,0 +1,12 @@ +const ora = require('ora'); + +module.exports = async (text, promise, callback) => { + let spin = ora(text).start(); + await promise + .catch(e => callback(spin, e)) + .then((...args) => { + spin.stop(); + spin.clear(); + callback(spin, args); + }); +}; \ No newline at end of file diff --git a/src/util/misc/wait.js b/src/util/misc/wait.js new file mode 100644 index 0000000..8589f00 --- /dev/null +++ b/src/util/misc/wait.js @@ -0,0 +1,7 @@ +module.exports = (time) => { + return new Promise(function (resolve) { + setTimeout(function () { + resolve(null); + }, time); + }); +}; \ No newline at end of file