Skip to content

Instantly share code, notes, and snippets.

@bfollington
Created November 12, 2023 23:56
Show Gist options
  • Save bfollington/5797e8641cbc14be050c064eef13a6aa to your computer and use it in GitHub Desktop.
Save bfollington/5797e8641cbc14be050c064eef13a6aa to your computer and use it in GitHub Desktop.
Contribution Graph for Directory
import calendar
from datetime import datetime, timedelta
from pathlib import Path
import platform
def create_ascii_art_grid():
# Get the current year
current_year = datetime.now().year
start_date = datetime(current_year, 1, 1).date() # Convert to date
end_date = datetime(current_year, 12, 31).date() # Convert to date
# Initialize a 2D list (7 rows for days, and columns for weeks)
grid = [["_" for _ in range((end_date - start_date).days // 7 + 1)] for _ in range(7)]
# Use the current working directory as the folder path
folder_path = Path.cwd()
# Create a set to store dates with file creations
creation_dates = set()
# Traverse the folder and add creation dates to the set
for file in folder_path.glob('*'):
if file.is_file():
file_stat = file.stat()
# Use st_birthtime for macOS and st_ctime for other OS
if platform.system() == "Darwin": # Darwin is the system name for macOS
creation_time = datetime.fromtimestamp(file_stat.st_birthtime).date()
else:
creation_time = datetime.fromtimestamp(file_stat.st_ctime).date()
if start_date <= creation_time <= end_date:
creation_dates.add(creation_time)
# Fill the grid with data
for creation_date in creation_dates:
day_of_week = creation_date.weekday()
week_of_year = (creation_date - start_date).days // 7
grid[day_of_week][week_of_year] = "#"
# Adjust grid for the first day of each month
month_starts = [datetime(current_year, m, 1).date() for m in range(1, 13)]
for month_start in month_starts:
if start_date <= month_start <= end_date:
day_of_week = month_start.weekday()
week_of_year = (month_start - start_date).days // 7
# Generate the ASCII grid string
day_labels = ["M", "T", "W", "T", "F", "S", "S"]
# Create a header with month labels
header = " "
month_positions = [(m - start_date).days // 7 for m in month_starts]
for i, month in enumerate(calendar.month_abbr[1:]):
# Calculate the space needed before the month label
space = month_positions[i] * 3 - len(header)
header += " " * space + month
ascii_grid = ""
for i, row in enumerate(grid):
ascii_grid += day_labels[i] + " " + "".join(row) + "\n"
return ascii_grid
# Generate and print the ASCII grid for the current folder
ascii_grid = create_ascii_art_grid()
print(ascii_grid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment