zsh-users
 help / color / mirror / Atom feed
* Call a function when idle?
@ 2020-12-14 11:57 A. Wik
  2020-12-14 14:57 ` Mikael Magnusson
  0 siblings, 1 reply; 4+ messages in thread
From: A. Wik @ 2020-12-14 11:57 UTC (permalink / raw)
  To: zsh-users

Hi all,

For the most part, when I get started working at a terminal, I can do
just fine with a simple and short prompt like "% ".  A prompt that
includes the current directory often gets annoyingly long so that you
can rarely type a command line that doesn't scroll onto another line,
which is harder to read.

However, when I've been away from a terminal (window or tab) for some
time, I would like to know where in the file system I am, to remind me
(hopefully) of what I was doing.  If the current working directory is
not in the prompt, I have to type "pwd" manually.

To solve the problem, I was thinking of getting a function to run
after a specified period of idle time.  This function could then run
"print -P" to print useful pieces of data, such as the hostname, the
tty name, etc., and of course the current directory.

So, the question is how to set up an idle timer to call the function.
I figured someone had already done something like that before (perhaps
to log out automatically).

Cheers,
Albert Wik.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Call a function when idle?
  2020-12-14 11:57 Call a function when idle? A. Wik
@ 2020-12-14 14:57 ` Mikael Magnusson
  2020-12-14 16:06   ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Mikael Magnusson @ 2020-12-14 14:57 UTC (permalink / raw)
  To: A. Wik; +Cc: zsh-users

On 12/14/20, A. Wik <awik32@gmail.com> wrote:
> Hi all,
>
> For the most part, when I get started working at a terminal, I can do
> just fine with a simple and short prompt like "% ".  A prompt that
> includes the current directory often gets annoyingly long so that you
> can rarely type a command line that doesn't scroll onto another line,
> which is harder to read.
>
> However, when I've been away from a terminal (window or tab) for some
> time, I would like to know where in the file system I am, to remind me
> (hopefully) of what I was doing.  If the current working directory is
> not in the prompt, I have to type "pwd" manually.
>
> To solve the problem, I was thinking of getting a function to run
> after a specified period of idle time.  This function could then run
> "print -P" to print useful pieces of data, such as the hostname, the
> tty name, etc., and of course the current directory.
>
> So, the question is how to set up an idle timer to call the function.
> I figured someone had already done something like that before (perhaps
> to log out automatically).

TMOUT  If  this  parameter  is  nonzero, the shell will receive an ALRM
       signal if a command is not entered within the  specified  number
       of  seconds  after  issuing  a  prompt.  If  there  is a trap on
       SIGALRM, it will be executed and a new alarm is scheduled  using
       the  value  of the TMOUT parameter after executing the trap.  If
       no trap is set, and the idle time of the terminal  is  not  less
       than  the  value of the TMOUT parameter, zsh terminates.  Other‐
       wise a new alarm is scheduled to TMOUT seconds  after  the  last
       keypress.

You may want to unset TMOUT in your handler and then arrange for it to
be set when you enter a new command line and/or any relevant
information has changed, otherwise you will come back to a lot of the
same information. And/or use zle -M to print the information, it will
be printed below the prompt (replacing any previous such message), it
will then be lost when overwritten by further commands/prompts. eg,

zle -N zle-line-init _zle_line_init
function _zle_line_init() {
  TMOUT=3
}
TMOUT=3
TRAPALRM() { zle -M "hi there, you're in $PWD"; unset TMOUT }

-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Call a function when idle?
  2020-12-14 14:57 ` Mikael Magnusson
@ 2020-12-14 16:06   ` Bart Schaefer
  2020-12-15 11:09     ` A. Wik
  0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2020-12-14 16:06 UTC (permalink / raw)
  To: Zsh Users; +Cc: A. Wik

On Mon, Dec 14, 2020 at 6:57 AM Mikael Magnusson <mikachu@gmail.com> wrote:
>
> On 12/14/20, A. Wik <awik32@gmail.com> wrote:
> >
> > So, the question is how to set up an idle timer to call the function.
> > I figured someone had already done something like that before (perhaps
> > to log out automatically).
>
> You may want to unset TMOUT in your handler and then arrange for it to
> be set when you enter a new command line and/or any relevant
> information has changed, otherwise you will come back to a lot of the
> same information.

I've been using the following for years to maintain the title bars in
my xterm-compatible shell windows:

typeset top=$'\C-[]0;'
typeset back=$'\C-G'
title () {
    setopt localoptions promptpercent
    print -nP "%{$top${*:-%m.%l:%2c - %t}$back%}"
}
TRAPALRM () {
    TMOUT=60
    title
}
precmd () {
    TMOUT=600
    title
}

This updates the title bar on every prompt, and then after 10 minutes
of idle time it updates it again every minute.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Call a function when idle?
  2020-12-14 16:06   ` Bart Schaefer
@ 2020-12-15 11:09     ` A. Wik
  0 siblings, 0 replies; 4+ messages in thread
From: A. Wik @ 2020-12-15 11:09 UTC (permalink / raw)
  To: zsh-users

Thanks for the replies.

I adapted Bart's code as follows:

TRAPALRM () {
    TMOUT=60
    title
    if ((!$DIDPRINT)) {
        DIDPRINT=1
        print -nP '\r%n@%m(%y):%d%# \n%# '
    }
}
precmd () {
    TMOUT=120
    DIDPRINT=0
    title
}

-aw


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-12-15 11:09 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-14 11:57 Call a function when idle? A. Wik
2020-12-14 14:57 ` Mikael Magnusson
2020-12-14 16:06   ` Bart Schaefer
2020-12-15 11:09     ` A. Wik

zsh-users

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/zsh-users

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 zsh-users zsh-users/ http://inbox.vuxu.org/zsh-users \
		zsh-users@zsh.org
	public-inbox-index zsh-users

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.users


code repositories for the project(s) associated with this inbox:

	https://git.vuxu.org/mirror/zsh/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git