我想连接两个 figlet 输出(不同颜色)
I would like to concatenate two figlet outputs (with different colors)
目前我有这样的输出格式:
{ echo "$(figlet buddhi)"; echo "$(figlet lw)"; }
_ _ _ _ _
| |__ _ _ __| | __| | |__ (_)
| '_ \| | | |/ _` |/ _` | '_ \| |
| |_) | |_| | (_| | (_| | | | | |
|_.__/ \__,_|\__,_|\__,_|_| |_|_|
_
| |_ __
| \ \ /\ / /
| |\ V V /
|_| \_/\_/
我想要这样的输出格式:
figlet buddhi lw
_ _ _ _ _ _
| |__ _ _ __| | __| | |__ (_) | |_ __
| '_ \| | | |/ _` |/ _` | '_ \| | | \ \ /\ / /
| |_) | |_| | (_| | (_| | | | | | | |\ V V /
|_.__/ \__,_|\__,_|\__,_|_| |_|_| |_| \_/\_/
原因是:我想给每个名字(buddhi, lw)涂上不同的颜色。但是,保留连续字符串的格式,或者最多 space 分隔,如上所述。
示例:
#COMMANDS CREATED INSIDE /ETC/BASH.BASHRC FILE
# USING ANSI COLORS
RED="\e[31m"
ORANGE="\e[33m"
BLUE="\e[94m"
GREEN="\e[92m"
STOP="\e[0m"
printf "${GREEN}"
printf "=================================\n"
printf "${ORANGE}"
figlet -f standard "Buddhi"
printf "${BLUE}"
figlet -f small "LW"
printf "${GREEN}"
printf "=================================\n"
printf "${STOP}"
将每个单词的行存储在数组中,逐行输出两个数组。由于“Buddhi”的第一行好像少了一个字,所以我把第一个字的最长行长存到一个变量里,用%-s
的格式来填充每一行。
#! /bin/bash
RED="\e[31m"
ORANGE="\e[33m"
BLUE="\e[94m"
GREEN="\e[92m"
STOP="\e[0m"
mapfile -t left < <(figlet -f standard "Buddhi")
mapfile -t right < <(figlet -f small "LW")
maxlength=0
for line in "${left[@]}" ; do
if (( ${#line} > maxlength )) ; then
maxlength=${#line}
fi
done
printf "${GREEN}"
printf "=================================\n"
for ((i=0; i<=${#left[@]}; ++i)) ; do
printf "${ORANGE}%-${maxlength}s ${GREEN}%s\n" "${left[i]}" "${right[i]}"
done
printf "${GREEN}"
printf "=================================\n"
printf "${STOP}"
代替 figlet
我将使用以下内容作为我的输入:
$ cat buddhi
_ _ _ _ _
| |__ _ _ __| | __| | |__ (_)
| '_ \| | | |/ _` |/ _` | '_ \| |
| |_) | |_| | (_| | (_| | | | | |
|_.__/ \__,_|\__,_|\__,_|_| |_|_|
$ cat lw
_
| |_ __
| \ \ /\ / /
| |\ V V /
|_| \_/\_/
假设figlet
为每个输入字符串生成相同数量的输出行,我们可以使用paste
(@
作为分隔符)和while/read
循环来生成所需的输出:
printf "${GREEN}"
printf "============================\n"
maxwidth=$(awk '{max=length([=11=]) > max ? length([=11=]) : max}END{print max}' buddhi)
while IFS='@' read -r col1 col2
do
printf "${ORANGE}%-*s ${BLUE}%s\n" "${maxwidth}" "${col1}" "${col2}"
done < <(paste -d"@" buddhi lw)
printf "${GREEN}"
printf "============================\n"
这会生成:
扩展到 3 个输入流:
printf "${GREEN}"
printf "============================\n"
max1=$(awk '{max=length([=12=]) > max ? length([=12=]) : max}END{print max}' buddhi)
max2=$(awk '{max=length([=12=]) > max ? length([=12=]) : max}END{print max}' lw)
while IFS='@' read -r col1 col2 col3
do
printf "${ORANGE}%-*s ${BLUE}%-*s ${RED}%s\n" "${max1}" "${col1}" "${max2}" "${col2}" "${col3}"
done < <(paste -d"@" buddhi lw buddhi)
printf "${GREEN}"
printf "============================\n"
这会生成:
如果您需要更短的版本:
printf "$GREEN=================================\n"
{ figlet Buddhi; echo 'EOF'; figlet LW; } | awk 'NF==1&&=="EOF" {noskip=1; next; } noskip==0 { f[++c]=[=10=]; next; } { printf "%s%s%s%s\n","'"$ORANGE"'",f[++k],"'"$BLUE"'",[=10=];}'
printf "$GREEN=================================\n"
tput sgr0
我建议使用 tput 来设置颜色,因为并非每个终端都知道您的转义序列
发明 shell 的人也发明了 awk 供 shell 调用来操作文本。这些转义序列不会在我的终端上改变颜色,它们只是按原样显示(幸运的是你可以看到脚本将它们放在哪里):
$ cat tst.sh
#!/usr/bin/env bash
awk '
BEGIN {
red = "\e[31m"
orange = "\e[33m"
blue = "\e[94m"
green = "\e[92m"
stop = "\e[0m"
}
{
val[(NR==FNR),FNR] = [=10=]
}
NR == FNR {
wid = length([=10=])
maxWid = ( wid > maxWid ? wid : maxWid )
}
END {
for ( lineNr=1; lineNr<=FNR; lineNr++ ) {
printf "%s%-*s%s%s%s\n", orange, maxWid, val[1,lineNr], blue, val[0,lineNr], stop
}
}
' <(cat Buddhi) <(cat LW)
$ ./tst.sh
\e[33m _ _ _ _ _ \e[94m _\e[0m
\e[33m| |__ _ _ __| | __| | |__ (_)\e[94m| |_ __\e[0m
\e[33m| '_ \| | | |/ _` |/ _` | '_ \| |\e[94m| \ \ /\ / /\e[0m
\e[33m| |_) | |_| | (_| | (_| | | | | |\e[94m| |\ V V /\e[0m
\e[33m|_.__/ \__,_|\__,_|\__,_|_| |_|_|\e[94m|_| \_/\_/\e[0m
因为我没有 figlet
,所以我 运行 这些文件的以上内容:
$ head Buddhi LW
==> Buddhi <==
_ _ _ _ _
| |__ _ _ __| | __| | |__ (_)
| '_ \| | | |/ _` |/ _` | '_ \| |
| |_) | |_| | (_| | (_| | | | | |
|_.__/ \__,_|\__,_|\__,_|_| |_|_|
==> LW <==
_
| |_ __
| \ \ /\ / /
| |\ V V /
|_| \_/\_/
只需更改脚本的最后一行:
' <(cat Buddhi) <(cat LW)
到
' <(figlet Buddhi) <(figlet LW)
使用实际 figlet
输出。
以上假定您只有 2 个 figlet
输出字符串要连接并且两组输出的长度相同,如果这些假设中的任何一个是错误的,很容易调整。
使用坐标
#!/bin/bash
RED='\e[31m'
GRN='\e[32m'
XY(){ printf "\e[;H"; }
mapfile -t frst < <(figlet -f standard "Buddhi")
mapfile -t scnd < <(figlet -f small "LW")
XY 1 1 "$GRN==============================================="; y=2
for line in "${frst[@]}"; { XY 0 $y "$RED$line"; ((y++)); }; y=2
for line in "${scnd[@]}"; { XY 35 $y "$GRN$line"; ((y++)); }
XY 1 8 "$GRN==============================================="
目前我有这样的输出格式:
{ echo "$(figlet buddhi)"; echo "$(figlet lw)"; }
_ _ _ _ _
| |__ _ _ __| | __| | |__ (_)
| '_ \| | | |/ _` |/ _` | '_ \| |
| |_) | |_| | (_| | (_| | | | | |
|_.__/ \__,_|\__,_|\__,_|_| |_|_|
_
| |_ __
| \ \ /\ / /
| |\ V V /
|_| \_/\_/
我想要这样的输出格式:
figlet buddhi lw
_ _ _ _ _ _
| |__ _ _ __| | __| | |__ (_) | |_ __
| '_ \| | | |/ _` |/ _` | '_ \| | | \ \ /\ / /
| |_) | |_| | (_| | (_| | | | | | | |\ V V /
|_.__/ \__,_|\__,_|\__,_|_| |_|_| |_| \_/\_/
原因是:我想给每个名字(buddhi, lw)涂上不同的颜色。但是,保留连续字符串的格式,或者最多 space 分隔,如上所述。
示例:
#COMMANDS CREATED INSIDE /ETC/BASH.BASHRC FILE
# USING ANSI COLORS
RED="\e[31m"
ORANGE="\e[33m"
BLUE="\e[94m"
GREEN="\e[92m"
STOP="\e[0m"
printf "${GREEN}"
printf "=================================\n"
printf "${ORANGE}"
figlet -f standard "Buddhi"
printf "${BLUE}"
figlet -f small "LW"
printf "${GREEN}"
printf "=================================\n"
printf "${STOP}"
将每个单词的行存储在数组中,逐行输出两个数组。由于“Buddhi”的第一行好像少了一个字,所以我把第一个字的最长行长存到一个变量里,用%-s
的格式来填充每一行。
#! /bin/bash
RED="\e[31m"
ORANGE="\e[33m"
BLUE="\e[94m"
GREEN="\e[92m"
STOP="\e[0m"
mapfile -t left < <(figlet -f standard "Buddhi")
mapfile -t right < <(figlet -f small "LW")
maxlength=0
for line in "${left[@]}" ; do
if (( ${#line} > maxlength )) ; then
maxlength=${#line}
fi
done
printf "${GREEN}"
printf "=================================\n"
for ((i=0; i<=${#left[@]}; ++i)) ; do
printf "${ORANGE}%-${maxlength}s ${GREEN}%s\n" "${left[i]}" "${right[i]}"
done
printf "${GREEN}"
printf "=================================\n"
printf "${STOP}"
代替 figlet
我将使用以下内容作为我的输入:
$ cat buddhi
_ _ _ _ _
| |__ _ _ __| | __| | |__ (_)
| '_ \| | | |/ _` |/ _` | '_ \| |
| |_) | |_| | (_| | (_| | | | | |
|_.__/ \__,_|\__,_|\__,_|_| |_|_|
$ cat lw
_
| |_ __
| \ \ /\ / /
| |\ V V /
|_| \_/\_/
假设figlet
为每个输入字符串生成相同数量的输出行,我们可以使用paste
(@
作为分隔符)和while/read
循环来生成所需的输出:
printf "${GREEN}"
printf "============================\n"
maxwidth=$(awk '{max=length([=11=]) > max ? length([=11=]) : max}END{print max}' buddhi)
while IFS='@' read -r col1 col2
do
printf "${ORANGE}%-*s ${BLUE}%s\n" "${maxwidth}" "${col1}" "${col2}"
done < <(paste -d"@" buddhi lw)
printf "${GREEN}"
printf "============================\n"
这会生成:
扩展到 3 个输入流:
printf "${GREEN}"
printf "============================\n"
max1=$(awk '{max=length([=12=]) > max ? length([=12=]) : max}END{print max}' buddhi)
max2=$(awk '{max=length([=12=]) > max ? length([=12=]) : max}END{print max}' lw)
while IFS='@' read -r col1 col2 col3
do
printf "${ORANGE}%-*s ${BLUE}%-*s ${RED}%s\n" "${max1}" "${col1}" "${max2}" "${col2}" "${col3}"
done < <(paste -d"@" buddhi lw buddhi)
printf "${GREEN}"
printf "============================\n"
这会生成:
如果您需要更短的版本:
printf "$GREEN=================================\n"
{ figlet Buddhi; echo 'EOF'; figlet LW; } | awk 'NF==1&&=="EOF" {noskip=1; next; } noskip==0 { f[++c]=[=10=]; next; } { printf "%s%s%s%s\n","'"$ORANGE"'",f[++k],"'"$BLUE"'",[=10=];}'
printf "$GREEN=================================\n"
tput sgr0
我建议使用 tput 来设置颜色,因为并非每个终端都知道您的转义序列
发明 shell 的人也发明了 awk 供 shell 调用来操作文本。这些转义序列不会在我的终端上改变颜色,它们只是按原样显示(幸运的是你可以看到脚本将它们放在哪里):
$ cat tst.sh
#!/usr/bin/env bash
awk '
BEGIN {
red = "\e[31m"
orange = "\e[33m"
blue = "\e[94m"
green = "\e[92m"
stop = "\e[0m"
}
{
val[(NR==FNR),FNR] = [=10=]
}
NR == FNR {
wid = length([=10=])
maxWid = ( wid > maxWid ? wid : maxWid )
}
END {
for ( lineNr=1; lineNr<=FNR; lineNr++ ) {
printf "%s%-*s%s%s%s\n", orange, maxWid, val[1,lineNr], blue, val[0,lineNr], stop
}
}
' <(cat Buddhi) <(cat LW)
$ ./tst.sh
\e[33m _ _ _ _ _ \e[94m _\e[0m
\e[33m| |__ _ _ __| | __| | |__ (_)\e[94m| |_ __\e[0m
\e[33m| '_ \| | | |/ _` |/ _` | '_ \| |\e[94m| \ \ /\ / /\e[0m
\e[33m| |_) | |_| | (_| | (_| | | | | |\e[94m| |\ V V /\e[0m
\e[33m|_.__/ \__,_|\__,_|\__,_|_| |_|_|\e[94m|_| \_/\_/\e[0m
因为我没有 figlet
,所以我 运行 这些文件的以上内容:
$ head Buddhi LW
==> Buddhi <==
_ _ _ _ _
| |__ _ _ __| | __| | |__ (_)
| '_ \| | | |/ _` |/ _` | '_ \| |
| |_) | |_| | (_| | (_| | | | | |
|_.__/ \__,_|\__,_|\__,_|_| |_|_|
==> LW <==
_
| |_ __
| \ \ /\ / /
| |\ V V /
|_| \_/\_/
只需更改脚本的最后一行:
' <(cat Buddhi) <(cat LW)
到
' <(figlet Buddhi) <(figlet LW)
使用实际 figlet
输出。
以上假定您只有 2 个 figlet
输出字符串要连接并且两组输出的长度相同,如果这些假设中的任何一个是错误的,很容易调整。
使用坐标
#!/bin/bash
RED='\e[31m'
GRN='\e[32m'
XY(){ printf "\e[;H"; }
mapfile -t frst < <(figlet -f standard "Buddhi")
mapfile -t scnd < <(figlet -f small "LW")
XY 1 1 "$GRN==============================================="; y=2
for line in "${frst[@]}"; { XY 0 $y "$RED$line"; ((y++)); }; y=2
for line in "${scnd[@]}"; { XY 35 $y "$GRN$line"; ((y++)); }
XY 1 8 "$GRN==============================================="