From b072d8bc48e35dc814642ae0cee190db42fb56cf Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Fri, 14 Feb 2025 13:31:17 +0800 Subject: [PATCH] reop_commit: Fix immediate newlines after

---
 http_handle_repo_commit.go      | 20 ++++++++++++++++++--
 static/style.css                |  8 ++++----
 templates/repo_commit.html.tmpl |  6 +++---

diff --git a/http_handle_repo_commit.go b/http_handle_repo_commit.go
index d4956d11d136ab6af9a97e6796b4dd955e46f14b..8e5cdbfc6ed16ec7c9eb2149ff321eb48122eb0c 100644
--- a/http_handle_repo_commit.go
+++ b/http_handle_repo_commit.go
@@ -14,7 +14,12 @@
 type usable_file_patch struct {
 	From   diff.File
 	To     diff.File
-	Chunks []diff.Chunk
+	Chunks []usable_chunk
+}
+
+type usable_chunk struct {
+	Operation diff.Operation
+	Content   string
 }
 
 func handle_repo_commit(w http.ResponseWriter, r *http.Request, params map[string]any) {
@@ -70,8 +75,19 @@ 		}
 		if to == nil {
 			to = fake_diff_file_null
 		}
+		chunks := []usable_chunk{}
+		for _, chunk := range file_patch.Chunks() {	
+			content := chunk.Content()
+			if len(content) > 0 && content[0] == '\n' {
+				content = "\n" + content
+			} // Horrible hack to fix how browsers newlines that immediately proceed 
+			chunks = append(chunks, usable_chunk{
+				Operation: chunk.Type(),
+				Content:   content,
+			})
+		}
 		usable_file_patch := usable_file_patch{
-			Chunks: file_patch.Chunks(),
+			Chunks: chunks,
 			From:   from,
 			To:     to,
 		}
diff --git a/static/style.css b/static/style.css
index fae12c9b473c59148549399c515f669fabb05185..8441a0c7f8092b74e3a6d59f655395116bc58b85 100644
--- a/static/style.css
+++ b/static/style.css
@@ -147,18 +147,18 @@ .chunk-unchanged {
 	color: grey;
 }
 .chunk-addition {
-	color: green;
+	background-color: green;
 }
 @media (prefers-color-scheme: dark) {
 	.chunk-addition {
-		color: lime;
+		background-color: lime;
 	}
 }
 .chunk-deletion {
-	color: red;
+	background-color: red;
 }
 .chunk-unknown {
-	color: yellow;
+	background-color: yellow;
 }
 pre.chunk {
 	margin-top: 0;
diff --git a/templates/repo_commit.html.tmpl b/templates/repo_commit.html.tmpl
index 658ed572049127a0381e662d0e741204697baa9f..d07845bf6b97a6cd5ebaf641f5f802ba18c63868 100644
--- a/templates/repo_commit.html.tmpl
+++ b/templates/repo_commit.html.tmpl
@@ -69,11 +69,11 @@ 						
 					
 					
{{ range .Chunks }} - {{ if eq .Type 0 }} + {{ if eq .Operation 0 }}
{{ .Content }}
- {{ else if eq .Type 1 }} + {{ else if eq .Operation 1 }}
{{ .Content }}
- {{ else if eq .Type 2 }} + {{ else if eq .Operation 2 }}
{{ .Content }}
{{ else }}
{{ .Content }}
-- 2.48.1