gpt4 book ai didi

linux - Bash - 获取带有颜色代码的子字符串

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:24:38 24 4
gpt4 key购买 nike

在 bash 中有没有办法在保留颜色代码的同时获得一定长度的子字符串(如它在终端中占用的 # 个字符)?

我想我可以用一些代码更好地解释我的意思(假设粗体表示绿色):

$ # One string is green and another is normal.$ green_string="\e[1;32mFoo bar baz buzz.\e[0m"$ normal_string=`echo -e $green_string | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g"`$ $ # Echo both of them out to show the difference$ echo -e $green_stringFoo bar baz buzz.$ echo -e $normal_stringFoo bar baz buzz.$ $ # Get a substring of both.$ green_sub=${green_string:0:11}$ normal_sub=${normal_string:0:11}$ $ # Since the special characters are a part of the string, the colored$ # substring (rightfully) has fewer printable characters in it.$ echo -e "$green_sub\e[0m"Foo$ echo -e $normal_subFoo bar baz$ $ # Is there any built in way of doing something like this:$ green_sub=`some_method $green_string 11`$ normal_sub=`some_method $normal_string 11`$ echo -e "$green_sub\e[0m"; echo -e $normal_subFoo bar bazFoo bar baz

For people like me, here's a copy/paste version:

green_string="\e[1;32mFoo bar baz buzz.\e[0m"
normal_string=`echo -e $green_string | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g"`
echo -e $green_string
echo -e $normal_string
green_sub=${green_string:0:11}
normal_sub=${normal_string:0:11}
echo -e "$green_sub\e[0m"
echo -e $normal_sub
# green_sub=`some_method $green_string 11`
# normal_sub=`some_method $normal_string 11`
# echo -e "$green_sub\e[0m"; echo -e $normal_sub

我做了一个函数,为了复制/粘贴演示目的,它获取 ls 的输出并使其正好填满终端的一行(或更短):

function lsline {
color_out=$( ls --color=always | tr "\n" " " )
max_len=`tput cols`
cur_len=0
is_esc=0
# This is the build of the final string that will be printed out.
build=""
# This is the build of any escape sequence so I know not to
# include it in the substring count
esc_build=""
for (( i=0; i<${#color_out}; i++ )); do
char="${color_out:$i:1}"
# Is there an easier way to check if a char is an escape character?
code=$( printf %x "'$char" )

# Currently in an escape sequence, so don't count towards max length.
if [ "$is_esc" -eq "1" ]; then
esc_build="$esc_build$char"
if [ "$char" = "m" ]; then
is_esc=0
build="$build$esc_build"
esc_build=""
fi
elif [ "$code" = "1b" ]; then
# 27 is escape character.
is_esc=1
esc_build="$char"
else
# Not an escape sequence, so build our normal string and update
# the current substring length.
build="$build$char"
((cur_len++))
if [ "$cur_len" -eq "$max_len" ]; then
build="$build$( tput sgr0 )"
break
fi
fi
done

echo "$build"
}

此代码有效,但太慢了

请告诉我有更快/更简单的方法来做到这一点!

最佳答案

使用正则表达式跳过 ANSI 序列,只捕获“正常”文本。下面是捕获(可见)字符串的前 5 个字符的正则表达式示例,该示例同时应用于常规字符串和绿色字符串。

$ regex='\e\[[^m]*m(.{5}).*\e\[0m'
$ for str in '\e[1;32mFoo bar baz\e[0m' 'Foo bar baz'; do
> [[ $str =~ $regex ]] && substr=${BASH_REMATCH[1]}
> echo "$substr"
> done
Foo b
Foo b

关于linux - Bash - 获取带有颜色代码的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37397492/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com