-
Notifications
You must be signed in to change notification settings - Fork 63
/
print_lines.py
executable file
·48 lines (42 loc) · 1.36 KB
/
print_lines.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Colorize the current line in the preview window in bold red."""
import os.path as path
import sys
import subprocess
line = int(sys.argv[1])
file = sys.argv[2]
height = int(sys.argv[3])
def opcount(fname):
with open(fname) as f:
for i, _ in enumerate(f):
pass
return i + 1
if __name__ == "__main__":
try:
# fail fast
subprocess.check_call(["bat"])
lines = opcount(file)
cmd = [
"bat",
"--style=numbers",
"--color=always",
"--highlight-line={}".format(line),
]
if (line > (height / 2)) and (lines >= height):
cmd.append("--line-range={}:{}".format(int(line - (height / 2)), lines))
cmd.append(path.normpath(file))
subprocess.run(cmd)
except Exception:
is_sel = False
# ANSI escape sequences for coloring matched line
RED = "\033[1;31m"
RESET = "\033[0;0m"
BOLD = "\033[;1m"
with open(path.normpath(file)) as f:
for linenum, line_content in enumerate(f, start=1):
if linenum == line:
print(BOLD + RED + line_content.rstrip() + RESET)
is_sel = True
elif is_sel or (line - linenum <= (height / 2 - 1)):
print(line_content.rstrip())