nboard/nboard
lickthecheese 1dec01c4de fixed &
2020-03-23 17:58:30 -04:00

117 lines
3.2 KiB
Python
Executable file

#!/usr/bin/env python3
import curses,time,json,sys
dataPath = '/home/lickthecheese/nboard/nboard.json'
allowedChars = " `~1234567890-=!@#$%^&*()_+qwertyuiop[]\\QWERTYUIOP{}|asdfghjkl;'ASDFGHJKL:\"zxcvbnm,./ZXCVBNM<>?"
data = {}
def main(stdscr):
stdscr.erase()
stdscr.refresh()
k='NOU'
height, width = stdscr.getmaxyx()
vx = vy = tx = ty = 0
cx = width // 2
cy = height // 2
stdscr.move(cy,cx)
while True:
stdscr.clear()
height, width = stdscr.getmaxyx()
# detect where to move cursor
if k == 'KEY_UP':
cy += -1
if k == 'KEY_DOWN':
cy += 1
if k == 'KEY_LEFT':
cx += -1
if k == 'KEY_RIGHT':
cx += 1
# enter view move mode
if k == '&':
stdscr.addstr(height-1, 0, 'Welcome to switch view mode. press an arrow key to move the view or a if you wanted a &'[:width-1])
k = stdscr.getkey()
if k == 'a':
k = '&'
if k == 'KEY_UP':
vy += -1
cy = height
if k == 'KEY_DOWN':
vy += 1
cy = 0
if k == 'KEY_LEFT':
vx += -1
cx = width
if k == 'KEY_RIGHT':
vx += 1
cx = 0
# make sure the cursor is on the screen
# this is not nessesary for the view as it is infinite
cx = max(0, cx)
cx = min(width-1, cx)
cy = max(0, cy)
cy = min(height-2, cy)
# calculate true position
tx = cx + (vx * width) - width // 2
ty = cy + (vy * height) - (height-1) // 2
# get json data
with open(dataPath, 'r') as openfile:
data = json.load(openfile)
# if valid key pressed, write it
if k in allowedChars:
data[str((ty,tx))] = k
cx = min(width-1, cx+1)
with open(dataPath, 'w') as outfile:
json.dump(data, outfile)
tx = cx + (vx * width) - width // 2
# draw the screen
stdscr.move(0, 0)
for y in range(height-1):
for x in range(width):
stdscr.move(y,x)
stdscr.addstr(data.get(str((ty - cy + y,tx - cx + x)), ' '))
# display some info
stdscr.addstr(height-1, 0, 'x: {}, y: {}, arrow keys to move, & key to move to the next pane'.format(tx, ty)[:width-1])
# move the cursor where its actually supposed to be
stdscr.move(cy,cx)
#print(k) # debug keycodes
k = stdscr.getkey()
if __name__ == "__main__":
if len(sys.argv) > 1 and (sys.argv[1] == '--help' or sys.argv[1] == 'help'):
print("""
nboard is like yourworldoftext.com, except its in a terminal.
use the arrow keys to move arount, and to go to the next 'pane',
press & (ampersand, usually shift+7 on standard us keyboards) and then
an arrow key
once you are in the place you want, you can start typing!
please report any bugs to ~lickthecheese on IRC or on the github page,
https://github.com/LickTheCheese/nboard
""")
else:
try:
curses.wrapper(main)
except KeyboardInterrupt:
print('bye!')