Created
December 22, 2021 22:36
-
-
Save kaecy/17e657c8dc2534e7334e8807cff2e3b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def listgen(n, obj=0): | |
return [obj for _ in range(n)] | |
def makeLineToCells(line, rowSize=0): | |
line = line.strip() | |
if line.startswith("|"): line = line[1:] | |
if line.endswith("|"): line = line[:-1] | |
cells = line.split("|") | |
cellsLen = len(cells) | |
for i in range(cellsLen): | |
cells[i] = cells[i].strip() | |
extraElmsCount = rowSize - cellsLen | |
if extraElmsCount: | |
cells.extend(listgen(extraElmsCount, "")) | |
return cells | |
def parseMdTable(string): | |
lines = string.strip().split("\n") | |
table = [] | |
tableRowSize = 0 | |
if len(lines) > 1: | |
config = makeLineToCells(lines[1]) | |
for dashes in config: | |
if dashes.find("---") == -1: | |
return None | |
tableRowSize = len(config) | |
del lines[1] | |
for line in lines: | |
table.append(makeLineToCells(line, tableRowSize)) | |
return table | |
def printMdTable(table, alignment=None): | |
height, width = (len(table), len(table[0])) | |
print("Height:", height, ", Width:", width) | |
largestColumnSizes = [ 0 for _ in range(width) ] | |
for y in range(height): | |
for x in range(width): | |
value = table[y][x] | |
valueLen = len(value) | |
if valueLen > largestColumnSizes[x]: | |
largestColumnSizes[x] = valueLen | |
if alignment: | |
alignment.extend(listgen(width - len(alignment))) | |
row = table[0] | |
for i in range(len(row)): | |
value = row[i] | |
if alignment == None or alignment[i] == 0: | |
row[i] = value + " " * (largestColumnSizes[i] - len(value)) | |
else: | |
row[i] = " " * (largestColumnSizes[i] - len(value)) + value | |
print(" " + " | ".join(row)) | |
row = [] | |
for i in range(width): | |
row.append("-" * (largestColumnSizes[i])) | |
print(" " + " | ".join(row)) | |
for y in range(1,height): | |
row = table[y] | |
for i in range(len(row)): | |
value = row[i] | |
if alignment == None or alignment[i] == 0: | |
row[i] = value + " " * (largestColumnSizes[i] - len(value)) | |
else: | |
row[i] = " " * (largestColumnSizes[i] - len(value)) + value | |
print(" " + " | ".join(row)) | |
def getFilelistTable(path): | |
import os, datetime | |
path = os.path.realpath(path) | |
table = [["", "File Name", "Date"]] | |
items = os.listdir(path) | |
for item in items: | |
itemPath = os.path.join(path, item) | |
creationTime = str(datetime.date.fromtimestamp(os.lstat(itemPath).st_ctime)) | |
table.append(["DIR" if os.path.isdir(itemPath) else "", item, creationTime]) | |
return table | |
if __name__ == "__main__": | |
mdTable = \ | |
""" | |
Artist | Song | |
---|--- | |
Sugababes | About You Now | |
Take That | Rule The World | |
Taylor Swift | ...Ready For It? | |
Tessa Violet | Crush | |
Tristania | Deadlocked | |
Ward Thomas | Someday | |
Wham! | Last Christmas | |
""" | |
table = parseMdTable(mdTable) | |
if table: | |
printMdTable(table, [1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment