aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Riehs <tristan.riehs@inria.fr>2025-12-25 16:20:55 +0100
committerTristan Riehs <tristan.riehs@inria.fr>2025-12-25 16:20:55 +0100
commit6ea0e40086017982efcdfd1b85086dbf18b82724 (patch)
tree7dcecc8cf6c6890c02dcdf5382c175080b3fadfe
parentcc7693f3edd9349c66b3f248b0a8ecb86cff93fb (diff)
Add interactive option to "ftag file add"
-rw-r--r--src/main.c51
1 files changed, 50 insertions, 1 deletions
diff --git a/src/main.c b/src/main.c
index 31685db..49bfe3c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -69,6 +69,28 @@ static void assert_db_exists(void)
exit(EXIT_FAILURE);
}
+/* Prompt the user for yes or no (default is yes). Before calling, a prompt
+ * should be printed to stdout, eventually not with an ending newline. */
+int prompt_yes_no(void)
+{
+ printf(" [Y/n]\n");
+ fflush(stdout);
+ size_t line_size = 3;
+ char *line = malloc(line_size);
+ int attempts = 0;
+ do {
+ getline(&line, &line_size, stdin);
+ char input = line[0];
+ if (input == '\n'/*no input*/ || input == 'y' || input == 'Y')
+ return 1;
+ else if (input == 'n' || input == 'N')
+ return 0;
+ attempts++;
+ }
+ while(attempts < 3);
+ return 0;
+}
+
/* Convert heap-allocated *STR to C string to SQL string, essentially by adding
* single quotes to escape single quotes. */
static void sanitize_sql_str(char **str)
@@ -352,19 +374,46 @@ static void ftag_add_one_file(sqlite3 *db, int *next_id, const char *file)
free(description);
}
+static void ftag_file_add_usage(void)
+{
+ fprintf(stderr, "Usage: ftag file add [-i] FILE...\n");
+}
+
/* Add new files to the database. */
static void ftag_file_add(int argc, char **argv)
{
if (argc == 0) {
- fprintf(stderr, "ftag file add: must supply file names\n");
+ ftag_file_add_usage();
exit(EXIT_FAILURE);
}
sqlite3 *db = NULL;
+ int interactive = 0;
int rc;
+ while (argv[0][0] == '-') {
+ switch (argv[0][1]) {
+ case 'i':
+ interactive = 1;
+ break;
+ default:
+ ftag_file_add_usage();
+ exit(EXIT_FAILURE);
+ }
+ argv++;
+ argc--;
+ }
rc = sqlite3_open(DATABASE_PATH, &db);
sqlite3_check(rc, db);
int next_id = table_next_id(db, "files");
for (int i = 0; i < argc; i++) {
+ char *file = argv[i];
+ if (interactive) {
+ printf("Add \"%s\" to database ?", file);
+ if (!prompt_yes_no())
+ continue; /* do not add this file */
+ }
+ else {
+ printf("ftag file add: adding file \"%s\" to database\n", file);
+ }
ftag_add_one_file(db, &next_id, argv[i]);
}
sqlite3_close(db);