forked from alok/notational-fzf-vim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_lines.py
executable file
·33 lines (27 loc) · 906 Bytes
/
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Colorize the current line in the preview window in bold red."""
import os
import os.path as path
import shutil
import sys
line = int(sys.argv[1])
file = sys.argv[2]
# Use a large default for width since the extra doesn't get displayed anyway
width = max(1, shutil.get_terminal_size().lines // 2)
end_width = 200
# file numbers start at 1
beginning = max(1, line - width)
end = line + end_width
# ANSI escape sequences for coloring matched line
RED = "\033[1;31m"
RESET = "\033[0;0m"
BOLD = "\033[;1m"
if __name__ == "__main__":
with open(path.normpath(file)) as f:
for linenum, line_content in enumerate(f, start=1):
if beginning <= linenum <= end:
if linenum == line:
print(BOLD + RED + line_content.rstrip() + RESET)
else:
print(line_content.rstrip())