From 2fd9879eecfa799dec599cad53da14cee7d26b1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E6=9F=93?= <56108982+XR-stb@users.noreply.github.com> Date: Mon, 3 Apr 2023 11:22:01 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E5=89=8D?= =?UTF-8?q?=E7=AB=AF=E8=BE=93=E5=85=A5=E4=B8=AD=E6=96=87=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E5=86=8D=E5=88=A0=E9=99=A4=E4=BC=9A=E5=87=BA=E7=8E=B0=E7=9A=84?= =?UTF-8?q?bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 具体的bug复现,见:https://github.com/blueloveTH/pocketpy/issues/50 --- web/index.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/web/index.js b/web/index.js index 8755b9a6..1a723488 100644 --- a/web/index.js +++ b/web/index.js @@ -145,7 +145,12 @@ function term_init() { case '\u007F': // Backspace (DEL) // Do not delete the prompt if (term._core.buffer.x > 4) { // '>>> ' or '... ' - term.write('\b \b'); + var re=/[^\u4E00-\u9FA5]/; + if (re.test(command.charAt(command.length-1))){//判断前一个字符是否为中文 + term.write('\b \b');//false + }else{ + term.write('\b\b \b');//中文占两个字节,ascii字符占一个字节 + } if (command.length > 0) { command = command.substr(0, command.length - 1); } @@ -158,4 +163,4 @@ function term_init() { } } }); -} \ No newline at end of file +} From f884351c4eb11a9abe23da13687a7b86e359f3b7 Mon Sep 17 00:00:00 2001 From: BLUELOVETH Date: Tue, 4 Apr 2023 21:10:02 +0800 Subject: [PATCH 2/2] Update index.js --- web/index.js | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/web/index.js b/web/index.js index 1a723488..2621b788 100644 --- a/web/index.js +++ b/web/index.js @@ -143,18 +143,14 @@ function term_init() { term.write(need_more_lines ? "... " : ">>> "); break; case '\u007F': // Backspace (DEL) - // Do not delete the prompt - if (term._core.buffer.x > 4) { // '>>> ' or '... ' - var re=/[^\u4E00-\u9FA5]/; - if (re.test(command.charAt(command.length-1))){//判断前一个字符是否为中文 - term.write('\b \b');//false - }else{ - term.write('\b\b \b');//中文占两个字节,ascii字符占一个字节 - } - if (command.length > 0) { - command = command.substr(0, command.length - 1); - } - } + var cnt = term._core.buffer.x-4; + if(cnt<=0 || command.length==0) break; + // delete the last unicode char + command = command.replace(/.$/u, ""); + // clear the whole line + term.write('\b \b'.repeat(cnt)); + // re-write the command + term.write(command); break; default: // Print all other characters for demo if (e >= String.fromCharCode(0x20) && e <= String.fromCharCode(0x7E) || e >= '\u00a0') {