Terminal in React Plugins

February 25, 2018 ยท View on GitHub

Table of contents

Basic structure

Each plugin for the terminal should inherit from the plugin base class.

import PluginBase from 'terminal-in-react/lib/js/components/Plugin';

class MyPlugin extends PluginBase {
  ...
}

This gives you the required plugin structure, but there are attributes that can be and those that should be overwritten.

The required overrides are (static displayName, static version):

class MyPlugin extends PluginBase {
  static displayName = 'MyPlugin';
  static version = '1.0.0';
}

Static attributes

AttributeTypeDefaultDescription
displayNamestatic string''Used so other plugins can access data and methods as such should be unique
versionstatic string'1.0.0'Used so other plugins can check that your plugin is a certain version
defaultDatastatic any''Used for data storage that other plugins can get access to
commandsstatic commands{}Commands that are simple and don't need access to the full plugin api
descriptionsstatic command descriptions{}Descriptions for your static commands
shortcutsstatic shortcuts{}Simple shortcuts that call only existing commands or any static plugin command

Instance attributes and methods

AttributeTypeDefaultDescription
commandscommands{}Commands that need access to the full plugin api
descriptionscommand descriptions{}Descriptions for your commands
shortcutsshortcuts{}Shortcuts that call more complicated methods
getPublicMethodsfunction() => ({})A method to return the public methods your plugin exposes to other plugins

Plugin Setup

If you need to use the class constructor you need pass all inputs to super.

import PluginBase from 'terminal-in-react/lib/js/components/Plugin';

class MyPlugin extends PluginBase {
  static displayName = 'MyPlugin';
  static version = '1.0.0';

  constructor(api, config) {
    super(api, config);
  }
}

Plugin API

The plugin api will be available in all plugin instance methods as this.api.

KeyParamsDescription
printLinecontent:anyUsed to add a new line to the output, can be of any type.
removeLinelineNumber:integer:-1Used to remove a line from output. If -1 will remove last line.
runCommandcmdText:string, force:bool:falseUsed to run a command based on the text. force is used when a plugin has taken control.
setCanScrollcanScroll:boolUsed to turn on and off scroll
setScrollPositionposition:floatSet scroll top of the terminal
focusInputUsed to focus the input
setPromptPrefixpromptPrefix:stringUsed to set the prompt prefix of the current tab
setPromptSymbolpromptSymbol:stringUsed to set the prompt symbol ie '>' or '$'
getPluginMethodpluginName:string, methodName:stringUsed to get a public method from another plugin
takeControlcontroller:object, newPromptSymbol:string, newPromptPrefix:stringUsed to take full control over the terminal
releaseControlUsed to release full control
getDataUsed to get the plugin's public data object
setDatadata:anyUsed to set the plugin's public data object
checkVersioncomparator:string, version:stringUsed to check if the Terminal version meets certain criteria. ['=', '!=', '>', '<', '<=', '>='] ie ('>=', '3.2.0')
versionNOT A FUNCTIONThe Terminal's version
osNOT A FUNCTIONThe os of the current user

Taking Control

One of the things a Plugin can do is take "full" control of the Terminal. If done none of the defualt commands or other plugin's commands will work.

To take "control" use:

this.api.takeControl(controller);

The controller object

  • shortcuts : Shortcuts that only work in this mode [Optional]
  • history : If the inputs by are user should be saved to the default input history. Defaults to false [Optional]
  • onKeyPress : A function to handle the key press event. Params are the key object that was pressed [Optional]
  • runCommand : A function to take the input text and run commands with. [Optional]
  • commands : A object of commands that can be run in this mode. Can't be used along with runCommand. runCommand will take precedence. [Optional]

Using runCommand controller

This allows you to "fully" control all commands that are run even if other plugins call this.api.runCommand. This is mostly true there exists a option on the api runCommand method to bypass the controller. That is the full params for this.api.runCommand are (inputText, force). If force is set to true then the controller will not be called. It is suggested that only a controller call this.api.runCommand with force set to true for instances where the built in commands want to be called ie: this.api.runCommand('clear', true) to clear the screen.

Releasing Control

To do so just call this.api.releaseControl()