Linux Bash-使用颜色代码获取子字符串

Linux Bash-使用颜色代码获取子字符串,linux,string,bash,Linux,String,Bash,在bash中,有没有一种方法可以在保留颜色代码的同时获得特定长度的子字符串(作为在终端中占用的#个字符) 我想我可以通过一些代码更好地解释我的意思(假装粗体表示绿色): 我制作了一个函数,用于复制/粘贴演示,获取ls的输出并使其正好填充终端的一行(或更短): 函数lsline{ 颜色输出=$(ls--color=always | tr“\n”“”) max_len=`tput cols` cur_len=0 是0吗 #这是将被打印出来的最终字符串的构建。 build=“” #这是任何转义序列的构

在bash中,有没有一种方法可以在保留颜色代码的同时获得特定长度的子字符串(作为在终端中占用的#个字符)

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

我制作了一个函数,用于复制/粘贴演示,获取ls的输出并使其正好填充终端的一行(或更短):

函数lsline{
颜色输出=$(ls--color=always | tr“\n”“”)
max_len=`tput cols`
cur_len=0
是0吗
#这是将被打印出来的最终字符串的构建。
build=“”
#这是任何转义序列的构建,所以我知道不要
#将其包含在子字符串计数中
esc_build=“”

对于((i=0;i使用正则表达式跳过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

使用正则表达式跳过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

这如何适用于包含多个颜色代码的字符串?”\e[1;32mGreen\e[0m和\e[1;31mRed\e[0m”本质上,您需要解析字符串,这不是任何shell语言的强项。对于彩色字符串的外观没有特定的标准。这些字节不是标记标记;它们只是终端改变其显示以下文本方式的指令。这如何适用于具有多个颜色代码的字符串?“\e[1;32mGreen\e[0m和\e[1;31mRed\e[0m”本质上,您需要解析字符串,这不是任何shell语言的强项。对于彩色字符串的外观没有特定的标准。这些字节不是标记标记;它们只是终端改变其显示以下文本方式的指令。
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"
}
$ 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