diff options
author | Jakob Kaivo <jkk@ung.org> | 2022-04-18 21:48:27 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2022-04-18 21:48:27 -0400 |
commit | fb7e658faa5d279d97d54e5b6c1105b3a6bdeab2 (patch) | |
tree | 1941bec72e82a61108d26c1f4916a48860d0d196 | |
parent | 05d5a4db2e33ad6940bcb3820591eecbbb85a4cd (diff) |
add help, open tty if stdin is redirected
-rw-r--r-- | more.c | 88 |
1 files changed, 76 insertions, 12 deletions
@@ -29,6 +29,8 @@ enum { CTRL_U = 0x15, }; +static int more(const char *); + static void more_refresh(struct more_file *mf) { for (size_t i = mf->topline; i < mf->topline + LINES - 1; i++) { @@ -86,6 +88,64 @@ static void jump(struct more_file *mf) } } +static void more_help(void) +{ + char path[L_tmpnam + 1]; + FILE *f = fopen(tmpnam(path), "w"); + if (f == NULL) { + wprintw(more_status, "error creating help: %s", strerror(errno)); + return; + } + + fprintf(f, "^X means hold control and press X\n"); + fprintf(f, "\n"); + + fprintf(f, "Non-Scrolling Commands\n"); + fprintf(f, "h display this help\n"); + fprintf(f, ":e filename<newline> open filename\n"); + fprintf(f, "#:n go to the count-th next file\n"); + fprintf(f, "#:p go to the count-th previous file\n"); + fprintf(f, "v edit file with $EDITOR (default vi)\n"); + fprintf(f, "= ^G display current file information\n"); + fprintf(f, "q :q ZZ quit\n"); + + fprintf(f, "r ^L redraw the current screen\n"); + fprintf(f, "R discard buffered input and redraw screen\n"); + fprintf(f, "\n"); + + fprintf(f, "Scrolling Commands: Prefix with a count for number of lines, otherwise as shown\n"); + fprintf(f, "f ^F forward one screen\n"); + fprintf(f, "b ^B backward one screen\n"); + fprintf(f, "j <space> <newline> forward one line\n"); + fprintf(f, "k backward one line\n"); + fprintf(f, "d ^D forward one half screen\n"); + fprintf(f, "u ^U backward one half screen\n"); + fprintf(f, "\n"); + + fprintf(f, "Jumping Commands\n"); + fprintf(f, "#s skip count lines after the end of the current screen\n"); + fprintf(f, "#g go to line count, or 1\n"); + fprintf(f, "#G go to line count, or end of file\n"); + fprintf(f, ":t tag<newline> jump to tag\n"); + + fprintf(f, "mletter mark the current position as letter\n"); + fprintf(f, "'letter return to the position marked letter\n"); + fprintf(f, "'' return to last position before large movement\n"); + fprintf(f, "\n"); + + fprintf(f, "Searching Commands\n"); + fprintf(f, "#/[!]pattern<newline> search for the count-th occurence of pattern\n"); + fprintf(f, "#?[!]pattern<newline> search backward\n"); + fprintf(f, "#n repeat search\n"); + fprintf(f, "#N repeat search backwards\n"); + + + fclose(f); + + more(path); + remove(path); +} + static struct more_file more_invoke_editor(struct more_file *mf) { extern char **environ; @@ -203,8 +263,7 @@ static int more(const char *path) break; case 'h': - //show_help(); - wprintw(more_status, "Help!"); + more_help(); break; case 'f': @@ -387,7 +446,7 @@ static void adjust_args(int *argc, char ***argv) if (env) { char **newargv = malloc((*argc + 2) * sizeof(*newargv)); newargv[0] = (*argv)[0]; - + /* TODO: account for spaces in env */ newargv[1] = env; @@ -400,7 +459,7 @@ static void adjust_args(int *argc, char ***argv) *argv = newargv; (*argc)++; } - + for (int i = 1; i < *argc; i++) { if (!strcmp((*argv)[i], "--")) { return; @@ -527,11 +586,16 @@ int main(int argc, char *argv[]) return ret; } - if (optind >= argc) { - more("-"); + if (isatty(STDIN_FILENO)) { + initscr(); + } else { + FILE *tty = fopen("/dev/tty", "r"); + if (tty == NULL) { + perror("more: couldn't open terminal"); + return 1; + } + newterm(NULL, stdout, tty); } - - initscr(); cbreak(); noecho(); @@ -546,17 +610,17 @@ int main(int argc, char *argv[]) clear(); } + if (optind >= argc) { + more("-"); + } + int min = optind; - int max = argc - 1; while (optind < argc) { int next = more(argv[optind]); optind += next; if (optind < min) { optind = min; } - if (optind > max) { - optind = max; - } } (void)fastexit; |