How to run repeatedly a proc in Tcl

Viewed 50

I have written a proc in tcl which takes one argument (a positive integer) und displays a text. Lets call it permu (for permutation). I would like to execute this proc permanently, so

puts [permu 3]

and with the same argument (here 3), lets say every 2 or 3 seconds or so, without removing the previous outcome of the code. How can I do this?

The second question: Same question as above but I would like to clear the screen when the new outcome of permu is displayed.

Third question: In case that I decide to stop a running code (I work with Linux), for example the one above, how can I do this?

Thanks in advance!

1 Answers

Here's one way to do the repeated output:

proc repeat_permu {arg delay} {
   puts [permu $arg]
   after $delay [list repeat_permu $arg $delay]
}

# Note that the delay is in milliseconds
repeat_permu 3 3000

# Now start the event loop
vwait forever

To clear the screen you need to send the appropriate escape sequence before each new output. Most terminal emulators will accept the vt100 code, so you would do puts "\x1b[2J".

Normally you could just stop your program running by typing control-c, or do you want some means of doing this programmatically?

Update: A simpler way to do the repetition if you don't need to process any other events in parallel is just: while 1 {puts [permu 3]; after 3000}

Related