Skip to content

Instantly share code, notes, and snippets.

@DataKinds
Last active August 29, 2015 14:10
Show Gist options
  • Save DataKinds/d02380667be743be1310 to your computer and use it in GitHub Desktop.
Save DataKinds/d02380667be743be1310 to your computer and use it in GitHub Desktop.
Fungeoid IDE
def safeMoveCursor(stdscr, direction, screenPosition, screenSize, cursorPosition, padX, padY):
if direction == "up":
if cursorPosition[0] == 0:
if screenPosition[0] > 0:
screenPosition[0] -= 1
else:
stdscr.move(cursorPosition[0] - 1, cursorPosition[1])
elif direction == "down":
if cursorPosition[0] == screenSize[0] - 1:
if screenPosition[0] < padY - screenSize[0]:
screenPosition[0] += 1
else:
stdscr.move(cursorPosition[0] + 1, cursorPosition[1])
elif direction == "left":
if cursorPosition[1] == 0:
if screenPosition[1] > 0:
screenPosition[1] -= 1
else:
stdscr.move(cursorPosition[0], cursorPosition[1] - 1)
elif direction == "right":
if cursorPosition[1] == screenSize[1] - 1:
if screenPosition[1] < padX - screenSize[1]:
screenPosition[1] += 1
else:
stdscr.move(cursorPosition[0], cursorPosition[1] + 1)
return screenPosition
def changeDirection(c, prevDirection):
if c == "^":
return "up"
elif c == "v":
return "down"
elif c == "<":
return "left"
elif c == ">":
return "right"
else:
return prevDirection
import sys
def normalizeLineLength(scriptList):
maxLineLength = max(scriptList, key=len)
for index, line in enumerate(scriptList):
if len(line) < maxLineLength:
scriptList[index] += " " * (maxLineLength - len(line))
return scriptList
def trimFile(scriptList):
"""
Takes a split script string with representing a funge program and makes the whitespace properly trimmed
"""
if scriptList == []:
return scriptList
minTrailingWhitespaceString = min(scriptList, key = lambda l: len(l) - len(l.rstrip()))
minTrailingWhitespace = len(minTrailingWhitespaceString) - len(minTrailingWhitespaceString.rstrip())
if minTrailingWhitespace > 0:
scriptList = [l[:-minTrailingWhitespace] for l in scriptList]
#horizontally trimmed
for line in reversed(scriptList):
if not line[:].strip(): #if it's all whitespace
scriptList.pop(scriptList.index(line))
else:
break
#vertically trimmed
return scriptList
def setupPad(mainPad, padX, padY):
for y in range(0, padY - 1):
for x in range(0, padX - 1):
mainPad.addch(y, x, ord(" "))
scriptList = []
f = open(sys.argv[1], "r+")
s = f.read()
scriptList = trimFile(s.splitlines())
f.close()
data = [[" " for _ in range(0, padX)] for y in range(0, padY)] #2d list of singleton lists which contain a char. it's mutable, so shut up
for y, yList in enumerate(scriptList):
for x, xChar in enumerate(yList):
data[y][x] = xChar
mainPad.addch(y, x, ord(xChar))
return mainPad, data
from curses import *
from curses import ascii
from cursorhandler import *
from fileloader import *
import sys, os
def isAscii(c):
return 31 < c < 127
def drawData(mainPad, data):
trimmedData = trimFile(["".join(d) for d in data])
for y, yList in enumerate(trimmedData):
for x, xChar in enumerate(yList):
mainPad.addch(y, x, ord(xChar))
return mainPad
def main(stdscr):
padX = 1000
padY = 1000
mainPad = newpad(padX, padY)
cursorPosition = list(stdscr.getyx())
direction = "right"
screenPosition = [0, 0]
mainPad, data = setupPad(mainPad, padX, padY)
while True:
screenSize = stdscr.getmaxyx()
cursorPosition = list(stdscr.getyx())
mainPad = drawData(mainPad, data)
mainPad.refresh(screenPosition[0],screenPosition[1], 0,0, screenSize[0]-1,screenSize[1]-1)
c = stdscr.getch()
if c == KEY_UP:
screenPosition = safeMoveCursor(stdscr, "up", screenPosition, screenSize, cursorPosition, padX, padY)
elif c == KEY_DOWN:
screenPosition = safeMoveCursor(stdscr, "down", screenPosition, screenSize, cursorPosition, padX, padY)
elif c == KEY_LEFT:
screenPosition = safeMoveCursor(stdscr, "left", screenPosition, screenSize, cursorPosition, padX, padY)
elif c == KEY_RIGHT:
screenPosition = safeMoveCursor(stdscr, "right", screenPosition, screenSize, cursorPosition, padX, padY)
elif c == KEY_BACKSPACE or c == KEY_DL or c == KEY_DC:
oppDirection = ""
if direction == "up":
oppDirection = "down"
elif direction == "down":
oppDirection = "up"
elif direction == "left":
oppDirection = "right"
else:
oppDirection = "left"
screenPosition = safeMoveCursor(stdscr, oppDirection, screenPosition, screenSize, cursorPosition, padX, padY)
cursorPosition = list(stdscr.getyx())
data[cursorPosition[0] + screenPosition[0]][cursorPosition[1] + screenPosition[1]] = " "
else:
f = open(sys.argv[1], "w") #MOVE THIS SOMEWHERE ELSE
f.write("\n".join(trimFile(["".join(d) for d in data])))
f.close()
if isAscii(c):
data[cursorPosition[0] + screenPosition[0]][cursorPosition[1] + screenPosition[1]] = chr(c)
direction = changeDirection(chr(c), direction)
screenPosition = safeMoveCursor(stdscr, direction, screenPosition, screenSize, cursorPosition, padX, padY)
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Please provide an file as a command line argument.")
sys.exit()
wrapper(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment