Guest User

Untitled

a guest
Jul 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. #! /usr/bin/env python3
  2.  
  3. def sign(x):
  4. return '-' if x < 0 else '+'
  5.  
  6. def txt2forth(src, dst, xoffset: int, yoffset: int, comment: str):
  7. lines = src.readlines()
  8. rows = len(lines)
  9. cols = max(map(len, lines))
  10. dst.write(': logo ( x y -- ) \\ {comment} ({rows} rows x {cols} columns)\n\n'.format(comment=comment,
  11. rows=rows, cols=cols))
  12. dst.write('\t{xabs} {xsign} swap {yabs} {ysign} swap\n\n'.format(xabs=abs(xoffset), xsign=sign(xoffset),
  13. yabs=abs(yoffset), ysign=sign(yoffset)))
  14. for line in lines[:-1]:
  15. dst.write('\t2dup at-xy ." {}" 1+\n'.format(line.replace('\n', '')))
  16. dst.write( '\t at-xy ." {}"\n\n'.format(lines[-1].replace('\n', '')))
  17. dst.write( '\t\\ Put the cursor back at the bottom\n\t0 25 at-xy\n;\n')
  18.  
  19. def main():
  20. import sys
  21.  
  22. if len(sys.argv) != 4:
  23. print("usage: {} xoffset yoffset comment")
  24. print(" Reads ASCII art from stdin, writes Forth to stdout")
  25. exit(1)
  26.  
  27. txt2forth(src = sys.stdin,
  28. dst = sys.stdout,
  29. xoffset = int(sys.argv[1]),
  30. yoffset = int(sys.argv[2]),
  31. comment = sys.argv[3])
  32.  
  33. if __name__ == "__main__":
  34. main()
Add Comment
Please, Sign In to add comment