Difference between revisions of "Colored Pacman output"

From ArchWiki
Jump to navigation Jump to search
(More relevant article, the pacman article doesn't mention "color" at all)
(9 intermediate revisions by 9 users not shown)
Line 1: Line 1:
[[Category:Package management (English)]]
#REDIRECT [[Color output in console#pacman]]
[[Category:Eye candy (English)]]
[[Category:HOWTOs (English)]]
{{i18n_entry|English|Colored Pacman output}}
{{i18n_entry|Español|Salida a Color en Pacman}}
{{i18n_entry|简体中文|让 Pacman 的输出带有色彩}}
{{i18n_entry|Türkçe|Renkli Pacman Çıktıları (Türkçe)}}
== 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 'portage' uses colors extensively, and as you can see in this [http://gentoo-portage.com/up_img/1026.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#core/.*#\\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:
  echo -e "$(pacman -Ss $@ | sed \
  -e 's#core/.*#\\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 ====
An alternative is to use this python 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).
import os
import re
import sys
import urllib2
OFFICIAL_QUERY = "http://archlinux.org/packages/search/\?q="
AUR_QUERY = "http://aur.archlinux.org/packages.php?K="
# Repos and colors
repos = {"Core":'32',"Extra":'36',"Testing":'31',"community":'33',"unsupported":'35'}
def strip_html(buffer):
    <nowiki>buffer = re.sub('<[^>]*>','',buffer)</nowiki>
    <nowiki>buffer = re.sub('(?m)^[ \t]*','',buffer)</nowiki>
    return buffer
def cut_html(beg,end,buffer):
    <nowiki>buffer = re.sub('(?s).*' + beg,'',buffer)</nowiki>
    <nowiki>buffer = re.sub('(?s)' + end + '.*','',buffer)</nowiki>
    return buffer
class RepoSearch:
    def __init__(self,keyword):
        self.keyword = keyword
        <nowiki>self.results = ''</nowiki>
        for name in ['official','aur']:
    def get_search_results(self,name):
        if name == "official":
            query = OFFICIAL_QUERY
        elif name == "aur":
            query = AUR_QUERY
        f = urllib2.urlopen( query + self.keyword )
        self.search_results = f.read()
    def preformat(self,header,a,b):
        <nowiki>self.buffer = cut_html('<table class=\"' + header + '\"[^>]*>','</table',self.search_results)</nowiki>
        self.buffer = strip_html(self.buffer)
        self.buffer = self.buffer.split('\n')
        self.buffer = [line for line in self.buffer if line]
        del self.buffer[a:b]
    def parse_results(self,name):
        <nowiki>self.buffer = ''</nowiki>
        if name == 'official':
            <nowiki>if re.search('<table class=\"results\"',self.search_results):</nowiki>
            <nowiki>elif re.search('<div class=\"box\">',self.search_results):</nowiki>
                <nowiki>temp = re.search('<h2 class=\"title\">([^<]*)</h2>',self.search_results)</nowiki>
                temp = temp.group(1)
                temp = temp.split()
                for i in range(0,3): del self.buffer[i]
                for i in temp: self.buffer.insert(temp.index(i) + 2,i)
        elif name == 'aur':
            <nowiki>p = re.compile('<td class=.data[^>]*>')</nowiki>
            self.buffer = self.search_results.split('\n')
            self.buffer = [strip_html(line) for line in self.buffer if p.search(line)]
        l = len(self.buffer)/6
        <nowiki>parsed_buf = ''</nowiki>
        for i in range(l):
            parsed_buf += self.buffer[i*6] + '/'
            parsed_buf += self.buffer[i*6+1] + ' '*(24-len(self.buffer[i*6] + self.buffer[i*6+1]))
            parsed_buf += self.buffer[i*6+2]
            if name == "official":
                parsed_buf += ' ' + self.buffer[i*6+3]
            parsed_buf += '\n' + self.buffer[i*6+4] + '\n'
        self.results += parsed_buf
    def colorize(self):
        for repo,repo_color in repos.iteritems():
            <nowiki>self.results = re.sub(repo + '/.*','\\033[1;' + repo_color + 'm' + '\g<0>' + '\\033[0;0m',self.results)</nowiki>
if __name__ == "__main__":
    if len(sys.argv) < 2:
        print "Usage: " + sys.argv[0] + " <keyword>"
    reposearch = RepoSearch(sys.argv[1])
==== 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]
==== Alternative ====
In AUR is avaible [http://aur.archlinux.org/packages.php?do_Details=1&ID=11827 PKGBUILD] with color patch for pacman from Vogo.

Latest revision as of 16:27, 5 May 2017