1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-02-18 12:00:02 +00:00

Enhance terminal colour support

This commit is contained in:
Jeremy Ruston 2024-10-19 17:05:34 +01:00
parent 2d89228d25
commit bd86723b72

View File

@ -46,9 +46,10 @@ exports.terminalQuestion = function(options) {
callback(err); // Pass the error to the callback callback(err); // Pass the error to the callback
}); });
// Prompt user for input // Prompt user for input
var prompt = exports.terminalColourText(promptText,"yellow"); var prompt = exports.terminalColourText(promptText,["yellow","bold"]);
if(defaultResult) { if(defaultResult) {
prompt += exports.terminalColourText(" (" + defaultResult + ")","blue"); prompt += " ";
prompt += exports.terminalColourText("(" + defaultResult + ")",["blue","underline"]);
} }
prompt += exports.terminalColourText(": "); prompt += exports.terminalColourText(": ");
rl.question(prompt,function(input) { rl.question(prompt,function(input) {
@ -61,43 +62,86 @@ exports.terminalQuestion = function(options) {
}); });
}; };
/*
Wrap a string in colour codes. Colour can be an array
*/
exports.terminalColourText = function(text,colour) { exports.terminalColourText = function(text,colour) {
return exports.terminalColour(colour) + text + exports.terminalColour("reset"); if(!$tw.utils.isArray(colour)) {
colour = [colour];
}
$tw.utils.each(colour,function(code) {
text = exports.terminalColour(code) + text + exports.terminalColour(code,true);
});
return text;
}; };
exports.terminalColour = function(colour) { /*
Returns a terminal colour string. Set "closed" to true to return the closing code
*/
exports.terminalColour = function(colour,closed) {
if(!$tw.browser && $tw.node && process.stdout.isTTY) { if(!$tw.browser && $tw.node && process.stdout.isTTY) {
if(colour) { if(colour) {
var code = exports.terminalColourLookup[colour]; var code = exports.terminalColourLookup[colour];
if(code) { if(code) {
return "\x1b[" + code + "m"; return "\x1b[" + code[closed ? 1 : 0] + "m";
} }
} else {
return "\x1b[0m"; // Cancel colour
} }
return "\x1b[0m"; // Reset
} }
return ""; return "";
}; };
exports.terminalColourLookup = { exports.terminalColourLookup = {
"reset": "0;0", // Modifiers
"black": "0;30", "reset": [0,0],
"red": "0;31", "bold": [1,22],
"green": "0;32", "dim": [2,22],
"yellow": "0;33", "italic": [3,23],
"brown/orange": "0;33", // Backwards compatbility "underline": [4,24],
"blue": "0;34", "overline": [53,55],
"purple": "0;35", "inverse": [7,27],
"cyan": "0;36", "hidden": [8,28],
"white": "0;37", "strikethrough": [9,29],
"gray": "0;90", // Colours
"light red": "0;91", "black": [30,39],
"light green": "0;92", "red": [31,39],
"light yellow": "0;93", "green": [32,39],
"light blue": "0;94", "yellow": [33,39],
"light purple": "0;95", "brown/orange": [33,39], // Backwards compatbility
"light cyan": "0;96", "blue": [34,39],
"light gray": "0;97" "magenta": [35,39], // Backwards compatbility
"purple": [35,39],
"cyan": [36,39],
"white": [37,39],
"gray": [90,39],
"light red": [91,39],
"light green": [92,39],
"light yellow": [93,39],
"light blue": [94,39],
"light magenta": [95,39],
"light purple": [95,39], // Backwards compatbility
"light cyan": [96,39],
"light gray": [97,39],
// Background colours
"black background": [40,49],
"red background": [41,49],
"green background": [42,49],
"yellow background": [43,49],
"brown/orange background": [43,49], // Backwards compatbility
"blue background": [44,49],
"magenta background": [45,49], // Backwards compatbility
"purple background": [45,49],
"cyan background": [46,49],
"white background": [47,49],
"gray background": [100,49],
"light red background": [101,49],
"light green background": [102,49],
"light yellow background": [103,49],
"light blue background": [104,49],
"light magenta background": [105,49],
"light purple background": [105,49], // Backwards compatbility
"light cyan background": [106,49],
"light gray background": [107,49]
}; };
/* /*