(repl, contextSymbol, bufferSymbol, active)
| 145 | } |
| 146 | |
| 147 | function setupPreview(repl, contextSymbol, bufferSymbol, active) { |
| 148 | // Simple terminals can't handle previews. |
| 149 | if (process.env.TERM === 'dumb' || !active) { |
| 150 | return { showPreview() {}, clearPreview() {} }; |
| 151 | } |
| 152 | |
| 153 | let inputPreview = null; |
| 154 | |
| 155 | let previewCompletionCounter = 0; |
| 156 | let completionPreview = null; |
| 157 | |
| 158 | let hasCompletions = false; |
| 159 | |
| 160 | let wrapped = false; |
| 161 | |
| 162 | let escaped = null; |
| 163 | |
| 164 | function getPreviewPos() { |
| 165 | const displayPos = repl._getDisplayPos(`${repl.getPrompt()}${repl.line}`); |
| 166 | const cursorPos = repl.line.length !== repl.cursor ? |
| 167 | repl.getCursorPos() : |
| 168 | displayPos; |
| 169 | return { displayPos, cursorPos }; |
| 170 | } |
| 171 | |
| 172 | function isCursorAtInputEnd() { |
| 173 | const { cursorPos, displayPos } = getPreviewPos(); |
| 174 | return cursorPos.rows === displayPos.rows && |
| 175 | cursorPos.cols === displayPos.cols; |
| 176 | } |
| 177 | |
| 178 | const clearPreview = (key) => { |
| 179 | if (inputPreview !== null) { |
| 180 | const { displayPos, cursorPos } = getPreviewPos(); |
| 181 | const rows = displayPos.rows - cursorPos.rows + 1; |
| 182 | moveCursor(repl.output, 0, rows); |
| 183 | clearLine(repl.output); |
| 184 | moveCursor(repl.output, 0, -rows); |
| 185 | inputPreview = null; |
| 186 | } |
| 187 | if (completionPreview !== null) { |
| 188 | // Prevent cursor moves if not necessary! |
| 189 | const move = repl.line.length !== repl.cursor; |
| 190 | let pos, rows; |
| 191 | if (move) { |
| 192 | pos = getPreviewPos(); |
| 193 | cursorTo(repl.output, pos.displayPos.cols); |
| 194 | rows = pos.displayPos.rows - pos.cursorPos.rows; |
| 195 | moveCursor(repl.output, 0, rows); |
| 196 | } |
| 197 | const totalLine = `${repl.getPrompt()}${repl.line}${completionPreview}`; |
| 198 | const newPos = repl._getDisplayPos(totalLine); |
| 199 | // Minimize work for the terminal. It is enough to clear the right part of |
| 200 | // the current line in case the preview is visible on a single line. |
| 201 | if (newPos.rows === 0 || (pos && pos.displayPos.rows === newPos.rows)) { |
| 202 | clearLine(repl.output, 1); |
| 203 | } else { |
| 204 | clearScreenDown(repl.output); |
no test coverage detected
searching dependent graphs…