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

Re: PATCH: subwindows, touching, better refreshing



On Sun, 28 Oct 2007 19:30:39 +0000
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx> wrote:
> Use the function keys to move round a rectangle.  You get a warning
> and a second's delay if you hit the edge (input timeouts won't be hard
> since curses does the hard work and they're on my list).

They weren't, except I kept getting the function wrong.

curses_bang() {
  # Arrow keys to move, anything else to exit.
  zmodload zsh/curses
  
  local REPLY key newkey
  integer h=$(( LINES - 10 )) w=$((COLUMNS - 20))
  integer x=1 y=1
  
  bang() {
    zcurses addwin bang 1 5 $(( y + 5 )) $(( x + 10 ))
    zcurses attr bang red/green bold
    zcurses string bang 'BANG!'
    zcurses refresh bang
    zcurses timeout bang 1000
    zcurses input bang REPLY key
    zcurses delwin bang
    zcurses touch main
    zcurses refresh stdscr main
  }
  
  {
    zcurses init
  
    zcurses addwin main $(( LINES - 10 )) $(( COLUMNS - 20 )) 5 10
    zcurses border main
  
    zcurses move main $y $x
    zcurses refresh main
  
    while true; do
      if [[ -z $key ]]; then
        zcurses input main REPLY key
      fi
      newkey=$key
      key=    
      case $newkey in
        (UP)
        if (( y == 1 )); then
          bang
        else
          zcurses string main "^"
          (( y-- ))
          zcurses move main $y $x
          zcurses refresh main
        fi
        ;;
  
        (DOWN)
        if (( y == h - 2 )); then
          bang
        else
          zcurses string main "v"
          (( y++ ))
          zcurses move main $y $x
          zcurses refresh main
        fi
        ;;
  
        (LEFT)
        if (( x == 1 )); then
          bang
        else
          zcurses string main "<"
          (( x-- ))
          zcurses move main $y $x
          zcurses refresh main
        fi
        ;;
  
        (RIGHT)
        if (( x == w - 2 )); then
          bang
        else
          zcurses string main ">"
          (( x++ ))
          zcurses move main $y $x
          zcurses refresh main
        fi
        ;;
  
        ("")
        break
        ;;
      esac
    done
  
  } always {
    zcurses delwin main
    zcurses end
  }
}


Index: Doc/Zsh/mod_curses.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/mod_curses.yo,v
retrieving revision 1.16
diff -u -r1.16 mod_curses.yo
--- Doc/Zsh/mod_curses.yo	28 Oct 2007 19:52:40 -0000	1.16
+++ Doc/Zsh/mod_curses.yo	28 Oct 2007 20:23:35 -0000
@@ -23,7 +23,8 @@
 xitem(tt(zcurses) tt(border) var(targetwin) var(border) )(
 xitem(tt(zcurses) tt(attr) var(targetwin) [ var({+/-}attribute) | var(fg_col)tt(/)var(bg_col) ] [...])
 xitem(tt(zcurses) tt(scroll) [ tt(on) | tt(off) | {+/-}var(lines) ])
-item(tt(input) var(targetwin) [ var(param) [ var(kpparm) ] ])(
+xitem(tt(zcurses) tt(input) var(targetwin) [ var(param) [ var(kpparm) ] ])
+item(tt(zcurses) tt(timeout) var(targetwin) var(intval))(
 Manipulate curses windows.  All uses of this command should be
 bracketed by `tt(zcurses init)' to initialise use of curses, and
 `tt(zcurses end)' to end it; omitting `tt(zcurses end)' can cause
@@ -125,6 +126,14 @@
 var(param) as before.  On a succesful return only one of var(param) or
 var(kpparm) contains a non-empty string; the other is set to an empty
 string.
+
+tt(timeout) specifies a timeout value for input from var(targetwin).
+If var(intval) is negative, `tt(zcurses input)' waits indefinitely for
+a character to be typed; this is the default.  If var(intval) is zero,
+`tt(zcurses input)' returns immediately; if there is typeahead it is
+returned, else no input is done and status 1 is returned.  If var(intval)
+is positive, `tt(zcurses input)' waits var(intval) milliseconds for
+input and if there is none at the end of that period returns status 1.
 )
 enditem()
 
Index: Src/Modules/curses.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Modules/curses.c,v
retrieving revision 1.28
diff -u -r1.28 curses.c
--- Src/Modules/curses.c	28 Oct 2007 19:52:41 -0000	1.28
+++ Src/Modules/curses.c	28 Oct 2007 20:23:35 -0000
@@ -562,7 +562,7 @@
 
 
 static int
-zccmd_move(const char *nam, char **args)
+zccmd_move(const char *nam,  char **args)
 {
     int y, x;
     LinkNode node;
@@ -936,6 +936,33 @@
 
 
 static int
+zccmd_timeout(const char *nam, char **args)
+{
+    LinkNode node;
+    ZCWin w;
+    int to;
+    char *eptr;
+
+    node = zcurses_validate_window(args[0], ZCURSES_USED);
+    if (node == NULL) {
+	zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), args[0]);
+	return 1;
+    }
+
+    w = (ZCWin)getdata(node);
+
+    to = (int)zstrtol(args[1], &eptr, 10);
+    if (*eptr) {
+	zwarnnam(nam, "timeout requires an integer: %s", args[1]);
+	return 1;
+    }
+
+    wtimeout(w->win, to);
+    return 0;
+}
+
+
+static int
 zccmd_position(const char *nam, char **args)
 {
     LinkNode node;
@@ -1019,6 +1046,7 @@
 	{"attr", zccmd_attr, 2, -1},
 	{"scroll", zccmd_scroll, 2, 2},
 	{"input", zccmd_input, 1, 3},
+	{"timeout", zccmd_timeout, 2, 2},
 	{"touch", zccmd_touch, 1, -1},
 	{NULL, (zccmd_t)0, 0, 0}
     };


-- 
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/



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