Hello

I would like to propose a simple, but imho really useful hack for cwm.
It boils down to colorization of remote terminals (Meta-Dot command)
based on crc24 of a hostname.
This way each remote connection gets its own color, such that different
hosts are easier to distinguish.
On the same time, different sessions to the same host gets always same
color, which improves visual feedback.

wbr

-- 
Dimitri Sokolyuk -- http://www.dim13.org/

diff --git a/Makefile b/Makefile
index 4c34ee4..020724f 100644
--- a/Makefile
+++ b/Makefile
@@ -7,12 +7,12 @@ PREFIX?=	/usr/local
 
 SRCS=		calmwm.c screen.c xmalloc.c client.c menu.c \
 		search.c util.c xutil.c conf.c xevents.c group.c \
-		kbfunc.c mousefunc.c parse.y
+		kbfunc.c mousefunc.c parse.y colorize.c
 
 OBJS=		calmwm.o screen.o xmalloc.o client.o menu.o \
 		search.o util.o xutil.o conf.o xevents.o group.o \
 		kbfunc.o mousefunc.o strlcpy.o strlcat.o y.tab.o \
-		strtonum.o fgetln.o
+		strtonum.o fgetln.o colorize.o
 
 CPPFLAGS+=	`pkg-config --cflags fontconfig x11 xft xinerama xrandr`
 
diff --git a/calmwm.h b/calmwm.h
index b56a9d7..40f1fb1 100644
--- a/calmwm.h
+++ b/calmwm.h
@@ -302,6 +302,7 @@ struct conf {
 	struct ignore_q		 ignoreq;
 	struct cmd_q		 cmdq;
 #define	CONF_STICKY_GROUPS		0x0001
+#define	CONF_COLORIZE_SSH		0x0002
 	int			 flags;
 #define CONF_BWIDTH			1
 	int			 bwidth;
@@ -583,4 +584,8 @@ int			 xasprintf(char **, const char *, ...)
 			    __attribute__((__format__ (printf, 2, 3)))
 			    __attribute__((__nonnull__ (2)));
 
+long			 crc24(char *);
+long			 tint(long);
+long			 shade(long);
+
 #endif /* _CALMWM_H_ */
diff --git a/colorize.c b/colorize.c
new file mode 100644
index 0000000..c60d75b
--- /dev/null
+++ b/colorize.c
@@ -0,0 +1,67 @@
+/* $Id$ */
+/*
+ * Copyright (c) 2015 Dimitri Sokolyuk <de...@dim13.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "calmwm.h"
+
+#define CRC24_INIT	0x0B704CEL
+#define CRC24_POLY	0x1864CFBL
+
+long
+crc24(char *s)
+{
+	long crc;
+	int i;
+
+	for (crc = CRC24_INIT; *s; s++) {
+		crc ^= *s << 0x10;
+		for (i = 0; i < 8; i++) {
+			crc <<= 1;
+			if (crc & 0x1000000)
+				crc ^= CRC24_POLY;
+		}
+	}
+
+	return crc;
+}
+
+long
+shade(long c)
+{
+	unsigned char r = c >> 0x10;
+	unsigned char g = c >> 0x08;
+	unsigned char b = c;
+
+	r >>= 2;
+	g >>= 2;
+	b >>= 2;
+
+	return (r << 0x10) | (g << 0x8) | b;
+}
+
+long
+tint(long c)
+{
+	unsigned char r = c >> 0x10;
+	unsigned char g = c >> 0x08;
+	unsigned char b = c;
+
+	r += (UCHAR_MAX - r) >> 1;
+	g += (UCHAR_MAX - g) >> 1;
+	b += (UCHAR_MAX - b) >> 1;
+
+	return (r << 0x10) | (g << 0x8) | b;
+}
diff --git a/kbfunc.c b/kbfunc.c
index 8ad5b99..3a6c7f8 100644
--- a/kbfunc.c
+++ b/kbfunc.c
@@ -326,6 +326,7 @@ kbfunc_ssh(struct client_ctx *cc, union arg *arg)
 	char			*buf, *lbuf, *p;
 	char			 hostbuf[HOST_NAME_MAX+1];
 	char			 path[PATH_MAX];
+	long			 color;
 	int			 l;
 	size_t			 len;
 
@@ -371,8 +372,16 @@ kbfunc_ssh(struct client_ctx *cc, union arg *arg)
 	    search_match_exec, NULL)) != NULL) {
 		if (mi->text[0] == '\0')
 			goto out;
-		l = snprintf(path, sizeof(path), "%s -T '[ssh] %s' -e ssh %s",
-		    cmd->path, mi->text, mi->text);
+		if (Conf.flags & CONF_COLORIZE_SSH) {
+			color = crc24(mi->text);
+			l = snprintf(path, sizeof(path),
+			    "%s -T '[ssh] %s' -fg #%.6x -bg #%.6x -e ssh %s",
+			    cmd->path, mi->text, tint(color), shade(color),
+			    mi->text);
+		} else
+			l = snprintf(path, sizeof(path),
+			    "%s -T '[ssh] %s' -e ssh %s",
+			    cmd->path, mi->text, mi->text);
 		if (l == -1 || l >= sizeof(path))
 			goto out;
 		u_spawn(path);
diff --git a/parse.y b/parse.y
index eb8ed64..9922dc5 100644
--- a/parse.y
+++ b/parse.y
@@ -70,7 +70,7 @@ typedef struct {
 
 %}
 
-%token	FONTNAME STICKY GAP MOUSEBIND
+%token	FONTNAME STICKY GAP MOUSEBIND COLORIZE
 %token	AUTOGROUP BIND COMMAND IGNORE
 %token	YES NO BORDERWIDTH MOVEAMOUNT
 %token	COLOR SNAPDIST
@@ -119,6 +119,12 @@ main		: FONTNAME STRING		{
 			else
 				conf->flags |= CONF_STICKY_GROUPS;
 		}
+		| COLORIZE yesno {
+			if ($2 == 0)
+				conf->flags &= ~CONF_COLORIZE_SSH;
+			else
+				conf->flags |= CONF_COLORIZE_SSH;
+		}
 		| BORDERWIDTH NUMBER {
 			if ($2 < 0 || $2 > UINT_MAX) {
 				yyerror("invalid borderwidth: %lld", $2);
@@ -276,6 +282,7 @@ lookup(char *s)
 		{ "bind",		BIND},
 		{ "borderwidth",	BORDERWIDTH},
 		{ "color",		COLOR},
+		{ "colorize",		COLORIZE},
 		{ "command",		COMMAND},
 		{ "font",		FONTCOLOR},
 		{ "fontname",		FONTNAME},

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to