Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | import _ from 'lodash'; import React from 'react'; import {getSiShellCommands, Shell} from 'sportident-testbench-shell/lib'; import {ISiDevice, ISiDeviceDriverData} from 'sportident/lib/SiDevice/ISiDevice'; import {SiExternalApplication} from './SiExternalApplication'; const keyCodeFromDomEventKey = (domKey: string) => { switch (domKey) { case 'Backspace': return 8; case 'Enter': return 13; case 'Escape': return 27; case 'Shift': return undefined; case 'Tab': return 9; default: { Iif (domKey.length === 1) { return domKey.charCodeAt(0); } console.warn(`Undefined Special Key: ${domKey}`); return undefined; } } }; const TerminalCursor = () => { const [shown, setIsShown] = React.useState(true); React.useEffect(() => { const interval = setInterval(() => { setIsShown((oldIsShown) => !oldIsShown); }, 500); return () => { clearInterval(interval); }; }, []); return <span>{shown ? '\u2588' : '\u00A0'}</span>; // \u2588=box, \u00A0=space }; export const Terminal = ( props: { selectedDevice: ISiDevice<ISiDeviceDriverData<unknown>>|undefined, }, ): React.ReactElement => { const [shell, setShell] = React.useState<Shell|null>(null); const [shellContent, setShellContent] = React.useState<string>(''); const [inputQueue, setInputQueue] = React.useState<number[]>([]); const [outputQueue, setOutputQueue] = React.useState<number[]>([]); const terminalElem = React.useRef<HTMLDivElement>(null); React.useEffect(() => { Iif (terminalElem && terminalElem.current) { terminalElem.current.scrollTop = terminalElem.current.scrollHeight; } }, [shellContent]); React.useEffect(() => { Iif (terminalElem && terminalElem.current) { terminalElem.current.focus(); } }, []); const onKeyDown: React.KeyboardEventHandler = React.useCallback((event) => { Iif (!event.key || event.ctrlKey) { return; } event.preventDefault(); const keyCode = keyCodeFromDomEventKey(event.key); Iif (keyCode === undefined) { return; } setInputQueue([...inputQueue, keyCode]); }, [inputQueue]); const onPaste: React.ClipboardEventHandler = React.useCallback((event) => { const pastedText = event.clipboardData.getData('text'); const charCodes = [...pastedText].map((char) => char.charCodeAt(0)); setInputQueue([...inputQueue, ...charCodes]); }, [inputQueue]); const appendToShellContent = (charCodes: number[]) => { let newContent = shellContent; for (const charCode of charCodes) { switch (charCode) { case 8: { const contentLength = newContent.length; const toDelete = newContent.charCodeAt(contentLength - 1); Iif (toDelete === 10 || toDelete === 13) { return; } newContent = newContent.substring(0, contentLength - 1); break; } default: { const newChar = String.fromCharCode(charCode); newContent = `${newContent}${newChar}`; break; } } } setShellContent(newContent); }; Iif (outputQueue.length > 0) { setTimeout(() => { appendToShellContent(outputQueue); setOutputQueue([]); }, 1); } const pushToOutputQueue = (charCode: number) => { setOutputQueue((outputQueue_) => [...outputQueue_, charCode]); }; React.useEffect(() => { const siShell = new Shell( { getChar: () => undefined, putChar: (char) => pushToOutputQueue(char), }, getSiShellCommands(), { initialEnv: { device: props.selectedDevice, externalApplication: SiExternalApplication, }, }, ); console.warn('RUN SHELL'); siShell.run(); setShell(siShell); return () => { // TODO: Somehow close the shell console.warn('CLOSE SHELL'); }; }, []); const popFromInputQueue = React.useCallback(() => { Iif (inputQueue.length === 0) { return undefined; } const [charCode, ...newInputQueue] = inputQueue; setInputQueue(newInputQueue); Iif (shell) { shell.ui = { getChar: () => undefined, putChar: (char) => pushToOutputQueue(char), }; } return charCode; }, [inputQueue]); Iif (shell) { shell.ui = { getChar: () => popFromInputQueue(), putChar: (char) => pushToOutputQueue(char), }; } const shellLines = shellContent.split(/[\n\r]+/); return ( <div id='si-terminal' className='terminal' onKeyDown={onKeyDown} onPaste={onPaste} ref={terminalElem} tabIndex={-1} > {shellLines.map((line, index) => { const cursor = index === shellLines.length - 1 && (<TerminalCursor />); return <div key={`line-${index}`}>{line}{cursor}</div>; })} </div> ); }; |