Re: [hackers] [dmenu][RFC][PATCH] History functionality

2015-12-09 Thread Roberto E. Vargas Caballero
On Wed, Dec 09, 2015 at 10:31:09AM +0100, Silvan Jegen wrote:
> I realized that I am not dealing with the case that the history file
> does not exist already. I added a simple check for that (although I
> was considering just putting in a comment saying that it has to).
> 

> +if [ ! -e $historyfile ]; then
> +   touch $historyfile
> +fi

Why the if?, why not directly touch the file?

Regards,




Re: [hackers] [dmenu][RFC][PATCH] History functionality

2015-12-09 Thread Silvan Jegen
On Tue, Dec 8, 2015 at 8:34 PM, Silvan Jegen  wrote:
> Heyhey
>
> On Thu, Dec 03, 2015 at 02:57:52AM -0800, Xarchus wrote:
>> [...]
>>
>> - improved the history/cache parsing/de-duplication awk one-liner in
>>   dmenu_path; the former 'NR==FNR' test was not enough: in case of a not
>> supplied or empty history file it attempted to remove a count followed by a
>> tab from the file names in the cache. Obviously to find such an occurrence
>> would be crazy rare, so it was quite harmless (I suppose that you don't
>> have any executables in your path of the form "count\tname", do you ?
>> Anyway, now you can :) )
>>
>> New patch attached.
>
> I have tested it briefly and it works for me. I would remove all the
> checks as done in the attached version of the patch.
>
> If you are happy with this version we should put it on the website. The
> question is whether we should replace the current version that does not
> apply to tip or just add it as an alternate history patch.


I realized that I am not dealing with the case that the history file
does not exist already. I added a simple check for that (although I
was considering just putting in a comment saying that it has to).

I also missed the opportunity to remove file checks from the awk code.

Find a new patch containing these changes attached.
diff --git a/dmenu_path b/dmenu_path
old mode 100644
new mode 100755
index 338bac4..c928173
--- a/dmenu_path
+++ b/dmenu_path
@@ -1,4 +1,6 @@
 #!/bin/sh
+HISTORY="$1"
+
 cachedir=${XDG_CACHE_HOME:-"$HOME/.cache"}
 if [ -d "$cachedir" ]; then
 	cache=$cachedir/dmenu_run
@@ -7,7 +9,6 @@ else
 fi
 IFS=:
 if stest -dqr -n "$cache" $PATH; then
-	stest -flx $PATH | sort -u | tee "$cache"
-else
-	cat "$cache"
+	stest -flx $PATH | sort -u > "$cache"
 fi
+awk '!cache { sub("^[0-9]+\t","") } !x[$0]++' "$HISTORY" cache=1 "$cache"
diff --git a/dmenu_run b/dmenu_run
index 834ede5..ef7c579 100755
--- a/dmenu_run
+++ b/dmenu_run
@@ -1,2 +1,31 @@
 #!/bin/sh
-dmenu_path | dmenu "$@" | ${SHELL:-"/bin/sh"} &
+
+historyfile=~/.cache/dmenu/history
+if [ ! -e $historyfile ]; then
+   touch $historyfile
+fi
+
+dmenu_path $historyfile | dmenu "$@" \
+	| awk -v histfile=$historyfile '
+		BEGIN {
+			FS=OFS="\t"
+			while ( (getline < histfile) > 0 ) {
+count=$1
+sub("^[0-9]+\t","")
+fname=$0
+history[fname]=count
+			}
+			close(histfile)
+		}
+
+		{
+			history[$0]++
+			print
+		}
+
+		END {
+			for (f in history)
+print history[f],f | "sort -t '\t' -k1rn >" histfile
+		}
+	' \
+	| while read cmd; do ${SHELL:-"/bin/sh"} -c "$cmd" & done


Re: [hackers] [PATCH] [sent] Quick patch to replace png with farbfeld

