-
Notifications
You must be signed in to change notification settings - Fork 0
Todo cli #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
EricLee9208
wants to merge
6
commits into
main
Choose a base branch
from
todo_cli
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Todo cli #3
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
35535db
initial_commit_hw3
EricLee9208 ada596b
deleted turtle directory
EricLee9208 703b2e8
added_safeguard_to_delete_function
EricLee9208 b2244e7
added strecth
EricLee9208 116a75d
minor changes to files
EricLee9208 85bac83
deleted one file
EricLee9208 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| [ | ||
| { "completed": true, "title": "Feed the raptops" }, | ||
| { "completed": false, "title": "Have lunch" }, | ||
| { "completed": false, "title": "Go see Deadpool 2" }, | ||
| { "completed": false, "title": "Write a script to fix the climate" } | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| const fs = require("fs"); | ||
| const readline = require("readline"); | ||
| const rl = readline.createInterface({ | ||
| input: process.stdin, | ||
| output: process.stdout, | ||
| }); | ||
|
|
||
| let todo_list = []; | ||
|
|
||
| const file = process.argv[2]; | ||
|
|
||
| //Reading json file if "json" exist in user input and adding the data to todo_list | ||
| if (file != undefined && file.includes(".json")) { | ||
| fs.readFile(file, "utf8", (err, data) => { | ||
| if (err) { | ||
| console.error; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need an argument |
||
| } else { | ||
| let words = JSON.parse(data); | ||
| words.forEach((element, index) => { | ||
| if (element.completed == true) { | ||
| todo_list.push(`${index}[✔] ${element.title}`); | ||
| } else { | ||
| todo_list.push(`${index}[] ${element.title}`); | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| function view() { | ||
| if (todo_list.length > 0) { | ||
| for (const todo of todo_list) { | ||
| console.log(todo.toString()); | ||
| } | ||
| } else { | ||
| console.log("List is empty..."); | ||
| } | ||
| todo_function(); | ||
| } | ||
|
|
||
| function add() { | ||
| rl.question("What? \n", (input) => { | ||
| if (input != "") { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| todo_list.push(`${todo_list.length}[] ${input}`); | ||
| } | ||
| todo_function(); | ||
| }); | ||
| } | ||
|
|
||
| function complete(index) { | ||
| if (index < todo_list.length) { | ||
| let a = todo_list[index].toString(); | ||
| let start = a.indexOf(" "); | ||
| console.log(`Completed "${a.slice(start + 1)}"`); | ||
| todo_list[index] = todo_list[index].toString().replace("[]", `[✔]`); | ||
| todo_function(); | ||
| } else { | ||
| console.log("Index out of range."); | ||
| todo_function(); | ||
| } | ||
| } | ||
|
|
||
| function deleting(index) { | ||
| if (index < todo_list.length) { | ||
| let a = todo_list[index].toString(); | ||
| let start = a.indexOf(" "); | ||
| console.log(`Deleted "${a.slice(start + 1)}"`); | ||
| todo_list.splice(index, 1); | ||
| for (let i = index; i < todo_list.length; i++) { | ||
| let a = todo_list[i].toString(); | ||
| let start = a.indexOf("["); | ||
| todo_list[i] = `${i}${a.slice(start)}`; | ||
| } | ||
| } else { | ||
| console.log("Index out of range."); | ||
| } | ||
| todo_function(); | ||
| } | ||
|
|
||
| //converting todo_list to JSON | ||
| function todo_list_To_JSON() { | ||
| let result = []; | ||
|
|
||
| for (const todo of todo_list) { | ||
| let startIndex = todo.indexOf(" "); | ||
|
|
||
| if (todo.includes("[✔]")) { | ||
| let obj = { completed: true, title: todo.slice(startIndex + 1) }; | ||
| let todoJSON = JSON.stringify(obj); | ||
| result.push(todoJSON); | ||
| } else { | ||
| let obj = { completed: false, title: todo.slice(startIndex + 1) }; | ||
| let todoJSON = JSON.stringify(obj); | ||
| result.push(todoJSON); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| function save() { | ||
| let result = todo_list_To_JSON(); | ||
|
|
||
| rl.question("What? (myTodos.json)\n", (input) => { | ||
| // if user inputs path, save data to that path | ||
| if (input.includes(".json")) { | ||
| fs.writeFile(input, `[${result}]`, "utf8", (err) => { | ||
| if (err) { | ||
| console.error; | ||
| } else { | ||
| console.log(`List saved to "${input}"`); | ||
| todo_function(); | ||
| } | ||
| }); | ||
| } else { | ||
| // if user does not pass in path, save to default path "myTodos.json" | ||
| fs.writeFile("myTodos.json", `[${result}]`, "utf8", (err) => { | ||
| if (err) { | ||
| console.error; | ||
| } else { | ||
| console.log(`List saved to "myTodos.json"`); | ||
| todo_function(); | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| // created todo_function to do recurssion | ||
| function todo_function() { | ||
| rl.question( | ||
| "(v) View ● (n) New ● (cX) Complete ● (dX) Delete ● (s) Save ● (q) Quit \n", | ||
| (input) => { | ||
| if (input === "v") { | ||
| view(); | ||
| } else if (input === "n") { | ||
| add(); | ||
| } else if (input[0] === "c") { | ||
| let addIndex = parseInt(input.slice(1)); | ||
| complete(addIndex); | ||
| } else if (input[0] === "d") { | ||
| let deleteIndex = parseInt(input.slice(1)); | ||
| deleting(deleteIndex); | ||
| } else if (input === "q") { | ||
| console.log("See you soon! 😄"); | ||
| rl.close(); | ||
| } else if (input === "s") { | ||
| save(); | ||
| } else { | ||
| todo_function(); | ||
| } | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| console.log("\nWelcome to Todo CLI!"); | ||
| console.log("---------------------"); | ||
|
|
||
| todo_function(); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (file && file.includes(".json"))