navid/veins / commit

a076006b11a0

Navid M <navidmomtahen@gmail.com> · 2026-11-26

parents: 6c29333dcac4

Browse files at this commit · Download source

all: proper diffs and colors for status + diff output
--- a/docs/SPECIFICATION_V1.adoc+++ b/docs/SPECIFICATION_V1.adoc@@ -1086,6 +1086,8 @@ |===  `vn log` prints commits newest first in aligned columns with no borders. Column titles are shown by default and can be omitted with `-no-col-titles`. Output is colored when standard output is a terminal; setting `NO_COLOR` disables color.++`vn status` colors its change markers (`A` bright green, `M` cyan, `D` red, and `C` bright red). `vn diff` prints unified hunks with context; removed lines are red and added lines are green.  All commands that change state MUST be atomic with respect to the operation log: they either produce one operation or leave the repository unchanged. --- a/veins/src/cli/diff_cmd.sx+++ b/veins/src/cli/diff_cmd.sx@@ -3,6 +3,7 @@ val str      = use("std/string") val stdio    = use("std/stdio") val msg      = use("../core/msg.sx")+val terminal = use("std/terminal") val bytes    = use("../core/bytes.sx") val lists    = use("../core/lists.sx") val objid    = use("../object/id.sx")@@ -19,6 +20,10 @@ val files    = use("../io/files.sx") val object   = use("../git/object.sx") val snapshot = use("../worktree/snapshot.sx")++val OP_CONTEXT: u8 = 32+val OP_ADD: u8 = 43+val OP_DEL: u8 = 45  type DiffOp = {     tag: mut u8@@ -85,6 +90,212 @@         i++     }     return out+}++fn new_op(tag: u8, ai: usize, bi: usize) ref DiffOp = trust {+    val op: mut ref DiffOp = @alloc(@sizeof(DiffOp)) as ref DiffOp+    op.tag = tag+    op.ai = ai+    op.bi = bi+    return op+}++fn free_ops(ops: list[ref DiffOp]) void = trust {+    for op in ops {+        val p: mut ref void = op as ref void+        @free(p)+    }+    @listfree(ops)+}++fn op_at(ops: list[ref DiffOp], i: usize) ref DiffOp = trust {+    val o = @get(ops, i)+    if o is some op {+        return op+    }+    assert false+    return none+}++fn reverse_ops(ops: mut list[ref DiffOp]) void = trust {+    val n: usize = @len(ops)+    val i: mut usize = 0+    for i < n / 2 {+        val a: ref DiffOp = op_at(ops, i)+        val b: ref DiffOp = op_at(ops, n - 1 - i)+        @set(ops, i, b)+        @set(ops, n - 1 - i, a)+        i+++    }+}++fn lcs_get(dp: ref u32, idx: usize) u32 = trust {+    val p: ref u32 = @ptradd(dp as ref void, (idx * 4) as i64) as ref u32+    return @load32(p) as u32+}++fn lcs_set(dp: ref u32, idx: usize, value: u32) void = trust {+    val p: ref u32 = @ptradd(dp as ref void, (idx * 4) as i64) as ref u32+    @store32(p, value as i32)+}++fn build_ops(a: list[bytes.Bytes], b: list[bytes.Bytes]) list[ref DiffOp] = trust {+    val n: usize = @len(a)+    val m: usize = @len(b)+    val cols: usize = m + 1+    val cells: usize = (n + 1) * cols+    val dp: ref u32 = @alloc(cells * 4) as ref u32+    @memset(dp as ref void, 0, cells * 4)+    val i: mut usize = 1+    for i <= n {+        val j: mut usize = 1+        for j <= m {+            if bytes.Bytes.eql(line_at(a, i - 1), line_at(b, j - 1)) {+                lcs_set(dp, i * cols + j, lcs_get(dp, (i - 1) * cols + (j - 1)) + 1)+            } else {+                val up: u32 = lcs_get(dp, (i - 1) * cols + j)+                val left: u32 = lcs_get(dp, i * cols + (j - 1))+                val best: mut u32 = left+                if up > best {+                    best = up+                }+                lcs_set(dp, i * cols + j, best)+            }+            j+++        }+        i+++    }+    val ops: mut list[ref DiffOp] = lists.new[ref DiffOp]()+    val oi: mut usize = n+    val oj: mut usize = m+    for oi > 0 || oj > 0 {+        if oi > 0 && oj > 0 && bytes.Bytes.eql(line_at(a, oi - 1), line_at(b, oj - 1)) {+            @append(ops, new_op(OP_CONTEXT, oi - 1, oj - 1))+            oi--+            oj--+        } elif oj > 0+        && (oi == 0 || lcs_get(dp, oi * cols + (oj - 1)) >= lcs_get(dp, (oi - 1) * cols + oj)) {+            @append(ops, new_op(OP_ADD, 0, oj - 1))+            oj--+        } else {+            @append(ops, new_op(OP_DEL, oi - 1, 0))+            oi--+        }+    }+    reverse_ops(ops)+    @free(dp as ref void)+    return ops+}++fn change_at(xs: list[usize], i: usize) usize = trust {+    val o = @get(xs, i)+    if o is some v {+        return v+    }+    return 0+}++fn render_unified(a: list[bytes.Bytes], b: list[bytes.Bytes]) void = trust {+    val ops: list[ref DiffOp] = build_ops(a, b)+    val nops: usize = @len(ops)+    val changes: mut list[usize] = lists.new[usize]()+    val k: mut usize = 0+    for k < nops {+        val op: ref DiffOp = op_at(ops, k)+        if op.tag != OP_CONTEXT {+            @append(changes, k)+        }+        k+++    }+    val context: usize = 3+    val pos: mut usize = 0+    val old_seen: mut usize = 0+    val new_seen: mut usize = 0+    val ci: mut usize = 0+    for ci < @len(changes) {+        val first: usize = change_at(changes, ci)+        val start: mut usize = 0+        if first > context {+            start = first - context+        }+        val last: mut usize = first+        ci+++        for ci < @len(changes) {+            val c: usize = change_at(changes, ci)+            if c > last + context * 2 {+                break+            }+            last = c+            ci+++        }+        val end: mut usize = last + context + 1+        if end > nops {+            end = nops+        }+        for pos < start {+            val skip: ref DiffOp = op_at(ops, pos)+            if skip.tag == OP_CONTEXT {+                old_seen+++                new_seen+++            } elif skip.tag == OP_DEL {+                old_seen+++            } else {+                new_seen+++            }+            pos+++        }+        val old_count: mut usize = 0+        val new_count: mut usize = 0+        val e: mut usize = start+        for e < end {+            val op: ref DiffOp = op_at(ops, e)+            if op.tag == OP_CONTEXT {+                old_count+++                new_count+++            } elif op.tag == OP_DEL {+                old_count+++            } else {+                new_count+++            }+            e+++        }+        val old_start: mut usize = old_seen+        if old_count > 0 {+            old_start+++        }+        val new_start: mut usize = new_seen+        if new_count > 0 {+            new_start+++        }+        stdio.print(+            "@@ -{d},{d} +{d},{d} @@\n",+            {old_start, old_count, new_start, new_count}+        )+        for pos < end {+            val op: ref DiffOp = op_at(ops, pos)+            if op.tag == OP_CONTEXT {+                val line: bytes.Bytes = line_at(a, op.ai)+                stdio.print(" {s}\n", {str.view(line.ptr, line.len as i64)})+                old_seen+++                new_seen+++            } elif op.tag == OP_DEL {+                val line: bytes.Bytes = line_at(a, op.ai)+                terminal.print(str.from_cstr(c"-"), terminal.Color.Red)+                terminal.print(str.view(line.ptr, line.len as i64), terminal.Color.Red)+                stdio.print("\n", {})+                old_seen+++            } else {+                val line: bytes.Bytes = line_at(b, op.bi)+                terminal.print(str.from_cstr(c"+"), terminal.Color.Green)+                terminal.print(str.view(line.ptr, line.len as i64), terminal.Color.Green)+                stdio.print("\n", {})+                new_seen+++            }+            pos+++        }+    }+    @listfree(changes)+    free_ops(ops) }  fn render_one(@@ -118,44 +329,25 @@     if contains_nul(old_data) || contains_nul(new_data) {         stdio.print("Binary files a/{s} and b/{s} differ\n", {path_view, path_view})     } else {-        stdio.print("--- ", {})+        terminal.print(str.from_cstr(c"--- "), terminal.Color.Red)         if change.old_kind == treewalk.KIND_MISSING {-            stdio.print("/dev/null", {})+            terminal.print(str.from_cstr(c"/dev/null"), terminal.Color.Red)         } else {-            stdio.print("a/{s}", {path_view})+            terminal.print(str.from_cstr(c"a/"), terminal.Color.Red)+            terminal.print(path_view, terminal.Color.Red)         }-        stdio.print("\n+++ ", {})+        stdio.print("\n", {})+        terminal.print(str.from_cstr(c"+++ "), terminal.Color.Green)         if change.new_kind == treewalk.KIND_MISSING {-            stdio.print("/dev/null", {})+            terminal.print(str.from_cstr(c"/dev/null"), terminal.Color.Green)         } else {-            stdio.print("b/{s}", {path_view})+            terminal.print(str.from_cstr(c"b/"), terminal.Color.Green)+            terminal.print(path_view, terminal.Color.Green)         }         stdio.print("\n", {})         val a = split_lines(old_data)         val b = split_lines(new_data)-        val a_count: usize = @len(a)-        val b_count: usize = @len(b)-        val a_start: mut usize = 1-        if a_count == 0 {-            a_start = 0-        }-        val b_start: mut usize = 1-        if b_count == 0 {-            b_start = 0-        }-        stdio.print("@@ -{d},{d} +{d},{d} @@\n", {a_start, a_count, b_start, b_count})-        val ai: mut usize = 0-        for ai < a_count {-            val line = line_at(a, ai)-            stdio.print("-{s}\n", {str.view(line.ptr, line.len as i64)})-            ai++-        }-        val bi: mut usize = 0-        for bi < b_count {-            val line2 = line_at(b, bi)-            stdio.print("+{s}\n", {str.view(line2.ptr, line2.len as i64)})-            bi++-        }+        render_unified(a, b)         @listfree(a)         @listfree(b)     }--- a/veins/src/cli/status_cmd.sx+++ b/veins/src/cli/status_cmd.sx@@ -2,6 +2,7 @@  val stdio    = use("std/stdio") val msg      = use("../core/msg.sx")+val terminal = use("std/terminal") val str      = use("std/string") val objid    = use("../object/id.sx") val tree     = use("../object/tree.sx")@@ -84,14 +85,18 @@     for c in changes {         val path_view = str.view(c.path.ptr, c.path.len as i64)         if c.old_kind == treewalk.KIND_MISSING {-            stdio.print("A {s}\n", {path_view})+            terminal.print(str.from_cstr(c"A"), terminal.Color.BrightGreen)+            stdio.print(" {s}\n", {path_view})         } elif c.new_kind == treewalk.KIND_MISSING {-            stdio.print("D {s}\n", {path_view})+            terminal.print(str.from_cstr(c"D"), terminal.Color.Red)+            stdio.print(" {s}\n", {path_view})         } elif c.new_kind == tree.KIND_CONFLICT {-            stdio.print("C {s}\n", {path_view})+            terminal.print(str.from_cstr(c"C"), terminal.Color.BrightRed)+            stdio.print(" {s}\n", {path_view})             conflicted_count++         } else {-            stdio.print("M {s}\n", {path_view})+            terminal.print(str.from_cstr(c"M"), terminal.Color.Cyan)+            stdio.print(" {s}\n", {path_view})         }     }     if @len(changes) == 0 {