commit 9aa8b046e4afd2306021ddc2c2ee36340c96e84a
parent 7d61b0086b50d196ea3f2dda7e2c38ec4ece78e8
Author: Ed van Bruggen <edvb54@gmail.com>
Date: Sun, 12 Mar 2017 20:58:46 -0700
Add edit option
Diffstat:
3 files changed, 37 insertions(+), 6 deletions(-)
diff --git a/README.md b/README.md
@@ -2,7 +2,7 @@
## SYNOPSIS
-`nt` [-lv] [-f *FILE*] [-d *NOTE*] [-s *SEARCH*] [-n *NUM* | -*NUM*] [*NOTE* ...]
+`nt` [-lv] [-f *FILE*] [-e *NOTE*] [-d *NOTE*] [-s *SEARCH*] [-n *NUM* | -*NUM*] [*NOTE* ...]
## DESCRIPTION
diff --git a/nt.1 b/nt.1
@@ -4,7 +4,7 @@
nt \- simple note taker
.SH SYNOPSIS
.PP
-\fB\fCnt\fR [\-lv] [\-f \fIFILE\fP] [\-d \fINOTE\fP] [\-s \fISEARCH\fP] [\-n \fINUM\fP | \-\fINUM\fP] [\fINOTE\fP ...]
+\fB\fCnt\fR [\-lv] [\-f \fIFILE\fP] [\-e \fINOTE\fP] [\-d \fINOTE\fP] [\-s \fISEARCH\fP] [\-n \fINUM\fP | \-\fINUM\fP] [\fINOTE\fP ...]
.SH DESCRIPTION
.PP
Simple note taker for the command line.
diff --git a/nt.c b/nt.c
@@ -16,12 +16,14 @@
char *trimwhitespace(char *str);
void nt_new(void);
+void nt_edit(void);
void nt_del(void);
void nt_search(void);
void nt_list_all(void);
void nt_list_n(int n);
void run(void);
+void setup(void);
void cleanup(void);
void usage(void);
@@ -98,6 +100,28 @@ nt_del(void)
rename(tmpfpname, fname);
}
+void
+nt_edit()
+{
+ if (sub == NULL) usage();
+ int i, found = 0;
+
+ for (i = 0; i < notec; i++)
+ if (strcmp(notes[i], sub) == 0 && !found) {
+ fgets(notes[i], MAX_SUB, stdin);
+ trimwhitespace(notes[i]);
+ found = 1;
+ }
+ if (!found)
+ die("%s: edit: '%s' not found", argv0, sub);
+
+ fclose(fp);
+ fp = fopen(fname, "w");
+ for (i = 0; i < notec; i++)
+ fprintf(fp, "%s\n", notes[i]);
+ rewind(fp);
+}
+
/* search notes for given note */
void
nt_search()
@@ -141,6 +165,9 @@ run(void)
case 'd':
nt_del();
break;
+ case 'e':
+ nt_edit();
+ break;
case 'l':
nt_list_all();
exit(0);
@@ -196,19 +223,23 @@ cleanup(void)
void
usage(void)
{
- die("usage: %s [-lv] [-f FILE] [-d NOTE] [-s SEARCH] [-n NUM | -NUM] [NOTE ...]", argv0);
+ die("usage: %s [-lv] [-f FILE] [-e NOTE] [-d NOTE]\n"
+ " [-s SEARCH] [-n NUM | -NUM] [NOTE ...]", argv0);
}
int
main(int argc, char *argv[])
{
ARGBEGIN {
- case 'f':
- fname = EARGF(usage());
- break;
case 'd':
mode = 'd';
break;
+ case 'e':
+ mode = 'e';
+ break;
+ case 'f':
+ fname = EARGF(usage());
+ break;
case 'l':
mode = 'l';
break;