简体   繁体   中英

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 orsvn-color .

Here is an example of the code above:
以颜色显示的 svn 命令

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!

您是否尝试运行svn update --accept postpone以便svn不提示冲突?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM