Terminal look finished

This commit is contained in:
2026-11-08 11:10:33 +01:00
parent 3f7ce146ae
commit baa0d34cf4
8 changed files with 186 additions and 37 deletions
+79 -15
View File
@@ -2,13 +2,17 @@
import { onMount } from 'svelte';
let {
children,
value = $bindable(''),
cursorPrompt = '$',
blinkDelay = 250,
autofocus = true,
oninput = () => {},
onsubmit = () => {}
}: {
children: any;
value?: string;
cursorPrompt?: string;
blinkDelay?: number;
autofocus?: boolean;
oninput?: (value: string) => void;
@@ -16,6 +20,8 @@
} = $props();
let container: HTMLElement;
let textField: HTMLElement;
let cursor: HTMLElement;
let cursorPos: number = $state(value.length);
let anchorPos: number | null = $state(null);
let isDragging: boolean = $state(false);
@@ -80,6 +86,62 @@
anchorPos = null;
}
// ---- cursor alignment ---------------------------------------------------
/** Move the caret to the exact pixel where the character at cursorPos lives. */
function syncCursor() {
if (!textField || !cursor) return;
/* const containerRect = container.getBoundingClientRect();
if (containerRect.width === 0) return;
const chars = container.querySelectorAll<HTMLElement>('.char');
// With the placeholder, chars is never empty — but guard anyway.
if (chars.length === 0) return; */
const containerRect = textField.getBoundingClientRect();
const chars = textField.querySelectorAll<HTMLElement>('.char');
if (chars.length === 0) {
cursor.style.transform = `translateX(0px)`;
return;
}
let refRect: DOMRect;
let x: number;
if (cursorPos >= chars.length) {
refRect = chars[chars.length - 1].getBoundingClientRect();
x = refRect.right - containerRect.left;
} else {
refRect = chars[cursorPos].getBoundingClientRect();
x = refRect.left - containerRect.left;
if (chars[cursorPos].classList.contains('selected')) {
cursor.style.backgroundColor = 'var(--color-bg-hard)';
} else {
cursor.style.backgroundColor = 'var(--color-fg1)';
}
}
cursor.style.transform = `translateX(${x}px)`;
}
// Re-sync after every value / cursor movement (runs after DOM update).
$effect(() => {
void value;
void cursorPos;
syncCursor();
});
// Re-sync on webfont load and on resize (font metrics can change).
$effect(() => {
if (!container) return;
const ro = new ResizeObserver(() => syncCursor());
ro.observe(container);
document.fonts?.ready.then(() => syncCursor());
return () => ro.disconnect();
});
$effect(() => {
if (autofocus && container) {
container.focus();
@@ -303,7 +365,7 @@
<div
id="terminal-input"
class="relative inline-block font-mono whitespace-nowrap outline-none select-none"
class="relative inline-block font-mono leading-relaxed whitespace-nowrap outline-none select-none"
bind:this={container}
tabindex="1"
role="textbox"
@@ -313,38 +375,40 @@
onkeydown={handleKeydown}
onpaste={handlePaste}
onmousedown={handleMouseDown}
style="--cursor-index: {cursorPos};"
>
<!-- <span class="text-fg-dim inline-block">~/Downloads</span> -->
<span class="prompt text-fg inline-block w-[1ch] text-center">$</span>
<span class="inline cursor-text" aria-hidden="true">
{@render children()}<span
bind:this={textField}
class="text-fg0 relative inline cursor-text"
aria-hidden="true"
>
{#each value.split('') as char, i}
<span
class="char text-glow inline-block w-[1ch] text-center leading-relaxed whitespace-pre-wrap"
class="char h-3 w-[1ch] py-0.5 leading-relaxed whitespace-pre-wrap"
class:selected={isSelected(i)}
class:text-bg={isSelected(i)}
data-char-index={i}>{char}</span
>
{/each}
<div
id="curs"
bind:this={cursor}
class="cursor bg-fg1 absolute bottom-0 left-0 h-px w-[1ch]"
class:animate-cursor-blink={!blinkPaused && !selection.active}
></div>
</span>
<div
id="curs"
class="cursor bg-fg1 absolute bottom-1 h-px w-[1ch]"
class:animate-cursor-blink={!blinkPaused && !selection.active}
></div>
</div>
<style>
.cursor {
left: calc(var(--cursor-index) * 1ch + 8.5px + 1ch);
transition: all 0.1s ease;
transition: all 80ms ease;
}
.char {
transition: all linear 0.1s;
transition: all 80ms ease;
}
.char.selected {
transition: none;
background-color: var(--);
background-color: var(--fg1);
border-radius: 2px;
}