As of 2016-02-26, there will be no more posts for this blog. s/blog/pba/
Showing posts with label prompt. Show all posts

Have you ever done this?


git status

on a Mercurial repo and stared at the error message for a couple of seconds?

I had seen some PS1 with repo information and I thought it’s time for adding that to my Bash loadable Vim-like PS1.

I found a great project called vcprompt, which supports Git, Mercurial, Subversion, CVS, and Fossil. I only need the first two actually. It is fast and that is the point of why I chose to use this.

If your PS1 is Bash script based (I probably am the only one whose PS1 is written in C code on Earth), then you can use:


__git_ps1
hg branch

to get the current branch name. The first one (a Bash function) comes with Bash completion and it runs in reasonably fast speed, but Mercurial command isn’t that fast. I was giving up when I realized it would take almost one second for first run on a directory, then I saw vcprompt.

Not only it is fast but also I can have multiple VCS support at the same time with customizable output format, it is awesome. The only issue is I needed to re-code my PS1 C code because the position of the output of vcprompt.

However, it could be an advantage if I include vcprompt’s code in my C code. I made these changes and this is what my PS1 looks now:

http://farm8.staticflickr.com/7191/6820798790_e359d3b132_z.jpg

Prompt with VCS status information

A year ago, I didn't even know you can write a C code for Bash extension. Yesterday, I stumbled on Using and Writing Bash Dynamically Loadable Built-In Commands and I knew I could do something with this new discovery.

So, I wrote a C code for replacing my Bash version script. The speed is only three times fast, I am so disappointed I expected much faster. Anyway, it's really hardly can gain much from improving PS1, it's not as if your PS1 is required to update 1000 times per second.

You can go check some loadable builtins comes with Bash, they should be installed at /usr/lib[64]/bash/. Use enable <name> to enable and to disable with -d.

If you want to develop your own builtin, you will need to download the Bash source code. I doubt any distro will install header files of Bash for you. A basic ./configure is needed to generate necessary header files and also you will need to run make pathnames.h for that header at least.

Two and a half years ago, I wrote a PS1 which renders current working directory with colors and abbreviate directory names. Today, I recoded it a bit.

http://farm3.static.flickr.com/2049/2140298262_f6f0d3b090_o.png

Old PS1

http://farm5.static.flickr.com/4039/4664700598_e6da3e7c09_b.jpg

New PS1

Screenshot on top is the old one. You can see this diff view of my .bashrc. Generally, the newer one has same result, I only added color to the ~. I used the newer Bash syntax [[]] and (()) instead of the old test [].

My original plan was to use C code, unfortunately, those escape code, \[, \], or \e, must not be from $variable or results from subshell, they have to be apparent strings in PS1. If you know how to resolve this issue, please let me know. If you only want to shorten PWD and do not need these fancy colors, there is an one-liner1 version.

Two and a half years, many things have changed, you just can see from the screenshots. I have switched to Gentoo from Fedora, GNOME to Fluxbox, GNOME Terminal to urxvt, GNU/Screen to tmux (this didn’t show in screenshot), eyecandy (Compiz Fusion, IIRC) to plain, mainly use CLI. I used to run su -, now I just sudo. And the kernel version from 2.6.23 to 2.6.32.

Many things have changed.

[1]https://bbs.archlinux.org/viewtopic.php?pid=767150#p767150 is gone.