Skip to content

Instantly share code, notes, and snippets.

@cubicdaiya
Created October 22, 2015 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cubicdaiya/8adfc08d91f8bacec57d to your computer and use it in GitHub Desktop.
Save cubicdaiya/8adfc08d91f8bacec57d to your computer and use it in GitHub Desktop.
colorized unidiff
--- unidiff.cpp 2015-10-23 08:47:36.000000000 +0900
+++ unidiffcolor.cpp 2015-10-23 08:51:06.000000000 +0900
@@ -14,6 +14,61 @@
using dtl::Diff;
using dtl::elemInfo;
using dtl::uniHunk;
+using dtl::SES_ADD;
+using dtl::SES_DELETE;
+using dtl::SES_COMMON;
+using dtl::Printer;
+using dtl::CommonPrinter;
+
+
+/**
+ * ses element printer class template
+ */
+template <typename sesElem, typename stream = ostream >
+class ChangeColorPrinter : public Printer < sesElem, stream >
+{
+public :
+ ChangeColorPrinter () : Printer < sesElem, stream > () {}
+ ChangeColorPrinter (stream& out) : Printer < sesElem, stream > (out) {}
+ ~ChangeColorPrinter () {}
+ void operator() (const sesElem& se) const {
+ switch (se.second.type) {
+ case SES_ADD:
+ this->out_ << "\033[32m+" << se.first << endl;
+ break;
+ case SES_DELETE:
+ this->out_ << "\033[31m-" << se.first << endl;
+ break;
+ case SES_COMMON:
+ this->out_ << SES_MARK_COMMON << se.first << endl;
+ break;
+ }
+ }
+};
+
+/**
+ * unified format element printer class template
+ */
+template <typename sesElem, typename stream = ostream >
+class UniHunkColorPrinter
+{
+public :
+ UniHunkColorPrinter () : out_(cout) {}
+ UniHunkColorPrinter (stream& out) : out_(out) {}
+ ~UniHunkColorPrinter () {}
+ void operator() (const uniHunk< sesElem >& hunk) const {
+ out_ << "\033[36m@@"
+ << " -" << hunk.a << "," << hunk.b
+ << " +" << hunk.c << "," << hunk.d
+ << " @@\033[0m" << endl;
+
+ for_each(hunk.common[0].begin(), hunk.common[0].end(), CommonPrinter< sesElem, stream >(out_));
+ for_each(hunk.change.begin(), hunk.change.end(), ChangeColorPrinter< sesElem, stream >(out_));
+ for_each(hunk.common[1].begin(), hunk.common[1].end(), CommonPrinter< sesElem, stream >(out_));
+ }
+private :
+ stream& out_;
+};
static void showStats (string fp1, string fp2);
static void unifiedDiff (string fp1, string fp2);
@@ -77,7 +132,9 @@
}
diff.composeUnifiedHunks();
- diff.printUnifiedFormat();
+ vector< uniHunk< sesElem > > uniHunks;
+ uniHunks = diff.getUniHunks();
+ for_each(uniHunks.begin(), uniHunks.end(), UniHunkColorPrinter< sesElem, ostream >(cout));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment