diff options
-rw-r--r-- | more.c | 87 |
1 files changed, 72 insertions, 15 deletions
@@ -72,6 +72,33 @@ enum { CTRL_U = 0x15, }; +static void more_tput(size_t n, char buf[static n], const char *cap) +{ + char cmd[strlen(cap) + 6]; + sprintf(cmd, "tput %s", cap); + FILE *p = popen(cmd, "r"); + if (p == NULL) { + return; + } + fread(buf, 1, n, p); + fclose(p); +} + +static void more_echo(int on) +{ + struct termios ti; + tcgetattr(fileno(more_in), &ti); + if (on) { + ti.c_lflag |= ECHO; + } else { + ti.c_lflag &= ~ECHO; + } + ti.c_cc[VMIN] = 1; + ti.c_cc[VTIME] = 0; + tcsetattr(fileno(more_in), TCSANOW, &ti); +} + + static void more_status(const char *fmt, ...) { va_list ap; @@ -85,12 +112,7 @@ static void more_clear(more_clear_action action) static char clearcmd[64] = ""; if (action == ENABLE) { - FILE *p = popen("tput clear", "r"); - if (p == NULL) { - return; - } - fread(clearcmd, 1, sizeof(clearcmd), p); - fclose(p); + more_tput(sizeof(clearcmd), clearcmd, "clear"); } if (clearcmd[0] == '\0') { @@ -180,6 +202,28 @@ static void more_close(struct more_file *mf) free(mf->buf); } +static void more_underline(int on) +{ + static char underline[64] = ""; + static char ununderline[64] = ""; + if (underline[0] == '\0') { + more_tput(sizeof(underline), underline, "smul"); + more_tput(sizeof(ununderline), ununderline, "rmul"); + } + printf("%s", on ? underline : ununderline); +} + +static void more_bold(int on) +{ + static char bold[64] = ""; + static char unbold[64] = ""; + if (bold[0] == '\0') { + more_tput(sizeof(bold), bold, "bold"); + more_tput(sizeof(unbold), unbold, "sgr0"); + } + printf("%s", on ? bold : unbold); +} + static int more_printline(char *s) { static int columns = 0; @@ -191,26 +235,31 @@ static int more_printline(char *s) int ret = -1; for (size_t i = 0; s[i] != '\0'; i++) { - int attr = 0; + enum { NONE, UNDERLINE, BOLD } attr = NONE; if (more_backspace && s[i + 1] == '\b') { if (s[i + 2] == '_') { - //attr = A_UNDERLINE; + more_underline(1); + attr = UNDERLINE; } else if (s[i + 2] == s[i]) { - //attr = A_BOLD; + more_bold(1); + attr = BOLD; } else { i++; continue; } } - if (attr) { - //wattron(more_win, attr); - } + putchar(s[i]); if (i % columns == 0) { ret++; } - if (attr) { - //wattroff(more_win, attr); + + if (attr == UNDERLINE) { + more_underline(0); + i += 2; + } + if (attr == BOLD) { + more_bold(0); i += 2; } } @@ -378,8 +427,13 @@ static void more_invoke_editor(struct more_file *mf) static struct more_file more_examine(struct more_file *mf) { //wprintw(more_status, "e"); + more_echo(1); + printf(":e"); + fflush(stdout); char filename[1024]; fgets(filename, sizeof(filename), more_in); + more_echo(0); + printf("\r%*s\r", (int)strlen(filename) + 2, " "); wordexp_t we = { 0 }; if (wordexp(filename, &we, WRDE_NOCMD) != 0) { @@ -448,7 +502,6 @@ static int more(struct more_file *mf) int count = 0; while (mf) { int at_eof = (mf->eof && (mf->topline + more_lines >= mf->nlines)); - //wmove(more_status, 0, 0); if (at_eof) { if (more_fastexit && mf->next == NULL) { return 0; @@ -813,6 +866,10 @@ int main(int argc, char *argv[]) struct more_file *head = NULL; do { + if (head != NULL && argv[optind] == NULL) { + break; + } + struct more_file *mf = calloc(1, sizeof(*mf)); if (!mf) { perror("more"); |