From 6db0708d10192a24760035e12bb7eee8c2b4e2d7 Mon Sep 17 00:00:00 2001 From: Bobby Date: Wed, 27 Mar 2024 22:36:48 -0400 Subject: Update: View Text Files in Repos --- dev_status/utils.py | 87 +++++++++++++++++++++++++++++++++++++++++++++++++---- dev_status/views.py | 15 ++++++++- 2 files changed, 95 insertions(+), 7 deletions(-) (limited to 'dev_status') diff --git a/dev_status/utils.py b/dev_status/utils.py index 02876dc5..ffe7e029 100644 --- a/dev_status/utils.py +++ b/dev_status/utils.py @@ -1,4 +1,34 @@ from datetime import datetime +from pygments import highlight +from pygments.lexers import get_lexer_for_filename +from pygments.lexers.special import TextLexer +from pygments.formatters import HtmlFormatter + + +def text_lines(text): + # return the number of lines in a text + return len(text.split("\n")) - 1 + + +def text_loc(text): + text = text.strip() + + # return the number of lines of code in a text + return len([line for line in text.split("\n") if line.strip()]) + + +def size_format(size_bytes): + if size_bytes == 0: + return "0B" + + size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB") + + i = int(size_bytes // 1024) + for size in size_name: + if i == 0: + return "{:.1f} {}".format(size_bytes, size) + size_bytes /= 1024 + i = int(size_bytes // 1024) def relative_date(entry): @@ -8,18 +38,63 @@ def relative_date(entry): now = datetime.now() diff = now - committedDate if diff.days > 365: - entry["commit"]["committedDate"] = str(diff.days // 365) + " year" + ("s" if diff.days // 365 > 1 else "") + " ago" + entry["commit"]["committedDate"] = ( + str(diff.days // 365) + + " year" + + ("s" if diff.days // 365 > 1 else "") + + " ago" + ) elif diff.days > 30: - entry["commit"]["committedDate"] = str(diff.days // 30) + " month" + ("s" if diff.days // 30 > 1 else "") + " ago" + entry["commit"]["committedDate"] = ( + str(diff.days // 30) + + " month" + + ("s" if diff.days // 30 > 1 else "") + + " ago" + ) elif diff.days > 7: - entry["commit"]["committedDate"] = str(diff.days // 7) + " week" + ("s" if diff.days // 7 > 1 else "") + " ago" + entry["commit"]["committedDate"] = ( + str(diff.days // 7) + " week" + ("s" if diff.days // 7 > 1 else "") + " ago" + ) elif diff.days > 0: - entry["commit"]["committedDate"] = str(diff.days) + " day" + ("s" if diff.days > 1 else "") + " ago" + entry["commit"]["committedDate"] = ( + str(diff.days) + " day" + ("s" if diff.days > 1 else "") + " ago" + ) elif diff.seconds > 3600: - entry["commit"]["committedDate"] = str(diff.seconds // 3600) + " hour" + ("s" if diff.seconds // 3600 > 1 else "") + " ago" + entry["commit"]["committedDate"] = ( + str(diff.seconds // 3600) + + " hour" + + ("s" if diff.seconds // 3600 > 1 else "") + + " ago" + ) elif diff.seconds > 60: - entry["commit"]["committedDate"] = str(diff.seconds // 60) + " minute" + ("s" if diff.seconds // 60 > 1 else "") + " ago" + entry["commit"]["committedDate"] = ( + str(diff.seconds // 60) + + " minute" + + ("s" if diff.seconds // 60 > 1 else "") + + " ago" + ) else: entry["commit"]["committedDate"] = "just now" return entry + + +def highlight_code(text, filename): + print(filename) + print(text) + try: + lexer = get_lexer_for_filename(filename, stripall=True) + except: + lexer = None + + formatter = HtmlFormatter( + noclasses=True, + style="native", + wrapcode=True, + linenos="inline", + nobackground=True, + ) + if lexer: + return highlight(text, lexer, formatter) + else: + return highlight(text, TextLexer(), formatter) diff --git a/dev_status/views.py b/dev_status/views.py index 678be122..96ad7d42 100644 --- a/dev_status/views.py +++ b/dev_status/views.py @@ -6,7 +6,13 @@ import requests from django.shortcuts import render from dotenv import load_dotenv from github import Github -from dev_status.utils import relative_date +from dev_status.utils import ( + relative_date, + text_lines, + text_loc, + size_format, + highlight_code, +) load_dotenv() g = Github(os.getenv("GH_TOKEN")) @@ -172,6 +178,13 @@ def get_repo(request, r=None, p=None): else: # if it is a blob, display the file tree = data["data"]["repository"]["object"] + tree["path"] = p + tree["name"] = p.split("/")[-1] + if not tree["isBinary"]: + tree["lines"] = text_lines(tree["text"]) + tree["loc"] = text_loc(tree["text"]) + tree["size"] = size_format(tree["byteSize"]) + tree["text"] = highlight_code(tree["text"], tree["name"]) # get commit information for each file or directory if viewMode == "tree": -- cgit v1.2.3