Note that there are some explanatory texts on larger screens.

plurals
  1. POCode challenge: Bash prompt path shortener
    primarykey
    data
    text
    <p>I implemented a prompt path shortener for bash to be included in the PS1 environment variable, which shortens the working directory into something more compact but still descriptive. I'm curious what other ideas may exist.</p> <p>Here's the challenge:</p> <p>Create a bash function <code>_dir_chomp</code> which can be included into PS1 like this (line breaks inserted for readability):</p> <pre><code>PS1='\[\033[01;32m\]\u@\h\[\033[01;34m\] $( _dir_chomp "$(pwd)" 20 )\[\033[01;37m\]$(parse_git_branch)\[\033[01;34m\] \$\[\033[00m\] ' </code></pre> <p>with "20" being the parameter for the maximum length as soft limit. These are the examples:</p> <ol> <li><code>/usr/portage/media-plugins/banshee-community-extensions/files</code> becomes <code>/u/p/m/b/files</code></li> <li><code>/home/user1/media/video/music/live-sets</code> becomes <code>~/m/v/m/live-sets</code> (note the ~ character as replacement for $HOME)</li> <li><code>/home/user2/media</code> does NOT change (20 char limit not exceeded)</li> <li><code>/home/user1/this-is-a-very-long-path-name-with-more-than-20-chars</code> becomes <code>~/this-is-a-very-long-path-name-with-more-than-20-chars</code> (last component stays unshortened: soft limit)</li> <li><code>/home/user1/src</code> becomes <code>~/src</code> ($HOME always shortened)</li> <li><code>/home/user1/.kde4/share/config/kresources</code> becomes <code>~/.k/s/c/kresources</code> (note the prefixing dot is preserved)</li> </ol> <p>Current user is user1.</p> <p>It's allowed to call external interpreters like <code>awk</code>, <code>perl</code>, <code>ruby</code>, <code>python</code> but not compiled C programs or similar. In other words: external source files are not allowed, code must be inline. Shortest version wins. The length of the bash function body (and called sub functions) counts, means:</p> <pre><code>_sub_helper() { # this counts } _dir_chomp() { # these characters count (between { and }) _sub_helper "foobar" # _sub_helper body counts, too } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload