Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2b28776

Browse files
committed
Added "repeat.py" -- repeatedly execute a shell command (like
watch(1)). Updated and untabified the README file.
1 parent cadfaec commit 2b28776

2 files changed

Lines changed: 64 additions & 7 deletions

File tree

Demo/curses/README

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ originals. I won't attempt to `beautify' the program anytime soon, but
1010
I wouldn't mind someone else making an effort in that direction, of
1111
course.
1212

13-
ncurses.py -- currently only a panels demo
13+
ncurses.py -- currently only a panels demo
1414
XXX this won't work until panel support is checked in
15-
rain.py -- raindrops keep falling on my desktop
16-
tclock.py -- ASCII clock, by Howard Jones
17-
xmas.py -- I'm dreaming of an ASCII christmas
15+
rain.py -- raindrops keep falling on my desktop
16+
tclock.py -- ASCII clock, by Howard Jones
17+
xmas.py -- I'm dreaming of an ASCII christmas
1818

1919
Please send bugfixes and new contributions to me or, even better,
2020
submit them to the Python Bug Tracker on SourceForge
@@ -24,6 +24,5 @@ submit them to the Python Bug Tracker on SourceForge
2424
Other demos
2525
===========
2626

27-
life.py Simple game of Life
28-
29-
27+
life.py -- Simple game of Life
28+
repeat.py -- Repeatedly execute a shell command (like watch(1))

Demo/curses/repeat.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#! /usr/bin/env python
2+
3+
"""repeat <shell-command>
4+
5+
This simple program repeatedly (with 1-second intervals) executes the
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:
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+
58+
main()

0 commit comments

Comments
 (0)