blob: 10832397e691f52824d38a3c67b032832664a780 [file] [log] [blame]
Guido van Rossum2b287762001-01-05 20:54:07 +00001#! /usr/bin/env python
2
3"""repeat <shell-command>
4
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00005This simple program repeatedly (at 1-second intervals) executes the
Guido van Rossum2b287762001-01-05 20:54:07 +00006shell command given on the command line and displays the output (or as
7much of it as fits on the screen). It uses curses to paint each new
8output on top of the old output, so that if nothing changes, the
9screen doesn't change. This is handy to watch for changes in e.g. a
10directory or process listing.
11
12To end, hit Control-C.
13"""
14
15# Author: Guido van Rossum
16
17# Disclaimer: there's a Linux program named 'watch' that does the same
18# thing. Honestly, I didn't know of its existence when I wrote this!
19
20# To do: add features until it has the same functionality as watch(1);
21# then compare code size and development time.
22
23import os
24import sys
25import time
26import curses
27
28def main():
29 if not sys.argv[1:]:
Collin Winter6f2df4d2007-07-17 20:59:35 +000030 print(__doc__)
Guido van Rossum2b287762001-01-05 20:54:07 +000031 sys.exit(0)
32 cmd = " ".join(sys.argv[1:])
33 p = os.popen(cmd, "r")
34 text = p.read()
35 sts = p.close()
36 if sts:
Collin Winter6f2df4d2007-07-17 20:59:35 +000037 print("Exit code:", sts, file=sys.stderr)
Guido van Rossum2b287762001-01-05 20:54:07 +000038 sys.exit(sts)
39 w = curses.initscr()
40 try:
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000041 while True:
Guido van Rossum2b287762001-01-05 20:54:07 +000042 w.erase()
43 try:
44 w.addstr(text)
45 except curses.error:
46 pass
47 w.refresh()
48 time.sleep(1)
49 p = os.popen(cmd, "r")
50 text = p.read()
51 sts = p.close()
52 if sts:
Collin Winter6f2df4d2007-07-17 20:59:35 +000053 print("Exit code:", sts, file=sys.stderr)
Guido van Rossum2b287762001-01-05 20:54:07 +000054 sys.exit(sts)
55 finally:
56 curses.endwin()
57
58main()