Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is there a way to get the SVN controlled folders list with different colors in the terminal? Like an alias for "ls". So when we type "ls", all the folders with an ".svn" sub-folder should be yellow for example?

Thanks. (Sorry if that is a stupid question.)

share|improve this question

migrated from stackoverflow.com Jul 4 '12 at 13:51

This question came from our site for professional and enthusiast programmers.

    
this link seem like what you wanted. –  Jasonw Apr 13 '12 at 2:12
    
@Jasonw: Not quite. That link talks about the .svn directory only, not which are part of a SVN repo. –  Ignacio Vazquez-Abrams Apr 13 '12 at 3:07
    
This is duplicate question. Please have a look this link. stackoverflow.com/questions/4667887/… –  Sergey Beduev Apr 14 '12 at 19:11
    
Thanks. Sorry for the duplicate. –  symbolix Apr 18 '12 at 13:43

1 Answer 1

You'd need a wrapper around ls to do this.

I wrote such a wrapper below...

How it basically works:

  • ls -F is forced because directories are then guaranteed to have a / suffix that can be detected.
  • To resolve a directory's name to its location, either the current directory is used or the first non-dash argument (presumed to be a target directory).
  • The foreground/background colors are achieved using the first number sets of each line. The XTerm-style numbers are triplets, so the 3rd and 6th numbers are the colors (19 and 85 in my example). The colors can of course be changed to anything you want.
  • This relies on original versions of svn where .svn was present in every subdirectory. If you're using the latest version where .svn is only present at the top, the script should change to "drill upward" from a "$root/$dir" until it finds a parent directory containing .svn.

Here is my entire script (use instead of ls directly):

#!/usr/bin/env perl
# by Kevin Grant (kevingrant.engineer@gmail.com)
use FileHandle;
my $term = (exists $ENV{'TERM'} && defined $ENV{'TERM'}) ? $ENV{'TERM'} : 'vt100';
my $is_xterm = ($term =~ /xterm/);
my $ifh = new FileHandle("ls -F @ARGV|");
(defined $ifh) or die;
my $root = '.';
foreach (@ARGV) {
  (! /^\-/) and $root = $_; # assume bare argument is the directory being listed
}
foreach (<$ifh>) {
  # 1st is for "ls -l" output (no spaces supported), 2nd is normal "ls" output
  if (m|\s(\S+)/| or m|^([^/]+/)|) {
    my $dir = $1;
    if (-e "$root/$dir/.svn") {
      # has a ".svn" subdirectory; colorize it
      if ($is_xterm) {
        s/\Q${dir}\E/\033[48;5;19;38;5;85m${dir}\033[0m/;
      } else {
        s/\Q${dir}\E/\033[44;36m${dir}\033[0m/;
      }
    }
  }
  print;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.