Difference between revisions of "Colored Pacman output"

From ArchWiki
Jump to: navigation, search
m (fx redir)
(21 intermediate revisions by 17 users not shown)
Line 1: Line 1:
[[Category:Package Management]]
+
#REDIRECT[[Pacman Tips#Color output]]
 
+
 
+
{{i18n_links_start}}
+
{{i18n_entry|English|Colored Pacman output}}
+
{{i18n_entry|简体中文|让 Pacman 的输出带有色彩}}
+
{{i18n_links_end}}
+
 
+
== Colorizing the output of pacman ==
+
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.
+
 
+
==== Scripts ====
+
User citral uses the following script in his .bashrc:
+
 
+
alias pacs="pacsearch"
+
pacsearch () {
+
        echo -e "$(pacman -Ss $@ | sed \
+
        -e 's#current/.*#\\033[1;31m&\\033[0;37m#g' \
+
        -e 's#extra/.*#\\033[0;32m&\\033[0;37m#g' \
+
        -e 's#community/.*#\\033[1;35m&\\033[0;37m#g' \
+
        -e 's#^.*/.* [0-9].*#\\033[0;36m&\\033[0;37m#g' )"
+
}
+
 
+
Which is the cleanest solution. However, if you desire a system-wide script, do '''as root''':
+
  touch /usr/bin/pacs && chmod 755 /usr/bin/pacs
+
 
+
and then paste this into /usr/bin/pacs as root:
+
  #!/bin/bash
+
  echo -e "$(pacman -Ss $@ | sed \
+
  -e 's#current/.*#\\033[1;31m&\\033[0;37m#g' \
+
  -e 's#extra/.*#\\033[0;32m&\\033[0;37m#g' \
+
  -e 's#community/.*#\\033[1;35m&\\033[0;37m#g' \
+
  -e 's#^.*/.* [0-9].*#\\033[0;36m&\\033[0;37m#g' )"
+
 
+
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.
+
 
+
Using these commands is straightforward; simply use your new command instead of 'pacman', the rest is still the same!
+
 
+
==== Alternatives ====
+
qpkg in AUR also uses colored output and searches AUR and TURepositories too!
+
 
+
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.
+
 
+
#!/bin/bash
+
QUERY="$1"
+
SEARCH="http://archlinux.org/packages.php?s_keyword=$QUERY"
+
echo -e "$(curl -sN $SEARCH | grep data \
+
  | sed -e '0~6d' \
+
  -e 's|<[^>]*>||g' -e 's|^[ \t]*||g' \
+
  -e '1~6s|$|/|g' -e '2~6s|$|  |g'  -e '3~6s|$| |g' \
+
  -e '5~6s|$|NEWLINE|g' \
+
  -e '4~6s|$|\\033[0;0mNEWLINE|g'\
+
  -e 's|^Testing|\\033[1;31m&|g' \
+
  -e 's|^Current|\\033[1;32m&|g' \
+
  -e 's|^Extra|\\033[1;36m&|g' \
+
  -e 's|^Unstable|\\033[1;31m&|g' \
+
  | tr -d '\n' | sed 's/NEWLINE/\n/g')"
+
SEARCH="http://aur.archlinux.org/packages.php?K=$QUERY"
+
echo -e "$(curl -sN $SEARCH | grep data \
+
  | sed -e '0~6d' -e '4~6d' \
+
  -e 's|<[^>]*>||g' -e 's|^[ \t]*||g' \
+
  -e '1~6s|$|/|g' -e '2~6s|$|  |g' \
+
  -e '5~6s|$|NEWLINE|g' \
+
  -e '3~6s|$|\\033[0;0mNEWLINE|g' \
+
  -e 's|^community|\\033[1;33m&|g' \
+
  -e 's|^unsupported|\\033[1;35m&|g' \
+
  | tr -d '\n' | sed 's/NEWLINE/\n/g')"
+
 
+
==== Using 'acoc' ====
+
There is another, more general possibility of colorizing arbitrary command output.
+
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]. ).
+
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).
+
 
+
Installation is relatively straightforward, here's a quick walkthrough:
+
 
+
$ tar xvzf tpty-0.0.1.tar.gz
+
$ cd tpty-0.0.1
+
$ ruby extconf.rb
+
$ make
+
$ ruby ./test.rb
+
# make install
+
 
+
$ tar xvzf term-ansicolor-1.0.1.tar.gz
+
$ cd term-ansicolor-1.0.1
+
# ruby install.rb
+
 
+
And now acoc itself:
+
 
+
$ tar xvzf acoc-0.7.1.tar.gz
+
$ cd acoc-0.7.1
+
# make install
+
 
+
Now, just read the section "Advanced Installation" in acoc's INSTALL file, and configure acoc as you want to.
+
Create a link for 'pacman' as well, since that's primarily what we're doing this for.
+
Once acoc runs, you can add these lines to your acoc.conf:
+
 
+
[pacman -Si]
+
/^Name\s+:\s([\w.-]+)/                              bold
+
[pacman -Qi]
+
/^Name\s+:\s([\w.-]+)/                              bold
+
[pacman -Qi$]
+
/^([\w.-]+)\s([\w.-]+)/                bold,clear
+
[pacman -Ss]
+
/^([\w.-]+)\/([\w.-]+)\s+([\w.-]+)/    clear,bold,clear
+
[pacman -Qs]
+
/^([\w.-]+)\/([\w.-]+)\s+([\w.-]+)/    clear,bold,clear
+
[pacman -Sl]
+
/^([\w.-]+)\s([\w.-]+)\s([\w.-]+)/              clear,bold,clear
+
[pacman -Qo]
+
/^([\w.-\/]+)\sis\sowned\sby\s([\w.-]+)\s([\w.-]+)/    clear,bold,clear
+
[pacman -Qe$]
+
/^([\w.-]+)\s([\w.-]+)/                bold,clear
+
[pacman -Qg$]
+
/^([\w.-]+)\s([\w.-]+)/                clear,bold
+
 
+
It might not be perfect, or particularly nice, but so far it works fine for me.
+
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.
+
Read the acoc documentation contained in the source package for more information.
+
 
+
==== Links ====
+
[http://bbs.archlinux.org/viewtopic.php?t=12430&postdays=0&postorder=asc&start=15 Forum thread]
+

Revision as of 13:45, 1 January 2010