开发者

How can I add color between \/ in terminal?

I am making text ASCII art for my .profile in terminal, and trying to colorize it. At first I as going to use the cat command and heredoc for printing out my art, but then I couldn't get the colors inside of the heredoc to work. So I went with the dirty fix, I am using echo -e for each line and then coloring it. If there's a better way, please let me know! Right now, I am having this problem.

Full picture:

    _            _
 __| |_ __  __ _| |__
/ _` | '  \/ _` | / /
\__,_|_|_|_\__,_|_\_\

Part that I am coloring:

/ _` | '  \/ _` | / /

Coloring:

echo -e "\033[37m/ _\` |\033[36m '  \\\033[1;35m/ _\` | / /";

Outputs:

/ _` | '  \033[1;35m/ _` | / /

As you can see, I am trying to insert a new color in between the \/. T开发者_JAVA技巧he \ is treating the \033[1;35m literally. Is there a way to color the change the color between the \/ without altering the image?

Also, I am using Mac OSX Lion.


Instead of a heredoc you may use the $'string' feature of Bash which makes it possible to directly use ANSI C escape sequences for colouring output.

man bash | less -p "\\$'string'"

(
asciiart=$'
    _            _
 __| |_ __  __ _| |__
\033[37m/ _` |\033[36m \'  \\\033[1;35m/ _` | / /\033[m
\\__,_|_|_|_\\__,_|_\\_\\
'
echo "$asciiart" | sed '1d;$d'
)

To increase readability you may want to try figlet.

http://rudix.org/packages-def.html#figlet


Try with 5 bars instead of 3 \\\\\033[1;35m/

As for why, bash escape \\\\ to \\ then echo -e, escape it again to \. If you enable set -x (trace mode) you will see the command executed after bash processing (set +x to disable it).


What about simply using a few lines of POSIX printf

printf "\e[37m/ _\` |\e[36m \....\n"

instead of messing with all the pesky escape problems?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