Guido van Rossum | 2b28776 | 2001-01-05 20:54:07 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
| 3 | """repeat <shell-command> |
| 4 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame^] | 5 | This simple program repeatedly (at 1-second intervals) executes the |
Guido van Rossum | 2b28776 | 2001-01-05 20:54:07 +0000 | [diff] [blame] | 6 | shell command given on the command line and displays the output (or as |
| 7 | much of it as fits on the screen). It uses curses to paint each new |
| 8 | output on top of the old output, so that if nothing changes, the |
| 9 | screen doesn't change. This is handy to watch for changes in e.g. a |
| 10 | directory or process listing. |
| 11 | |
| 12 | To 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 | |
| 23 | import os |
| 24 | import sys |
| 25 | import time |
| 26 | import curses |
| 27 | |
| 28 | def 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: |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame^] | 41 | while True: |
Guido van Rossum | 2b28776 | 2001-01-05 20:54:07 +0000 | [diff] [blame] | 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 | |
| 58 | main() |