~fkfd/git.gmi

6333ef00cb5eeba691b83bbed84b7ffc2d583508 — Frederick Yin 1 year, 1 month ago a034660
Add line number in blob view
2 files changed, 19 insertions(+), 1 deletions(-)

M git-gmi/git.py
A git-gmi/utils.py
M git-gmi/git.py => git-gmi/git.py +2 -1
@@ 4,6 4,7 @@ from datetime import datetime
import mimetypes
from const import *
from config import *
from utils import *

mimetypes.add_type("text/gemini", ".gmi")
mimetypes.add_type("text/gemini", ".gemini")


@@ 273,7 274,7 @@ class GitGmiRepo:
        elif blob.size < MAX_DISPLAYED_BLOB_SIZE:
            response += (
                f"=> {blob.name}?raw view raw\n\n"
                "```\n" + blob.data.decode("utf-8") + "\n```"
                "```\n" + add_line_numbers(blob.data.decode("utf-8")) + "\n```"
            )
        else:
            response += (

A git-gmi/utils.py => git-gmi/utils.py +17 -0
@@ 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)