5

I want to be able to time how long I spend in front of a terminal. I'm thinking the best way will be to have some sort of timer that starts and stops when the terminal gains/loses focus. And it will have to work with multiple terminals...

Any ideas? I'm using GNOME.

migrated from stackoverflow.com Jul 22 '11 at 4:33

This question came from our site for professional and enthusiast programmers.

2

If your terminal shell has easy access to its own X window ID, you're probably doing something wrong! They have nothing to do with each other - e.g. you could (and should) be running long jobs inside screen which could in theory be outputting to any number of terminals anywhere in the world.

That being said, the way I solve this problem is by using the prompt's ability to update a terminal's "status"/"title" to report the shell's PID, as with the following prompt:

PS1=\u@\H:\w\$\ \[\e]2;\u@\H:\w [$$]\a\]

Any pseudo-terminal showing the shell with this prompt and PID 6399 have a title like user@host:~ [6399]. Then, using a tool like wmctrl, you can write a bash script such as this:

win_from_pid() {
   type wmctrl &>/dev/null || return 1
   wmctrl -l | awk '/^.*\['"$1"'\]$/ { print $1 }'
}

This searches the window list and gives you the X Window ID(s) of any ending with that title. Thus, the function win_from_pid $$ can tell you your window ID(s) on the same host running the script, if any. You can figure out how to determine focus from there. :)

1

You can do a stat on the terminal and get the Access or Modify times.

stat /dev/pts/2
  • What does that give you? Isn't the modified time updated whenever there's new output on the terminal? And I couldn't determine when the access time got updated from a short experiment. – musiphil Aug 1 '17 at 17:59
0

I expect you'll have to listen in on the X-window messages for the appropriate FOCUS message. Not sure how easy/difficult that will be though.

Alternatively https://stackoverflow.com/questions/1014822/how-to-know-which-window-has-focus-and-how-to-change-it talks about determining the window with focus and provides a couple of options: you could use that technique and just run it in a loop and track the focus changes.

  • Do you have any idea if the focus messages you're talking about are the same as the 'focus events' described in the Tmux manual? ...focus events are requested from the terminal if supported and passed through to applications running in tmux. Tmux isn't windowed and neither are the terminals inside it, so if not by X-window focus messages, then what kind of focus event could it be listening to? Thanks either way. – John P Oct 2 '18 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.