Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: PATCH: curses tweaks, maybe



On Sat, Oct 20, 2007 at 01:12:46PM +0100, Peter Stephenson wrote:
> If I get a chance I may look at turning the options into names in a
> table as suggested by Bart.  This will also give us a way of doing
> better argument length and other checking.

This needs the checking bit and a doc update.

zcurses init
zcurses addwin tw $(( LINES - 10 )) $(( COLUMNS - 20 )) 5 10
zcurses border tw
zcurses move tw 1 1
zcurses c tw B
zcurses c tw l
zcurses c tw a
zcurses c tw h
zcurses refresh tw
zcurses move tw 2 2
zcurses s tw String
zcurses move tw 3 3
zcurses attr tw +bold +underline
zcurses s tw BoLD
zcurses move tw 4 4
zcurses attr tw -bold -underline
zcurses color tw green/black
zcurses s tw Green
zcurses refresh tw
sleep 5
zcurses delwin tw
zcurses endwin

Index: Src/Modules/curses.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Modules/curses.c,v
retrieving revision 1.14
diff -u -r1.14 curses.c
--- Src/Modules/curses.c	20 Oct 2007 12:15:17 -0000	1.14
+++ Src/Modules/curses.c	20 Oct 2007 13:33:44 -0000
@@ -60,6 +60,12 @@
 };
 typedef struct colorpairnode *Colorpairnode;
 
+struct zcurses_subcommand {
+    struct zcurses_namenumberpair nn;
+    int minargs;
+    int maxargs;
+};
+
 static WINDOW *win_zero;
 static struct ttyinfo saved_tty_state;
 static struct ttyinfo curses_tty_state;
@@ -76,6 +82,18 @@
 #define ZCURSES_ATTRON 1
 #define ZCURSES_ATTROFF 2
 
+#define ZCURSES_SC_INIT 1
+#define ZCURSES_SC_ADDWIN 2
+#define ZCURSES_SC_DELWIN 3
+#define ZCURSES_SC_REFRESH 4
+#define ZCURSES_SC_MOVE 5
+#define ZCURSES_SC_CHAR 6
+#define ZCURSES_SC_STRING 7
+#define ZCURSES_SC_BORDER 8
+#define ZCURSES_SC_ENDWIN 9
+#define ZCURSES_SC_ATTR 10
+#define ZCURSES_SC_COLOR 11
+
 static int zc_errno, zc_color_phase=0;
 static short next_cp=0;
 
@@ -261,8 +279,42 @@
 static int
 bin_zcurses(char *nam, char **args, Options ops, UNUSED(int func))
 {
+    char **saargs;
+    struct zcurses_subcommand *zcsc;
+    int sc;
+
+    struct zcurses_subcommand scs[] = {
+	{{"init", ZCURSES_SC_INIT}, 0, 0},
+	{{"addwin", ZCURSES_SC_ADDWIN}, 5, 5},
+	{{"delwin", ZCURSES_SC_DELWIN}, 1, 1},
+	{{"refresh", ZCURSES_SC_REFRESH}, 0, 1},
+	{{"move", ZCURSES_SC_MOVE}, 3, 3},
+	{{"c", ZCURSES_SC_CHAR}, 2, 2},
+	{{"s", ZCURSES_SC_STRING}, 2, 2},
+	{{"border", ZCURSES_SC_BORDER}, 1, 5},
+	{{"endwin", ZCURSES_SC_ENDWIN}, 1, 1},
+	{{"attr", ZCURSES_SC_ATTR}, 2, 2},
+	{{"color", ZCURSES_SC_COLOR}, 2, 2},
+	{{NULL, -1}, 0, 0}
+    };
+
+    for(zcsc = scs; zcsc->nn.name; zcsc++) {
+	if(!strcmp(args[0], zcsc->nn.name))
+	    break;
+    }
+
+    sc = zcsc->nn.number;
+
+    if (sc == -1) {
+	zwarnnam(nam, "unknown subcommand: %s", args[0], 0);
+	return 1;
+    }
+
+    /* here would be a good place to validate number of args */
+    saargs = args + 1;
+
     /* Initialise curses */
-    if (OPT_ISSET(ops,'i')) {
+    if (sc == ZCURSES_SC_INIT) {
 	if (!win_zero) {
 	    gettyinfo(&saved_tty_state);
 	    win_zero = initscr();
@@ -272,8 +324,8 @@
 		zcurses_colorpairs = newhashtable(8, "zc_colorpairs", NULL);
 
 		zcurses_colorpairs->hash        = hasher;
-	        zcurses_colorpairs->emptytable  = emptyhashtable;
-	        zcurses_colorpairs->filltable   = NULL;
+		zcurses_colorpairs->emptytable  = emptyhashtable;
+		zcurses_colorpairs->filltable   = NULL;
 		zcurses_colorpairs->cmpnodes    = strcmp;
 		zcurses_colorpairs->addnode     = addhashnode;
 		zcurses_colorpairs->getnode     = gethashnode2;
@@ -290,27 +342,26 @@
 	    settyinfo(&curses_tty_state);
 	}
 	return 0;
-    }
-
-    if (OPT_ISSET(ops,'a')) {
+    } else
+    if (sc == ZCURSES_SC_ADDWIN) {
 	int nlines, ncols, begin_y, begin_x;
 	ZCWin w;
 
-	if (zcurses_validate_window(args[0], ZCURSES_UNUSED) == NULL && zc_errno) {
-	    zerrnam(nam, "%s: %s", zcurses_strerror(zc_errno), args[0], 0);
+	if (zcurses_validate_window(saargs[0], ZCURSES_UNUSED) == NULL && zc_errno) {
+	    zerrnam(nam, "%s: %s", zcurses_strerror(zc_errno), saargs[0], 0);
 	    return 1;
 	}
 
-	nlines = atoi(args[1]);
-	ncols = atoi(args[2]);
-	begin_y = atoi(args[3]);
-	begin_x = atoi(args[4]);
+	nlines = atoi(saargs[1]);
+	ncols = atoi(saargs[2]);
+	begin_y = atoi(saargs[3]);
+	begin_x = atoi(saargs[4]);
 
 	w = (ZCWin)zshcalloc(sizeof(struct zc_win));
 	if (!w)
 	    return 1;
 
-	w->name = ztrdup(args[0]);
+	w->name = ztrdup(saargs[0]);
 	w->win = newwin(nlines, ncols, begin_y, begin_x);
 
 	if (w->win == NULL) {
@@ -322,22 +373,21 @@
 	zinsertlinknode(zcurses_windows, lastnode(zcurses_windows), (void *)w);
 
 	return 0;
-    }
-
-    if (OPT_ISSET(ops,'d')) {
+    } else
+    if (sc == ZCURSES_SC_DELWIN) {
 	LinkNode node;
 	ZCWin w;
 
-	node = zcurses_validate_window(OPT_ARG(ops,'d'), ZCURSES_USED);
+	node = zcurses_validate_window(saargs[0], ZCURSES_USED);
 	if (node == NULL) {
-	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), OPT_ARG(ops,'d'), 0);
+	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), saargs[0], 0);
 	    return 1;
 	}
 
 	w = (ZCWin)getdata(node);
 
 	if (w == NULL) {
-	    zwarnnam(nam, "record for window `%s' is corrupt", OPT_ARG(ops, 'd'), 0);
+	    zwarnnam(nam, "record for window `%s' is corrupt", saargs[0], 0);
 	    return 1;
 	}
 	if (delwin(w->win)!=OK)
@@ -349,16 +399,15 @@
 	zfree((ZCWin)remnode(zcurses_windows, node), sizeof(struct zc_win));
 
 	return 0;
-    }
-
-    if (OPT_ISSET(ops,'r')) {
-	if (args[0]) {
+    } else
+    if (sc == ZCURSES_SC_REFRESH) {
+	if (saargs[0]) {
 	    LinkNode node;
 	    ZCWin w;
 
-	    node = zcurses_validate_window(args[0], ZCURSES_USED);
+	    node = zcurses_validate_window(saargs[0], ZCURSES_USED);
 	    if (node == NULL) {
-		zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), args[0],
+		zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), saargs[0],
 			0);
 		return 1;
 	    }
@@ -371,21 +420,20 @@
 	{
 	    return (refresh() != OK) ? 1 : 0;
 	}
-    }
-
-    if (OPT_ISSET(ops,'m')) {
+    } else
+    if (sc == ZCURSES_SC_MOVE) {
 	int y, x;
 	LinkNode node;
 	ZCWin w;
 
-	node = zcurses_validate_window(args[0], ZCURSES_USED);
+	node = zcurses_validate_window(saargs[0], ZCURSES_USED);
 	if (node == NULL) {
-	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), args[0], 0);
+	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), saargs[0], 0);
 	    return 1;
 	}
 
-	y = atoi(args[1]);
-	x = atoi(args[2]);
+	y = atoi(saargs[1]);
+	x = atoi(saargs[2]);
 
 	w = (ZCWin)getdata(node);
 
@@ -393,9 +441,8 @@
 	    return 1;
 
 	return 0;
-    }
-
-    if (OPT_ISSET(ops,'c')) {
+    } else
+    if (sc == ZCURSES_SC_CHAR) {
 	LinkNode node;
 	ZCWin w;
 #ifdef HAVE_SETCCHAR
@@ -403,16 +450,16 @@
 	cchar_t cc;
 #endif
 
-	node = zcurses_validate_window(args[0], ZCURSES_USED);
+	node = zcurses_validate_window(saargs[0], ZCURSES_USED);
 	if (node == NULL) {
-	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), args[0], 0);
+	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), saargs[0], 0);
 	    return 1;
 	}
 
 	w = (ZCWin)getdata(node);
 
 #ifdef HAVE_SETCCHAR
-	if (mbrtowc(&c, args[1], MB_CUR_MAX, NULL) < 1)
+	if (mbrtowc(&c, saargs[1], MB_CUR_MAX, NULL) < 1)
 	    return 1;
 
 	if (setcchar(&cc, &c, A_NORMAL, 0, NULL)==ERR)
@@ -421,14 +468,13 @@
 	if (wadd_wch(w->win, &cc)!=OK)
 	    return 1;
 #else
-	if (waddch(w->win, (chtype)args[1][0])!=OK)
+	if (waddch(w->win, (chtype)saargs[1][0])!=OK)
 	    return 1;
 #endif
 
 	return 0;
-    }
-
-    if (OPT_ISSET(ops,'s')) {
+    } else
+    if (sc == ZCURSES_SC_STRING) {
 	LinkNode node;
 	ZCWin w;
 
@@ -436,10 +482,10 @@
 	int clen;
 	wint_t wc;
 	wchar_t *wstr, *wptr;
-	char *str = args[1];
+	char *str = saargs[1];
 #endif
 
-	node = zcurses_validate_window(args[0], ZCURSES_USED);
+	node = zcurses_validate_window(saargs[0], ZCURSES_USED);
 	if (node == NULL) {
 	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), args[0], 0);
 	    return 1;
@@ -462,19 +508,18 @@
 	    return 1;
 	}
 #else
-	if (waddstr(w->win, args[1])!=OK)
+	if (waddstr(w->win, saargs[1])!=OK)
 	    return 1;
 #endif
 	return 0;
-    }
-
-    if (OPT_ISSET(ops,'b')) {
+    } else
+    if (sc == ZCURSES_SC_BORDER) {
 	LinkNode node;
 	ZCWin w;
 
-	node = zcurses_validate_window(OPT_ARG(ops,'b'), ZCURSES_USED);
+	node = zcurses_validate_window(saargs[0], ZCURSES_USED);
 	if (node == NULL) {
-	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), OPT_ARG(ops,'b'), 0);
+	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), saargs[0], 0);
 	    return 1;
 	}
 
@@ -484,10 +529,9 @@
 	    return 1;
 
 	return 0;
-    }
-
+    } else
     /* Finish using curses */
-    if (OPT_ISSET(ops,'e')) {
+    if (sc == ZCURSES_SC_ENDWIN) {
 	if (win_zero) {
 	    endwin();
 	    /* Restore TTY as it was before zcurses -i */
@@ -500,24 +544,24 @@
 	    gettyinfo(&shttyinfo);
 	}
 	return 0;
-    }
-    if (OPT_ISSET(ops,'A')) {
+    } else
+    if (sc == ZCURSES_SC_ATTR) {
 	LinkNode node;
 	ZCWin w;
 	char **attrs;
 
-	if (!args[0])
+	if (!saargs[0])
 	    return 1;
 
-	node = zcurses_validate_window(args[0], ZCURSES_USED);
+	node = zcurses_validate_window(saargs[0], ZCURSES_USED);
 	if (node == NULL) {
-	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), args[0], 0);
+	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), saargs[0], 0);
 	    return 1;
 	}
 
 	w = (ZCWin)getdata(node);
 
-	for(attrs = args+1; *attrs; attrs++) {
+	for(attrs = saargs+1; *attrs; attrs++) {
 	    switch(*attrs[0]) {
 		case '-':
 		    zcurses_attribute(w->win, (*attrs)+1, ZCURSES_ATTROFF);
@@ -531,23 +575,23 @@
 	    }
 	}
 	return 0;
-    }
-    if (OPT_ISSET(ops,'C')) {
+    } else
+    if (sc == ZCURSES_SC_COLOR) {
 	LinkNode node;
 	ZCWin w;
 
-	if (!args[0] || !args[1] || !zc_color_phase)
+	if (!saargs[0] || !saargs[1] || !zc_color_phase)
 	    return 1;
 
-	node = zcurses_validate_window(args[0], ZCURSES_USED);
+	node = zcurses_validate_window(saargs[0], ZCURSES_USED);
 	if (node == NULL) {
-	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), args[0], 0);
+	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), saargs[0], 0);
 	    return 1;
 	}
 
 	w = (ZCWin)getdata(node);
 
-	return zcurses_colorset(w->win, args[1]);
+	return zcurses_colorset(w->win, saargs[1]);
     }
 
     return 0;
@@ -558,7 +602,7 @@
  */
 
 static struct builtin bintab[] = {
-    BUILTIN("zcurses", 0, bin_zcurses, 0, 5, 0, "Aab:Ccd:eimrs", NULL),
+    BUILTIN("zcurses", 0, bin_zcurses, 1, 6, 0, "", NULL),
 };
 
 static struct features module_features = {



Messages sorted by: Reverse Date, Date, Thread, Author