~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/hud

A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts

0
0
263 lines · 6.5 KB Raw History
<script lang="ts" module>
	/** A diff's row: a line added, removed or of context (its line numbers
	 * as text, empty where it has none), a hunk's header or a note. */
	export type DiffRow = { kind: 'added' | 'removed' | 'hunk' | 'note' | 'context'; old: string; new: string; text: string };
</script>

<script lang="ts">
	import { FRAMED } from './window';

	// One file's unified diff, as knotless's pages draw a pull's: a header
	// (its `path`, the path it was renamed `from`, its `status`, the lines
	// `added` and `removed`), a `note` where its rows are not shown, and its
	// `rows` in monospace, unwrapped, the table scrolling sideways under its
	// line numbers, which stay put, `digits` wide. Without rows, the header
	// alone, as a pull's list of files changed. Rows are drawn in runs the
	// browser skips drawing while they are out of view. The tree's Diff
	// draws it.
	let {
		path,
		from,
		status,
		added,
		removed,
		note,
		rows,
		digits
	}: {
		path: string;
		from: string | null;
		status: string | null;
		added: number | null;
		removed: number | null;
		note: string | null;
		rows: readonly DiffRow[];
		digits: number;
	} = $props();

	/** Rows per run the browser draws or skips as one. */
	const RUN = 200;

	const runs = $derived(Array.from({ length: Math.ceil(rows.length / RUN) }, (_, i) => rows.slice(i * RUN, (i + 1) * RUN)));
	const sign = { added: '+', removed: '−', context: ' ', hunk: '', note: '' } as const;
</script>

<div class="diff" class:bare={rows.length === 0}>
	<div class="head">
		<span class="path">
			{#if from}<span class="from">{from} →</span>{/if}
			{path}
		</span>
		{#if status}<span class="status">{status}</span>{/if}
		{#if added !== null || removed !== null}
			<span class="counts">
				{#if added !== null}<span class="add">+{added}</span>{/if}
				{#if removed !== null}<span class="rem">−{removed}</span>{/if}
			</span>
		{/if}
	</div>
	{#if note}<p class="note">{note}</p>{/if}
	{#if rows.length > 0}
		<div class="scroll">
			<table class:skipped={!FRAMED} style:--digits={digits}>
				{#each runs as run, r (r)}
					<tbody style:contain-intrinsic-size={`auto ${run.length * 1.5}em`}>
						{#each run as row, i (i)}
							{#if row.kind === 'hunk' || row.kind === 'note'}
								<tr class={row.kind}>
									<td class="num" colspan="2"></td>
									<td class="code" colspan="2">{row.text}</td>
								</tr>
							{:else}
								<tr class={row.kind}>
									<td class="num old">{row.old}</td>
									<td class="num new">{row.new}</td>
									<td class="sign" aria-hidden="true">{sign[row.kind]}</td>
									<td class="code">{row.text}</td>
								</tr>
							{/if}
						{/each}
					</tbody>
				{/each}
			</table>
		</div>
	{/if}
</div>

<style>
	.diff {
		min-width: 0;
		border: var(--border-width-regular) solid var(--color-border-secondary);
		border-radius: var(--border-radius-md);
		background: var(--color-background-primary);
		overflow: hidden;
	}

	/* A boxed diff in a list's row needs no hairline above it: the boxes
	   stand apart by themselves, as knotless stacks a pull's files. */
	:global(li.item:has(> .content > .diff:not(.bare))) {
		border-top: 0;
	}

	/* The header alone, as a row of a list of files changed: no box. */
	.bare {
		border: 0;
		background: none;
	}

	.bare .head {
		padding: 0;
		background: none;
	}

	.bare .note {
		padding: var(--hud-space-1) 0 0;
		border-top: 0;
	}

	.head {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		gap: var(--hud-space-1) var(--hud-space-3);
		padding: var(--hud-space-2) var(--hud-space-3);
		background: var(--color-background-secondary);
		font-size: var(--font-text-sm-size);
		line-height: var(--font-text-sm-line-height);
	}

	.path {
		min-width: 0;
		font-family: var(--font-mono);
		font-weight: var(--font-weight-semibold);
		overflow-wrap: anywhere;
	}

	.from {
		color: var(--color-text-secondary);
		font-weight: var(--font-weight-normal);
	}

	.status {
		color: var(--color-text-secondary);
	}

	.counts {
		display: inline-flex;
		gap: var(--hud-space-2);
		margin-left: auto;
		font-family: var(--font-mono);
		font-variant-numeric: tabular-nums;
	}

	.add {
		color: var(--hud-color-open);
	}

	.rem {
		color: var(--hud-color-closed);
	}

	.note {
		margin: 0;
		padding: var(--hud-space-2) var(--hud-space-3);
		border-top: var(--border-width-regular) solid var(--color-border-secondary);
		color: var(--color-text-secondary);
		font-size: var(--font-text-sm-size);
	}

	/* The rows scroll sideways, never wrap; the numbers stay at the left. */
	.scroll {
		overflow-x: auto;
		border-top: var(--border-width-regular) solid var(--color-border-secondary);
	}

	table {
		min-width: 100%;
		border-collapse: collapse;
		font-family: var(--font-mono);
		font-size: var(--font-text-xs-size);
		line-height: 1.5;
	}

	/* Runs out of view are not drawn; in a frame, which cannot tell where its host's screen is, all are (./window.ts). */
	.skipped tbody {
		content-visibility: auto;
	}

	td {
		padding: 0;
		vertical-align: top;
		white-space: pre;
	}

	.num {
		position: sticky;
		box-sizing: content-box;
		width: calc(var(--digits) * 1ch);
		min-width: calc(var(--digits) * 1ch);
		padding: 0 var(--hud-space-2);
		background: var(--color-background-primary);
		color: var(--color-text-tertiary);
		text-align: right;
		user-select: none;
	}

	.old {
		left: 0;
	}

	.new {
		left: calc(var(--digits) * 1ch + 2 * var(--hud-space-2));
		border-right: var(--border-width-regular) solid var(--color-border-secondary);
	}

	.sign {
		width: 1ch;
		padding: 0 var(--hud-space-1);
		user-select: none;
	}

	.code {
		width: 100%;
		padding-right: var(--hud-space-3);
		color: var(--color-text-primary);
	}

	.added .code,
	.added .sign {
		background: var(--hud-diff-added);
	}

	.added .num {
		background: linear-gradient(var(--hud-diff-added-number), var(--hud-diff-added-number)), var(--color-background-primary);
		color: var(--color-text-secondary);
	}

	.removed .code,
	.removed .sign {
		background: var(--hud-diff-removed);
	}

	.removed .num {
		background: linear-gradient(var(--hud-diff-removed-number), var(--hud-diff-removed-number)), var(--color-background-primary);
		color: var(--color-text-secondary);
	}

	.hunk td {
		padding-top: var(--hud-space-1);
		padding-bottom: var(--hud-space-1);
		background: linear-gradient(var(--hud-diff-hunk), var(--hud-diff-hunk)), var(--color-background-primary);
		color: var(--color-text-secondary);
	}

	.hunk .code {
		padding-left: var(--hud-space-2);
	}

	.note .code {
		padding-left: var(--hud-space-2);
		color: var(--color-text-secondary);
		font-style: italic;
	}
</style>