blob: 158264cf6b7c75564c6bde4617be8d333d1c1d86 [file] [log] [blame]
Guido van Rossum2b287762001-01-05 20:54:07 +00001#! /usr/bin/env python
2
3"""repeat <shell-command>
4
5This simple program repeatedly (with 1-second intervals) executes the
6shell 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:]:
30 print __doc__
31 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:
37 print >>sys.stderr, "Exit code:", sts
38 sys.exit(sts)
39 w = curses.initscr()
40 try:
41 while 1:
42 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:
53 print >>sys.stderr, "Exit code:", sts
54 sys.exit(sts)
55 finally:
56 curses.endwin()
57
58main()