Elius94 / console-gui-tools

A simple library to draw option menu or other popup inputs and layout on Node.js console.
MIT License
111 stars 17 forks source link

New widget: Filemanager #26

Closed Elius94 closed 2 years ago

Elius94 commented 2 years ago

Fixes #20

Classes

FileManagerPopupEventEmitter

Constants

CM : ConsoleManager

the instance of ConsoleManager (singleton)

FileManagerPopup ⇐ EventEmitter

Kind: global class Extends: EventEmitter

new FileManagerPopup(id, title, basePath, [limitToPath], [allowedExtensions], visible)

This class is used to create a popup with a file input to select a file or a directory. It will run a promise with fs.readdir to get the list of files and directories. The user can select a file or a directory and the popup will be closed.

FileManagerPopup

Emits the following events:

Param Type Default Description
id string The id of the popup.
title string The title of the popup.
basePath string The main path of the popup. re case sensitive.
[limitToPath] boolean false If true, the user * @param {boolean} [selectDirectory=false] - If true, the user can select a directory. Otherwise, only files are selectable. When true, to enter a directory, the user must press the space key instead of the enter key.
[allowedExtensions] Array.<string> [] The allowed extensions. If not set, all extensions are allowed. The extensions a can only select files in the path. If false, the user can select files in the path and parent directories.
visible boolean If the popup is visible. Default is false (make it appears using show()).

Example

const popup = new FileManagerPopup("popup1", "Choose the file", "./examples").show().on("confirm", (selected) => { console.log(selected) }) // show the popup and wait for the user to confirm

fileManagerPopup.listDir(path) ⇒ Promise.<Array.<object>>

This function is used to load the list of files and directories in the current path. it return a promise with the list of files and directories. The list is an array of objects like this: [{text: "📄 file.ext", name: "file.ext", type: "file", path: "path/to/file.ext"}, {text: "📁 dir/", name: "dir", type: "dir", path: "path/to/dir"}]

Kind: instance method of FileManagerPopup Returns: Promise.<Array.<object>> - The list of files and directories.

Param Type Description
path string The path to load the list.

fileManagerPopup.updateList(path)

This function calls the updateList function and store the result to this.options, it also refresh the list of files and directories.

Kind: instance method of FileManagerPopup

Param Type Description
path string The path to load the list.

fileManagerPopup.keyListner(str, key)

This function is used to make the ConsoleManager handle the key events when the popup is showed. Inside this function are defined all the keys that can be pressed and the actions to do when they are pressed.

Kind: instance method of FileManagerPopup

Param Type Description
str string The string of the input.
key Object The key object.

fileManagerPopup.getSelected() ⇒ string | number

This function is used to get the selected option.

Kind: instance method of FileManagerPopup Returns: string | number - The selected value of the popup.

fileManagerPopup.setSelected(selected) ⇒ FileManagerPopup

This function is used to change the selection of the popup. It also refresh the ConsoleManager.

Kind: instance method of FileManagerPopup Returns: FileManagerPopup - The instance of the FileManagerPopup.

Param Type Description
selected string | number The new value of the selection.

fileManagerPopup.show() ⇒ FileManagerPopup

This function is used to show the popup. It also register the key events and refresh the ConsoleManager.

Kind: instance method of FileManagerPopup Returns: FileManagerPopup - The instance of the FileManagerPopup.

fileManagerPopup.hide() ⇒ FileManagerPopup

This function is used to hide the popup. It also unregister the key events and refresh the ConsoleManager.

Kind: instance method of FileManagerPopup Returns: FileManagerPopup - The instance of the FileManagerPopup.

fileManagerPopup.isVisible() ⇒ boolean

This function is used to get the visibility of the popup.

Kind: instance method of FileManagerPopup Returns: boolean - The visibility of the popup.

fileManagerPopup.manageInput() ⇒ FileManagerPopup

This function is used to add the FileManagerPopup key listener callback to te ConsoleManager.

Kind: instance method of FileManagerPopup Returns: FileManagerPopup - The instance of the FileManagerPopup.

fileManagerPopup.unManageInput() ⇒ FileManagerPopup

This function is used to remove the FileManagerPopup key listener callback to te ConsoleManager.

Kind: instance method of FileManagerPopup Returns: FileManagerPopup - The instance of the FileManagerPopup.

fileManagerPopup.draw() ⇒ FileManagerPopup

This function is used to draw the FileManagerPopup to the screen in the middle.

Kind: instance method of FileManagerPopup Returns: FileManagerPopup - The instance of the FileManagerPopup.

CM : ConsoleManager

the instance of ConsoleManager (singleton)

Kind: global constant