From 69b63d42271347218710b9dc09ea96fe55bee638 Mon Sep 17 00:00:00 2001 From: Ibrahim <98372964+abe2dev@users.noreply.github.com> Date: Thu, 10 Feb 2022 17:18:20 -0800 Subject: [PATCH 1/9] initial_commit_hw_3_todoCLI --- to_do_cli/todoCLI.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 to_do_cli/todoCLI.js diff --git a/to_do_cli/todoCLI.js b/to_do_cli/todoCLI.js new file mode 100644 index 0000000..e69de29 From 50f88006611013411cb3b1fa4db1bc80db0e1d83 Mon Sep 17 00:00:00 2001 From: Ibrahim <98372964+abe2dev@users.noreply.github.com> Date: Sat, 12 Feb 2022 18:21:34 -0800 Subject: [PATCH 2/9] Show Welcome message and prompt with menu --- to_do_cli/todoCLI.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/to_do_cli/todoCLI.js b/to_do_cli/todoCLI.js index e69de29..f9b8107 100644 --- a/to_do_cli/todoCLI.js +++ b/to_do_cli/todoCLI.js @@ -0,0 +1,26 @@ +const readline = require("readline") + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, + prompt: "> " +}); + + +const theMenu = ( "(v) View.(n) New.(cX) Complete.(dX) Delete,(q) Quit") + + +console.log("Welcome to Todo CLI!\n--------------------"); +console.log(theMenu); + +rl.prompt(); +rl.on("line", (action) => { + + if (action === "q") { + rl.close(); + + } else { + console.log(theMenu); + rl.prompt(); + } +}); From 7f204412f73cec777be32eff74b7f3a462902ae8 Mon Sep 17 00:00:00 2001 From: Ibrahim <98372964+abe2dev@users.noreply.github.com> Date: Sat, 12 Feb 2022 19:21:14 -0800 Subject: [PATCH 3/9] create view action, refactor menu --- to_do_cli/todoCLI.js | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/to_do_cli/todoCLI.js b/to_do_cli/todoCLI.js index f9b8107..279ece7 100644 --- a/to_do_cli/todoCLI.js +++ b/to_do_cli/todoCLI.js @@ -1,26 +1,43 @@ const readline = require("readline") - const rl = readline.createInterface({ input: process.stdin, output: process.stdout, - prompt: "> " + prompt: "> " // the shape of the prompt > also we can put % }); +function menuPrompt(){ + const theMenu = ( "(v) View.(n) New.(cX) Complete.(dX) Delete,(q) Quit") + console.log(theMenu); + rl.prompt() +} -const theMenu = ( "(v) View.(n) New.(cX) Complete.(dX) Delete,(q) Quit") +function showList(){ + theList.forEach((entry, index) => { + let status = entry[0] === true ? "✔": " "; + console.log(`${index} [${status}] ${entry[1]}`) + }); +} console.log("Welcome to Todo CLI!\n--------------------"); -console.log(theMenu); - -rl.prompt(); +menuPrompt(); rl.on("line", (action) => { if (action === "q") { rl.close(); - - } else { - console.log(theMenu); - rl.prompt(); - } + return; + } else if (action === "v") { + showList() + } + menuPrompt() }); + +const theList = [ + [true, "Take out the trash"], + [true, "Buy toothpaste"], + [false, "Buy Snickerdoodles"], + [false, "Fix the climate"], + [false, "Find a cure for aging"] +]; + + From 5fe663ad11b2be42667f04b063016b6a603ce4d4 Mon Sep 17 00:00:00 2001 From: Ibrahim <98372964+abe2dev@users.noreply.github.com> Date: Sat, 12 Feb 2022 21:01:03 -0800 Subject: [PATCH 4/9] Create new action --- to_do_cli/todoCLI.js | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/to_do_cli/todoCLI.js b/to_do_cli/todoCLI.js index 279ece7..20e4d89 100644 --- a/to_do_cli/todoCLI.js +++ b/to_do_cli/todoCLI.js @@ -1,16 +1,21 @@ +const { title } = require("process"); const readline = require("readline") + const rl = readline.createInterface({ input: process.stdin, output: process.stdout, prompt: "> " // the shape of the prompt > also we can put % }); + + function menuPrompt(){ const theMenu = ( "(v) View.(n) New.(cX) Complete.(dX) Delete,(q) Quit") console.log(theMenu); rl.prompt() } + function showList(){ theList.forEach((entry, index) => { let status = entry[0] === true ? "✔": " "; @@ -19,17 +24,41 @@ function showList(){ } +function newEntry(entry = "") { + if (entry === "") { + console.log("What?"); + rl.prompt() + } else { + theList.push([false, entry]) + } + +} + + console.log("Welcome to Todo CLI!\n--------------------"); menuPrompt(); -rl.on("line", (action) => { - if (action === "q") { +let curCmd = ""; +rl.on("line", (input) => { + + if (input === "q") { rl.close(); return; - } else if (action === "v") { + + } else if (input === "v") { showList() - } - menuPrompt() + menuPrompt(); + + } else if (input === "n"){ + curCmd = "n"; + newEntry(); + + } else if ( curCmd = "n"){ + newEntry(input) + curCmd = ""; + menuPrompt(); + } + }); const theList = [ From fe0150211b5060cda83d268299188c0016dadf7a Mon Sep 17 00:00:00 2001 From: Ibrahim <98372964+abe2dev@users.noreply.github.com> Date: Sat, 12 Feb 2022 21:49:44 -0800 Subject: [PATCH 5/9] Create complete action --- to_do_cli/todoCLI.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/to_do_cli/todoCLI.js b/to_do_cli/todoCLI.js index 20e4d89..dc8a46e 100644 --- a/to_do_cli/todoCLI.js +++ b/to_do_cli/todoCLI.js @@ -34,6 +34,14 @@ function newEntry(entry = "") { } +function completeItem(num) { + if (num >= 0 && num < theList.length) { + theList[num][0] = true; + console.log(`Completed "${theList[num][1]}"`); + } + +} + console.log("Welcome to Todo CLI!\n--------------------"); menuPrompt(); @@ -53,10 +61,15 @@ rl.on("line", (input) => { curCmd = "n"; newEntry(); - } else if ( curCmd = "n"){ + } else if ( curCmd === "n"){ newEntry(input) curCmd = ""; menuPrompt(); + + } else if (input[0] === "c") { + let num = parseInt(input.slice(1)) + completeItem(num); + menuPrompt(); } }); From d6165e829dd66502e6ddcc42c2133d90f0d6d921 Mon Sep 17 00:00:00 2001 From: Ibrahim <98372964+abe2dev@users.noreply.github.com> Date: Sat, 12 Feb 2022 22:14:01 -0800 Subject: [PATCH 6/9] Create delete action --- to_do_cli/todoCLI.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/to_do_cli/todoCLI.js b/to_do_cli/todoCLI.js index dc8a46e..04b0628 100644 --- a/to_do_cli/todoCLI.js +++ b/to_do_cli/todoCLI.js @@ -42,6 +42,14 @@ function completeItem(num) { } +function deleteItem(num) { + if (num >= 0 && num < theList.length) { + console.log(`Deleted "${theList.splice(num, 1)[0][1]}"`); + } +} + + + console.log("Welcome to Todo CLI!\n--------------------"); menuPrompt(); @@ -50,6 +58,7 @@ let curCmd = ""; rl.on("line", (input) => { if (input === "q") { + console.log("See you soon! 😊") rl.close(); return; @@ -70,7 +79,11 @@ rl.on("line", (input) => { let num = parseInt(input.slice(1)) completeItem(num); menuPrompt(); - } + } else if (input[0] === "d") { + let num = parseInt(input.slice(1)) + deleteItem(num); + menuPrompt(); +} }); From 4bb4f29ad0c79a14197316f84759750cf0b9f704 Mon Sep 17 00:00:00 2001 From: Ibrahim <98372964+abe2dev@users.noreply.github.com> Date: Sat, 12 Feb 2022 22:29:14 -0800 Subject: [PATCH 7/9] Clean up test data enteries, show list empty message --- to_do_cli/todoCLI.js | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/to_do_cli/todoCLI.js b/to_do_cli/todoCLI.js index 04b0628..17ab22c 100644 --- a/to_do_cli/todoCLI.js +++ b/to_do_cli/todoCLI.js @@ -1,4 +1,3 @@ -const { title } = require("process"); const readline = require("readline") const rl = readline.createInterface({ @@ -6,7 +5,8 @@ const rl = readline.createInterface({ output: process.stdout, prompt: "> " // the shape of the prompt > also we can put % }); - + +const theList = []; function menuPrompt(){ @@ -17,10 +17,16 @@ function menuPrompt(){ function showList(){ - theList.forEach((entry, index) => { + if (theList.length <= 0) { + console.log("List is empty..."); + + } else { + theList.forEach((entry, index) => { let status = entry[0] === true ? "✔": " "; console.log(`${index} [${status}] ${entry[1]}`) }); + } + } @@ -85,14 +91,4 @@ rl.on("line", (input) => { menuPrompt(); } -}); - -const theList = [ - [true, "Take out the trash"], - [true, "Buy toothpaste"], - [false, "Buy Snickerdoodles"], - [false, "Fix the climate"], - [false, "Find a cure for aging"] -]; - - +}); \ No newline at end of file From 4e89e13bad7c7054eaad9ba71156f9dcd0412381 Mon Sep 17 00:00:00 2001 From: Ibrahim <98372964+abe2dev@users.noreply.github.com> Date: Sun, 13 Feb 2022 17:48:07 -0800 Subject: [PATCH 8/9] Create save action --- to_do_cli/todoCLI.js | 128 +++++++++++++++++++++++-------------------- 1 file changed, 69 insertions(+), 59 deletions(-) diff --git a/to_do_cli/todoCLI.js b/to_do_cli/todoCLI.js index 17ab22c..ae096db 100644 --- a/to_do_cli/todoCLI.js +++ b/to_do_cli/todoCLI.js @@ -1,94 +1,104 @@ -const readline = require("readline") +const fs = require("fs"); +const readline = require("readline"); const rl = readline.createInterface({ - input: process.stdin, - output: process.stdout, - prompt: "> " // the shape of the prompt > also we can put % + input: process.stdin, + output: process.stdout, + prompt: "> ", // the shape of the prompt > also we can put % }); - -const theList = []; +const theList = []; -function menuPrompt(){ - const theMenu = ( "(v) View.(n) New.(cX) Complete.(dX) Delete,(q) Quit") - console.log(theMenu); - rl.prompt() +function menuPrompt() { + const theMenu = + "(v) View. (n) New. (cX) Complete. (dX) Delete. (s) Save . (q) Quit"; + console.log(theMenu); + rl.prompt(); } - -function showList(){ - if (theList.length <= 0) { - console.log("List is empty..."); - - } else { - theList.forEach((entry, index) => { - let status = entry[0] === true ? "✔": " "; - console.log(`${index} [${status}] ${entry[1]}`) +function showList() { + if (theList.length <= 0) { + console.log("List is empty..."); + } else { + theList.forEach((entry, index) => { + let status = entry.complete === true ? "✔" : " "; + console.log(`${index} [${status}] ${entry.title}`); }); } - } - function newEntry(entry = "") { - if (entry === "") { - console.log("What?"); - rl.prompt() - } else { - theList.push([false, entry]) - } - + if (entry === "") { + console.log("What?"); + rl.prompt(); + } else { + theList.push({ complete: false, title: entry }); + } } function completeItem(num) { - if (num >= 0 && num < theList.length) { - theList[num][0] = true; - console.log(`Completed "${theList[num][1]}"`); - } - + if (num >= 0 && num < theList.length) { + theList[num].complete = true; + console.log(`Completed "${theList[num].title}"`); + } } function deleteItem(num) { - if (num >= 0 && num < theList.length) { - console.log(`Deleted "${theList.splice(num, 1)[0][1]}"`); - } + if (num >= 0 && num < theList.length) { + const deletedItem = theList.splice(num, 1)[0]; + console.log(`Deleted "${deletedItem.title}"`); + } } - - +function saveList(filename = "") { + if (filename === "") { + console.log("Where? (myTodos.json)"); + rl.prompt(); + } else { + fs.writeFileSync(`./${filename}`, JSON.stringify(theList)); + console.log(`List saved to "${filename}"`); + } +} console.log("Welcome to Todo CLI!\n--------------------"); menuPrompt(); let curCmd = ""; rl.on("line", (input) => { - - if (input === "q") { - console.log("See you soon! 😊") + if (input === "q" && curCmd === "") { + console.log("See you soon! 😊"); rl.close(); return; - - } else if (input === "v") { - showList() + } else if (input === "v" && curCmd === "") { + showList(); menuPrompt(); - - } else if (input === "n"){ + } else if (input === "n" && curCmd === "") { curCmd = "n"; newEntry(); - - } else if ( curCmd === "n"){ - newEntry(input) - curCmd = ""; - menuPrompt(); - + } else if (curCmd === "n") { + newEntry(input); + curCmd = ""; + menuPrompt(); } else if (input[0] === "c") { - let num = parseInt(input.slice(1)) - completeItem(num); - menuPrompt(); + let num = parseInt(input.slice(1)); + completeItem(num); + menuPrompt(); } else if (input[0] === "d") { - let num = parseInt(input.slice(1)) + let num = parseInt(input.slice(1)); deleteItem(num); menuPrompt(); -} - -}); \ No newline at end of file + } else if (input === "s" && curCmd === "") { + curCmd = "s"; + saveList(); + } else if (curCmd === "s") { + if (input === "") { + input = "myTodos.JSON" + + } + saveList(input); + curCmd = ""; + menuPrompt(); + } else if (curCmd === "") { + menuPrompt(); + } +}); From be2f1a26822ffcf8507b3d8c2842245c96c17011 Mon Sep 17 00:00:00 2001 From: Ibrahim <98372964+abe2dev@users.noreply.github.com> Date: Sun, 13 Feb 2022 19:26:38 -0800 Subject: [PATCH 9/9] Create load file --- to_do_cli/todoCLI.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/to_do_cli/todoCLI.js b/to_do_cli/todoCLI.js index ae096db..0e4c00b 100644 --- a/to_do_cli/todoCLI.js +++ b/to_do_cli/todoCLI.js @@ -56,10 +56,19 @@ function saveList(filename = "") { rl.prompt(); } else { fs.writeFileSync(`./${filename}`, JSON.stringify(theList)); - console.log(`List saved to "${filename}"`); + console.log(`List saved to "${filename}"`); } } +if (process.argv.length === 3) { + const inputFilename = process.argv[2]; + fs.readFile(inputFilename, "utf8", (err, data) => { + const todos = JSON.parse(data); + todos.forEach((todo) => { + theList.push(todo); + }); + }); +} console.log("Welcome to Todo CLI!\n--------------------"); menuPrompt(); @@ -92,8 +101,7 @@ rl.on("line", (input) => { saveList(); } else if (curCmd === "s") { if (input === "") { - input = "myTodos.JSON" - + input = "myTodos.JSON"; } saveList(input); curCmd = "";