Skip to content
lambda-symbolicsPublic

About

A library for rendering colorful syntax highlighted diffs in terminal

Resources

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

13 Commits

Folders and files

Repository files navigation

Colordiff

Colordiff renders colorful syntax-highlighted diffs in a terminal. It produces a semantic row model for application integration and can render that model directly with ANSI colors.

Colordiff compares complete before-and-after documents. It removes their common prefix and suffix, keeps one surrounding context line, and renders one bounded middle change block.

Loading

Colordiff depends on ColorLisp and cl-colorist. With the systems available to ASDF:

(asdf:load-system "colordiff")

Rendering a diff

(let ((rows
        (colordiff:render-diff
         :removed-content "(defun answer () 41)"
         :added-content "(defun answer () 42)"
         :removed-start-line 10
         :added-start-line 10
         :source-path "answer.lisp")))
  (colordiff:write-ansi rows))

SOURCE-PATH is used only for language classification. Pass :SYNTAX-HIGHLIGHT-P NIL to skip highlighting.

Every changed row carries a line number. An absent :REMOVED-START-LINE or :ADDED-START-LINE inherits the other side’s coordinates, and when both are absent the documents number from their own first line. :LINE-LIMIT bounds each removed or added block independently.

Source previews and single rows

RENDER-SOURCE shows one document as a bounded, unnumbered preview under a source gutter, using the same highlighting and row model as diffs:

(colordiff:write-ansi
 (colordiff:render-source "ls -la"
                          :syntax-language (colorlisp:language-find "bash")
                          :prompt "$ "))

Trailing blank lines are dropped, and lines past :LINE-LIMIT become one “+N more lines” elision row. :PROMPT leads the first line and indents the rest.

RENDER-LINE-ROW builds one numbered context, removed, or added row exactly as RENDER-DIFF does, so an application can place its own message, such as “line unavailable”, in line with a diff.

Semantic rows

RENDER-DIFF returns a list of rows. By default, each row is a list of SPAN objects carrying a semantic role and display text. Roles distinguish:

  • context
  • removed and added gutters
  • ordinary code
  • elisions
  • ColorLisp syntax categories

Diff boundaries are computed from the original logical lines before display sanitization, so control-only changes stay visible.

Applications with an existing styled-text model can supply :SPAN-FUNCTION. The function receives a role keyword and text; its return values are placed directly into the rows. :SANITIZE-LINE-FUNCTION lets an application apply its own terminal-safety policy before highlighting and layout.

HIGHLIGHT-SPANS and HIGHLIGHT-LINES expose the same style-neutral ColorLisp mapping for callers that need syntax rendering outside a diff.

ANSI output

WRITE-ANSI writes semantic rows to a stream. ANSI-STRING returns the same presentation as a string. Both accept a Colorist color level and a replacement style table. Passing :NONE as the color level produces plain text. The final terminal boundary visibly escapes CSI, OSC, C0, C1, DEL, and embedded line controls even for spans constructed directly or returned by the standalone syntax helpers.

Development

./script/bootstrap
./script/check
./script/check --fix

./script/check runs the tests, then checks the source with cheque as cheque.sexp configures it. --fix skips the tests and repairs what cheque can repair. cheque and its tools are development dependencies: checks/qlfile lists them, and the check installs them on first use into ~/.cache/cheque/tools/.

License

Copyright 2026 Lambda Symbolics OÜ

Licensed under COLL-Attribution. See LICENSE.lisp for the authoritative terms.

About

A library for rendering colorful syntax highlighted diffs in terminal

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages