[hackers] [sent] allow to reload file || Markus Teich

From: <git_AT_suckless.org>
Date: Sat, 2 Sep 2017 19:30:18 +0200 (CEST)

commit 16e4843d0563c1b3aea1574adde8f91ed5de5db3
Author: Markus Teich <markus.teich_AT_stusta.mhn.de>
AuthorDate: Sat Sep 2 19:30:11 2017 +0200
Commit: Markus Teich <markus.teich_AT_stusta.mhn.de>
CommitDate: Sat Sep 2 19:30:11 2017 +0200

    allow to reload file

diff --git a/config.def.h b/config.def.h
index f66d121..60eb376 100644
--- a/config.def.h
+++ b/config.def.h
_AT_@ -46,6 +46,7 @@ static Shortcut shortcuts[] = {
         { XK_Prior, advance, {.i = -1} },
         { XK_n, advance, {.i = +1} },
         { XK_p, advance, {.i = -1} },
+ { XK_r, reload, {0} },
 };
 
 static Filter filters[] = {
diff --git a/sent.1 b/sent.1
index 7f6927d..fabc614 100644
--- a/sent.1
+++ b/sent.1
_AT_@ -35,6 +35,8 @@ Go to previous slide, if existent.
 .Bl -tag -width Ds
 .It Sy Escape | q
 Quit.
+.It Sy r
+Reload the slides. Only works on file input.
 .It Sy Right | Return | Space | l | j | Down | Next | n
 Go to next slide, if existent.
 .It Sy Left | Backspace | h | k | Up | Prior | p
diff --git a/sent.c b/sent.c
index 6cf53ff..0da2bff 100644
--- a/sent.c
+++ b/sent.c
_AT_@ -93,7 +93,8 @@ static void ffscale(Image *img);
 static void ffdraw(Image *img);
 
 static void getfontsize(Slide *s, unsigned int *width, unsigned int *height);
-static void cleanup();
+static void cleanup(int slidesonly);
+static void reload(const Arg *arg);
 static void load(FILE *fp);
 static void advance(const Arg *arg);
 static void quit(const Arg *arg);
_AT_@ -115,6 +116,7 @@ static void configure(XEvent *);
 #include "config.h"
 
 /* Globals */
+static const char *fname = NULL;
 static Slide *slides = NULL;
 static int idx = 0;
 static int slidecount = 0;
_AT_@ -346,18 +348,21 @@ getfontsize(Slide *s, unsigned int *width, unsigned int *height)
 }
 
 void
-cleanup()
+cleanup(int slidesonly)
 {
         unsigned int i, j;
 
- for (i = 0; i < NUMFONTSCALES; i++)
- drw_fontset_free(fonts[i]);
- free(sc);
- drw_free(d);
+ if (!slidesonly) {
+ for (i = 0; i < NUMFONTSCALES; i++)
+ drw_fontset_free(fonts[i]);
+ free(sc);
+ drw_free(d);
+
+ XDestroyWindow(xw.dpy, xw.win);
+ XSync(xw.dpy, False);
+ XCloseDisplay(xw.dpy);
+ }
 
- XDestroyWindow(xw.dpy, xw.win);
- XSync(xw.dpy, False);
- XCloseDisplay(xw.dpy);
         if (slides) {
                 for (i = 0; i < slidecount; i++) {
                         for (j = 0; j < slides[i].linecount; j++)
_AT_@ -366,12 +371,39 @@ cleanup()
                         if (slides[i].img)
                                 fffree(slides[i].img);
                 }
- free(slides);
- slides = NULL;
+ if (!slidesonly) {
+ free(slides);
+ slides = NULL;
+ }
         }
 }
 
 void
+reload(const Arg *arg)
+{
+ FILE *fp = NULL;
+ unsigned int i;
+
+ if (!fname) {
+ fprintf(stderr, "sent: Cannot reload from stdin. Use a file!\n");
+ return;
+ }
+
+ cleanup(1);
+ slidecount = 0;
+
+ if (!(fp = fopen(fname, "r")))
+ die("sent: Unable to open '%s' for reading:", fname);
+ load(fp);
+ fclose(fp);
+
+ LIMIT(idx, 0, slidecount-1);
+ for (i = 0; i < slidecount; i++)
+ ffload(&slides[i]);
+ xdraw();
+}
+
+void
 load(FILE *fp)
 {
         static size_t size = 0;
_AT_@ -658,9 +690,8 @@ main(int argc, char *argv[])
 
         if (!argv[0] || !strcmp(argv[0], "-"))
                 fp = stdin;
- else if (!(fp = fopen(argv[0], "r")))
- die("sent: Unable to open '%s' for reading:", argv[0]);
-
+ else if (!(fp = fopen(fname = argv[0], "r")))
+ die("sent: Unable to open '%s' for reading:", fname);
         load(fp);
         fclose(fp);
 
_AT_@ -670,6 +701,6 @@ main(int argc, char *argv[])
         xinit();
         run();
 
- cleanup();
+ cleanup(0);
         return 0;
 }
Received on Sat Sep 02 2017 - 19:30:18 CEST

This archive was generated by hypermail 2.3.0 : Sat Sep 02 2017 - 19:36:34 CEST