Skip to main content Link Search Menu Expand Document (external link)

ContextStyler overview

This module implements a type that applies to a string or text a style that depends on a context object. Two constructors are provided:

  • makeConstant: creates a ContextStyler that always applies the same style regardless of the context. Useful when a function expects a ContextStyler but should ignore the context.
  • makePaletteBased: creates a ContextStyler that contains a Palette (an array of styles) and a function that transforms a context object into an index. The style that is applied is the one at position index (modulo the number of styles in the Palette).

Table of contents


Constructors

makeConstant

Constructor of a constant ContextStyler that always applies the same style regardless of the context

Signature

export declare const makeConstant: ({ style }: { readonly style: ASStyle.Type }) => Type<unknown>

makePaletteBased

Constructor of a palette-based ContextStyler that selects a style from a palette based on a context object of type C. The indexFromContext function must return an integer index from a value of type C. Use a named function or function declaration for indexFromContext (don’t use an arrow function), otherwise the name of this ContextStyler will be meaningless (e.g. function depth(c) { return c. depth; }, not (c)=>c.depth ).

Signature

export declare const makePaletteBased: <C>({
  palette,
  indexFromContext
}: {
  readonly palette: ASPalette.Type
  readonly indexFromContext: (c: C) => number
}) => Type<C>

Getters

id

Gets the id of self

Signature

export declare const id: MTypes.OneArgFunction<Any, string>

style

Gets the style function of self

Signature

export declare const style: <C>(self: Type<C>) => Type<C>["style"]

Instances

black

Original black color instance

Signature

export declare const black: Never

blue

Original blue color instance

Signature

export declare const blue: Never

cyan

Original cyan color instance

Signature

export declare const cyan: Never

green

Original green color instance

Signature

export declare const green: Never

magenta

Original magenta color instance

Signature

export declare const magenta: Never

none

None ASContextStyler instance: does not apply any style

Signature

export declare const none: Never

red

Original red color instance

Signature

export declare const red: Never

white

Original white color instance

Signature

export declare const white: Never

yellow

Original yellow color instance

Signature

export declare const yellow: Never

Models

Any (interface)

Type of an ASContextStyler to which any ASContextStyler can be assigned

Signature

export interface Any extends Type<never> {}

Never (interface)

Type of an ASContextStyler that can be assigned to any ASContextStyler (i.e an ASContextStyler that works with any context object)

Signature

export interface Never extends Type<unknown> {}

Type (class)

Type that represents an ASContextStyler

Signature

export declare class Type<C> {
  public constructor({ id, style }: MTypes.Data<Type<C>>)
}

[MData.idSymbol] (method)

Returns the id of this

Signature

[MData.idSymbol](): string | (() => string)

id (property)

Id of this ContextStyler instance. Useful for equality and debugging

Signature

readonly id: string

style (property)

Function that takes a context c and returns an ASStyle

Signature

readonly style: (c: C) => ASStyle.Type

Module markers

moduleTag

Module tag

Signature

export declare const moduleTag: "@parischap/ansi-styles/ContextStyler/"