4wk_
4wk_

Reputation: 2733

SVN : Add colors on command-line svn with awk (in bash)

EDIT: Here is the GitHub updated version of this snippet, stable.


Here is a part of Bash code (I put it in .bashrc file) which works:

function svn {
  command svn "$@" | awk '
  BEGIN {
    cpt_c=0;
  }
  {
    if        ($1=="C") {
      cpt_c=cpt_c+1;
      print "\033[31m" $0 "\033[00m";  # Conflicts are displayed in red
    }
    else if   ($1=="A") {
      print "\033[32m" $0 "\033[00m";  # Add in green
    }
    else if   ($1=="?") {
      print "\033[36m" $0 "\033[00m";  # New in cyan
    }
    else if   ($1=="D") {
      print "\033[35m" $0 "\033[00m";  # Delete in magenta
    }
    else                {
      print $0;                        # No color, just print the line
    }
  }
  END {
    print cpt_c, " conflicts are found.";
  }';
}

This part of code do exactly what I want. svn functions (status, update, etc.) are printed with colors. For internally needs, I don't want to install such things as colorsvn or svn-color.

Here is an example of the code above:
svn command displayed in colors

And again, that is perfectly what I want. The problem happens when a conflict is found (for instance when I do an update): indeed, when a file is conflicted, svn is handed over to the user, to let him type a few letters (see below)

# svn update
Conflict discovered in 'test.txt'.
Select: (p) postpone, (df) diff-full, (e) edit,
        (mc) mine-conflict, (tc) theirs-conflict,
        (s) show all options:

Is there a way to awk to let the user type something (for instance e, which open in VIm the conflicted file), do something (for instance delete some lines after type e, then save&quit), and type another letter to confirm solving conflict, then finally display the next step of svn update (others files, etc)?

In other words, I want to "pause" the script which display colors to let user interact with svn, then svn update go on. I think it 'll be very useful for future to know how can let awk pause the caller script, then resume!

Upvotes: 7

Views: 3570

Answers (3)

JJD
JJD

Reputation: 51834

Although not particularly answering your question - I created a slightly modified and version of your script in which I bypass awk when running svn commit. Further, I added more colors to certain types of output. The script is available online - please feel free to commit your changes.

Upvotes: 2

Gedge
Gedge

Reputation: 315

Did you try to run svn update --accept postpone so that svn doesn't prompt for conflicts?

Upvotes: 3

Karolos
Karolos

Reputation: 329

Here's a simple and safe workaround (tested with your script). Because svn simply calls

system(getenv("SVN_EDITOR")) // or something similar

you simply can do the following (in bash, adjust for other shells):

export SVN_EDITOR="</dev/tty >&/dev/tty vi"

You will then be able to do your edits, and keep your coloring scheme from awk.

I don't know whether it works with other editors, but I hope it does. It should given these are standard redirects.

Upvotes: 0

Related Questions