https://wiki.archlinux.org/api.php?action=feedcontributions&user=Thrasher&feedformat=atomArchWiki - User contributions [en]2024-03-28T13:10:28ZUser contributionsMediaWiki 1.41.0https://wiki.archlinux.org/index.php?title=Colored_Pacman_output&diff=11064Colored Pacman output2006-04-08T22:51:42Z<p>Thrasher: </p>
<hr />
<div>[[Category:Package Management]]<br />
<br />
<br />
{{i18n_links_start}}<br />
{{i18n_entry|English|Colored Pacman output}}<br />
{{i18n_entry|简体中文|让 Pacman 的输出带有色彩}}<br />
{{i18n_links_end}}<br />
<br />
== Colorizing the output of pacman ==<br />
Now that makepkg has colorized output, why not pacman as well? [http://www.gentoo.org/ Gentoo]'s package manager application named 'emerge' uses colors extensively, and as you can see in this [http://gentoo-portage.com/up_img/124.png screenshot], it greatly enhances readability.<br />
<br />
==== Scripts ====<br />
User citral uses the following script in his .bashrc:<br />
<br />
alias pacs="pacsearch"<br />
pacsearch () {<br />
echo -e "$(pacman -Ss $@ | sed \<br />
-e 's#current/.*#\\033[1;31m&\\033[0;37m#g' \<br />
-e 's#extra/.*#\\033[0;32m&\\033[0;37m#g' \<br />
-e 's#community/.*#\\033[1;35m&\\033[0;37m#g' \<br />
-e 's#^.*/.* [0-9].*#\\033[0;36m&\\033[0;37m#g' )"<br />
}<br />
<br />
Which is the cleanest solution. However, if you desire a system-wide script, do '''as root''':<br />
touch /usr/bin/pacs && chmod 755 /usr/bin/pacs<br />
<br />
and then paste this into /usr/bin/pacs as root:<br />
#!/bin/bash<br />
echo -e "$(pacman -Ss $@ | sed \<br />
-e 's#current/.*#\\033[1;31m&\\033[0;37m#g' \<br />
-e 's#extra/.*#\\033[0;32m&\\033[0;37m#g' \<br />
-e 's#community/.*#\\033[1;35m&\\033[0;37m#g' \<br />
-e 's#^.*/.* [0-9].*#\\033[0;36m&\\033[0;37m#g' )"<br />
<br />
You can substitute "pacs" in these lines for anything you like. You can also alias "pacs" to something else in your .bashrc, as done above.<br />
<br />
Using these commands is straightforward; simply use your new command instead of 'pacman', the rest is still the same!<br />
<br />
==== Alternatives ====<br />
qpkg in AUR also uses colored output and searches AUR and TURepositories too!<br />
<br />
Yet another alternative is to use this script, it emulates the output of pacman -Ss (with color!) but fetches the package list from the web instead. It searches the official repositories and AUR (both community and unsupported). Needs curl to be installed. <br />
<br />
#!/bin/bash<br />
QUERY="$1"<br />
SEARCH="http://archlinux.org/packages.php?s_keyword=$QUERY"<br />
echo -e "$(curl -sN $SEARCH | grep data \<br />
| sed -e '0~6d' \<br />
-e 's|<[^>]*>||g' -e 's|^[ \t]*||g' \<br />
-e '1~6s|$|/|g' -e '2~6s|$| |g' -e '3~6s|$| |g' \<br />
-e '5~6s|$|NEWLINE|g' \<br />
-e '4~6s|$|\\033[0;0mNEWLINE|g'\<br />
-e 's|^Testing|\\033[1;31m&|g' \<br />
-e 's|^Current|\\033[1;32m&|g' \<br />
-e 's|^Extra|\\033[1;36m&|g' \<br />
-e 's|^Unstable|\\033[1;31m&|g' \<br />
| tr -d '\n' | sed 's/NEWLINE/\n/g')"<br />
SEARCH="http://aur.archlinux.org/packages.php?K=$QUERY"<br />
echo -e "$(curl -sN $SEARCH | grep data \<br />
| sed -e '0~6d' -e '4~6d' \<br />
-e 's|<[^>]*>||g' -e 's|^[ \t]*||g' \<br />
-e '1~6s|$|/|g' -e '2~6s|$| |g' \<br />
-e '5~6s|$|NEWLINE|g' \<br />
-e '3~6s|$|\\033[0;0mNEWLINE|g' \<br />
-e 's|^community|\\033[1;33m&|g' \<br />
-e 's|^unsupported|\\033[1;35m&|g' \<br />
| tr -d '\n' | sed 's/NEWLINE/\n/g')"<br />
<br />
==== Using 'acoc' ====<br />
There is another, more general possibility of colorizing arbitrary command output.<br />
You can download the small [http://www.ruby-lang.org/en/ Ruby] tool [http://raa.ruby-lang.org/project/acoc/ acoc] (and its requirements, [http://raa.ruby-lang.org/project/ansicolor/ term-ansicolor] and [http://raa.ruby-lang.org/cache/ruby-tpty/ tpty]. ).<br />
tpty is not really required, but some applications like "ls" won't run with acoc otherwise (they need to be started from a terminal (or pseudo terminal, in this case), or else they behave differently).<br />
<br />
Installation is relatively straightforward, here's a quick walkthrough:<br />
<br />
$ tar xvzf tpty-0.0.1.tar.gz<br />
$ cd tpty-0.0.1<br />
$ ruby extconf.rb<br />
$ make<br />
$ ruby ./test.rb<br />
# make install<br />
<br />
$ tar xvzf term-ansicolor-1.0.1.tar.gz<br />
$ cd term-ansicolor-1.0.1<br />
# ruby install.rb<br />
<br />
And now acoc itself:<br />
<br />
$ tar xvzf acoc-0.7.1.tar.gz<br />
$ cd acoc-0.7.1<br />
# make install<br />
<br />
Now, just read the section "Advanced Installation" in acoc's INSTALL file, and configure acoc as you want to.<br />
Create a link for 'pacman' as well, since that's primarily what we're doing this for.<br />
Once acoc runs, you can add these lines to your acoc.conf:<br />
<br />
[pacman -Si]<br />
/^Name\s+:\s([\w.-]+)/ bold<br />
[pacman -Qi]<br />
/^Name\s+:\s([\w.-]+)/ bold<br />
[pacman -Qi$]<br />
/^([\w.-]+)\s([\w.-]+)/ bold,clear<br />
[pacman -Ss]<br />
/^([\w.-]+)\/([\w.-]+)\s+([\w.-]+)/ clear,bold,clear<br />
[pacman -Qs]<br />
/^([\w.-]+)\/([\w.-]+)\s+([\w.-]+)/ clear,bold,clear<br />
[pacman -Sl]<br />
/^([\w.-]+)\s([\w.-]+)\s([\w.-]+)/ clear,bold,clear<br />
[pacman -Qo]<br />
/^([\w.-\/]+)\sis\sowned\sby\s([\w.-]+)\s([\w.-]+)/ clear,bold,clear<br />
[pacman -Qe$]<br />
/^([\w.-]+)\s([\w.-]+)/ bold,clear<br />
[pacman -Qg$]<br />
/^([\w.-]+)\s([\w.-]+)/ clear,bold<br />
<br />
It might not be perfect, or particularly nice, but so far it works fine for me.<br />
The above lines just make pacman print all package names in bold, which is particularly helpful when doing e.g. "pacman -Ss xfce". If you like it more colorful, you can modify the lines as you want.<br />
Read the acoc documentation contained in the source package for more information.<br />
<br />
==== Links ====<br />
[http://bbs.archlinux.org/viewtopic.php?t=12430&postdays=0&postorder=asc&start=15 Forum thread]</div>Thrasher