login

<     >

2022-02-27 10:00:28 (UTC-03:00)

Marcel Rodrigues <marcelgmr@gmail.com>

add diff styling

diff --git a/app.lua b/app.lua
index 7e40b4e..ec39964 100644
--- a/app.lua
+++ b/app.lua
@@ -25,6 +25,32 @@ local function time_fmt(sig)
     return s
 end
 
+local function diff_cb(line_type, line)
+    line = lud.template.escape(line:sub(1, #line-1))
+    if line_type == " " then
+        line = ' <span class="diff_ctx">' .. line .. '</span>'
+    elseif line_type == "+" then
+        line = '+<span class="diff_add">' .. line .. '</span>'
+    elseif line_type == "-" then
+        line = '-<span class="diff_del">' .. line .. '</span>'
+    elseif line_type == "=" then
+        line = ' <span class="diff_ctx_nonl">' .. line .. '</span>'
+    elseif line_type == ">" then
+        line = ' <span class="diff_old_nonl">' .. line .. '</span>'
+    elseif line_type == "<" then
+        line = ' <span class="diff_new_nonl">' .. line .. '</span>'
+    elseif line_type == "F" then
+        line = '\n<span class="diff_file_hdr">' .. line .. '</span>'
+    elseif line_type == "H" then
+        line = '<span class="diff_hunk_hdr">' .. line .. '</span>'
+    elseif line_type == "B" then
+        line = '<span class="diff_bin">' .. line .. '</span>'
+    else
+        line = line_type .. line
+    end
+    return line .. "\n"
+end
+
 local routes = {
     {"GET", "/", function (req)
         local gnames = {}
@@ -63,7 +89,7 @@ local routes = {
         local prev = repo:find_prev(commit:id(), 1)
         local sig = commit:signature()
         local time_str = time_fmt(sig)
-        local diff = repo:diff(commit)
+        local diff = repo:diff(commit, diff_cb)
         local env = {
             gname=gname, rname=rname, bname=bname, commit=commit,
             time_str=time_str, sig=sig, cid=cid, prev=prev, diff=diff,

diff --git a/git.lua b/git.lua
index 8a8ebe6..c444cc3 100644
--- a/git.lua
+++ b/git.lua
@@ -267,7 +267,7 @@ function Repo:find_prev(cid, dist)
     return nil
 end
 
-function Repo:diff(commit)
+function Repo:diff(commit, custom_cb)
     local new_tree = commit:tree()
     local parent = commit:parent()
     local old_tree = parent and parent:tree()
@@ -277,7 +277,14 @@ function Repo:diff(commit)
     local s = ""
     local function cb(delta, hunk, line, payload)
         local lua_line = ffi.string(line.content, line.content_len)
-        s = s .. string.char(line.origin) .. lua_line
+        local line_type = string.char(line.origin)
+        local diff_line
+        if custom_cb ~= nil then
+            diff_line = custom_cb(line_type, lua_line)
+        else
+            diff_line = line_type .. lua_line
+        end
+        s = s .. diff_line
         return 0
     end
     C.git_diff_print(diff, C.GIT_DIFF_FORMAT_PATCH, cb, nil)

diff --git a/view/commit.html b/view/commit.html
index 4be34f6..9cdf66d 100644
--- a/view/commit.html
+++ b/view/commit.html
@@ -3,6 +3,12 @@
 <head>
   <meta charset="utf-8">
   <title>cogit - {{$gname}} - {{$rname}} - {{$cid}}</title>
+  <style>
+    .diff_file_hdr { font-weight: bold; }
+    .diff_hunk_hdr { font-weight: bold; color: blue;}
+    .diff_add { color: green; }
+    .diff_del { color: red; }
+  </style>
 </head>
 <body>
   <p>
@@ -32,6 +38,6 @@
   <p>{{$sig.name}} &lt;{{$sig.email}}&gt;</p>
   <pre>{{$commit:message()}}</pre>
   <hr>
-  <pre>{{$diff}}</pre>
+  <pre>{{!$diff}}</pre>
 </body>
 </html>