javascript-获取索引处字符的ANSI颜色
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-获取索引处字符的ANSI颜色,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3677字,纯文字阅读大概需要6分钟。
内容图文
![javascript-获取索引处字符的ANSI颜色](/upload/InfoBanner/zyjiaocheng/661/08087f8809d044bc9b39af286f1e0b2c.jpg)
我已经开发了couleurs NPM package,可以将其设置为将rgb方法附加到String.prototype:
> console.log("Hello World!".rgb(255, 0, 0)) // "Hello World!" in red
Hello World!
undefined
> "Hello World!".rgb(255, 0, 0)
'\u001b[38;5;196mHello World!\u001b[0m'
这很好.在索引i处获取ANSI颜色/字符样式的正确方法是什么?
可能可以使用一些正则表达式来破解它,但是我不确定那是否真的很好(但是,如果可以使用正确的实现方式,我并不反对)…我更希望使用一种本机的方式来获得颜色/ style通过访问tty解释的字符.
> function getStyle (input, i) { /* get style at index `i` */ return style; }
> getStyle("Hello World!".rgb(255, 0, 0), 0); // Get style of the first char
{
start: "\u001b[38;5;196m",
end: "\u001b[0m",
char: "H"
}
> getStyle("Hello " + "World!".rgb(255, 0, 0), 0); // Get style of the first char
{
start: "",
end: "",
char: "H"
}
当我们具有多种组合样式时,事情变得复杂:
> console.log("Green and Italic".rgb(0, 255, 0).italic())
Green and Italic
undefined
> getStyle("Green and Italic".rgb(0, 255, 0).italic(), 0);
{
start: "\u001b[3m\u001b[38;5;46m",
end: "\u001b[0m\u001b[23m",
char: "G"
}
> getStyle(("Bold & Red".bold() + " but this one is only red").rgb(255, 0, 0), 0);
{
start: "\u001b[38;5;196m\u001b[1m",
end: "\u001b[22m\u001b[0m",
char: "B"
}
> getStyle(("Bold & Red".bold() + " but this one is only red").rgb(255, 0, 0), 11);
{
start: "\u001b[38;5;196m",
end: "\u001b[0m",
char: "u"
}
> ("Bold & Red".bold() + " but this one is only red").rgb(255, 0, 0)
'\u001b[38;5;196m\u001b[1mBold & Red\u001b[22m but this one is only red\u001b[0m'
就像我说的那样,我正在寻找一种本机方式(也许使用子进程).
那么,如何获得索引i的字符的完整ANSI样式?
解决方法:
有两种方法可以将格式“添加”到文本,这就是其中一种.问题是您将文本和样式混合到同一个对象(文本字符串)中.类似于RTF
Here is some \b bold\b0 and {\i italic} text\par
但不同于说Word .DOC文件的本机格式(可与文本一起运行):
(text) Here is some bold and italic text\r
(chp) 13 None
4 sprmCFBold
5 None
6 sprmCFItalic
6 None
-左边的数字是具有特定格式的字符数.
后一种格式是您要查找的格式,因为您要在纯文本中索引字符.减去格式长度将显示您感兴趣的格式长度.根据您希望请求格式的次数,您可以只运行一次,也可以将格式化的文本缓存在某处.
一次性运行需要检查编码字符串的每个元素,如果不在颜色字符串中,则增加“文本”索引,如果是,则更新“最后看到的”颜色字符串.我添加了兼容的getCharAt函数以进行调试.
var str = '\u001b[38;5;196m\u001b[1mBo\x1B[22mld & Red\u001b[22m but this one is only red\u001b[0m';
const map = {
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" ]
};
String.prototype.getColorAt = function(index)
{
var strindex=0, color=[], cmatch, i,j;
while (strindex < this.length)
{
cmatch = this.substr(strindex).match(/^(\u001B\[[^m]*m)/);
if (cmatch)
{
// Global reset?
if (cmatch[0] == '\x1B[0m')
{
color = [];
} else
{
// Off code?
for (i=0; i<map.length; i++)
{
if (map[i][1] == cmatch[0])
{
// Remove On code?
for (j=color.length-1; j>=0; j--)
{
if (color[j] == map[i][0])
color.splice (j,1);
}
break;
}
}
if (j==map.length)
color.push (cmatch[0]);
}
strindex += cmatch[0].length;
} else
{
/* a regular character! */
if (!index)
break;
strindex++;
index--;
}
}
return color.join('');
}
String.prototype.getCharAt = function(index)
{
var strindex=0, cmatch;
while (strindex < this.length)
{
cmatch = this.substr(strindex).match(/^(\u001B\[[^m]*m)/);
if (cmatch)
{
strindex += cmatch[0].length;
} else
{
/* a regular character! */
if (!index)
return this.substr(strindex,1);
strindex++;
index--;
}
}
return '';
}
console.log (str);
color = str.getColorAt (1);
text = str.getCharAt (1);
console.log ('color is '+color+color.length+', char is '+text);
返回的颜色仍然是其原始的转义编码.通过将它们添加到原始映射数组中,可以使其返回某种常量.
内容总结
以上是互联网集市为您收集整理的javascript-获取索引处字符的ANSI颜色全部内容,希望文章能够帮你解决javascript-获取索引处字符的ANSI颜色所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。