Skip to content

Instantly share code, notes, and snippets.

@Gabryxx7
Last active February 14, 2024 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gabryxx7/bd903b6af1a863d13876ecf0f4e524da to your computer and use it in GitHub Desktop.
Save Gabryxx7/bd903b6af1a863d13876ecf0f4e524da to your computer and use it in GitHub Desktop.
A one-file-import javascript file to color console.* functions output. Also includes a chat-like terminal print
const COLORS = {
RED1: '\x1b[38;5;9m',
GREEN1: '\x1b[38;5;77m',
YELLOW1: '\x1b[38;5;190m',
WHITE: '\x1b[38;5;231m',
CYAN1: '\x1b[38;5;44m',
ORANGE1: '\x1b[38;5;214m',
RESET: '\x1b[0m',
};
const BOX_CHARS = {
vLine: '\u2502',
hLine: '\u2500',
TopR: '\u256E',
TopL: '\u256D',
BottomR: '\u256F',
BottomL: '\u2570',
pad: '\u2591',
}
const TICK = {
Thin: {
TopL: '\u25F8',
TopR: '\u25F9',
BottomL: '\u25FA',
BottomR: '\u25FF'
},
Thick: {
TopL: '\u25E4',
TopR: '\u25E5',
BottomL: '\u25E3',
BottomR: '\u25E3',
},
Square: {
TopL: '\u250F',
TopR: '\u2513',
BottomL: '\u2517',
BottomR: '\u251B',
}
}
// args should be a normal array made from the function's arguments like Array.from(arguments)
const colouredConsoleOutput = function (color, args, fun) {
const LOG_PREFIX = color;
var args = Array.from(args);
// OR you can use: Array.prototype.slice.call( arguments );
// 2. Prepend log prefix log string
args.unshift(LOG_PREFIX);
args.push(COLORS.RESET);
// 3. Pass along arguments to console.log
fun.apply(console, args);
};
var log = console.log;
console.log = function () {
colouredConsoleOutput(COLORS.WHITE, Array.from(arguments), log);
};
var debug = console.debug;
console.debug = function () {
colouredConsoleOutput(COLORS.GREEN1, Array.from(arguments), debug);
};
var warn = console.warn;
console.warn = function () {
colouredConsoleOutput(COLORS.ORANGE1, Array.from(arguments), warn);
};
var error = console.error;
console.error = function () {
colouredConsoleOutput(COLORS.RED1, Array.from(arguments), error);
};
var info = console.info;
// console.info = function(){
// colouredConsoleOutput("\x1b[92m", Array.from(arguments), info);
// }
console.dump = function () {
var args = [].slice.call(arguments);
var argsWithObjectCopies = args.map(copyIfRegularObject);
return console.log.apply(console, argsWithObjectCopies);
};
function copyIfRegularObject(o) {
const isRegularObject = typeof o === 'object' && !(o instanceof RegExp);
return isRegularObject ? copyObject(o) : o;
}
function copyObject(o) {
return JSON.parse(JSON.stringify(o));
}
console.prompt = function () {
colouredConsoleOutput(COLORS.YELLOW1, Array.from(arguments), log);
};
console.answer = function () {
colouredConsoleOutput(COLORS.CYAN1, Array.from(arguments), log);
};
function chunkSubstr(str, size) {
const chunks = [];
let chunk = '';
const words = str.split(" ");
while(true){
if(chunk.length >= size || words.length <= 0){
chunks.push(chunk)
chunk = '';
if(words.length <= 0) break;
}
chunk += words.shift() +" ";
}
return chunks;
}
function textBox(str, customProps){
const props = {
boxWidth: 80,
boxOffset: 0,
color: COLORS.RESET,
tickPos: 0,
padding: 2,
// padding: { top: 2, bottom: 2, left: 3, right: 3},
...customProps
}
var lines = str.split('\n').map(l => chunkSubstr(l, props.boxWidth).map(s => s.trim())).flat();
// if(pos > 0) lines = lines.map(t => t.padStart(colLength*1.8, ' '))
const maxLen = Math.max(...lines.map(s => s.length));
const offset = props.boxWidth * props.boxOffset;
const lMargin = props.boxOffset > 0 ? ' '.repeat(offset + Math.abs(props.boxWidth - maxLen)) : '';
props.padding = (typeof props.padding === 'number') ? { top: props.padding-1, bottom: props.padding-1, left: props.padding, right: props.padding } : props.padding;
const pad = {};
pad.Left = ' '.repeat(props.padding.left);
pad.Right = ' '.repeat(props.padding.right);
const hBorder = BOX_CHARS.hLine.repeat(maxLen + props.padding.left + props.padding.right);
const lStart = lMargin + BOX_CHARS.vLine + pad.Left;
const lEnd = pad.Right + BOX_CHARS.vLine;
const emptyLine = "\n" + lStart + ' '.repeat(maxLen) + lEnd;
pad.Top = emptyLine.repeat(props.padding.top);
pad.Bottom = emptyLine.repeat(props.padding.bottom);
var textLines = '';
lines.forEach(line => {
textLines += '\n' + lStart + line.padEnd(maxLen) + lEnd;
})
const tickChars = TICK.Thick;
const corners = {
TopL: props.tickPos == 0 ? tickChars.TopL : BOX_CHARS.TopL,
TopR: props.tickPos == 1 ? tickChars.TopR : BOX_CHARS.TopR,
BottomL: props.tickPos == 2 ? tickChars.BottomL : BOX_CHARS.BottomL,
BottomR: props.tickPos == 3 ? tickChars.BottomR : BOX_CHARS.BottomR,
}
const box = {
top: '\n' + lMargin + corners.TopL + hBorder + corners.TopR,
content: textLines,
bottom: '\n' + lMargin + corners.BottomL + hBorder + corners.BottomR,
}
const boxString = box.top + pad.Top + box.content + pad.Bottom + box.bottom;
return props.color + boxString + COLORS.RESET;
// colouredConsoleOutput(color, boxString, log);
}
// console.log(boxText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque viverra lacus at eros facilisis cursus. Morbi ullamcorper cursus porta. Mauris eu congue eros. Quisque feugiat orci a urna interdum venenatis. Ut tincidunt turpis neque, quis facilisis erat mattis vel. Mauris quis sagittis nibh. Donec pharetra orci vitae commodo ultricies."));
console.chat = function(prompt, answer, gap=1, boxProps=null) {
console.log(textBox(prompt, { color: COLORS.CYAN1, boxOffset: 0.5, tickPos: 1, ...boxProps }) +
'\n'.repeat(gap) +
textBox(answer, { color: COLORS.YELLOW1, ...boxProps }) +
'\n'.repeat(gap-1)
);
}
export default console;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment