Skip to content

Commit 2725812

Browse files
Merge pull request #20 from felipealfonsog/development
fixed a bug related with a loop in the case the user could choose a d…
2 parents f3b4150 + a8b81d5 commit 2725812

5 files changed

Lines changed: 471 additions & 52 deletions

File tree

Formula/term-notes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class TermNotes < Formula
22
desc "Terminal-based note-taking application"
33
homepage "https://github.com/felipealfonsog/TermNotes"
4-
url "https://github.com/felipealfonsog/TermNotes/archive/refs/tags/v.1.0.1.tar.gz"
4+
url "https://github.com/felipealfonsog/TermNotes/archive/refs/tags/v.1.0.2.tar.gz"
55
sha256 "43db1b333d8902b15df4444d12e76c6d80fa9535cb377f7594d3cfa19c64e869"
66
license "MIT"
77

src/notes.txt

Whitespace-only changes.

src/term_notes.c

Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,8 @@
1-
/*************************************
2-
Terminal Notes Utility written in C
3-
*************************************
4-
* Simple: Create and manage notes from the terminal
5-
*************************************
6-
* Developed and engineered by
7-
* Felipe Alfonso Gonzalez <f.alfonso@res-ear.ch>
8-
* Computer Science Engineer
9-
* Chile
10-
*************************************
11-
* To create the term_notes binary:
12-
*
13-
* 1. Extract the source code if needed:
14-
* tar -xf term_notes.tar.gz
15-
*
16-
* 2. Compile the source code:
17-
* gcc -o term_notes term_notes.c
18-
*
19-
* 3. Elevate to superuser (if necessary):
20-
* sudo su
21-
*
22-
* 4. Move the binary to the system bin directory:
23-
* mv term_notes /usr/local/bin/
24-
*
25-
* 5. Exit superuser mode:
26-
* exit
27-
*
28-
* 6. You can now run term_notes from the terminal:
29-
* term_notes
30-
*
31-
*************************************
32-
* Please note that term_notes requires the following dependencies:
33-
* - Nano: A text editor (sudo apt-get install nano)
34-
* - Vim: A text editor (sudo apt-get install vim)
35-
*
36-
* If any of these dependencies are not installed, please install them
37-
* using the provided commands.
38-
*
39-
* For more information, please refer to the documentation.
40-
*************************************
41-
*/
42-
431
#include <stdio.h>
442
#include <stdlib.h>
453
#include <string.h>
464
#include <unistd.h>
5+
#include <ctype.h>
476

487
#define MAX_NOTES 100
498

@@ -76,7 +35,11 @@ void addNote() {
7635

7736
printf("Choose the editor (1. Nano, 2. Vim): ");
7837
int editorChoice;
79-
scanf("%d", &editorChoice);
38+
while (scanf("%d", &editorChoice) != 1) {
39+
while (getchar() != '\n');
40+
printf("Invalid editor choice. Please enter a number.\n");
41+
printf("Choose the editor (1. Nano, 2. Vim): ");
42+
}
8043

8144
char tempFileName[20];
8245
sprintf(tempFileName, "temp_note_%d.txt", getpid());
@@ -152,7 +115,11 @@ void editNote() {
152115

153116
int choice;
154117
printf("Enter the note number to edit: ");
155-
scanf("%d", &choice);
118+
while (scanf("%d", &choice) != 1) {
119+
while (getchar() != '\n');
120+
printf("Invalid note number. Please enter a number.\n");
121+
printf("Enter the note number to edit: ");
122+
}
156123

157124
if (choice < 1 || choice > count) {
158125
printf("Invalid note number.\n");
@@ -186,7 +153,11 @@ void editNote() {
186153

187154
printf("Choose the editor (1. Nano, 2. Vim): ");
188155
int editorChoice;
189-
scanf("%d", &editorChoice);
156+
while (scanf("%d", &editorChoice) != 1) {
157+
while (getchar() != '\n');
158+
printf("Invalid editor choice. Please enter a number.\n");
159+
printf("Choose the editor (1. Nano, 2. Vim): ");
160+
}
190161

191162
char tempFileName[20];
192163
sprintf(tempFileName, "temp_note_%d.txt", getpid());
@@ -253,7 +224,11 @@ void deleteNote() {
253224

254225
int id;
255226
printf("Enter the note ID to delete: ");
256-
scanf("%d", &id);
227+
while (scanf("%d", &id) != 1) {
228+
while (getchar() != '\n');
229+
printf("Invalid note ID. Please enter a number.\n");
230+
printf("Enter the note ID to delete: ");
231+
}
257232

258233
FILE *file = fopen("notes.txt", "r");
259234
if (file != NULL) {
@@ -291,9 +266,13 @@ void deleteAllNotes() {
291266

292267
printf("Are you sure you want to delete all notes? (Y/N): ");
293268
char confirm;
294-
scanf(" %c", &confirm);
269+
while (scanf(" %c", &confirm) != 1 || (tolower(confirm) != 'y' && tolower(confirm) != 'n')) {
270+
while (getchar() != '\n');
271+
printf("Invalid choice. Please enter 'Y' or 'N'.\n");
272+
printf("Are you sure you want to delete all notes? (Y/N): ");
273+
}
295274

296-
if (confirm == 'Y' || confirm == 'y') {
275+
if (tolower(confirm) == 'y') {
297276
FILE *file = fopen("notes.txt", "w");
298277
if (file != NULL) {
299278
fclose(file);
@@ -324,7 +303,11 @@ void showNote() {
324303

325304
int id;
326305
printf("Enter the note ID to show: ");
327-
scanf("%d", &id);
306+
while (scanf("%d", &id) != 1) {
307+
while (getchar() != '\n');
308+
printf("Invalid note ID. Please enter a number.\n");
309+
printf("Enter the note ID to show: ");
310+
}
328311

329312
FILE *file = fopen("notes.txt", "r");
330313
if (file != NULL) {
@@ -363,7 +346,11 @@ void notesMenu() {
363346
do {
364347
showNotesMenu();
365348
printf("Enter an option: ");
366-
scanf("%d", &option);
349+
while (scanf("%d", &option) != 1) {
350+
while (getchar() != '\n');
351+
printf("Invalid option. Please enter a number.\n");
352+
printf("Enter an option: ");
353+
}
367354

368355
switch (option) {
369356
case 1:
@@ -402,7 +389,11 @@ void mainMenu() {
402389
printf("0. Exit\n");
403390
printf("******************\n");
404391
printf("Enter an option: ");
405-
scanf("%d", &option);
392+
while (scanf("%d", &option) != 1) {
393+
while (getchar() != '\n');
394+
printf("Invalid option. Please enter a number.\n");
395+
printf("Enter an option: ");
396+
}
406397

407398
switch (option) {
408399
case 1:

0 commit comments

Comments
 (0)