import React from "react"; import { observer } from "mobx-react"; import { action, makeObservable, observable } from "mobx"; import { Splitter } from "eez-studio-ui/splitter"; import { IconAction } from "eez-studio-ui/action"; import * as notification from "eez-studio-ui/notification"; import type { InstrumentAppStore } from "instrument/window/app-store"; import { executeShortcut } from "instrument/window/script"; import { HistoryView, HistoryTools } from "instrument/window/history/history-view"; import { ShortcutsToolbar } from "instrument/window/terminal/toolbar"; import { CommandsBrowser } from "instrument/window/terminal/commands-browser"; import { parseScpi, SCPI_PART_QUERY } from "eez-studio-shared/scpi-parser"; import { AppBar } from "instrument/window/app"; import { TerminalState } from "instrument/window/terminal/terminalState"; //////////////////////////////////////////////////////////////////////////////// const CONF_COMMANDS_HISTORY_SIZE = 1000; //////////////////////////////////////////////////////////////////////////////// const Input = observer( class Input extends React.Component<{ appStore: InstrumentAppStore; sendCommand: () => void; sendFileToInstrumentHandler: (() => void) | undefined; showDocumentation: boolean; terminalState: TerminalState; }> { input: HTMLInputElement; constructor(props: any) { super(props); const commandsHistoryJSON = window.localStorage.getItem( `instrument/${this.props.appStore.history.oid}/window/terminal/commands-history` ); if (commandsHistoryJSON) { try { this.commandsHistory = JSON.parse(commandsHistoryJSON); } catch (error) { console.error(error); } } } commandsHistory: string[] = []; historyItemIndex: number | undefined; moveCursorToTheEnd: boolean; componentDidMount() { this.input.focus(); } componentDidUpdate() { if (this.moveCursorToTheEnd) { this.input.selectionStart = this.input.selectionEnd = this.props.terminalState.command.length; this.moveCursorToTheEnd = false; } } handleHelpClick = () => { this.props.appStore.toggleHelpVisible(); }; handleChange = (event: any) => { this.props.terminalState.command = event.target.value; }; sendCommand() { if (this.props.terminalState.command) { this.commandsHistory.push(this.props.terminalState.command); if (this.commandsHistory.length > CONF_COMMANDS_HISTORY_SIZE) { this.commandsHistory.splice(0, 1); } window.localStorage.setItem( `instrument/${this.props.appStore.history.oid}/window/terminal/commands-history`, JSON.stringify(this.commandsHistory) ); this.historyItemIndex = undefined; this.props.sendCommand(); } } findPreviousCommand() { while (true) { let historyItemIndex = this.historyItemIndex; if (historyItemIndex === undefined) { historyItemIndex = this.commandsHistory.length - 1; } else { if (historyItemIndex >= 0) { historyItemIndex--; } } this.historyItemIndex = historyItemIndex; if ( this.historyItemIndex < 0 || this.historyItemIndex >= this.commandsHistory.length ) { return undefined; } return this.commandsHistory[this.historyItemIndex]; } } findNextCommand() { while (true) { let historyItemIndex = this.historyItemIndex; if (historyItemIndex === undefined) { return undefined; } else { if (historyItemIndex < this.commandsHistory.length) { historyItemIndex++; } } this.historyItemIndex = historyItemIndex; if ( this.historyItemIndex < 0 || this.historyItemIndex >= this.commandsHistory.length ) { return undefined; } return this.commandsHistory[this.historyItemIndex]; } } handleKeyDown = (event: any) => { if (event.key === "Enter") { event.preventDefault(); this.sendCommand(); } else if (event.key === "ArrowUp") { event.preventDefault(); this.props.terminalState.command = this.findPreviousCommand() || ""; this.moveCursorToTheEnd = true; } else if (event.key === "ArrowDown") { event.preventDefault(); this.props.terminalState.command = this.findNextCommand() || ""; this.moveCursorToTheEnd = true; } }; handleSendCommandClick = () => { this.sendCommand(); }; render() { return (