From 6333ef00cb5eeba691b83bbed84b7ffc2d583508 Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Wed, 22 Jul 2020 16:55:00 +0800 Subject: Add line number in blob view --- git-gmi/utils.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 git-gmi/utils.py (limited to 'git-gmi/utils.py') diff --git a/git-gmi/utils.py b/git-gmi/utils.py new file mode 100644 index 0000000..c3dbd59 --- /dev/null +++ b/git-gmi/utils.py @@ -0,0 +1,17 @@ +import math + + +def add_line_numbers(code: str) -> str: + lines = code.splitlines() + if not lines: + return code # empty anyway + + # cannot use math.ceil() here bc lg100=2 + max_digits = math.floor(math.log10(len(lines))) + 1 + + for n, l in enumerate(lines, 1): + digits_in_n = math.floor(math.log10(n)) + 1 + spaces_before_number = max_digits - digits_in_n + lines[n - 1] = " " * spaces_before_number + str(n) + " " + l + + return "\n".join(lines) -- cgit v1.2.3