Skip to content
This repository has been archived by the owner on Sep 23, 2021. It is now read-only.

clebert/apochromat

Repository files navigation

apochromat

This project will not be further developed. You may be interested in the conceptually similar successor project reactmpl.

Dynamic text rendering for interactive command line apps.

Installation

npm install apochromat --save

Usage

Hello World

import {Lens} from 'apochromat';

const greeting = new Lens();
const salutation = new Lens();
const subject = new Lens();

greeting.subscribe((event) => {
  if (event === 'render') {
    console.log(greeting.frame);
  }
});

salutation.render`Hi`;
subject.render`everyone`;
greeting.render`${salutation}, ${subject}!`;
subject.render`world`;
salutation.render`Hello`;
Hi, everyone!
Hi, world!
Hello, world!

Rendering of dynamic lists

import {Lens, list} from 'apochromat';

const foobarbaz = new Lens();
const foo = new Lens();
const bar = new Lens();
const baz = new Lens();

foo.render`foo`;
bar.render`bar`;
baz.render`baz`;
foobarbaz.render(...list(',', foo, bar, baz));
console.log(foobarbaz.frame);
foo,bar,baz

Types

type LensListener = (event: LensEvent) => void;
type LensEvent = 'attach' | 'detach' | 'render';

class Lens {
  get frame(): string;
  render(segments: readonly string[], ...children: readonly unknown[]): boolean;
  subscribe(listener: LensListener): () => void;
}
function list(
  delimiter: string,
  ...elements: readonly unknown[]
): [readonly string[], ...(readonly unknown[])];

Copyright 2021 Clemens Akens. All rights reserved. MIT license.