2015-12-09 Thread Dimitris Papastamos
On Wed, Dec 09, 2015 at 12:37:00AM +0100, Markus Teich wrote:
> Heyho,
> 
> Dimitris Papastamos wrote:
> > I would be inclined to keep regex matching.  2ff is a helper.  One can 
> > imagine
> > piping gnuplot files to generate graphs.
> 
> But then we have to adapt it to use `sh` to exec the bin from the filter 
> array.
> Otherwise we cannot use pipelines like `gnuplot bla | 2ff`. Or how would you 
> do
> that?
> 
> As I see 2ff, it is a helper which converts any input file to a farbfeld file.
> This is the exact thing sent needs, so I would rather build the gnuplot 
> pipeline
> into 2ff script. The other way arround if you could use gnuplot files in sent,
> but not with 2ff would exclude users who may want to generate a farbfeld 
> diagram
> not used in a presentation.

Hm, yes I guess that makes sense then.



Re: [hackers] [dmenu][RFC][PATCH] History functionality

2015-12-09 Thread Silvan Jegen
On Wed, Dec 9, 2015 at 11:12 AM, Roberto E. Vargas Caballero
 wrote:
> On Wed, Dec 09, 2015 at 10:31:09AM +0100, Silvan Jegen wrote:
>> I realized that I am not dealing with the case that the history file
>> does not exist already. I added a simple check for that (although I
>> was considering just putting in a comment saying that it has to).
>>
>
>> +if [ ! -e $historyfile ]; then
>> +   touch $historyfile
>> +fi
>
> Why the if?, why not directly touch the file?

You are right. I confused the history file with the cache file. The
cache file's time stamp is compared to the directories in the PATH to
determine if it has to be updated or not.

Removing the if is even simpler.



[hackers] [sent] [PATCH 1/2] filter via shell pipeline

2015-12-09 Thread Grant Mathews
Instead of requiring an executable, allow building arbitrary shell
pipelines to filter filetypes through.
---
 sent.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/sent.c b/sent.c
index fc5e389..99361e8 100644
--- a/sent.c
+++ b/sent.c
@@ -152,8 +152,8 @@ filter(int fd, const char *cmd)
dup2(fds[1], 1);
close(fds[0]);
close(fds[1]);
-   execlp(cmd, cmd, (char *)0);
-   eprintf("execlp %s:", cmd);
+   execlp("sh", "sh", "-c", cmd, (char *)0);
+   eprintf("execlp sh -c '%s':", cmd);
}
close(fds[1]);
return fds[0];
-- 
2.4.10




[hackers] [sent] [PATCH 2/2] replace farbfeld with libnetpbm

2015-12-09 Thread Grant Mathews
Since the PNM/PAM format already exist as a minimal intermediate
representation with a rich set of commandline tools to manipulate them,
use Netpbm to handle images.
---
 README.md|   8 ++---
 config.def.h |   5 +--
 config.mk|   2 +-
 example  |   2 +-
 sent.c   | 105 ++-
 5 files changed, 62 insertions(+), 60 deletions(-)

diff --git a/README.md b/README.md
index 3fda5ee..43ba423 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,7 @@
 sent is a simple plaintext presentation tool.
 
 sent does not need latex, libreoffice or any other fancy file format, it uses
-plaintext files to describe the slides and can include images via farbfeld.
+plaintext files to describe the slides and can include images via libnetpbm.
 Every paragraph represents a slide in the presentation.
 
 The presentation is displayed in a simple X11 window. The content of each slide
@@ -11,8 +11,8 @@ worry about alignment. Instead you can really concentrate on 
the content.
 
 Dependencies
 
-You need Xlib to build sent and the farbfeld[0] tools installed to use images 
in
-your presentations.
+You need Xlib and libnetpbm to build sent and the Netpbm tools installed to use
+images in your presentations.
 
 Demo
 
@@ -53,5 +53,3 @@ Development
 
 sent is developed at http://tools.suckless.org/sent
 
-
-0: http://git.2f30.org/farbfeld/about/
diff --git a/config.def.h b/config.def.h
index 94ed09e..3a4f1d8 100644
--- a/config.def.h
+++ b/config.def.h
@@ -47,6 +47,7 @@ static Shortcut shortcuts[] = {
 };
 
 static Filter filters[] = {
-   { "\\.png$",   "png2ff" },
-   { "\\.(jpg|gif)$", "2ff" },
+   { "\\.png$",   "pngtopam -alphapam" },
+   { "\\.(jpg|gif)$", "anytopnm" },
+   { "\\.p[bgp]m$",   "cat" },
 };
