-
Notifications
You must be signed in to change notification settings - Fork 13
/
hi
executable file
·148 lines (123 loc) · 4.28 KB
/
hi
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python3
# hi -- show recent IRC highlights
import argparse
import datetime
import fcntl
import io
import re
import os
import subprocess
import textwrap
from nullroute.core import Env
def tty_width():
return int(os.environ.get("COLUMNS", os.get_terminal_size().columns))
def download_log(remote_host, remote_path, local_path):
with open(local_path, "ab") as fh:
fcntl.flock(fh, fcntl.LOCK_EX)
try:
osize = os.stat(local_path).st_size
rcmd = f"tail -c +{osize+1} '{remote_path}'"
except FileNotFoundError:
osize = 0
rcmd = f"cat '{remote_path}'"
subprocess.run(["ssh", remote_host, rcmd],
stdout=fh,
check=True)
nsize = os.stat(local_path).st_size
return nsize-osize, osize
def parse_log(fh):
for line in fh:
date, wname, text = line.split("\t", 2)
date = datetime.datetime.strptime(date, "%Y-%m-%d %H:%M:%S %z")
wname = re.sub(r"^<(.+)>$", r"\1", wname)
text = text.rstrip()
if "#" in wname:
# Channel messages
name = "?"
if m := re.match(r"^<-i-> Mode (\S+) \[(.+?)\] by (\S+)$", text):
# /mode
name = "-- %s:" % m[3]
text = "Mode %s [%s]" % (m[1], m[2])
elif m := re.match(r"^<(\S+)> (.*)$", text):
# Regular
name = "(%s)" % m[1]
text = m[2]
else:
# All kinds of non-channel messages
if m := re.match(r"^<(\S+)> \1: (.*)$", text):
# /notice
name = "Msg: -%s-" % m[1]
text = m[2]
elif m := re.match(r"^<(\S+)> \1 (.*)$", text):
# /me
name = "Msg: * %s" % m[1]
text = m[2]
elif m := re.match(r"^<(\S+)> (.*)$", text):
# /msg
name = "Msg: (%s)" % m[1]
text = m[2]
if re.match(r"^-(nick|chan)serv-$", name, re.I):
continue
yield (date, wname, name, text)
def sgr(arg, text):
return "\033[%sm%s\033[m" % (arg, text)
def vis(text):
return re.sub(r"[\x00-\x1F\x80-\x9F]",
lambda m: "\033[2m<%02X>\033[m" % ord(m[0]),
text)
def print_log(fh, unseen):
SGR_DATE = "1;31"
SGR_WNAME = "1;32"
SGR_WTAIL = "32"
SGR_TIME = "38;5;244"
columns = tty_width() - len(" HH:MM | ") - len(" ")
lastdate = None
lastwname = None
for date, wname, name, text in parse_log(fh):
if wname != lastwname:
hdrdate = date.strftime("%b %-d")
hdrname = wname
hdrtail = ""
if m := re.match(r"^(.+)(#.+)$", wname):
hdrname = m[2]
hdrtail = "(%s)" % m[1]
if unseen:
hdrdate = sgr(SGR_DATE, hdrdate)
hdrname = sgr(SGR_WNAME, hdrname)
hdrtail = sgr(SGR_WTAIL, hdrtail)
if lastwname:
print()
print(hdrdate, hdrname, hdrtail)
lines = textwrap.wrap(name + " " + text, columns)
sdate = date.strftime("%H:%M")
for line in lines:
print(" "*2, sgr(SGR_TIME, sdate), sgr(SGR_TIME, "│"), vis(line))
sdate = " " * len(sdate)
lastdate = date
lastwname = wname
parser = argparse.ArgumentParser()
parser.add_argument("-q", "--quiet",
action="store_true",
help="don't show old messages")
args = parser.parse_args()
os.umask(0o077)
rhost = "star"
rpath = "irclogs/perl.highmon.log"
cachepath = Env.find_cache_file("highlights.txt")
delta, pos = download_log(rhost, rpath, cachepath)
if delta:
with open(cachepath, "rb") as fh:
# In case `hi` hasn't been run for a very long time on this system,
# limit output to ~32k so that it doesn't kill the terminal.
if delta > 32*1024:
fh.seek(-32*1024, os.SEEK_END)
fh.readline()
else:
fh.seek(pos)
print_log(io.TextIOWrapper(fh), unseen=True)
elif args.quiet:
print("hi: no new messages")
else:
with os.popen("tail -n 10 '%s'" % cachepath, "r") as fh:
print_log(fh, unseen=False)
print("hi: no new messages")