nt

Unnamed repository; edit this file 'description' to name the repository.
git clone git://edryd.org/nt
Log | Files | Refs | LICENSE

commit f57477daf48dae78ca68eda3e2fdb24f9b16ef57
parent babb8ed59908b2afbbd16410c7e4f811a5f6951f
Author: Ed van Bruggen <edvb54@gmail.com>
Date:   Sun, 19 Mar 2017 20:12:43 -0700

Add confirmation prompt when deleting a note

Skip the prompt with added -y to get old behaviour. Option can be configured in
config.h to be on by default.  Also Updated man page and moved trimwhitespace
to util.c under strtrim.

Diffstat:
README.md | 22++++++++++++++--------
config.def.h | 4+++-
nt.c | 59++++++++++++++++++++++++++++++-----------------------------
util.c | 26+++++++++++++++++++++++++-
util.h | 1+
5 files changed, 73 insertions(+), 39 deletions(-)

diff --git a/README.md b/README.md @@ -2,7 +2,7 @@ ## SYNOPSIS -`nt` [-lv] [-f *FILE*] [-e *NOTE*] [-d *NOTE*] [-s *SEARCH*] [-n *NUM* | -*NUM*] [*NOTE* ...] +`nt` [-lvy] [-f *FILE*] [-e *NOTE*] [-d *NOTE*] [-s *SEARCH*] [-n *NUM* | -*NUM*] [*NOTE* ...] ## DESCRIPTION @@ -10,8 +10,11 @@ Simple note taker for the command line. ## OPTIONS --v - Print version info and exit +-d *NOTE* + Delete *NOTE* from notes + +-e *NOTE* + Edit *NOTE* with new text given through stdin -f *FILE* Load *FILE* instead of default one @@ -19,14 +22,17 @@ Simple note taker for the command line. -l List notes --n *NUM* +-n *NUM*, -*NUM* List last *NUM* notes -s *SEARCH* - Search for *SEARCH* in notes + Search for pattern *SEARCH* in notes --d *NOTE* - Delete *NOTE* from notes +-v + Print version info and exit + +-y + Auto reply yes to confirmation prompts ## USAGE @@ -78,7 +84,7 @@ Search notes with a specified term: Remove given note: - $ nt -d buy pie + $ nt -yd buy pie $ nt -l clean dishes wash car diff --git a/config.def.h b/config.def.h @@ -1 +1,3 @@ -char *fname = "todo"; +char *fname = "todo"; /* default notes file name, overridden with -f */ + +int yes = 0; /* 0: prompt to confirm actions such as delete, 1: no prompt */ diff --git a/nt.c b/nt.c @@ -1,5 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include <ctype.h> +#include <stdarg.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -19,7 +19,7 @@ typedef struct Note { } Note; /* functions */ -char *trimwhitespace(char *str); +int confirm(const char *msg, ...); void nt_add(char *str); void nt_edit(void); @@ -45,26 +45,22 @@ int lsnum; #include "config.h" -/* remove tailing or leading white space from str */ -char * -trimwhitespace(char *str) +int +confirm(const char *msg, ...) { - char *end; - - /* trim leading space */ - while(isspace((unsigned char)*str)) str++; + if (yes) return 1; - if (*str == 0) /* all spaces? */ - return str; + char input = 'n'; + va_list ap; - /* trim trailing space */ - end = str + strlen(str) - 1; - while (end > str && isspace((unsigned char)*end)) end--; + va_start(ap, msg); + printf("%s: ", argv0); + vprintf(msg, ap); + printf("? [y/N] "); + va_end(ap); + scanf("%c", &input); - /* write new null terminator */ - *(end+1) = 0; - - return str; + return (input == 'y' || input == 'Y') ? 1 : 0; } /* create a new note */ @@ -106,15 +102,17 @@ nt_del(void) for (; cur; cur = cur->next) { if (strcmp(cur->str, sub) == 0) { - if (!cur->prev) { /* beginning */ - head = cur->next; - } else if (!cur->next) { /* end */ - cur->prev->next = NULL; - } else { /* middle */ - cur->prev->next = cur->next; - cur->next->prev = cur->prev; + if (confirm("delete note '%s'", cur->str)) { + if (!cur->prev) { /* beginning */ + head = cur->next; + } else if (!cur->next) { /* end */ + cur->prev->next = NULL; + } else { /* middle */ + cur->prev->next = cur->next; + cur->next->prev = cur->prev; + } + free(cur); } - free(cur); return; } } @@ -131,7 +129,7 @@ nt_edit() for (; cur; cur = cur->next) if (strcmp(cur->str, sub) == 0) { fgets(cur->str, MAX_SUB, stdin); - trimwhitespace(cur->str); + strtrim(cur->str); return; } @@ -246,7 +244,7 @@ cleanup(void) void usage(void) { - die("usage: %s [-lv] [-f FILE] [-e NOTE] [-d NOTE]\n" + die("usage: %s [-lvy] [-f FILE] [-e NOTE] [-d NOTE]\n" " [-s SEARCH] [-n NUM | -NUM] [NOTE ...]", argv0); } @@ -280,6 +278,9 @@ main(int argc, char *argv[]) case 'v': printf("%s v%s\n", argv0, VERSION); return 0; + case 'y': + yes = 1; + break; default: usage(); } ARGEND; @@ -290,7 +291,7 @@ main(int argc, char *argv[]) fgets(sub, MAX_SUB, stdin); else if (argc > 0) sub = strconcat(argv, argc); - trimwhitespace(sub); + strtrim(sub); run(); diff --git a/util.c b/util.c @@ -1,4 +1,5 @@ /* See LICENSE file for copyright and license details. */ +#include <ctype.h> #include <assert.h> #include <stdarg.h> #include <stdio.h> @@ -116,8 +117,31 @@ strsplit(const char *s, const char a_delim, int *size) return ret; } +/* remove tailing or leading white space from s */ +char * +strtrim(char *s) +{ + char *end; + + /* trim leading space */ + while (isspace((unsigned char)*s)) s++; + + if (*s == 0) /* all spaces? */ + return s; + + /* trim trailing space */ + end = s + strlen(s) - 1; + while (end > s && isspace((unsigned char)*end)) end--; + + /* write new null terminator */ + *(end+1) = 0; + + return s; +} + void -die(const char *fmt, ...) { +die(const char *fmt, ...) +{ va_list ap; va_start(ap, fmt); diff --git a/util.h b/util.h @@ -12,5 +12,6 @@ char *estrdup(char *s); char *strconcat(char **s, int c); char **strsplit(const char *s, const char a_delim, int *size); +char *strtrim(char *s); void die(const char *errstr, ...);