From 4fc982301fcf2f7abad1fc7fa99e9a9b20e3e7e0 Mon Sep 17 00:00:00 2001 From: Vuk Petrovic Date: Mon, 15 Aug 2022 19:52:41 -0400 Subject: [PATCH 1/3] Removed unused variable --- index.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/index.js b/index.js index 076d9f8..63c84bf 100644 --- a/index.js +++ b/index.js @@ -79,7 +79,6 @@ function create(config) { } var cycle = 0; - var prevComplete; while (true) { read = fs.readSync(fd, buf, 0, 3); @@ -176,9 +175,7 @@ function create(config) { if (str == res[0]) { res = autocomplete(''); - } else { - prevComplete = res.length; - } + } if (res.length == 0) { process.stdout.write('\t'); From 4f047f6b3e9e358028c1993ca57a3104e1f99859 Mon Sep 17 00:00:00 2001 From: Vuk Petrovic Date: Mon, 15 Aug 2022 21:25:50 -0400 Subject: [PATCH 2/3] Implemented fix. --- index.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 63c84bf..b3cfd41 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ var fs = require('fs'); var stripAnsi = require('strip-ansi'); var term = 13; // carriage return + /** * create -- sync function for reading user input from stdin * @param {Object} config { @@ -71,6 +72,7 @@ function create(config) { var buf = Buffer.alloc(3); var str = '', character, read; + var autoCompleteSearchTerm; savedstr = ''; @@ -171,11 +173,11 @@ function create(config) { // catch a TAB and implement autocomplete if (character == 9) { // TAB - res = autocomplete(str); + // first TAB hit, save off original input + if(autoCompleteSearchTerm === undefined) + autoCompleteSearchTerm = str; - if (str == res[0]) { - res = autocomplete(''); - } + res = autocomplete(autoCompleteSearchTerm); if (res.length == 0) { process.stdout.write('\t'); @@ -187,8 +189,12 @@ function create(config) { if (item) { process.stdout.write('\r\u001b[K' + ask + item); str = item; + insert = item.length; } + } else { + // user entered anything other than TAB; reset from last use of autocomplete + autoCompleteSearchTerm = undefined; } if (character == 127 || (process.platform == 'win32' && character == 8)) { //backspace From 060def66bdefefd79af8aa1e331a3f39a18f827e Mon Sep 17 00:00:00 2001 From: Vuk Petrovic Date: Mon, 15 Aug 2022 21:31:17 -0400 Subject: [PATCH 3/3] Fixed another issue whereby subsequent autocomplete attempts would not start at the beginning of the newly found list --- index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.js b/index.js index b3cfd41..be51cfd 100644 --- a/index.js +++ b/index.js @@ -195,6 +195,8 @@ function create(config) { } else { // user entered anything other than TAB; reset from last use of autocomplete autoCompleteSearchTerm = undefined; + // reset cycle - next time user hits tab might yield a different result list + cycle = 0; } if (character == 127 || (process.platform == 'win32' && character == 8)) { //backspace