From 7c48e86a9938aa0767c5cb4ec7222b2170c95011 Mon Sep 17 00:00:00 2001 From: Rodrigo Fernandes Date: Sun, 15 May 2016 23:43:51 +0100 Subject: [PATCH] Add icons and tags to files indicating the type of change --- src/file-list-printer.js | 10 +++- src/hoganjs-utils.js | 12 ++-- src/line-by-line-printer.js | 13 ++++- src/printer-utils.js | 19 +++++++ src/templates/diff2html-templates.js | 13 ++++- src/templates/file-summary-line.mustache | 6 +- src/templates/icon-file-added.mustache | 4 ++ src/templates/icon-file-changed.mustache | 4 ++ src/templates/icon-file-deleted.mustache | 4 ++ src/templates/icon-file-renamed.mustache | 4 ++ src/templates/icon-file.mustache | 3 + src/templates/line-by-line-file-diff.mustache | 11 +--- src/templates/tag-file-added.mustache | 1 + src/templates/tag-file-changed.mustache | 1 + src/templates/tag-file-deleted.mustache | 1 + src/templates/tag-file-renamed.mustache | 1 + src/ui/css/diff2html.css | 55 ++++++++++++++++--- 17 files changed, 132 insertions(+), 30 deletions(-) create mode 100644 src/templates/icon-file-added.mustache create mode 100644 src/templates/icon-file-changed.mustache create mode 100644 src/templates/icon-file-deleted.mustache create mode 100644 src/templates/icon-file-renamed.mustache create mode 100644 src/templates/icon-file.mustache create mode 100644 src/templates/tag-file-added.mustache create mode 100644 src/templates/tag-file-changed.mustache create mode 100644 src/templates/tag-file-deleted.mustache create mode 100644 src/templates/tag-file-renamed.mustache diff --git a/src/file-list-printer.js b/src/file-list-printer.js index 5b6d8ff..18ccbbe 100644 --- a/src/file-list-printer.js +++ b/src/file-list-printer.js @@ -11,17 +11,25 @@ var hoganUtils = require('./hoganjs-utils.js').HoganJsUtils; var baseTemplatesPath = 'file-summary'; + var iconsBaseTemplatesPath = 'icon'; function FileListPrinter() { } FileListPrinter.prototype.generateFileList = function(diffFiles) { + var lineTemplate = hoganUtils.template(baseTemplatesPath, 'line'); + var files = diffFiles.map(function(file) { - return hoganUtils.render(baseTemplatesPath, 'line', { + var fileTypeName = printerUtils.getFileTypeIcon(file); + var iconTemplate = hoganUtils.template(iconsBaseTemplatesPath, fileTypeName); + + return lineTemplate.render({ fileHtmlId: printerUtils.getHtmlId(file), fileName: printerUtils.getDiffName(file), deletedLines: '-' + file.deletedLines, addedLines: '+' + file.addedLines + }, { + fileIcon: iconTemplate }); }).join('\n'); diff --git a/src/hoganjs-utils.js b/src/hoganjs-utils.js index 0f050d7..70a5d12 100644 --- a/src/hoganjs-utils.js +++ b/src/hoganjs-utils.js @@ -20,10 +20,7 @@ } HoganJsUtils.prototype.render = function(namespace, view, params, configuration) { - var config = configuration || {}; - var templateKey = this._templateKey(namespace, view); - - var template = this._getTemplate(templateKey, config); + var template = this.template(namespace, view, configuration); if (template) { return template.render(params); } @@ -31,6 +28,13 @@ return null; }; + HoganJsUtils.prototype.template = function(namespace, view, configuration) { + var config = configuration || {}; + var templateKey = this._templateKey(namespace, view); + + return this._getTemplate(templateKey, config); + }; + HoganJsUtils.prototype._getTemplate = function(templateKey, config) { var template; diff --git a/src/line-by-line-printer.js b/src/line-by-line-printer.js index 401cb22..6086bec 100644 --- a/src/line-by-line-printer.js +++ b/src/line-by-line-printer.js @@ -14,18 +14,27 @@ var hoganUtils = require('./hoganjs-utils.js').HoganJsUtils; var baseTemplatesPath = 'line-by-line'; + var iconsBaseTemplatesPath = 'icon'; + var tagsBaseTemplatesPath = 'tag'; function LineByLinePrinter(config) { this.config = config; } LineByLinePrinter.prototype.makeFileDiffHtml = function(file, diffs) { - return hoganUtils.render(baseTemplatesPath, 'file-diff', { + var fileDiffTemplate = hoganUtils.template(baseTemplatesPath, 'file-diff'); + var fileIconTemplate = hoganUtils.template(iconsBaseTemplatesPath, 'file'); + var fileTagTemplate = hoganUtils.template(tagsBaseTemplatesPath, printerUtils.getFileTypeIcon(file)); + + return fileDiffTemplate.render({ file: file, fileDiffName: printerUtils.getDiffName(file), fileHtmlId: printerUtils.getHtmlId(file), diffs: diffs - }); + }, { + fileIcon: fileIconTemplate, + fileTag: fileTagTemplate + }) }; LineByLinePrinter.prototype.makeLineByLineHtmlWrapper = function(content) { diff --git a/src/printer-utils.js b/src/printer-utils.js index adf879e..1add31c 100644 --- a/src/printer-utils.js +++ b/src/printer-utils.js @@ -95,6 +95,25 @@ return 'unknown/file/path'; }; + PrinterUtils.prototype.getFileTypeIcon = function(file) { + var templateName = 'file-changed'; + + if (file.isRename) { + templateName = 'file-renamed'; + } else if (file.isCopy) { + templateName = 'file-renamed'; + } else if (file.isNew) { + templateName = 'file-added'; + } else if (file.isDeleted) { + templateName = 'file-deleted'; + } else if (file.newName !== file.oldName) { + // If file is not Added, not Deleted and the names changed it must be a rename :) + templateName = 'file-renamed'; + } + + return templateName; + }; + PrinterUtils.prototype.diffHighlight = function(diffLine1, diffLine2, config) { var linePrefix1, linePrefix2, unprefixedLine1, unprefixedLine2; diff --git a/src/templates/diff2html-templates.js b/src/templates/diff2html-templates.js index 6b11375..0dd9b78 100644 --- a/src/templates/diff2html-templates.js +++ b/src/templates/diff2html-templates.js @@ -1,11 +1,20 @@ (function() { if (!!!global.browserTemplates) global.browserTemplates = {}; -var Hogan = require("hogan.js");global.browserTemplates["file-summary-line"] = new Hogan.Template({code: function (c,p,i) { var t=this;t.b(i=i||"");t.b("
  • ");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b(" ");t.b(t.v(t.f("fileName",c,p,0)));t.b("");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b(" ");t.b(t.v(t.f("addedLines",c,p,0)));t.b("");t.b(t.v(t.f("deletedLines",c,p,0)));t.b("");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b("
  • ");return t.fl(); },partials: {}, subs: { }}); +var Hogan = require("hogan.js");global.browserTemplates["file-summary-line"] = new Hogan.Template({code: function (c,p,i) { var t=this;t.b(i=i||"");t.b("
  • ");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b(" ");t.b(t.rp("");t.b("\n" + i);t.b(" ");t.b(t.v(t.f("fileName",c,p,0)));t.b("");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b(" ");t.b(t.v(t.f("addedLines",c,p,0)));t.b("");t.b(t.v(t.f("deletedLines",c,p,0)));t.b("");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b("
  • ");return t.fl(); },partials: {"");t.b("\n" + i);t.b("
    ");t.b("\n" + i);t.b(" Files changed (");t.b(t.v(t.f("filesNumber",c,p,0)));t.b(")");t.b("\n" + i);t.b(" hide");t.b("\n" + i);t.b(" show");t.b("\n" + i);t.b("
    ");t.b("\n" + i);t.b("
      ");t.b("\n" + i);t.b(" ");t.b(t.t(t.f("files",c,p,0)));t.b("\n" + i);t.b("
    ");t.b("\n" + i);t.b("");return t.fl(); },partials: {}, subs: { }}); +global.browserTemplates["icon-file-added"] = new Hogan.Template({code: function (c,p,i) { var t=this;t.b(i=i||"");t.b("");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b("");return t.fl(); },partials: {}, subs: { }}); +global.browserTemplates["icon-file-changed"] = new Hogan.Template({code: function (c,p,i) { var t=this;t.b(i=i||"");t.b("");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b("");return t.fl(); },partials: {}, subs: { }}); +global.browserTemplates["icon-file-deleted"] = new Hogan.Template({code: function (c,p,i) { var t=this;t.b(i=i||"");t.b("");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b("");return t.fl(); },partials: {}, subs: { }}); +global.browserTemplates["icon-file-renamed"] = new Hogan.Template({code: function (c,p,i) { var t=this;t.b(i=i||"");t.b("");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b("");return t.fl(); },partials: {}, subs: { }}); +global.browserTemplates["icon-file"] = new Hogan.Template({code: function (c,p,i) { var t=this;t.b(i=i||"");t.b("");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b("");return t.fl(); },partials: {}, subs: { }}); global.browserTemplates["line-by-line-column-line-number"] = new Hogan.Template({code: function (c,p,i) { var t=this;t.b(i=i||"");t.b("");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b("
    ");t.b(t.t(t.f("blockHeader",c,p,0)));t.b("
    ");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b("");return t.fl(); },partials: {}, subs: { }}); global.browserTemplates["line-by-line-empty-diff"] = new Hogan.Template({code: function (c,p,i) { var t=this;t.b(i=i||"");t.b("");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b("
    ");t.b("\n" + i);t.b(" File without changes");t.b("\n" + i);t.b("
    ");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b("");return t.fl(); },partials: {}, subs: { }}); -global.browserTemplates["line-by-line-file-diff"] = new Hogan.Template({code: function (c,p,i) { var t=this;t.b(i=i||"");t.b("
    ");t.b("\n" + i);t.b("
    ");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b(" ");t.b(t.v(t.f("fileDiffName",c,p,0)));t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b("
    ");t.b("\n" + i);t.b("
    ");t.b("\n" + i);t.b("
    ");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b(" ");t.b(t.t(t.f("diffs",c,p,0)));t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b("
    ");t.b("\n" + i);t.b("
    ");t.b("\n" + i);t.b("
    ");t.b("\n" + i);t.b("
    ");return t.fl(); },partials: {}, subs: { }}); +global.browserTemplates["line-by-line-file-diff"] = new Hogan.Template({code: function (c,p,i) { var t=this;t.b(i=i||"");t.b("
    ");t.b("\n" + i);t.b("
    ");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b(" ");t.b(t.rp("");t.b("\n" + i);t.b(" ");t.b(t.v(t.f("fileDiffName",c,p,0)));t.b("");t.b("\n" + i);t.b(t.rp("");t.b("\n" + i);t.b("
    ");t.b("\n" + i);t.b("
    ");t.b("\n" + i);t.b("
    ");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b(" ");t.b(t.t(t.f("diffs",c,p,0)));t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b("
    ");t.b("\n" + i);t.b("
    ");t.b("\n" + i);t.b("
    ");t.b("\n" + i);t.b("
    ");return t.fl(); },partials: {"");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b("
    ");t.b(t.v(t.f("oldNumber",c,p,0)));t.b("
    ");t.b("\n" + i);t.b("
    ");t.b(t.v(t.f("newNumber",c,p,0)));t.b("
    ");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b("
    ");t.b("\n" + i);if(t.s(t.f("prefix",c,p,1),c,p,0,253,329,"{{ }}")){t.rs(c,p,function(c,p,t){t.b(" ");t.b(t.t(t.f("prefix",c,p,0)));t.b("");t.b("\n" + i);});c.pop();}if(t.s(t.f("content",c,p,1),c,p,0,361,435,"{{ }}")){t.rs(c,p,function(c,p,t){t.b(" ");t.b(t.t(t.f("content",c,p,0)));t.b("");t.b("\n" + i);});c.pop();}t.b("
    ");t.b("\n" + i);t.b(" ");t.b("\n" + i);t.b("");return t.fl(); },partials: {}, subs: { }}); global.browserTemplates["line-by-line-wrapper"] = new Hogan.Template({code: function (c,p,i) { var t=this;t.b(i=i||"");t.b("
    ");t.b("\n" + i);t.b(" ");t.b(t.t(t.f("content",c,p,0)));t.b("\n" + i);t.b("
    ");return t.fl(); },partials: {}, subs: { }}); +global.browserTemplates["tag-file-added"] = new Hogan.Template({code: function (c,p,i) { var t=this;t.b(i=i||"");t.b("ADDED");return t.fl(); },partials: {}, subs: { }}); +global.browserTemplates["tag-file-changed"] = new Hogan.Template({code: function (c,p,i) { var t=this;t.b(i=i||"");t.b("CHANGED");return t.fl(); },partials: {}, subs: { }}); +global.browserTemplates["tag-file-deleted"] = new Hogan.Template({code: function (c,p,i) { var t=this;t.b(i=i||"");t.b("DELETED");return t.fl(); },partials: {}, subs: { }}); +global.browserTemplates["tag-file-renamed"] = new Hogan.Template({code: function (c,p,i) { var t=this;t.b(i=i||"");t.b("RENAMED");return t.fl(); },partials: {}, subs: { }}); module.exports = global.browserTemplates; })(); diff --git a/src/templates/file-summary-line.mustache b/src/templates/file-summary-line.mustache index d4cae43..279d3f4 100644 --- a/src/templates/file-summary-line.mustache +++ b/src/templates/file-summary-line.mustache @@ -1,10 +1,6 @@
  • - - - + {{>fileIcon}} {{fileName}} diff --git a/src/templates/icon-file-added.mustache b/src/templates/icon-file-added.mustache new file mode 100644 index 0000000..cf855b9 --- /dev/null +++ b/src/templates/icon-file-added.mustache @@ -0,0 +1,4 @@ + diff --git a/src/templates/icon-file-changed.mustache b/src/templates/icon-file-changed.mustache new file mode 100644 index 0000000..19a150e --- /dev/null +++ b/src/templates/icon-file-changed.mustache @@ -0,0 +1,4 @@ + diff --git a/src/templates/icon-file-deleted.mustache b/src/templates/icon-file-deleted.mustache new file mode 100644 index 0000000..fc3b8f4 --- /dev/null +++ b/src/templates/icon-file-deleted.mustache @@ -0,0 +1,4 @@ + diff --git a/src/templates/icon-file-renamed.mustache b/src/templates/icon-file-renamed.mustache new file mode 100644 index 0000000..1bf266d --- /dev/null +++ b/src/templates/icon-file-renamed.mustache @@ -0,0 +1,4 @@ + diff --git a/src/templates/icon-file.mustache b/src/templates/icon-file.mustache new file mode 100644 index 0000000..514972c --- /dev/null +++ b/src/templates/icon-file.mustache @@ -0,0 +1,3 @@ + diff --git a/src/templates/line-by-line-file-diff.mustache b/src/templates/line-by-line-file-diff.mustache index c711788..23efff8 100644 --- a/src/templates/line-by-line-file-diff.mustache +++ b/src/templates/line-by-line-file-diff.mustache @@ -1,14 +1,9 @@
    - - - - - {{fileDiffName}} - + {{>fileIcon}} + {{fileDiffName}} + {{>fileTag}}
    diff --git a/src/templates/tag-file-added.mustache b/src/templates/tag-file-added.mustache new file mode 100644 index 0000000..3c32eb0 --- /dev/null +++ b/src/templates/tag-file-added.mustache @@ -0,0 +1 @@ +ADDED diff --git a/src/templates/tag-file-changed.mustache b/src/templates/tag-file-changed.mustache new file mode 100644 index 0000000..5e83001 --- /dev/null +++ b/src/templates/tag-file-changed.mustache @@ -0,0 +1 @@ +CHANGED diff --git a/src/templates/tag-file-deleted.mustache b/src/templates/tag-file-deleted.mustache new file mode 100644 index 0000000..f1aed8b --- /dev/null +++ b/src/templates/tag-file-deleted.mustache @@ -0,0 +1 @@ +DELETED diff --git a/src/templates/tag-file-renamed.mustache b/src/templates/tag-file-renamed.mustache new file mode 100644 index 0000000..9a67397 --- /dev/null +++ b/src/templates/tag-file-renamed.mustache @@ -0,0 +1 @@ +RENAMED diff --git a/src/ui/css/diff2html.css b/src/ui/css/diff2html.css index abab8d5..e02efe4 100644 --- a/src/ui/css/diff2html.css +++ b/src/ui/css/diff2html.css @@ -16,17 +16,13 @@ } .d2h-file-stats { - display: block; - text-align: center; - float: right; + display: flex; + margin-left: auto; font-size: 14px; - top: 2px; - position: relative; } .d2h-lines-added { text-align: right; - /*background-color: #ceffce;*/ border: 1px solid #b4e2b4; border-radius: 5px 0 0 5px; color: #399839; @@ -36,19 +32,21 @@ .d2h-lines-deleted { text-align: left; - /*background-color: #f7c8c8;*/ border: 1px solid #e9aeae; border-radius: 0 5px 5px 0; color: #c33; padding: 2px; vertical-align: middle; + margin-left: 1px; } .d2h-file-name-wrapper { - display: inline-flex; + display: flex; + align-items: center; width: 90%; font-family: "Source Sans Pro", "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 15px; + line-height: 15px; } .d2h-file-name { @@ -249,6 +247,7 @@ } .d2h-file-list-line { + display: flex; text-align: left; } @@ -286,6 +285,46 @@ .d2h-icon { vertical-align: middle; margin-right: 10px; + fill: currentColor; +} + +.d2h-deleted { + color: #c33; +} + +.d2h-added { + color: #399839; +} + +.d2h-changed { + color: #d0b44c; +} + +.d2h-moved { + color: #3572b0; +} + +.d2h-tag { + display: flex; + font-size: 10px; + margin-left: 5px; + padding: 0 2px; +} + +.d2h-deleted-tag { + border: #c33 1px solid; +} + +.d2h-added-tag { + border: #399839 1px solid; +} + +.d2h-changed-tag { + border: #d0b44c 1px solid; +} + +.d2h-moved-tag { + border: #3572b0 1px solid; } /*