diff options
author | Oskar <[email protected]> | 2023-12-21 14:24:26 +0100 |
---|---|---|
committer | Oskar <[email protected]> | 2023-12-21 14:24:26 +0100 |
commit | d8650986677010cec774c98923b61c54bbca4258 (patch) | |
tree | 3de4d3c5586183500f04387f183827edbfa78dd2 /startmode.c | |
parent | f66a4530ea2fb6097d2aee3105c732aa79267add (diff) |
fixed some annoyances with L and some issues with how X handles the lines if there is no newlines at the end. Also fixed NEW_LINE function to be more useful
Diffstat (limited to 'startmode.c')
-rw-r--r-- | startmode.c | 63 |
1 files changed, 59 insertions, 4 deletions
diff --git a/startmode.c b/startmode.c index d9779ba..3fcc046 100644 --- a/startmode.c +++ b/startmode.c @@ -37,7 +37,7 @@ int startmode(char filename[]) { uint64_t focus = 1; while(1) { - + firstwhile: fprintf(stdout, "(%lu): ", focus); char command = getchar(); if (command == '\n') { @@ -58,6 +58,10 @@ int startmode(char filename[]) { fprintf(stderr, "Too many characters\n"); break; } + if (buf[0] == '\n') { + goto firstwhile; // start the first while loop aka startmode + } + char *endptr; Lfocus = strtol(buf, &endptr, 10); @@ -129,10 +133,61 @@ int startmode(char filename[]) { NCAT(filename); break; case 'n': - case 'N': - - NEW_LINE(filename); + case 'N': { + // The "focus" that the newline will be inserted afterwards + uint64_t new_line_pos_temp = 0; // temp + char buf[1024]; + int success; + + do { + fprintf(stdout, "Create a new line after: "); + if (!fgets(buf, 1024, stdin)) { // take input from user + fprintf(stderr, "Too many characters\n"); + break; + } + char *endptr; + + new_line_pos_temp = strtol(buf, &endptr, 10); + errno = 0; + if (errno == ERANGE) { + fprintf(stderr, "Sorry, this number is too small or too large.\n"); + success = 0; + } + else if (endptr == buf) { + // no character was read + success = 0; + } + else if (*endptr && *endptr != '\n') { + success = 0; + } + + else { + success = 1; + } + } while (!success); + NEW_LINE(filename, new_line_pos_temp); + break; } + case 'X': + case 'x': + /* + if (focus == Flines) { + size_t Plines; + int ret = COUNT_LINES_IN_FILE_POSIX(filename, &Plines); + + if (Flines != Plines) { + NEW_LINE(filename); + } + } + */ + int choice = CHOICE(); + if (choice == 0) { + remove_line_contents(filename, focus); + } + break; + case 'D': + case 'd': + fprintf(stdout, "delete\n"); break; default: fprintf(stdout, "?\n"); |