A Very Silly Thing

I have fully embraced the modern nightmare of containerized web servers and the page you're reading right now is served up by a laptop in my home office. One of the nice things about this is I use File Browser which allows me to very easily upload files to my website from anywhere, and it even has a code editor for html and such!

Having direct access to all of this running on my own machine has empowered me to indulge in silly ideas. The most recent being thought of displaying a live-updating view of how much code I've written for my game.

I am in a weird spot where I think that LoC is a terrible metric for code quality or code complexity whilst simultaneously being absolutely addicted to a big number going up.

I have long had a script for running cloc, a neat full-featured utility to count LoC with a ton of options. The script runs the utility with options designed around ignoring dependencies, generated files, or any other “not lines I wrote myself” and I often enjoy checking in on it after a while to see how much larger the number has gotten.

Sometimes I even want to post that number to social media, but I worry about the optics of that and so I figured why not embrace the silliness and just make a place where anyone can easily see my big number and observe how big it is!

First we need some python that will update a copy of the repository, run cloc, and then send the output of the final line count to a file...

#!/usr/bin/env python3

import os
import subprocess
import json


# Define your repository path and the path to cloc.exe
REPO_PATH = '<path_to_checked_out_repo>'
CLOC_PATH = 'cloc'
OUTPUT_FILE = '<path_to_static_web_files>/cloc_out.txt'


def extract_sum_code(cloc_output):
    """Extract the SUM->code value from cloc JSON output."""
    cloc_data = json.loads(cloc_output)
    sum_code = cloc_data["SUM"]["code"]
    print(f"Found sum code: {sum_code}")
    return sum_code

def update_repo():
    """Pull the latest changes from the git repository."""
    os.chdir(REPO_PATH)
    subprocess.run(['git', 'pull', 'origin', 'master'], check=True)

def get_loc_count():
    """Run cloc.exe and get the lines of code count."""
    result = subprocess.run([CLOC_PATH, 'chron4/', '--exclude-dir=assets,x64', '--exclude-ext=inl,filters,vcxproj,recipe,ini,user,chrep,chf,temp,natvis,x', '--exclude-list-file=cloc-exclude.txt', '--json'], capture_output=True, text=Tru>
    cloc_output = result.stdout
    return cloc_output

def write_loc_to_file(loc_count):
    """Write the lines of code count to a file."""
    with open(OUTPUT_FILE, 'w') as file:
        file.write(str(loc_count))
        print("Wrote to file")

def main():
    try:
        update_repo()
        loc_count = get_loc_count()
        sum_code = extract_sum_code(loc_count)
        write_loc_to_file(sum_code)
    except Exception as e:
        print(f"An error occurred: {e}")


if __name__ == "__main__":
    main()

Next we add our script to cron...

0 0 * * * /usr/bin/python3 /<path-to>/runcloc.py >> /<path-to>/cron.log 2>&1

We sent cloc_out.txt to our static files on our nginx web server so referencing the content on our new website is as easy aaaas....

    <div style="text-align:center;">
       <img style="width:25%;" src="pikachu.gif" />
       <p><b><span id="loc"></span></b> lines of code have been written.</p>
       <p><a href="https://blog.brianna.town">Follow Development Updates</a></p>
       <p><a href="https://brianna.town">Return to Author's HomePage</a></p>
       
    </div>

    <script>
        async function fetchLoc() {
            const response = await fetch('cloc_out.txt');
            let loc = await response.text();
            loc = Number(loc).toLocaleString();
            document.getElementById('loc').textContent = loc;
        }
        fetchLoc();
    </script>

And there you go! Please enjoy the awkwardly-domain-named https://chrongame.com and look forward to a release date when the number is much much larger ♥

| 🌐 | 🙋‍ | @britown@blog.brianna.town