UNPKG

console-gui-tools

Version:

A simple library to draw option menu, text popup or other widgets and layout on a Node.js console.

311 lines 14.5 kB
/// <reference types="node" /> /// <reference types="node" /> import { EventEmitter } from "events"; import PageBuilder from "./components/PageBuilder.js"; import InPageWidgetBuilder from "./components/InPageWidgetBuilder.js"; import Screen from "./components/Screen.js"; import { CustomPopup, PopupConfig } from "./components/widgets/CustomPopup.js"; import { ButtonPopup, ButtonPopupConfig } from "./components/widgets/ButtonPopup.js"; import { ConfirmPopup, ConfirmPopupConfig } from "./components/widgets/ConfirmPopup.js"; import { FileSelectorPopup, FileSelectorPopupConfig } from "./components/widgets/FileSelectorPopup.js"; import { InputPopup, InputPopupConfig } from "./components/widgets/InputPopup.js"; import { OptionPopup, OptionPopupConfig } from "./components/widgets/OptionPopup.js"; import { Control, ControlConfig } from "./components/widgets/Control.js"; import { Box, BoxConfig, BoxStyle } from "./components/widgets/Box.js"; import { Button, ButtonConfig, ButtonKey, ButtonStyle } from "./components/widgets/Button.js"; import { Progress, ProgressConfig, Orientation, ProgressStyle } from "./components/widgets/ProgressBar.js"; import LayoutManager, { LayoutOptions } from "./components/layout/LayoutManager.js"; import { MouseEvent, MouseManager, MouseEventArgs, RelativeMouseEvent } from "./components/MouseManager.js"; import { PhisicalValues, StyledElement, SimplifiedStyledElement, StyleObject } from "./components/Utils.js"; import { EOL } from "node:os"; /** * @description This type is used to define the parameters of the KeyListener event (keypress). * @typedef {Object} KeyListenerArgs * @prop {string} name - The name of the key pressed. * @prop {boolean} ctrl - If the ctrl key is pressed. * @prop {boolean} shift - If the shift key is pressed. * @prop {boolean} alt - If the alt key is pressed. * @prop {boolean} meta - If the meta key is pressed. * @prop {boolean} sequence - If the sequence of keys is pressed. * * @export * @interface KeyListenerArgs */ export interface KeyListenerArgs { name: string; sequence: string; ctrl: boolean; alt: boolean; shift: boolean; meta: boolean; code: string; } /** * @description This type is used to define the ConsoleGui options. * @typedef {Object} ConsoleGuiOptions * @prop {string} [title] - The title of the ConsoleGui. * @prop {0 | 1 | 2 | 3 | "popup"} [logLocation] - The location of the logs. * @prop {string} [showLogKey] - The key to show the log. * @prop {number} [logPageSize] - The size of the log page. * @prop {LayoutOptions} [layoutOptions] - The options of the layout. * @prop {boolean} [enableMouse] - If the mouse should be enabled. * @prop {boolean} [overrideConsole = true] - If the console.log|warn|error|info should be overridden. * @prop {string} [focusKey = "tab"] - The key to focus the next widget. * * @export * @interface ConsoleGuiOptions */ export interface ConsoleGuiOptions { logLocation?: 0 | 1 | 2 | 3 | "popup"; showLogKey?: string; logPageSize?: number; layoutOptions?: LayoutOptions; title?: string; enableMouse?: boolean; overrideConsole?: boolean; focusKey?: string; } /** * @class ConsoleManager * @extends EventEmitter * @description This class is used to manage the console GUI and all the widgets. * This is a singleton class, so you can use it like this: const CM = new ConsoleManager() * Emits the following events: * - "keypressed" to propagate the key pressed event to the application * - "layoutratiochanged" when the layout ratio is changed * - "exit" when the user wants to exit the application * @param {object} options - The options of the ConsoleManager. * @example const CM = new ConsoleManager({ logPageSize: 10, layoutBorder: true, changeLayoutKey: 'ctrl+l', title: 'Console Application' }) */ declare class ConsoleManager extends EventEmitter { Terminal: NodeJS.WriteStream & { fd: 1; }; Input: NodeJS.ReadStream & { fd: 0; }; static instance: ConsoleManager; Screen: Screen; popupCollection: { [key: string]: any; }; controlsCollection: { [key: string]: Control; }; eventListenersContainer: { [key: string]: (_str: string, key: KeyListenerArgs) => void; } | { [key: string]: (key: MouseEvent) => void; }; logLocation: 0 | 1 | 2 | 3 | "popup"; logPageSize: number; logPageTitle: string; pages: PageBuilder[]; layoutOptions: LayoutOptions; layout: LayoutManager; changeLayoutKey: string; maxListeners: number; private changeLayoutkeys; applicationTitle: string; private showLogKey; stdOut: PageBuilder; mouse: MouseManager; private parsingMouseFrame; private previousFocusedWidgetsId; private focusKey; constructor(options?: ConsoleGuiOptions | undefined); private inputInterface; /** * @description This method is used to change the layout options. * if update is true, the layout will be updated. * * @param {LayoutOptions} options * @param {boolean} [update=true] * @memberof ConsoleManager * * @example CM.setLayoutOptions({ showTitle: true, boxed: true, boxColor: 'cyan', boxStyle: 'bold', changeFocusKey: 'ctrl+l', type: 'double', direction: 'vertical' }) * @example CM.setLayoutOptions({ ...CM.getLayoutOptions(), type: "quad" }) */ setLayoutOptions(options: LayoutOptions, update?: boolean): void; /** * @description This method is used to update the layout * @memberof ConsoleManager */ updateLayout(): void; /** * @description This method is used to get the layout options. * @returns {LayoutOptions} The layout options. * @memberof ConsoleManager * @example CM.getLayoutOptions() * @example CM.getLayoutOptions().boxed */ getLayoutOptions(): LayoutOptions; /** * @description This method is used to get the log page size. * @returns {number} The log page size. * @memberof ConsoleManager * @example CM.getLogPageSize() */ getLogPageSize(): number; /** * @description This method is used to set the log page size. * @param {number} size - The new log page size. * @returns {void} * @example CM.setLogPageSize(10) */ setLogPageSize(size: number): void; /** * @description This method is used to remove focus from other widgets. * * @param {string} widget * @memberof ConsoleManager */ unfocusOtherWidgets(widget: string): void; restoreFocusInWidgets(): void; /** * @description This function is used to make the ConsoleManager handle the key events when no widgets are showed. * Inside this function are defined all the keys that can be pressed and the actions to do when they are pressed. * @memberof ConsoleManager */ private addGenericListeners; /** * @description This function is used to set a key listener for a specific widget. The event listener is stored in the eventListenersContainer object. * @param {string} id - The id of the widget. * @param {function} manageFunction - The function to call when the key is pressed. * @memberof ConsoleManager * @example CM.setKeyListener('inputPopup', popup.keyListener) */ setKeyListener(id: string, manageFunction: (_str: string, key: KeyListenerArgs) => void): void; /** * @description This function is used to remove a key listener for a specific widget. The event listener is removed from the eventListenersContainer object. * @param {string} id - The id of the widget. * @memberof ConsoleManager * @example CM.removeKeyListener('inputPopup') */ removeKeyListener(id: string): void; /** * @description This function is used to set a mouse listener for a specific widget. The event listener is stored in the eventListenersContainer object. * @param {string} id - The id of the widget. * @param {function} manageFunction - The function to call when the key is pressed. * @memberof ConsoleManager * @example CM.setMouseListener('inputPopup', popup.mouseListener) */ setMouseListener(id: string, manageFunction: (key: MouseEvent) => void): void; /** * @description This function is used to remove a mouse listener for a specific widget. The event listener is removed from the eventListenersContainer object. * @param {string} id - The id of the widget. * @memberof ConsoleManager * @example CM.removeMouseListener('inputPopup') */ removeMouseListener(id: string): void; /** * @description This function is used to register a popup. The popup is stored in the popupCollection object. That is called by the popups in show(). * @param {popup} popup - The popup to register. * @memberof ConsoleManager */ registerPopup(popup: any): void; /** * @description This function is used to unregister a popup. The popup is removed from the popupCollection object. That is called by the popups in hide(). * @param {string} id - The id of the popup. * @memberof ConsoleManager */ unregisterPopup(popup: any): void; /** * @description This function is used to register a control. The control is stored in the controlCollection object. That is called by the controls in show(). * @param {control} control - The control to register. * @memberof ConsoleManager */ registerControl(control: any): void; /** * @description This function is used to unregister a control. The control is removed from the controlCollection object. That is called by the controls in hide(). * @param {string} id - The id of the control. * @memberof ConsoleManager */ unregisterControl(control: any): void; /** * @description This function is used to set the home page. It also refresh the screen. * @param {PageBuilder} page - The page to set as home page. * @memberof ConsoleManager * @example CM.setHomePage(p) * @deprecated since version 1.1.12 - Use setPage or setPages instead */ setHomePage(page: PageBuilder): void; /** * @description This function is used to set a page of layout. It also refresh the screen. * @param {PageBuilder} page - The page to set as home page. * @param {number} [pageNumber] - The page number to set. 0 is the first page, 1 is the second page. * @param {string | null} [title] - The title of the page to overwrite the default title. Default is null. * @memberof ConsoleManager * @example CM.setPage(p, 0) */ setPage(page: PageBuilder, pageNumber?: number, title?: string | null): void; /** * @description This function is used to set both pages of layout. It also refresh the screen. * @param {Array<PageBuilder>} pages - The page to set as home page. * @param {string[] | null} [titles] - The titles of the page to overwrite the default titles. Default is null. * @memberof ConsoleManager * @example CM.setPages([p1, p2], 0) */ setPages(pages: Array<PageBuilder>, titles?: string[] | null): void; /** * @description This function is used to refresh the screen. It do the following sequence: Clear the screen, draw layout, draw widgets and finally print the screen to the stdOut. * @memberof ConsoleManager * @example CM.refresh() */ refresh(): void; /** * @description This function is used to show a popup containing all the stdOut of the console. * @memberof ConsoleManager * @returns the instance of the generated popup. * @example CM.showLogPopup() */ showLogPopup(): CustomPopup; /** * @description This function is used to log a message. It is used to log messages in the log page. Don't add colors to the message. * @param {string} message - The message to log. * @memberof ConsoleManager * @example CM.log("Hello world") */ log(message: string): void; /** * @description This function is used to log an error message. It is used to log red messages in the log page. Don't add colors to the message. * @param {string} message - The message to log. * @memberof ConsoleManager * @example CM.error("Anomaly detected") */ error(message: string): void; /** * @description This function is used to log a warning message. It is used to log yellow messages in the log page. Don't add colors to the message. * @param {string} message - The message to log. * @memberof ConsoleManager * @example CM.warn("Anomaly detected") */ warn(message: string): void; /** * @description This function is used to log an info message. It is used to log blue messages in the log page. Don't add colors to the message. * @param {string} message - The message to log. * @memberof ConsoleManager * @example CM.info("Anomaly detected") */ info(message: string): void; /** * @description This function is used to update the logs console. It is called by the log functions. * @param {boolean} resetCursor - If true, the log scroll index is resetted. * @memberof ConsoleManager */ private updateLogsConsole; /** * @description This function is used to override the console.log, console.error, console.warn and console.info functions. * @memberof ConsoleManager * @example CM.overrideConsole() * @example console.log("Hello world") // Will be logged in the log page. * @example console.error("Anomaly detected") // Will be logged in the log page. * @example console.warn("Anomaly detected") // Will be logged in the log page. * @example console.info("Anomaly detected") // Will be logged in the log page. * @example console.debug("Anomaly detected") // Will be logged in the log page. * @since 1.1.42 */ private overrideConsole; } export { EOL, RelativeMouseEvent, MouseEventArgs, MouseEvent, PhisicalValues, StyledElement, SimplifiedStyledElement, StyleObject, PageBuilder, InPageWidgetBuilder, Control, ControlConfig, Box, BoxConfig, BoxStyle, Button, ButtonConfig, ButtonStyle, ButtonKey, Progress, ProgressConfig, ProgressStyle, Orientation, ConsoleManager, OptionPopup, OptionPopupConfig, InputPopup, InputPopupConfig, ConfirmPopup, ConfirmPopupConfig, ButtonPopup, ButtonPopupConfig, CustomPopup, PopupConfig, FileSelectorPopup, FileSelectorPopupConfig, }; //# sourceMappingURL=ConsoleGui.d.ts.map