From 8689da8fc361118b05e33f3d19b269e9463040d0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Michael=20G=C3=B6hler?= Date: Tue, 20 Jan 2015 00:31:14 +0100 Subject: [PATCH] new feature - reload a file with a keypress, closes #69 --- README.md | 3 ++- include/viewer.h | 2 +- sample.md | 14 ++++++++++ src/main.c | 67 +++++++++++++++++++++++++++++++++--------------- src/viewer.c | 39 ++++++++++++++++++++++++---- 5 files changed, 97 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index e6cf4a9..f88c02f 100644 --- a/README.md +++ b/README.md @@ -59,12 +59,13 @@ Review sample.md for more details. ***Controls:*** -- h, j, k, l, Cursor keys, +- h, j, k, l, Arrow keys, Space, Enter, Backspace, Page Up, Page Down - next/previous slide - Home - go to first slide - End - go to last slide - 1-9 - go to slide n +- r - reload input file - q - exit diff --git a/include/viewer.h b/include/viewer.h index bcd022f..263f703 100644 --- a/include/viewer.h +++ b/include/viewer.h @@ -51,7 +51,7 @@ #define FADE_DELAY 15000 // micro seconds -int ncurses_display(deck_t *deck, int notrans, int nofade, int invert); +int ncurses_display(deck_t *deck, int notrans, int nofade, int invert, int reload, int noreload); void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors); void inline_display(WINDOW *window, const char *c, const int colors); void fade_out(WINDOW *window, int trans, int colors, int invert); diff --git a/sample.md b/sample.md index bf24ce0..b717393 100644 --- a/sample.md +++ b/sample.md @@ -7,6 +7,20 @@ -> A command-line based markdown presentation tool. <- +_Basic controls:_ + +next slide *Enter*, *Space*, *Page Down*, *j*, *l*, + *Down Arrow*, *Right Arrow* + +previous slide *Backspace*, *Page Up*, *h*, *k*, + *Up Arrow*, *Left Arrow* + +quit *q* +reload *r* +slide N *1..9* +first slide *Home* +last slide *End* + ------------------------------------------------- -> # Supported markdown formatting's <- diff --git a/src/main.c b/src/main.c index e1829a8..37af9bc 100644 --- a/src/main.c +++ b/src/main.c @@ -49,9 +49,11 @@ void version() { } int main(int argc, char *argv[]) { - int notrans = 0; - int nofade = 0; - int invert = 0; + int notrans = 0; // disable transparency + int nofade = 0; // disable fading + int invert = 0; // invert color (black on white) + int reload = 0; // reload page N (0 means no reload) + int noreload = 1; // reload disabled until we know input is a file // define command-line options struct option longopts[] = { @@ -81,7 +83,7 @@ int main(int argc, char *argv[]) { } // open file or set input to STDIN - char *file; + char *file = NULL; FILE *input; if (optind < argc) { do { @@ -96,34 +98,57 @@ int main(int argc, char *argv[]) { fprintf(stderr, "%s: %s: %s\n", argv[0], file, strerror(errno)); exit(EXIT_FAILURE); } + // enable reload because input is a file + noreload = 0; } } else { input = stdin; } - // load deck object from input - deck_t *deck; - deck = markdown_load(input); + // reload loop + do { + + // reopen input file on reload + if(noreload == 0 && reload > 0) { + if(file) { + input = fopen(file,"r"); + if(!input) { + fprintf(stderr, "%s: %s: %s\n", argv[0], file, strerror(errno)); + exit(EXIT_FAILURE); + } + } else { + fprintf(stderr, "%s: %s\n", argv[0], "no input file"); + exit(EXIT_FAILURE); + } + } - // close file - fclose(input); + // load deck object from input + deck_t *deck; + deck = markdown_load(input); - // replace stdin with current tty if input was a pipe - if(input == stdin) { - input = freopen("/dev/tty", "rw", stdin); - if(!input) { - fprintf(stderr, "%s: %s: %s\n", argv[0], "/dev/tty", strerror(errno)); - exit(EXIT_FAILURE); + // close file + fclose(input); + + // replace stdin with current tty if input was a pipe + // if input was a pipe reload is disabled, so we simply check that + if(noreload == 1) { + input = freopen("/dev/tty", "rw", stdin); + if(!input) { + fprintf(stderr, "%s: %s: %s\n", argv[0], "/dev/tty", strerror(errno)); + exit(EXIT_FAILURE); + } } - } - if(debug > 0) { - markdown_debug(deck, debug); - } + if(debug > 0) { + markdown_debug(deck, debug); + } + + reload = ncurses_display(deck, notrans, nofade, invert, reload, noreload); - ncurses_display(deck, notrans, nofade, invert); + free_deck(deck); - free_deck(deck); + // reload if supported and requested + } while(noreload == 0 && reload > 0); return EXIT_SUCCESS; } diff --git a/src/viewer.c b/src/viewer.c index 50646d7..42088a5 100644 --- a/src/viewer.c +++ b/src/viewer.c @@ -60,7 +60,7 @@ static short red_ramp_invert[24] = { 15, 231, 231, 224, 224, 225, 206, 207, 201, 200, 199, 199, 198, 198, 197, 197, 196, 196}; -int ncurses_display(deck_t *deck, int notrans, int nofade, int invert) { +int ncurses_display(deck_t *deck, int notrans, int nofade, int invert, int reload, int noreload) { int c = 0; // char int i = 0; // iterate @@ -120,7 +120,8 @@ int ncurses_display(deck_t *deck, int notrans, int nofade, int invert) { fprintf(stderr, "Error: Terminal width (%i columns) too small. Need at least %i columns.\n", COLS, i); fprintf(stderr, "You may need to shorten some lines by inserting line breaks.\n"); - return 1; + // no reload + return 0; } // set max_cols @@ -156,7 +157,8 @@ int ncurses_display(deck_t *deck, int notrans, int nofade, int invert) { fprintf(stderr, "Error: Terminal height (%i lines) too small. Need at least %i lines.\n", LINES, max_lines + bar_top + bar_bottom); fprintf(stderr, "You may need to add additional horizontal rules ('***') to split your file in shorter slides.\n"); - return 1; + // no reload + return 0; } // disable cursor @@ -240,6 +242,17 @@ int ncurses_display(deck_t *deck, int notrans, int nofade, int invert) { wbkgd(content, COLOR_PAIR(CP_WHITE)); slide = deck->slide; + + // find slide to reload + while(reload > 1 && reload <= deck->slides) { + slide = slide->next; + sc++; + reload--; + } + + // reset reload indicator + reload = 0; + while(slide) { url_init(); @@ -395,10 +408,24 @@ int ncurses_display(deck_t *deck, int notrans, int nofade, int invert) { } break; + // reload + case 'r': + if(noreload == 0) { + // reload slide N + reload = sc; + slide = NULL; + } else { + // disable fading if reload is not possible + fade = false; + } + break; + // quit case 'q': // do not fade out on exit fade = false; + // do not reload + reload = 0; slide = NULL; break; @@ -420,9 +447,11 @@ int ncurses_display(deck_t *deck, int notrans, int nofade, int invert) { // free ncurses memory delwin(content); - delwin(stdscr); + if(reload == 0) + delwin(stdscr); - return 0; + // return reload indicator (0 means no reload) + return reload; } void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors) { -- 2.20.1