Added type definitions for terminal-menu (#11697)

This commit is contained in:
Arun Aravind
2016-10-04 22:20:05 +05:30
committed by Mohamed Hegazy
parent fe6b43c5bd
commit 1cc8629b01
2 changed files with 199 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
/// <reference path="terminal-menu.d.ts" />
import * as tty from "tty"; // For typing
let stdin = (<tty.ReadStream> process.stdin);
if(!stdin.isTTY) {
console.log("Terminal not supported");
process.exit(0);
}
const MenuContainerFactory = require("terminal-menu");
let menu = MenuContainerFactory({ width: 29, x: 4, y: 2 });
menu.reset();
menu.write('SERIOUS BUSINESS TERMINAL\n');
menu.write('-------------------------\n');
menu.add('ADD TRANSACTION INVOICE');
menu.add('BUSINESS INTELLIGENCE');
menu.add('ACCOUNTS PAYABLE');
menu.add('LEDGER BOOKINGS');
menu.add('INDICATOR CHART METRICS');
menu.add('BACKUP DATA TO FLOPPY DISK');
menu.add('RESTORE FROM FLOPPY DISK');
menu.add('EXIT');
menu.on('select', function (label: string) {
menu.close();
console.log('SELECTED: ' + label);
});
stdin.pipe(menu.createStream()).pipe(process.stdout);
stdin.setRawMode(true);
menu.on('close', function () {
stdin.setRawMode(false);
stdin.end();
});

159
terminal-menu/terminal-menu.d.ts vendored Normal file
View File

@@ -0,0 +1,159 @@
// Type definitions for terminal-menu v2.1.1
// Project: https://github.com/substack/terminal-menu
// Definitions by: Arun Aravind <https://github.com/aravindarun>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference path="../node/node.d.ts" />
declare module "terminal-menu" {
import * as stream from "stream";
/**
* Creates a TerminalMenu with default settings.
*/
function MenuContainerFactory(): MenuContainerFactory.TerminalMenu;
/**
* Creates a TerminalMenu using options to override default settings.
* @param options Override values for available settings.
*/
function MenuContainerFactory(options: MenuContainerFactory.TerminalMenuOptions): MenuContainerFactory.TerminalMenu;
namespace MenuContainerFactory {
/**
* A Thickness structure specifying the amount of padding to apply.
*/
export interface Thickness {
/**
* Represents width of the left side of the bounding rectangle.
*/
left: number,
/**
* Represents width of the right side of the bounding rectangle.
*/
right: number,
/**
* Represents width of the upper side of the bounding rectangle.
*/
top: number,
/**
* Represents width of the lower side of the bounding rectangle.
*/
bottom: number
}
/**
* Options to configure the menu.
*/
export interface TerminalMenuOptions {
/**
* Menu width in columns.
* Default = 50.
*/
width?: number;
/**
* Horizontal offset for top-left corner.
* Default = 1
*/
x?: number;
/**
* Vertical offset for top-left corner.
* Default = 1
*/
y?: number;
/**
* Foreground color for the menu.
* Default = 'white'
*/
fg?: string;
/**
* Background color for the menu.
* Default = 'blue'
*/
bg?: string;
/**
* Padding for the bounding rectangle.
* If a number is passed, all elements of the Thickness structure will be set to
* that value.
* Default = {
* left: 2,
* right: 2,
* top: 1,
* bottom: 1
* }
*/
padding?: number | Thickness;
/**
* Index of the menu item to be selected.
* Default = 0
*/
selected?: number
}
/**
* Retro ansi terminal menus.
*/
export interface TerminalMenu extends NodeJS.EventEmitter {
/**
* Create a new selectable menu item with label as the anchor.
* @param label Label to use as the menu item anchor.
*/
add(label: string): void;
/**
* Create a new selectable menu item with label as the anchor.
* @param label Label to use as the menu item anchor.
* @param callback Callback to invoke when the menu item is selected.
*/
add(label: string, callback: (label: string, index: number) => void): void;
/**
* Writes a message to the terminal.
* @param msg Message to be written.
*/
write(msg: string): void;
/**
* Return a duplex stream to wire up input and output.
*/
createStream(): stream.Duplex;
/**
* Reset the terminal, clearing all content.
*/
reset(): void;
/**
* Unregister all listeners and puts the terminal back to its original state.
*/
close(): void;
/**
* When a menu item is selected, this event is fired.
* @param eventName Name of the event. Only value available for eventName is "select"
* @param callback Handler for the event specified by eventName
*/
on(eventName: string | symbol, callback: (label: string, index: number) => void): this;
/**
* When a menu item is selected, this event is fired.
* This overload ensures backward compatibility with older versions of NodeJS (< 6.0)
* @param eventName Name of the event. Only value available for eventName is "select"
* @param callback Handler for the event specified by eventName
*/
on(eventName: string, callback: (label: string, index: number) => void): this;
}
}
export = MenuContainerFactory;
}