From f49facdbf5e9818d93fcbe438ad2fb2fd76cf11f Mon Sep 17 00:00:00 2001 From: Brice Leroy Date: Mon, 17 Jun 2019 18:28:07 -0700 Subject: [PATCH] J'Adore le Switch --- index.js | 66 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/index.js b/index.js index 8cbed76..e686ef7 100755 --- a/index.js +++ b/index.js @@ -52,33 +52,47 @@ const prompt = filename => draw(active, filename); process.stdin.on('keypress', function(ch, key) { - if (key && key.name === 'down') { - if (active >= choices.length - 1) { - active = 0; - } else { - active += 1; - } - draw(active, filename); + if (!key) { + return; } - if (key && key.name === 'up') { - if (active <= 0) { - active = choices.length - 1; - } else { - active -= 1; - } - draw(active, filename); - } - if (key && key.name === 'return') { - process.stdin.setRawMode(false); - process.stdout.write('\033c'); - if (active === 0) { - yay(); - } else { - process.exit(); - } - } - if (key && key.ctrl && key.name == 'c') { - process.stdin.setRawMode(false); + + // / to select an option + // to confirm a selection + // + for le exit + switch (key) { + case "down": + if (active >= choices.length - 1) { + active = 0; + } else { + active += 1; + } + draw(active, filename); + break; + + case "up": + if (active <= 0) { + active = choices.length - 1; + } else { + active -= 1; + } + draw(active, filename); + break; + + case "return": + process.stdin.setRawMode(false); + process.stdout.write("\033c"); + if (active === 0) { + yay(); + } else { + process.exit(); + } + break; + + // le CTRL + C + case "c": + if (key.ctrl) { + process.stdin.setRawMode(false); + } } });