-
Notifications
You must be signed in to change notification settings - Fork 1
/
pipe-timestamper.py
executable file
·41 lines (31 loc) · 1.06 KB
/
pipe-timestamper.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
#!/usr/bin/env python3
# Author: github.com/danielhoherd
# License: MIT
"""Takes a stream from stdin and prefixes it with the timestamp it was read and
the delta from the previous line.
Most useful for slow streams.
"""
from sys import argv, stdin
import pendulum
import typer
def timestamp_lines(show_delta: bool = typer.Option(None, help="Show the time delta between each line.")):
"""Takes a stream from stdin and prefixes it with the timestamp it was read
and the delta from the previous line.
Most useful for slow streams.
"""
old = pendulum.now()
if stdin.isatty():
print(f"Description: {__doc__}")
print(f"Usage: <some_command> | {argv[0]}")
raise SystemExit(1)
for line in stdin:
line = line.strip("\n")
new = pendulum.now()
delta = (new - old).in_words()
if show_delta:
print(f"{new.to_iso8601_string()} ({delta}) {line}")
else:
print(f"{new.to_iso8601_string()} {line}")
old = new
if __name__ == "__main__":
typer.run(timestamp_lines)