Skip to content

feat(tui): color unified-diff lines inside code blocks - #135

Open
Ariestar wants to merge 1 commit into
tui/content-mode-unifyfrom
tui/content-diff
Open

feat(tui): color unified-diff lines inside code blocks#135
Ariestar wants to merge 1 commit into
tui/content-mode-unifyfrom
tui/content-diff

Conversation

@Ariestar

Copy link
Copy Markdown
Owner

Purpose

Color git-diff code blocks in the read view: @@\ hunk headers in accent, +\ additions in success green, -\ deletions in failure red, file headers bold.

Changes

  • \markdown.rs\ gathers each fenced code block as one unit before painting it.
  • A block is treated as a diff when its fence language is \diff\ or its body contains an @@\ hunk header, so raw tool output pasted as a diff colors up without markup.
  • Non-diff code blocks keep the plain code color (regression-tested).

Validation

  • \cargo test --workspace\ (335 + 205), clippy -D warnings, \cargo fmt --check\ pass; three new diff-rendering tests.

Stacked on #134.

@coderabbitai

coderabbitai Bot commented Aug 13, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 73b72cf7-0d09-4a36-9499-15bf3d37fce2

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Comment @coderabbitai help to get the list of available commands.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Aug 13, 2026

Copy link
Copy Markdown

Deploying sivtr with  Cloudflare Pages  Cloudflare Pages

Latest commit: 96c84e5
Status: ✅  Deploy successful!
Preview URL: https://2c3b81b8.sivtr.pages.dev
Branch Preview URL: https://tui-content-diff.sivtr.pages.dev

View logs

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a7e146162a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +122 to +123
let trimmed = line.trim_start();
if trimmed.starts_with("@@") {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 保留 unified diff 的上下文标记

当 unified diff 的上下文内容本身以 +- 开头时,该行编码为 +... -...;这里的 trim_start() 会移除必需的空格上下文标记,随后将其误判为新增或删除行。例如未修改的源码行 +value 会显示为绿色。应仅移除围栏自身的缩进,再按 diff 标记所在的第一列分类。

Useful? React with 👍 / 👎.

Comment on lines +114 to +115
fn is_diff_hunk_header(line: &str) -> bool {
line.trim_start().starts_with("@@")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 验证完整的 unified-diff hunk 头

当普通代码块包含以 @@ 开头但并非 hunk 头的代码时,这个宽松判断会把整个块识别成 diff。例如 Ruby 类变量声明 @@instances = 0 会触发 diff 模式,随后块内以 +/- 开头的源码被错误着色。应验证注释所述的 @@ -old +new @@ 结构,而不只是检查前缀。

Useful? React with 👍 / 👎.

Comment on lines +127 to +130
} else if trimmed.starts_with("+++") || trimmed.starts_with("---") {
Style::default()
.fg(crate::tui::theme::muted())
.add_modifier(Modifier::BOLD)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 区分文件头与连续加减号的变更行

当新增源码本身以 ++ 开头时,其 diff 行会是 +++...,这里会在新增分支之前将它误判为文件头;删除以 -- 开头的源码也同样受影响。例如新增 C/C++ 语句 ++counter; 会显示为灰色粗体而不是绿色。文件头判断应要求 +++/--- 后存在文件头分隔符,而不能只匹配三个字符的前缀。

Useful? React with 👍 / 👎.

Fenced code blocks that are git diffs now render change lines with
diff colors: @@ hunk headers in accent, + additions in success green,
- deletions in failure red, and file headers bold. A block is treated
as a diff when its fence language is diff or its body contains an
@@ hunk header, so raw tool output pasted as a diff colors up without
any markup.
@Ariestar
Ariestar marked this pull request as ready for review August 14, 2026 09:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants