- Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy pathrepeat.py
executable file
·58 lines (49 loc) · 1.48 KB
/
repeat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#! /usr/bin/env python
"""repeat <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.
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.
importos
importsys
importtime
importcurses
defmain():
ifnotsys.argv[1:]:
print__doc__
sys.exit(0)
cmd=" ".join(sys.argv[1:])
p=os.popen(cmd, "r")
text=p.read()
sts=p.close()
ifsts:
print>>sys.stderr, "Exit code:", sts
sys.exit(sts)
w=curses.initscr()
try:
whileTrue:
w.erase()
try:
w.addstr(text)
exceptcurses.error:
pass
w.refresh()
time.sleep(1)
p=os.popen(cmd, "r")
text=p.read()
sts=p.close()
ifsts:
print>>sys.stderr, "Exit code:", sts
sys.exit(sts)
finally:
curses.endwin()
main()