-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock-overlay
More file actions
executable file
·52 lines (44 loc) · 1.13 KB
/
clock-overlay
File metadata and controls
executable file
·52 lines (44 loc) · 1.13 KB
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
#!/usr/bin/python2
import sys, os
from time import strftime
from time import sleep
from signal import signal, SIGALRM, setitimer, ITIMER_REAL
from errno import EINTR
format_escapes = {
r'\e': '\x1b',
r'\t': '\t',
r'\n': '\x1b[B\x1b[G',
r'\\': '\\'
}
CLEAR_NEXT_LINE = False # true to clear the line below the output as well
def main(progname, _time_format='\e[1;34m%X\e[m'):
global time_format
time_format = replace_all(_time_format, format_escapes)
signal(SIGALRM, on_alarm)
setitimer(ITIMER_REAL, 1, 1)
patrick()
def on_alarm(signum, frame):
clear_next_s = "\x1b[B\x1b[2K" if CLEAR_NEXT_LINE else ''
s = "\x1b[s\x1b[H\x1b[2K" + strftime(time_format) + clear_next_s + "\x1b[u"
while s:
n = os.write(sys.stdout.fileno(), s)
s = s[n:]
def patrick():
c = None
while 1:
try:
n = 0
c = sys.stdin.read(1) if not c else c
if not c:
break
n = os.write(sys.stdout.fileno(), c)
except (OSError, IOError), ex:
if ex.errno != EINTR:
raise
if n: c = None
def replace_all(s, replacements):
for item in replacements.items():
s = s.replace(*item)
return s
if __name__=='__main__':
sys.exit(main(*sys.argv))