|
| 1 | +# Terminal in React Plugins |
| 2 | + |
| 3 | +## Table of contents |
| 4 | + |
| 5 | +* [Basic Structure](#basic-structure) |
| 6 | + |
| 7 | +* [Plugin Setup](#plugin-setup) |
| 8 | + |
| 9 | +* [Plugin API](#plugin-api) |
| 10 | + |
| 11 | +* [Taking Control](#taking-control) |
| 12 | + |
| 13 | +* [Releasing Control](#releasing-control) |
| 14 | + |
| 15 | +## Basic structure |
| 16 | + |
| 17 | +Each plugin for the terminal should inherit from the plugin base class. |
| 18 | + |
| 19 | +```javascript |
| 20 | +import PluginBase from 'terminal-in-react/lib/js/components/Plugin'; |
| 21 | + |
| 22 | +class MyPlugin extends PluginBase { |
| 23 | + ... |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +This gives you the required plugin structure, but there are attributes that can be |
| 28 | +and those that should be overwritten. |
| 29 | + |
| 30 | +The required overrides are (static displayName, static version): |
| 31 | + |
| 32 | +```javascript |
| 33 | +class MyPlugin extends PluginBase { |
| 34 | + static displayName = 'MyPlugin'; |
| 35 | + static version = '1.0.0'; |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +### Static attributes |
| 40 | + |
| 41 | +| Attribute | Type | Default | Description | |
| 42 | +|:---------------- |:---------------------------:|:-------:|:-----------:| |
| 43 | +| **displayName** | static string | '' | Used so other plugins can access data and methods as such should be unique | |
| 44 | +| **version** | static string | '1.0.0' | Used so other plugins can check that your plugin is a certain version | |
| 45 | +| **defaultData** | static any | '' | Used for data storage that other plugins can get access to | |
| 46 | +| **commands** | static commands | {} | Commands that are simple and don't need access to the full plugin api | |
| 47 | +| **descriptions** | static command descriptions | {} | Descriptions for your static commands | |
| 48 | +| **shortcuts** | static shortcuts | {} | Simple shortcuts that call only existing commands or any static plugin command | |
| 49 | + |
| 50 | +### Instance attributes and methods |
| 51 | + |
| 52 | +| Attribute | Type | Default | Description | |
| 53 | +|:--------------------:|:--------------------:|:----------:|:-----------:| |
| 54 | +| **commands** | commands | {} | Commands that need access to the full plugin api | |
| 55 | +| **descriptions** | command descriptions | {} | Descriptions for your commands | |
| 56 | +| **shortcuts** | shortcuts | {} | Shortcuts that call more complicated methods | |
| 57 | +| **getPublicMethods** | <object>function | () => ({}) | A method to return the public methods your plugin exposes to other plugins | |
| 58 | + |
| 59 | +## Plugin Setup |
| 60 | +If you need to use the class constructor you need pass all inputs to `super`. |
| 61 | + |
| 62 | +```javascript |
| 63 | +import PluginBase from 'terminal-in-react/lib/js/components/Plugin'; |
| 64 | + |
| 65 | +class MyPlugin extends PluginBase { |
| 66 | + static displayName = 'MyPlugin'; |
| 67 | + static version = '1.0.0'; |
| 68 | + |
| 69 | + constructor(api, config) { |
| 70 | + super(api, config); |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +## Plugin API |
| 76 | + |
| 77 | +The plugin api will be available in all plugin instance methods as `this.api`. |
| 78 | + |
| 79 | +| Key | Params | Description | |
| 80 | +|:---------------------:|:---------------------:|:-----------:| |
| 81 | +| **printLine** | content:any | Used to add a new line to the output, can be of any type. | |
| 82 | +| **removeLine** | lineNumber:integer:-1 | Used to remove a line from output. If -1 will remove last line. | |
| 83 | +| **runCommand** | cmdText:string, force:bool:false | Used to run a command based on the text. `force` is used when a plugin has taken control. | |
| 84 | +| **setCanScroll** | canScroll:bool | Used to turn on and off scroll | |
| 85 | +| **setScrollPosition** | position:float | Set scroll top of the terminal | |
| 86 | +| **focusInput** | | Used to focus the input | |
| 87 | +| **setPromptPrefix** | promptPrefix:string | Used to set the prompt prefix of the current tab | |
| 88 | +| **setPromptSymbol** | promptSymbol:string | Used to set the prompt symbol ie '>' or '$' | |
| 89 | +| **getPluginMethod** | pluginName:string, methodName:string | Used to get a public method from another plugin | |
| 90 | +| **takeControl** | controller:object, newPromptSymbol:string, newPromptPrefix:string | Used to take full control over the terminal | |
| 91 | +| **releaseControl** | | Used to release full control | |
| 92 | +| **getData** | | Used to get the plugin's public data object | |
| 93 | +| **setData** | data:any | Used to set the plugin's public data object | |
| 94 | +| **checkVersion** | comparator:string, version:string | Used to check if the Terminal version meets certain criteria. ['=', '!=', '>', '<', '<=', '>='] ie ('>=', '3.2.0') | |
| 95 | +| **version** | NOT A FUNCTION | The Terminal's version | |
| 96 | +| **os** | NOT A FUNCTION | The os of the current user | |
| 97 | + |
| 98 | +## Taking Control |
| 99 | + |
| 100 | +One of the things a Plugin can do is take "full" control of the Terminal. If done |
| 101 | +none of the defualt commands or other plugin's commands will work. |
| 102 | + |
| 103 | +To take "control" use: |
| 104 | + |
| 105 | +```javascript |
| 106 | +this.api.takeControl(controller); |
| 107 | +``` |
| 108 | + |
| 109 | +### The controller object |
| 110 | + |
| 111 | + - **shortcuts** : Shortcuts that only work in this mode [Optional] |
| 112 | + - **history** : If the inputs by are user should be saved to the default input history. Defaults to `false` [Optional] |
| 113 | + - **onKeyPress** : A function to handle the key press event. Params are the key object that was pressed [Optional] |
| 114 | + - **runCommand** : A function to take the input text and run commands with. [Optional] |
| 115 | + - **commands** : A object of commands that can be run in this mode. Can't be used along with `runCommand`. `runCommand` will take precedence. [Optional] |
| 116 | + |
| 117 | +### Using `runCommand` controller |
| 118 | + |
| 119 | +This allows you to "fully" control all commands that are run even if other |
| 120 | +plugins call `this.api.runCommand`. This is mostly true there exists a option on |
| 121 | +the api `runCommand` method to bypass the controller. That is the full params for |
| 122 | +`this.api.runCommand` are (`inputText`, `force`). If `force` is set to `true` then |
| 123 | +the controller will not be called. It is suggested that only a controller call |
| 124 | +`this.api.runCommand` with `force` set to `true` for instances where the built in |
| 125 | +commands want to be called ie: `this.api.runCommand('clear', true)` to clear |
| 126 | +the screen. |
| 127 | + |
| 128 | +## Releasing Control |
| 129 | + |
| 130 | +To do so just call `this.api.releaseControl()` |
0 commit comments