monotone-colorize.pl 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!@l_prefix@/bin/perl
  2. ##
  3. ## monotone-colorize -- colorize output of mtn(1)
  4. ## Copyright (c) 2007 Ralf S. Engelschall <rse@engelschall.com>
  5. ##
  6. ## This program is free software; you can redistribute it and/or modify
  7. ## it under the terms of the GNU General Public License as published by
  8. ## the Free Software Foundation; either version 2 of the License, or
  9. ## (at your option) any later version.
  10. ##
  11. ## This program is distributed in the hope that it will be useful,
  12. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ## General Public License for more details.
  15. ##
  16. ## You should have received a copy of the GNU General Public License
  17. ## along with this program; if not, write to the Free Software
  18. ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  19. ## USA, or contact Ralf S. Engelschall <rse@engelschall.com>.
  20. ##
  21. ## monotone-colorize.pl: filter implementation (language: Perl)
  22. ##
  23. require 5.008;
  24. use Term::ANSIColor qw(color);
  25. # color definitions
  26. my $color = {
  27. -standout => color("bold"),
  28. -inverse => color("bold white on_black"),
  29. -positive => color("blue"),
  30. -negative => color("red"),
  31. -none => color("reset"),
  32. };
  33. # if connected to the TTY, colorize and connect to less(1)
  34. my $ontty = 0;
  35. if (-t STDOUT) {
  36. $ontty = 1;
  37. open(FP, "|@l_prefix@/bin/less -E -r");
  38. *STDOUT = *FP;
  39. }
  40. # determine mtn(1) command
  41. my $cmd = shift(@ARGV);
  42. # process mtn(1) output
  43. my $in = { -changelog => 0 };
  44. while (<STDIN>) {
  45. if ($ontty) {
  46. # dispatch according to mtn(1) command
  47. if ($cmd eq "update") {
  48. # colorize branch/revision information
  49. s/(expanded\s+selector\s+')([^']*)('\s+->\s+')([^']*)/$1$color->{-positive}$2$color->{-none}$3$color->{-positive}$4$color->{-none}/o;
  50. s/((?:updating\s+along\s+branch|expanding\s+selection)\s+')([^']*)(')/$1$color->{-positive}$2$color->{-none}$3/o;
  51. s/(expanded\s+to\s+')([^']*)(')/$1$color->{-standout}$2$color->{-none}$3/o;
  52. s/((?:already\s+up\s+to\s+date\s+at|selected\s+update\s+target|updated\s+to\s+base\s+revision)\s+)(\S+)/$1$color->{-standout}$2$color->{-none}/o;
  53. s/((?:switching\s+to\s+branch|next\s+commit\s+will\s+use\s+branch)\s+)(\S+)/$1$color->{-positive}$2$color->{-none}$3/o;
  54. # colorize status list
  55. s/(\s+dropping\s+)(\S+)/$1$color->{-negative}$2$color->{-none}/o;
  56. s/(\s+modifying\s+)(\S+)/$1$color->{-positive}$2$color->{-none}/o;
  57. s/(\s+adding\s+)(\S+)/$1$color->{-standout}$color->{-positive}$2$color->{-none}/o;
  58. # colorize errors
  59. s/(misuse:)(.+)$/$1$color->{-negative}$2$color->{-none}/o;
  60. }
  61. elsif ($cmd eq "status") {
  62. # colorize headers
  63. s/^(Current branch:\s+)(\S+)/$1$color->{-standout}$2$color->{-none}/o;
  64. s/^(Changes against parent\s+)(\S+)/$1$color->{-standout}$2$color->{-none}/o;
  65. # colorize status list
  66. s/^(\s+patched\s+)(\S+)/$1$color->{-positive}$2$color->{-none}/o;
  67. s/^(\s+dropped\s+)(\S+)/$1$color->{-negative}$2$color->{-none}/o;
  68. s/^(\s+added\s+)(\S+)/$1$color->{-standout}$color->{-positive}$2$color->{-none}/o;
  69. }
  70. elsif ($cmd eq "log") {
  71. # colorize certs
  72. s/(Revision:\s+)(.+)/$color->{-inverse}$1$2$color->{-none}/o;
  73. s/((?:Ancestor|Author|Date|Branch|Tag):\s+)(.+)/$1$color->{-standout}$2$color->{-none}/o;
  74. # colorize changelog text
  75. if (m/---------------+/s) {
  76. $in->{-changelog} = 0;
  77. s/^([\s|\\\/o]\s*)?(---+)\r?\n$/$1.("_" x 78)."\n"/oes;
  78. }
  79. s/^([\s|\\\/o]\s*)(.+)$/$1$color->{-positive}$2$color->{-none}/o if ($in->{-changelog});
  80. $in->{-changelog} = 1 if (m/ChangeLog:\s*$/s);
  81. }
  82. elsif ($cmd eq "diff") {
  83. # expand tabs
  84. 1 while (s/\t/" " x (8 - ((length($`)-1) % 8))/e);
  85. # colorize diff header
  86. s/^Index:\s+(\S+).*$/("_" x 78)."\n"."$color->{-inverse} ".$1." $color->{-none}\n"/oes;
  87. s/^========+\s*$//os;
  88. # colorize unified diff
  89. s/^(@@.*$)/$color->{-standout}$1$color->{-none}/o;
  90. s/^(\+.*)$/$color->{-positive}$1$color->{-none}/o;
  91. s/^(---\s+\S+\s+.+)$/$color->{-negative}$1$color->{-none}/o;
  92. s/^(-([^-].*|))$/$color->{-negative}$1$color->{-none}/o;
  93. # colorize context diff
  94. s/^(--- \d+,\d+ ----.*)$/$color->{-standout}$1$color->{-none}/o;
  95. s/^(\*\*\* \d+,\d+ *\*\*\*.*)$/$color->{-standout}$1$color->{-none}/o;
  96. s/^(\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*)$/$color->{-standout}$1$color->{-none}/o;
  97. s/^(!.*)$/$color->{-positive}$1$color->{-none}/o;
  98. }
  99. elsif ($cmd eq "list-tags") {
  100. # colorize tag names and revisions
  101. s/^(\S+)(\s+)(\S+)(\s+)(.+)$/$color->{-positive}$1$color->{-none}$2$color->{-standout}$3$color->{-none}$4$5/o;
  102. }
  103. elsif ($cmd eq "list-branches") {
  104. # colorize branch names
  105. s/^(\S+)$/$color->{-positive}$1$color->{-none}/o;
  106. }
  107. }
  108. print STDOUT $_;
  109. }
  110. # close connection to less(1)
  111. if ($ontty) {
  112. close(FP);
  113. }