Add SSH terminal colorization

This commit is contained in:
Dimitri Sokolyuk 2015-01-29 19:37:22 +01:00
parent 976d77c76d
commit bcc4f43932
6 changed files with 96 additions and 5 deletions

View File

@ -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`

View File

@ -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_ */

67
colorize.c Normal file
View File

@ -0,0 +1,67 @@
/* $Id$ */
/*
* Copyright (c) 2015 Dimitri Sokolyuk <demon@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;
}

View File

@ -131,6 +131,9 @@ Set the color of the border of a window indicating urgency.
.It Ic color ungroupborder Ar color
Set the color of the border while ungrouping a window.
.Pp
.It Ic colorize Ic yes Ns \&| Ns Ic no
Colorize SSH terminals with hashed colors.
.Pp
.It Ic command Ar name path
Every
.Ar name

View File

@ -326,6 +326,8 @@ 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 = 0;
char colorarg[32];
int l;
size_t len;
@ -371,8 +373,15 @@ 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(colorarg, sizeof(colorarg),
" -fg #%.6x -bg #%.6x", tint(color), shade(color));
if (l == -1 || l >= sizeof(colorarg))
goto out;
}
l = snprintf(path, sizeof(path), "%s -T '[ssh] %s'%s -e ssh %s",
cmd->path, mi->text, color ? colorarg : "", mi->text);
if (l == -1 || l >= sizeof(path))
goto out;
u_spawn(path);

View File

@ -70,7 +70,7 @@ typedef struct {
%}
%token FONTNAME STICKY GAP MOUSEBIND
%token FONTNAME STICKY COLORIZE GAP MOUSEBIND
%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},