fix: incorrect cursor position in cli #120

This commit is contained in:
Lykin 2024-01-13 19:31:08 +08:00
parent 718e89a641
commit 70235cc295
2 changed files with 31 additions and 20 deletions

View File

@ -200,18 +200,30 @@ const moveInputCursor = (step) => {
inputCursor += step inputCursor += step
updateCursor = true updateCursor = true
} }
} else {
// update cursor position only
const currentLine = getCurrentInput()
inputCursor = Math.max(0, currentLine.length)
updateCursor = true
} }
if (updateCursor) { if (updateCursor) {
termInst.write(`\x1B[${prefixLen.value + inputCursor + 1}G`) moveInputCursorTo(inputCursor)
} }
} }
/**
* move cursor to the end of current line
*/
const moveInputCursorToEnd = () => {
moveInputCursorTo(Number.MAX_VALUE)
}
/**
* move cursor to pos
* @param {number} pos
*/
const moveInputCursorTo = (pos) => {
const currentLine = getCurrentInput()
inputCursor = Math.min(Math.max(0, pos), currentLine.length)
termInst.write(`\x1B[${prefixLen.value + inputCursor + 1}G`)
}
/** /**
* update current input cache and refresh term * update current input cache and refresh term
* @param {string} data * @param {string} data
@ -229,8 +241,7 @@ const updateInput = (data) => {
if (inputCursor < currentLine.length) { if (inputCursor < currentLine.length) {
// insert // insert
currentLine = currentLine.substring(0, inputCursor) + data + currentLine.substring(inputCursor) currentLine = currentLine.substring(0, inputCursor) + data + currentLine.substring(inputCursor)
replaceTermInput() replaceTermInput(currentLine)
termInst.write(currentLine)
moveInputCursor(data.length) moveInputCursor(data.length)
} else { } else {
// append // append
@ -260,15 +271,16 @@ const deleteInput = (back = false) => {
currentLine = currentLine.substring(0, inputCursor) + currentLine.substring(inputCursor + 1) currentLine = currentLine.substring(0, inputCursor) + currentLine.substring(inputCursor + 1)
} }
} else { } else {
if (back) {
// delete last one // delete last one
currentLine = currentLine.slice(0, -1) currentLine = currentLine.slice(0, -1)
inputCursor -= 1 inputCursor -= 1
} }
}
replaceTermInput() replaceTermInput(currentLine)
termInst.write(currentLine)
updateCurrentInput(currentLine) updateCurrentInput(currentLine)
moveInputCursor(0) moveInputCursorTo(inputCursor)
} }
const getCurrentInput = () => { const getCurrentInput = () => {
@ -315,9 +327,8 @@ const changeHistory = (prev) => {
return return
} }
replaceTermInput() replaceTermInput(currentLine)
termInst.write(currentLine) moveInputCursorToEnd()
moveInputCursor(0)
} }
return null return null
@ -338,7 +349,7 @@ const flushTermInput = (flushCmd = false) => {
/** /**
* clear current input line and replace with new content * clear current input line and replace with new content
* @param {string} [content] * @param {string|null} [content]
*/ */
const replaceTermInput = (content = '') => { const replaceTermInput = (content = '') => {
if (termInst == null) { if (termInst == null) {

View File

@ -14,16 +14,16 @@ const props = defineProps({
<template> <template>
<svg fill="none" viewBox="0 0 48 48" xmlns="http://www.w3.org/2000/svg"> <svg fill="none" viewBox="0 0 48 48" xmlns="http://www.w3.org/2000/svg">
<path <path
:stroke="color"
:stroke-width="props.strokeWidth" :stroke-width="props.strokeWidth"
d="M36.7279 36.7279C33.4706 39.9853 28.9706 42 24 42C14.0589 42 6 33.9411 6 24C6 14.0589 14.0589 6 24 6C28.9706 6 33.4706 8.01472 36.7279 11.2721C38.3859 12.9301 42 17 42 17" d="M36.7279 36.7279C33.4706 39.9853 28.9706 42 24 42C14.0589 42 6 33.9411 6 24C6 14.0589 14.0589 6 24 6C28.9706 6 33.4706 8.01472 36.7279 11.2721C38.3859 12.9301 42 17 42 17"
:stroke="color"
stroke-linecap="round" stroke-linecap="round"
stroke-linejoin="round" /> stroke-linejoin="round" />
<path <path
:stroke="color"
:stroke-width="props.strokeWidth" :stroke-width="props.strokeWidth"
class="default-stroke" class="default-stroke"
d="M42 8V17H33" d="M42 8V17H33"
:stroke="color"
stroke-linecap="round" stroke-linecap="round"
stroke-linejoin="round" /> stroke-linejoin="round" />
</svg> </svg>