Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions examples/tdiff.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from rich.terminal_theme import TerminalTheme

from textual import on
from textual.app import App, ComposeResult
from textual import containers
from textual import messages
from textual.reactive import var
from textual import widgets

Expand All @@ -22,12 +26,17 @@ class DiffApp(App):
def __init__(self, original: str, modified: str) -> None:
self.original = original
self.modified = modified
self._terminal_theme: TerminalTheme | None = None
super().__init__()

def compose(self) -> ComposeResult:
yield containers.VerticalScroll(id="diff-container")
yield widgets.Footer()

@on(messages.TerminalThemeReport)
def on_terminal_theme_report(self, event: messages.TerminalThemeReport) -> None:
self.query_one(DiffView).terminal_theme = event.theme

async def on_mount(self) -> None:
try:
diff_view = await DiffView.load(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies = [
exclude = ["tests/__snapshots__"]

[tool.uv.sources]
#textual = { path = "../textual", editable = true }
textual = { path = "../textual", editable = true }

[dependency-groups]
dev = [
Expand Down
65 changes: 58 additions & 7 deletions src/textual_diff_view/_diff_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@

from rich.segment import Segment
from rich.style import Style as RichStyle
from rich.terminal_theme import TerminalTheme

from textual.app import ComposeResult
from textual.content import Content, Span
from textual.geometry import Size
from textual import highlight
from textual import events

from textual.color import Color
from textual.css.styles import RulesMap
from textual.selection import Selection
from textual.strip import Strip
Expand Down Expand Up @@ -382,11 +384,13 @@ class DiffView(containers.VerticalGroup):
"""Automaticallly enable split view if there is enough space?"""
wrap: reactive[bool] = reactive(False, recompose=True)
"""Wrap long lines (rather than horizontal scroll)."""
terminal_theme: reactive[TerminalTheme | None] = reactive(None, recompose=True)
"""System terminal theme, used in ANSI themes if supplied."""

DEFAULT_CSS = """
DiffView {
width: 1fr;
height: auto;
height: auto;
.diff-group {
height: auto;
background: $foreground 4%;
Expand Down Expand Up @@ -515,10 +519,19 @@ class DiffView(containers.VerticalGroup):
& > .diff-view--annotation-unmodified {
background: ansi_default;
}
& > .diff-view--annotation-hatch {
color: $ansi-foreground;
text-style: dim;
&:dark {
& > .diff-view--annotation-hatch {
color: ansi_bright_black;
text-style: dim;
}
}
&:light {
& > .diff-view--annotation-hatch {
color: ansi_bright_white;
text-style: dim;
}
}


& > .diff-view--line-added {
background: ansi_default;
Expand All @@ -532,9 +545,17 @@ class DiffView(containers.VerticalGroup):
background: ansi_default;
}

& > .diff-view--line-hatch {
color: $ansi-foreground;
text-style: dim;
&:dark {
& > .diff-view--line-hatch {
color: ansi_bright_black;
text-style: dim;
}
}
&:light {
& > .diff-view--line-hatch {
color: ansi_bright_white;
text-style: dim;
}
}

& > .diff-view--edge-added {
Expand Down Expand Up @@ -593,6 +614,7 @@ def __init__(
name: str | None = None,
id: str | None = None,
classes: str | None = None,
terminal_theme: TerminalTheme | None = None,
):
"""Initialize diff view.

Expand All @@ -619,6 +641,7 @@ def __init__(
self.set_reactive(DiffView.annotations, annotations)
self.set_reactive(DiffView.auto_split, auto_split)
self.set_reactive(DiffView.wrap, wrap)
self.set_reactive(DiffView.terminal_theme, terminal_theme)

self._grouped_opcodes: list[list[tuple[str, int, int, int, int]]] | None = None
self._highlighted_code_lines: tuple[list[Content], list[Content]] | None = None
Expand Down Expand Up @@ -734,6 +757,30 @@ def _update_styles(self) -> None:
"-": self.get_visual_style("diff-view--edge-removed", partial=True),
" ": self.get_visual_style("diff-view--edge-unmodified", partial=True),
}
if self.app.native_ansi_color and self.terminal_theme is not None:
terminal_theme = self.terminal_theme
background = Color(*terminal_theme.background_color)
red = Color(*terminal_theme.ansi_colors[2])
green = Color(*terminal_theme.ansi_colors[1])

added = background + red.with_alpha(0.15)
removed = background + green.with_alpha(0.15)

number_styles = self._number_styles
number_styles["+"] = number_styles["+"].with_background(added)
number_styles["-"] = number_styles["-"].with_background(removed)

annotation_styles = self._annotation_styles
annotation_styles["+"] = annotation_styles["+"].with_background(added)
annotation_styles["-"] = annotation_styles["-"].with_background(removed)

line_styles = self._line_styles
line_styles["+"] = line_styles["+"].with_background(added)
line_styles["-"] = line_styles["-"].with_background(removed)

edge_styles = self._edge_styles
edge_styles["+"] = edge_styles["+"].with_background(added)
edge_styles["-"] = edge_styles["-"].with_background(removed)

async def prepare(self) -> None:
"""Do CPU work in a thread.
Expand Down Expand Up @@ -915,6 +962,10 @@ def get_title(self) -> Content:

def compose(self) -> ComposeResult:
"""Compose either split or unified view."""
self._number_styles.clear()
self._annotation_styles.clear()
self._line_styles.clear()
self._edge_styles.clear()
yield Static(self.get_title(), classes="title")
if self.split:
yield from self.compose_split()
Expand Down
37 changes: 30 additions & 7 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading