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

Re: Calling a zle widget from a function



On Tue 12.Jun.07 16:14, Peter Stephenson wrote:
If you have version 4.3.4 the sched mechanism will call functions
asynchronously within zle, if that's running.  So something like

 clear-zle-screen() {
   zle && zle clear-screen
   sched +60 clear-zle-screen
 }
 sched +60 clear-zle-screen

does the basics.  However, it will attempt to clear the screen *every*
60 seconds, not just after 60 seconds of idleness.  One possible way
around that is to look at the modification time for the terminal and compare
it with the current time:

 clear-zle-screen() {
   zmodload -i zsh/stat
   zmodload -i zsh/datetime
   local -a mtime
   integer diff

   stat -A mtime +mtime $TTY
   (( diff = EPOCHSECONDS - ${mtime} ))
   if (( diff > 60 )); then
     zle && zle clear-screen
     sched +60 clear-zle-screen
   else
     sched +$(( 60 - diff )) clear-zle-screen
   fi
 }

Amazing, thanks a lot, Peter!

Here's the (maybe) final setup:

## $fpath[1]/clear-zle-screen
zmodload -i zsh/stat
zmodload -i zsh/datetime

clear-zle-screen() {
   local -a mtime
   integer diff

   stat -A mtime +mtime $TTY
   (( diff = EPOCHSECONDS - ${mtime} ))
   if (( diff >= CLRTMOUT )); then
	zle && zle clear-screen
   # function called from precmd(), to avoid multiple clearouts
   #    sched +$CLRTMOUT clear-zle-screen
   else
	sched +$(( CLRTMOUT - diff )) clear-zle-screen
   fi
}

functions[precmd]+="; sched +$CLRTMOUT clear-zle-screen"
clear-zle-screen

## zshrc
## Clear the screen after CLRTMOUT seconds of inactivity
if [[ $ZSH_VERSION == 4.3.<4-> ]]; then
   CLRTMOUT=3600
   autoload clear-zle-screen
   clear-zle-screen
fi

Now, for every prompt that is printed I get a new sched job queued. This might get as big as 3600 or more. Do you think that is going to be a serious performance penalty on my daily zsh routine?

Cheers; and, again, thank you.

--
redondos

Attachment: pgpR4kiRIIJtu.pgp
Description: PGP signature



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