Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --pretty flag for array outputs in cli #54

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 117 additions & 6 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const fs = require("fs");
const figlet = require("figlet");
const chalk = require("chalk");
const path = "./.nc.config.js";
const Table = require("cli-table2");
pretty = require('pretty-cli')();

/**
* Display text as CLI logo
Expand All @@ -21,25 +23,133 @@ let logofied = text => {
logger(err, true);
return;
}
console.log(chalk.blue(data));
console.log(
pretty.log(chalk.blue(data));
pretty.log(
chalk.green("Sustainable Computing Research Group") +
chalk.blue(" (SCoRe)")
);
}
);
};

let tablerForArray = data => {
if (!Array.isArray(data)) {
return "";
}
let finalTable = new Table({
chars: {
top: "═",
"top-mid": "╤",
"top-left": "╔",
"top-right": "╗",
bottom: "═",
"bottom-mid": "╧",
"bottom-left": "╚",
"bottom-right": "╝",
left: "║",
"left-mid": "╟",
mid: "─",
"mid-mid": "┼",
right: "║",
"right-mid": "╢",
middle: "│"
}
});

let firstItem = data[0];
let head = ["#"];
if (typeof firstItem == "object") {
for (prop in firstItem) head.push(prop);
finalTable.push(head);
// making the body
data.forEach((item, index) => {
let temp = [index];
for (prop in item) {
temp.push(item[prop]);
}
finalTable.push(temp);
});
} else {
head.push("");
finalTable.push(head);
data.forEach((item, index) => {
finalTable.push([index, item]);
});
}
// console.log(finalTable);
return finalTable.toString();
};

let tablerForObject = data => {
let finalTable = new Table({
chars: {
top: "═",
"top-mid": "╤",
"top-left": "╔",
"top-right": "╗",
bottom: "═",
"bottom-mid": "╧",
"bottom-left": "╚",
"bottom-right": "╝",
left: "║",
"left-mid": "╟",
mid: "─",
"mid-mid": "┼",
right: "║",
"right-mid": "╢",
middle: "│"
}
});
let head = [];
let body = [];

// making the head of the table
for (prop in data) head.push(prop);
finalTable.push(head);

// making the body of the table
for (prop in data) {
if (typeof data[prop] == "object") {
if (Array.isArray(data[prop])) body.push(tablerForArray(data[prop]));
else {
body.push(tablerForObject(data[prop]));
}
} else {
body.push(data[prop]);
}
}
finalTable.push(body);
return finalTable.toString();
};

let tabler = data => {
if (Array.isArray(data)) {
return tablerForArray(data);
} else if (typeof data == "object") {
return tablerForObject(data);
} else {
return data;
}
};

/**
* Logs the values with colors according to the type
* @param {*} data
* @param {*} error
*/
let logger = (data, error) => {

let logger = (data, error, table) => {
let message = JSON.parse(data);
if (table) {
message = tabler(message);
} else {
message = JSON.stringify(message);
}

if (error) {
console.log(chalk.red(`${JSON.stringify(data, null, 2)}`));
pretty.error(chalk.red(chalk.bgWhite(message)));
} else {
console.log(chalk.green(`${JSON.stringify(data, null, 2)}`));
pretty.log(chalk.green(chalk.bgWhite(message)));
}
};

Expand All @@ -52,8 +162,9 @@ let checkPlugin = plugin => {
detectInstalled(plugin).then(exists => {
if (exists) {
resolve(true);
} else {
resolve(false);
}
resolve(false);
});
});
};
Expand Down
Loading