Open EdwardZZZ opened 7 years ago
const styles = {
bold : ['\x1B[1m', '\x1B[22m'],
italic : ['\x1B[3m', '\x1B[23m'],
underline : ['\x1B[4m', '\x1B[24m'],
inverse : ['\x1B[7m', '\x1B[27m'],
strikethrough: ['\x1B[9m', '\x1B[29m'],
white : ['\x1B[37m', '\x1B[39m'],
grey : ['\x1B[90m', '\x1B[39m'],
black : ['\x1B[30m', '\x1B[39m'],
blue : ['\x1B[34m', '\x1B[39m'],
cyan : ['\x1B[36m', '\x1B[39m'],
green : ['\x1B[32m', '\x1B[39m'],
magenta : ['\x1B[35m', '\x1B[39m'],
red : ['\x1B[31m', '\x1B[39m'],
yellow : ['\x1B[33m', '\x1B[39m'],
whiteBG : ['\x1B[47m', '\x1B[49m'],
greyBG : ['\x1B[100m', '\x1B[49m'],
blackBG : ['\x1B[40m', '\x1B[49m'],
blueBG : ['\x1B[44m', '\x1B[49m'],
cyanBG : ['\x1B[46m', '\x1B[49m'],
greenBG : ['\x1B[42m', '\x1B[49m'],
magentaBG : ['\x1B[45m', '\x1B[49m'],
redBG : ['\x1B[41m', '\x1B[49m'],
yellowBG : ['\x1B[43m', '\x1B[49m'],
};
type TColor = keyof typeof styles;
/**
* 转换颜色文本
* @param str text
* @param color color
* @returns msg with color info
*/
export function Color(str: string, color: TColor): string {
if (process.stdout.isTTY && color in styles) {
const [c1, c2] = styles[color];
return `${c1}${str}${c2}`;
}
return str;
}
export function red(str: string): string {
if (process.stdout.isTTY) {
const [c1, c2] = styles.red;
return `${c1}${str}${c2}`;
}
return str;
}
console.log(Color('hahahah123', 'yellow'), '\n');
console.log(Color(Color('hahahah123', 'white'), 'greyBG'));
const orignalSetItem = localStorage.setItem;
localStorage.setItem = function (key, newValue) {
const setItemEvent = new Event("setItemEvent");
setItemEvent.newValue = newValue;
window.dispatchEvent(setItemEvent);
orignalSetItem.apply(this, arguments);
}
window.addEventListener("setItemEvent", function (e) {
console.log(e.newValue);
});
window.addEventListener("storage", function (e) {
console.log(e);
console.log(e.newValue);
});
node --inspect-brk app.js