Skip to content

@jsvision/core / decode

Function: decode()

decode(bytes, state, options?): DecodeResult

Defined in: input/decoder.ts:98

Decode a chunk of terminal bytes into input events.

Always feed the returned state back into the next call — it carries an incomplete trailing sequence (or an in-progress paste) so a token split across two chunks is not lost. Capability-query replies are returned on the separate queries channel, never mixed into events.

Parameters

bytes

Uint8Array

The newly received bytes (e.g. a stdin 'data' chunk).

state

DecoderState

The state returned by the previous call, or createDecoderState().

options?

DecodeOptions

Optional capability profile and paste-cap override.

Returns

DecodeResult

The decoded events, isolated query replies (queries), the incomplete trailing bytes (rest), and the next state to pass to the following call.

Example

ts
import { createDecoderState, decode } from '@jsvision/core';

const enc = new TextEncoder();
let state = createDecoderState();

// A modified arrow key arriving split across two chunks. The partial CSI in the
// first chunk is carried; the second chunk completes it into one Ctrl+Right key.
let r = decode(enc.encode('\x1b[1'), state);
state = r.state; // r.events is empty — the sequence is not complete yet
r = decode(enc.encode(';5C'), state);
for (const ev of r.events) {
  if (ev.type === 'key') console.log(ev.key, ev.ctrl); // 'right' true
}