github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/ui/app/utils/classes/exec-command-editor-xterm-adapter.js (about) 1 import KEYS from 'nomad-ui/utils/keys'; 2 3 const REVERSE_WRAPAROUND_MODE = '\x1b[?45h'; 4 const BACKSPACE_ONE_CHARACTER = '\x08 \x08'; 5 6 // eslint-disable-next-line no-control-regex 7 const UNPRINTABLE_CHARACTERS_REGEX = /[\x00-\x1F]/g; 8 9 export default class ExecCommandEditorXtermAdapter { 10 constructor(terminal, setCommandCallback, command) { 11 this.terminal = terminal; 12 this.setCommandCallback = setCommandCallback; 13 14 this.command = command; 15 16 this.dataListener = terminal.onData(data => { 17 this.handleDataEvent(data); 18 }); 19 20 // Allows tests to bypass synthetic keyboard event restrictions 21 terminal.simulateCommandDataEvent = this.handleDataEvent.bind(this); 22 23 terminal.write(REVERSE_WRAPAROUND_MODE); 24 } 25 26 handleDataEvent(data) { 27 if ( 28 data === KEYS.LEFT_ARROW || 29 data === KEYS.UP_ARROW || 30 data === KEYS.RIGHT_ARROW || 31 data === KEYS.DOWN_ARROW 32 ) { 33 // Ignore arrow keys 34 } else if (data === KEYS.CONTROL_U) { 35 this.terminal.write(BACKSPACE_ONE_CHARACTER.repeat(this.command.length)); 36 this.command = ''; 37 } else if (data === KEYS.ENTER) { 38 this.terminal.writeln(''); 39 this.setCommandCallback(this.command); 40 this.dataListener.dispose(); 41 } else if (data === KEYS.DELETE) { 42 if (this.command.length > 0) { 43 this.terminal.write(BACKSPACE_ONE_CHARACTER); 44 this.command = this.command.slice(0, -1); 45 } 46 } else if (data.length > 0) { 47 const strippedData = data.replace(UNPRINTABLE_CHARACTERS_REGEX, ''); 48 this.terminal.write(strippedData); 49 this.command = `${this.command}${strippedData}`; 50 } 51 } 52 53 destroy() { 54 this.dataListener.dispose(); 55 } 56 }