diff options
author | Georg Brandl <georg@python.org> | 2010-12-30 21:33:07 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2010-12-30 21:33:07 (GMT) |
commit | 7fafbc95c0c963438197c9a43fe893c4ea6fe759 (patch) | |
tree | 5c9ceda4bdc5260236a230554b9ed56b8c0cdbd3 /Demo/curses/repeat.py | |
parent | 6f17e2df29a865a29447531e89fb22be710e382d (diff) | |
download | cpython-7fafbc95c0c963438197c9a43fe893c4ea6fe759.zip cpython-7fafbc95c0c963438197c9a43fe893c4ea6fe759.tar.gz cpython-7fafbc95c0c963438197c9a43fe893c4ea6fe759.tar.bz2 |
More cleanup: Move some demos into a dedicated Tools/demo dir, move 2to3 demo to Tools, and remove all the other Demo content.
Diffstat (limited to 'Demo/curses/repeat.py')
-rwxr-xr-x | Demo/curses/repeat.py | 80 |
1 files changed, 0 insertions, 80 deletions
diff --git a/Demo/curses/repeat.py b/Demo/curses/repeat.py deleted file mode 100755 index 570f221..0000000 --- a/Demo/curses/repeat.py +++ /dev/null @@ -1,80 +0,0 @@ -#! /usr/bin/env python3 - -"""repeat [-i SECONDS] <shell-command> - -This simple program repeatedly (at 1-second intervals) executes the -shell command given on the command line and displays the output (or as -much of it as fits on the screen). It uses curses to paint each new -output on top of the old output, so that if nothing changes, the -screen doesn't change. This is handy to watch for changes in e.g. a -directory or process listing. - -The -i option lets you override the sleep time between executions. - -To end, hit Control-C. -""" - -# Author: Guido van Rossum - -# Disclaimer: there's a Linux program named 'watch' that does the same -# thing. Honestly, I didn't know of its existence when I wrote this! - -# To do: add features until it has the same functionality as watch(1); -# then compare code size and development time. - -import os -import sys -import time -import curses -import getopt - -def main(): - interval = 1.0 - try: - opts, args = getopt.getopt(sys.argv[1:], "hi:") - except getopt.error as err: - print(err, file=sys.stderr) - sys.exit(2) - if not args: - print(__doc__) - sys.exit(0) - for opt, arg in opts: - if opt == "-i": - interval = float(arg) - if opt == "-h": - print(__doc__) - sys.exit(0) - cmd = " ".join(args) - cmd_really = cmd + " 2>&1" - p = os.popen(cmd_really, "r") - text = p.read() - sts = p.close() - text = addsts(interval, cmd, text, sts) - w = curses.initscr() - try: - while True: - w.erase() - try: - w.addstr(text) - except curses.error: - pass - w.refresh() - time.sleep(interval) - p = os.popen(cmd_really, "r") - text = p.read() - sts = p.close() - text = addsts(interval, cmd, text, sts) - finally: - curses.endwin() - -def addsts(interval, cmd, text, sts): - now = time.strftime("%H:%M:%S") - text = "%s, every %g sec: %s\n%s" % (now, interval, cmd, text) - if sts: - msg = "Exit status: %d; signal: %d" % (sts>>8, sts&0xFF) - if text and not text.endswith("\n"): - msg = "\n" + msg - text += msg - return text - -main() |