diff --git a/cptr/frontend/src/lib/apis/git.ts b/cptr/frontend/src/lib/apis/git.ts index 888ea7a..61d8329 100644 --- a/cptr/frontend/src/lib/apis/git.ts +++ b/cptr/frontend/src/lib/apis/git.ts @@ -39,8 +39,10 @@ export const getGitLog = (root: string, limit = 30) => export const getGitDiff = (params: string) => fetchJSON(`/api/git/diff?${params}`); -export const getGitShow = (root: string, ref: string) => - fetchJSON(`/api/git/show?root=${encodeURIComponent(root)}&ref=${encodeURIComponent(ref)}`); +export const getGitShow = (root: string, ref: string, ignoreWhitespace = false) => + fetchJSON( + `/api/git/show?root=${encodeURIComponent(root)}&ref=${encodeURIComponent(ref)}&ignore_whitespace=${ignoreWhitespace}` + ); export const getGitBranches = (root: string) => fetchJSON(`/api/git/branches?root=${encodeURIComponent(root)}`); diff --git a/cptr/frontend/src/lib/components/DiffHunkRows.svelte b/cptr/frontend/src/lib/components/DiffHunkRows.svelte new file mode 100644 index 0000000..57fa104 --- /dev/null +++ b/cptr/frontend/src/lib/components/DiffHunkRows.svelte @@ -0,0 +1,186 @@ + + +{#if $diffDisplayMode === 'split'} + {#each splitDiffRows(lines) as row} +
+ {#if showNumbers} + {row.oldLine?.oldNumber ?? ''} + {/if} +
+ {#if row.oldLine} + + {:else} + + {/if} +
+ {#if showNumbers} + {row.newLine?.newNumber ?? ''} + {/if} +
+ {#if row.newLine} + + {:else} + + {/if} +
+
+ {/each} +{:else} + {#each groupDiffLines(lines) as group} +
+ {#each group.lines as line} + {#if showNumbers} +
+ {line.oldNumber ?? ''} + {line.newNumber ?? ''} + {linePrefix(line.type)} + +
+ {:else} +
+ {linePrefix(line.type)} + +
+ {/if} + {/each} +
+ {/each} +{/if} + + diff --git a/cptr/frontend/src/lib/components/DiffSettingsMenu.svelte b/cptr/frontend/src/lib/components/DiffSettingsMenu.svelte new file mode 100644 index 0000000..c782686 --- /dev/null +++ b/cptr/frontend/src/lib/components/DiffSettingsMenu.svelte @@ -0,0 +1,94 @@ + + + +
+
+ Whitespace +
+ + + +
+ Diff Display +
+ + + +
+
diff --git a/cptr/frontend/src/lib/components/FileEditor.svelte b/cptr/frontend/src/lib/components/FileEditor.svelte index af82583..5dbb7b6 100644 --- a/cptr/frontend/src/lib/components/FileEditor.svelte +++ b/cptr/frontend/src/lib/components/FileEditor.svelte @@ -6,6 +6,7 @@ import { tooltip } from '$lib/tooltip'; import { readFile, writeFile } from '$lib/apis/files'; import { getGitDiff } from '$lib/apis/git'; + import { hideWhitespaceChanges } from '$lib/stores/gitDiffSettings'; import { gitStatusStore, type GitFile } from '$lib/stores/gitStatus.svelte'; import Icon from './Icon.svelte'; import SaveDialog from './SaveDialog.svelte'; @@ -20,8 +21,7 @@ import SvgPreview from './preview/SvgPreview.svelte'; import OfficePreview from './preview/OfficePreview.svelte'; import Spinner from './common/Spinner.svelte'; - import { groupDiffLines, languageForPath, withInlineDiffSegments } from '$lib/utils/diff'; - import SyntaxDiffLine from './SyntaxDiffLine.svelte'; + import DiffHunkRows from './DiffHunkRows.svelte'; import { EditorState, StateEffect, @@ -526,31 +526,6 @@ applyGitLineChanges(); } - function diffBlockClass(type: DiffLine['type']): string { - if (type === 'added') - return 'bg-green-100 border-l-[0.1875rem] border-l-green-500 dark:bg-green-500/15 dark:border-l-green-400'; - if (type === 'removed') return 'bg-red-100 diff-gutter-removed dark:bg-red-500/15'; - return ''; - } - - function diffTextClass(type: DiffLine['type']): string { - if (type === 'added') return 'text-green-900 dark:text-green-300'; - if (type === 'removed') return 'text-red-900 dark:text-red-300'; - return 'text-gray-600 dark:text-gray-400'; - } - - function diffPrefixClass(type: DiffLine['type']): string { - if (type === 'added') return 'text-green-600 dark:text-green-400'; - if (type === 'removed') return 'text-red-500 dark:text-red-400'; - return 'text-gray-400 dark:text-gray-600'; - } - - function diffPrefix(type: DiffLine['type']): string { - if (type === 'added') return '+'; - if (type === 'removed') return '-'; - return ' '; - } - // Editing state: true when the code editor should be active let isEditing = $derived( fileData && @@ -811,7 +786,8 @@ root: ws.path, file: relPath, staged: 'false', - untracked: String(fileStatus?.status === 'untracked') + untracked: String(fileStatus?.status === 'untracked'), + ignore_whitespace: String($hideWhitespaceChanges) }); const d = (await getGitDiff(params.toString())) as { files?: DiffFileEntry[] }; diffFiles = d.files ?? []; @@ -1226,34 +1202,7 @@ {hunk.header} - {#each groupDiffLines(withInlineDiffSegments(diffNumberedLines(hunk))) as group} -
- {#each group.lines as line} -
- {line.oldNumber ?? ''} - {line.newNumber ?? ''} - {diffPrefix(line.type)} - -
- {/each} -
- {/each} + {/each} {/each} @@ -1475,89 +1424,4 @@ font-size: 0.75rem; line-height: 1.125rem; } - - .diff-hunk-header { - padding: 0.125rem 0.5rem; - color: var(--color-gray-400); - background: var(--color-gray-50); - } - - :global(.dark) .diff-hunk-header { - background: rgba(255, 255, 255, 0.03); - color: var(--color-gray-600); - } - - .diff-line { - padding: 0 0.5rem; - white-space: pre-wrap; - word-break: break-all; - } - - .diff-prefix { - display: inline-block; - width: 1rem; - user-select: none; - } - - .diff-added { - background: #dcfce7; - color: #14532d; - } - - :global(.dark) .diff-added { - background: rgba(34, 197, 94, 0.12); - color: #86efac; - } - - .diff-added .diff-prefix { - color: #16a34a; - } - :global(.dark) .diff-added .diff-prefix { - color: #4ade80; - } - - .diff-removed { - background: #fee2e2; - color: #7f1d1d; - } - - :global(.dark) .diff-removed { - background: rgba(239, 68, 68, 0.12); - color: #fca5a5; - } - - .diff-removed .diff-prefix { - color: #ef4444; - } - :global(.dark) .diff-removed .diff-prefix { - color: #f87171; - } - - .diff-ctx { - color: var(--color-gray-600); - } - - :global(.dark) .diff-ctx { - color: var(--color-gray-400); - } - - .diff-ctx .diff-prefix { - color: var(--color-gray-400); - } - - :global(.dark) .diff-ctx .diff-prefix { - color: var(--color-gray-600); - } - - .diff-gutter-removed { - border-left: 0.1875rem solid transparent; - border-image: repeating-linear-gradient( - -45deg, - #ef4444 0, - #ef4444 1px, - transparent 1px, - transparent 0.1875rem - ) - 3; - } diff --git a/cptr/frontend/src/lib/components/GitBar.svelte b/cptr/frontend/src/lib/components/GitBar.svelte index b9cd9e8..af5227e 100644 --- a/cptr/frontend/src/lib/components/GitBar.svelte +++ b/cptr/frontend/src/lib/components/GitBar.svelte @@ -26,21 +26,16 @@ renameGitBranch } from '$lib/apis/git'; import { gitStatusStore } from '$lib/stores/gitStatus.svelte'; + import { diffDisplayMode, hideWhitespaceChanges } from '$lib/stores/gitDiffSettings'; import Icon from './Icon.svelte'; import DropdownMenu from './DropdownMenu.svelte'; import Modal from './Modal.svelte'; import { tooltip } from '$lib/tooltip'; import { t } from '$lib/i18n'; import Spinner from '$lib/components/common/Spinner.svelte'; - import { - groupDiffLines, - languageForPath, - numberDiffLines, - withInlineDiffSegments, - type DiffFile, - type DiffLine - } from '$lib/utils/diff'; - import SyntaxDiffLine from './SyntaxDiffLine.svelte'; + import DiffSettingsMenu from './DiffSettingsMenu.svelte'; + import type { DiffFile } from '$lib/utils/diff'; + import DiffHunkRows from './DiffHunkRows.svelte'; type GitFile = { path: string; @@ -84,8 +79,10 @@ let showDiff = $state(false); let showBranches = $state(false); let showWorktrees = $state(false); + let showDiffSettings = $state(false); let branchBtnEl = $state(); let worktreeBtnEl = $state(); + let diffSettingsBtnEl = $state(); let branchSearchInputEl = $state(); let worktreeSearchInputEl = $state(); let newBranchInputEl = $state(); @@ -187,6 +184,15 @@ } }); + let _prevHideWhitespace = false; + $effect(() => { + const hideWhitespace = $hideWhitespaceChanges; + if (hideWhitespace === _prevHideWhitespace) return; + _prevHideWhitespace = hideWhitespace; + if (!expanded || !showDiff) return; + reloadSelectedDiff(); + }); + // Auto-select first file $effect(() => { if ( @@ -213,7 +219,8 @@ const params = new URLSearchParams({ root: workspacePath, file: path, - staged: String(staged) + staged: String(staged), + ignore_whitespace: String($hideWhitespaceChanges) }); if (untracked) params.set('untracked', 'true'); const d = await getGitDiff(params.toString()); @@ -228,7 +235,7 @@ selectedFile = null; showDiff = true; try { - const d = await getGitShow(workspacePath, c.hash); + const d = await getGitShow(workspacePath, c.hash, $hideWhitespaceChanges); fileDiff = d.diff?.files ?? []; } catch { fileDiff = []; @@ -239,6 +246,16 @@ showDiff = false; } + function reloadSelectedDiff() { + if (selectedCommit) { + selectCommit(selectedCommit); + return; + } + if (!selectedFile) return; + const file = (gitStatus?.files ?? []).find((item) => item.path === selectedFile); + if (file) selectFile(file.path, file.staged, file.status === 'untracked'); + } + async function loadHistory() { try { commits = await getGitLog(workspacePath, 50); @@ -717,35 +734,6 @@ } } - function blockClass(type: DiffLine['type']): string { - switch (type) { - case 'added': - return 'bg-green-100 border-l-[0.1875rem] border-l-green-500 dark:bg-green-500/15 dark:border-l-green-400'; - case 'removed': - return 'bg-red-100 diff-gutter-removed dark:bg-red-500/15'; - default: - return ''; - } - } - - function textClass(type: DiffLine['type']): string { - if (type === 'added') return 'text-green-900 dark:text-green-300'; - if (type === 'removed') return 'text-red-900 dark:text-red-300'; - return 'text-gray-600 dark:text-gray-400'; - } - - function prefixClass(type: DiffLine['type']): string { - if (type === 'added') return 'text-green-600 dark:text-green-400'; - if (type === 'removed') return 'text-red-500 dark:text-red-400'; - return 'text-gray-400 dark:text-gray-600'; - } - - function linePrefix(type: DiffLine['type']): string { - if (type === 'added') return '+'; - if (type === 'removed') return '-'; - return ' '; - } - $effect(() => { if (!expanded) return; function onKeydown(e: KeyboardEvent) { @@ -1184,6 +1172,16 @@ {/if} + + + + {#if showDiffSettings && diffSettingsButtonEl} + (showDiffSettings = false)} /> + {/if} + {#if totalChanges > 0}
-
+
{#if file.diffFiles.some((diffFile) => diffFile.hunks.length > 0)} {#each file.diffFiles as diffFile} {#if file.diffFiles.length > 1} @@ -405,33 +381,7 @@ {hunk.header}
- {#each groupDiffLines(withInlineDiffSegments(numberDiffLines(hunk))) as group} -
- {#each group.lines as line} -
- {line.oldNumber ?? ''} - {line.newNumber ?? ''} - {linePrefix(line.type)} - -
- {/each} -
- {/each} + {/each} {/each} {:else} @@ -458,15 +408,7 @@ min-width: 100%; } - .diff-gutter-removed { - border-left: 0.1875rem solid transparent; - border-image: repeating-linear-gradient( - -45deg, - #ef4444 0, - #ef4444 1px, - transparent 1px, - transparent 0.1875rem - ) - 3; + .diff-content-split { + width: 100%; } diff --git a/cptr/frontend/src/lib/components/Icon.svelte b/cptr/frontend/src/lib/components/Icon.svelte index 72f45a3..3d6a4d2 100644 --- a/cptr/frontend/src/lib/components/Icon.svelte +++ b/cptr/frontend/src/lib/components/Icon.svelte @@ -143,6 +143,11 @@ + {:else if name === 'split-view'} + + {:else if name === 'chevron-right'} {:else if name === 'arrow-left'} diff --git a/cptr/frontend/src/lib/components/SyntaxDiffLine.svelte b/cptr/frontend/src/lib/components/SyntaxDiffLine.svelte index 139fe16..1ba4c90 100644 --- a/cptr/frontend/src/lib/components/SyntaxDiffLine.svelte +++ b/cptr/frontend/src/lib/components/SyntaxDiffLine.svelte @@ -68,6 +68,7 @@ content: string; segments: InlineDiffSegment[]; language: string; + wrap?: boolean; class?: string; } @@ -76,7 +77,7 @@ dark?: string; }; - let { type, content, segments, language, class: className = '' }: Props = $props(); + let { type, content, segments, language, wrap = false, class: className = '' }: Props = $props(); let renderedSegments = $state(null); $effect(() => { @@ -187,7 +188,11 @@ const html = $derived(htmlForSegments(type, fallbackSegments)); -{@html html} +{@html html}