UNPKG

@nu-art/commando

Version:
90 lines (89 loc) 3.52 kB
import { Logger, ResolvableContent } from '@nu-art/ts-common'; import { BlessedWidget, BlessedWidgetOptions, BlessedWidgetsType } from './types'; type KeyBinding = { keys: string[]; callback: VoidFunction; }; /** * An abstract class representing a container for Blessed widgets with state management and key bindings. * * @template Type - The type of the Blessed widget. * @template State - The type of the state object. */ export declare abstract class ConsoleContainer<Type extends BlessedWidgetsType, State extends object = {}> extends Logger { private readonly containerProps?; private readonly keyBinding; private enabled; protected state: State; protected container: BlessedWidget[Type]; protected type: Type; protected readonly widgets: BlessedWidget[BlessedWidgetsType][]; /** * Creates an instance of ConsoleContainer. * * @param {Type} type - The type of the container widget. * @param {BlessedWidgetOptions[Type]} [containerProps] - The properties to apply to the container widget. * @param {KeyBinding[]} [keyBinding] - An array of key bindings for the container widget. */ protected constructor(type: Type, containerProps?: BlessedWidgetOptions[Type], keyBinding?: KeyBinding[]); /** * Sets the state of the container and triggers a re-render. * * @param {ResolvableContent<Partial<State>>} state - The new state to set. * @returns {this} The current instance for method chaining. */ setState(state: ResolvableContent<Partial<State>>): this; /** * Creates a Blessed widget of the specified type and adds it to the container. * * @template WidgetType - The type of the widget to create. * @param {WidgetType} type - The type of the widget to create. * @param {BlessedWidgetOptions[WidgetType]} props - The properties to apply to the widget. * @returns {BlessedWidget[WidgetType]} The created widget. */ createWidget<WidgetType extends BlessedWidgetsType>(type: WidgetType, props: BlessedWidgetOptions[WidgetType]): BlessedWidget[WidgetType]; /** * Gets the currently focused widget within the container. * * @returns {Widgets.Node} The focused widget. */ getFocusedWidget(): BlessedWidget[BlessedWidgetsType]; private createContainer; private _createWidgets; /** * Creates the widgets within the container. * This method should be implemented by subclasses to define the specific widgets to create. */ protected abstract createContent(): void; /** * Creates the container and its widgets, and resumes rendering if not already enabled. * * @returns {this} The current instance for method chaining. */ create(): this; /** * Resumes rendering and enables focus on the widgets. * * @returns {this} The current instance for method chaining. */ readonly resume: () => this; private _render; /** * Renders the container. * This method should be implemented by subclasses to define the specific rendering logic. */ protected abstract render(): void; /** * Pauses rendering and disables focus on the widgets. * * @returns {this} The current instance for method chaining. */ readonly pause: () => this; /** * Disposes of the container and its widgets. * * @returns {this} The current instance for method chaining. */ readonly dispose: () => this; } export {};