diff --git a/config.mk b/config.mk
index 52d5fb1..78055bd 100644
--- a/config.mk
+++ b/config.mk
@@ -12,7 +12,7 @@ X11LIB = /usr/X11R6/lib
 
 # includes and libs
 INCS = -I. -I/usr/include -I/usr/include/freetype2 -I${X11INC}
-LIBS = -L/usr/lib -lc -lm -L${X11LIB} -lXft -lfontconfig -lX11
+LIBS = -L/usr/lib -lc -lm -L${X11LIB} -lXft -lfontconfig -lX11 -lnetpbm
 
 # flags
 CPPFLAGS = -DVERSION=\"${VERSION}\" -D_XOPEN_SOURCE=600
diff --git a/example b/example
index 8b385f0..f7f6f61 100644
--- a/example
+++ b/example
@@ -20,7 +20,7 @@ easy to use
 
 depends on
 ♽ Xlib
-☃ farbfeld
+☃ libnetpbm
 
 ~1000 lines of code
 
diff --git a/sent.c b/sent.c
index 99361e8..978e3ef 100644
--- a/sent.c
+++ b/sent.c
@@ -12,6 +12,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -41,7 +42,7 @@ typedef struct {
unsigned int bufwidth, bufheight;
imgstate state;
XImage *ximg;
-   int fd;
+   struct pam pamimg;
int numpasses;
 } Image;
 
@@ -87,12 +88,12 @@ typedef struct {
const Arg arg;
 } Shortcut;
 
-static Image *ffopen(char *filename);
-static void fffree(Image *img);
-static int ffread(Image *img);
-static int ffprepare(Image *img);
-static void ffscale(Image *img);
-static void ffdraw(Image *img);
+static Image *pamopen(char *filename);
+static void pamfree(Image *img);
+static int pamread(Image *img);
+static int pamprepare(Image *img);
+static void pamscale(Image *img);
+static void pamdraw(Image *img);
 
 static void getfontsize(Slide *s, unsigned int *width, unsigned int *height);
 static void cleanup();
@@ -159,9 +160,9 @@ filter(int fd, const char *cmd)
return fds[0];
 }
 
-Image *ffopen(char *filename)
+Image *pamopen(char *filename)
 {
-   unsigned char hdr[16];
+   struct pam inpam;
char *bin = NULL;
regex_t regex;
Image *img;
@@ -192,21 +193,17 @@ Image *ffopen(char *filename)
eprintf("Unable to filter %s:", filename);
close(tmpfd);
 
-   if (read(fd, hdr, 16) != 16)
-   return NULL;
-
-   if (memcmp("farbfeld", hdr, 8))
-   return NULL;
+   pnm_readpaminit(fdopen(fd, "r"), , PAM_STRUCT_SIZE(tuple_type));
 
img = calloc(1, sizeof(Image));
-   img->fd = fd;
-   img->bufwidth = ntohl(*(uint32_t *)[8]);
-   img->bufheight = ntohl(*(uint32_t *)[12]);
+   img->pamimg = inpam;
+   img->bufwidth = inpam.width;
+   img->bufheight = inpam.height;
 
return img;
 }
 
-void fffree(Image *img)
+void pamfree(Image *img)
 {
free(img->buf);
if (img->ximg)
@@ -214,14 +211,13 @@ void fffree(Image *img)
free(img);
 }
 
-int ffread(Image *img)
+int pamread(Image *img)
 {
-   uint32_t y, x;
-   uint16_t *row;
+   uint32_t y, x, depth;
uint8_t opac;
uint8_t fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
-   size_t rowlen, off, nbytes;
-   ssize_t count;
+   tuple *pamrow;
+   size_t off;
 
if (!img)
return 0;
@@ -236,9 +232,8 @@ int ffread(Image *img)
return 0;
 
/* scratch buffer to read row by row */
-   rowlen = img->bufwidth * 2 *