Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292import { createEffect, createMemo, Show, onCleanup, onSettled, type VoidComponent } from "solid-js";
import { commandLine } from "../lib/cli";import { useLibraryQuery } from "../lib/library/query";import { count } from "../lib/print";
/** * The line above the transport: one field, two verbs. * * `/` types a needle into the library. `:` types a command at the player. Both put * the caret in the same `<input>` — the reason the two halves are one component * rather than two that would have to negotiate the caret — and both are gone the * moment the field loses focus, which is the app's standing rule about a text field: * the keyboard belongs to the page unless the caret says otherwise. * * It sits beside `<PlayerBar />` and for the same reason the bar does (App.tsx): what * it controls belongs to the session, not to whichever view is up. The bar moves the * music and this line asks for the move; the toolbar at the top of the screen is the * library's own — counts, the crawl, the news of a key — and no longer holds the * field. * * Like the query box it replaced, the field is uncontrolled: the DOM owns the text * while the user edits it, and the effect below is the only way a string gets in from * outside. Everything the browser already knows about editing text — selection, * Ctrl-A, word-wise deletion, IME composition, paste — is the browser's, and is not * ours to get wrong. */export const CommandLine: VoidComponent = () => { const store = commandLine(); const query = useLibraryQuery(); useCommandLineKeys();
let box: HTMLInputElement | undefined; onCleanup(() => store.attach(null));
const commandMode = createMemo(() => store.mode() === "command", { name: "cli-is-command", }); /** The needle, kept lit only while it is actually narrowing something. */ const queryLive = createMemo(() => !!query.text().trim(), { name: "cli-query-live", }); const matches = store.matches; /** No needle is no verdict; a needle with nothing under it is the one worth colouring. */ const none = createMemo(() => queryLive() && matches() === 0, { name: "cli-none", });
/** * What the field should hold, which depends on which half of the line is up. The * search half shows the needle, command mode shows the command being typed — two * strings in two stores and one `<input>`, so switching modes is a write to the * field. * * The value guard is what keeps this from being a controlled input by another name: * typing writes the store with exactly what the field already holds, the effect runs, * finds no difference, and never reassigns `input.value`, which is the assignment that * drops the caret. * * The mode is read here rather than through a memo of the string to show: a memo whose * own dependency changes — `command()` while in command mode, `query()` outside it — * was the first shape, and the effect never heard it change, so a run command left its * text in the field for `/` to find. */ createEffect( () => (commandMode() ? store.command() : query.text()), (text) => { if (box && box.value !== text) box.value = text; }, { name: "cli-restore" }, );
/** * The readout beside the prompt. Three things want that gutter and only one of them * can have it, and the order is recency: * * 1. what the word being typed means — the live commentary, while there is a word; * 2. what the last command did — until the next key takes it, as the toolbar's share * line does; * 3. what the needle left — `9 tracks`, `no matches`, the search's own count. * * Every source is read before any branch, rather than read inside the arm that needs * it: a computation whose dependency set depends on its own result is a computation * that can stop hearing the source it stopped reading, and this one has three sources * and two modes to move between. */ const readout = createMemo( () => { const commentary = store.hint(); const hits = matches(); const live = queryLive(); const command = commandMode(); if (command) return commentary; if (commentary) return commentary; if (!live) return null; return { text: hits === null || hits === 0 ? "no matches" : count(hits, "track"), bad: hits === 0, }; }, { name: "cli-readout" }, );
return ( <div class="cmd-line" data-mode={store.mode()} data-filtered={!commandMode() && queryLive() ? "true" : "false"} data-typing={!commandMode() && store.focused() ? "true" : "false"} data-none={!commandMode() && none() ? "true" : "false"} > {/* The prompt is chrome outside the field, so selecting, copying and Ctrl-A see only the query or the command. It says one character and it says it only when it is true: the search half has no glyph at rest — the caret and the list above it already say the keyboard is yours, and an idle `/` was a hint about a key rather than a mark of a state. */} <span class="lib-query" role={commandMode() ? undefined : "search"} data-empty={queryLive() ? "false" : "true"} data-focused={store.focused() ? "true" : "false"} data-mode={store.mode()} > <Show when={commandMode()}> <span class="lib-query-prompt" aria-hidden="true"> : </span> </Show> <input ref={(el) => { box = el; store.attach(el); }} class="lib-query-input" type="text" aria-label={commandMode() ? "Command the player" : "Filter the library"} spellcheck="false" autocomplete="off" data-none={none() ? "true" : "false"} onInput={(e) => { // The peek, not the memo: the input event and the Enter that follows it are // the same task, and a signal read there can answer from before the write. if (store.peekMode() === "command") store.setCommand(e.currentTarget.value); else query.set(e.currentTarget.value); }} onFocus={() => store.setFocused(true)} onBlur={() => store.setFocused(false)} onKeyDown={(e) => { // Only the keys this field delegates back to the app. Ctrl-A, the arrows // excepted below, Delete, and everything else the browser already does to // an input are left entirely alone. And the mode is read off the store's // mirror: this handler reads what the press before it wrote. const field = e.currentTarget; const typing = store.peekMode() === "command"; if (e.key === "Enter") { e.preventDefault(); if (typing) { // A word nobody recognised stays put with its text on the line, which // is how a mistyped command gets finished instead of retyped; a word // that ran gives the keyboard back, as search Enter always has. if (store.submit()) field.blur(); return; } // Accept the filter and get out of the way: the query stays, the caret // goes, and the keyboard is the list's again. On a phone this also puts // the on-screen keyboard away, which is the same gesture doing double // duty. What the filter left is the view's to decide — see the host's // acceptSearch, which is where the one-track case plays a song. field.blur(); store.clearNotice(); store.acceptSearch(); } else if (e.key === "Escape") { e.preventDefault(); if (typing) { // Out, and the half-typed command with it: blur is what ends command // mode, so there is no second state to clear first. field.blur(); } else if (field.value) { query.set(""); } else { store.dismissSearch(); field.blur(); } } else if (e.key === ":" && !typing && field.value === "") { // The shell's bargain, offered from inside the field as well as outside it. // // A `:` typed in this field is a character: that guard is what lets a needle of // `Blade Runner:` or a seek of `1:20` be typed at all, and the window's own binding // stands down for INPUT targets precisely so the two rules cannot fight. But an empty // field has no needle to protect — nothing anybody filters for begins with a colon — // and the reader has just watched `:` mean "the prompt" on this same line from the // keyboard. So with the caret already in here, the only way to a command had been to // leave the field and come back: a rule nobody reads and everybody trips over, which is // what the screenshot in this comment's place was. // // Only the empty field, and only as the first keystroke: a colon typed into a needle // that already has text in it is editing that needle, and stays a character. e.preventDefault(); store.armCommand(); } else if ( e.key === "Backspace" && !field.value && field.selectionStart === field.selectionEnd ) { // Leaving the way it was entered: one more Backspace on an empty line // hands the keyboard back. e.preventDefault(); field.blur(); } else if (typing && (e.key === "ArrowUp" || e.key === "ArrowDown")) { // History, and only here: in the search half the arrows belong to the // caret, and the list's cursor is out of the question because the caret // is in this field. const back = store.recall(e.key === "ArrowUp" ? -1 : 1); if (back === null) return; // nothing typed yet: let the caret have them e.preventDefault(); field.value = back; store.setCommand(back); // The caret goes at the end of what was recalled, not where it was // sitting in the middle of the old text. field.setSelectionRange(back.length, back.length); } }} /> <Show when={readout()}> {(line) => ( <span class="lib-query-count" role="status" data-bad={line().bad ? "true" : undefined}> {line().text} </span> )} </Show> </span> </div> );};
/** * The two keys that open the line, bound on the window beside every other app key and * guarded the same way every other app key is: modifiers belong to the browser, and a * key pressed while the caret is in a field is typing, not a command. That second * guard is what lets a search for `cap/` and a seek of `1:20` be typed at all — the * `/` and the `:` that reach the field stay in the field. * * `:` is the prompt glyph and the key that asks for the prompt, which is the same * bargain a shell makes: you are not typing a command until you have said so. * * The line stays open across navigation, so the binding lives with the line rather * than with a view — and it deliberately does *not* clear the last outcome on `/` * and `:` themselves, which are the two keys that want to read it. * * The needle is written here, and only here, for the `/` that opens the search half: * a second `/` means "ask again", not "keep asking the same question", so the old * needle goes away with the key rather than waiting in the field for the next * keystroke to replace it. That is the one needle write outside the field's own * `onInput` and the Escape that undoes it, and it is a write to the query store * rather than to the element — the field is uncontrolled, and the store is what the * list, the count, and the wash are all reading. */export function useCommandLineKeys(): void { const line = commandLine(); const query = useLibraryQuery(); onSettled(() => { const onKey = (e: KeyboardEvent) => { if (e.metaKey || e.ctrlKey || e.altKey) return; const t = e.target as (HTMLElement & { isContentEditable?: boolean; type?: string }) | null; if (!t) return; if (t.tagName === "TEXTAREA" || t.isContentEditable) return; // Including this component's own field: `/` and `:` typed there are characters. if (t.tagName === "INPUT") return; if (e.key === "/") { e.preventDefault(); // Out with the old needle first, so the caret lands in an empty field: what // the next keystroke starts is a new search. Only when there is one to // remove — an empty query needs no write, and writing it would be a // persistence round-trip for nothing. if (query.peek()) query.set(""); line.armSearch(); return; } if (e.key === ":") { e.preventDefault(); line.armCommand(); return; } // Any other key takes the last command's outcome away, the way the next key // takes the toolbar's share line: it is feedback on a press. line.clearNotice(); }; window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); });}