nboard/nboard

133 lines
3.3 KiB
Plaintext
Raw Normal View History

2020-03-21 19:35:22 +00:00
#!/usr/bin/env python3
2020-03-22 10:37:00 +00:00
import curses,time,json,sys
2020-03-21 21:35:38 +00:00
2020-07-24 17:13:53 +00:00
dataPath = '/home/xfnw/nboard/nboard.json'
2020-03-27 00:41:42 +00:00
allowedChars = " `~1234567890-=!@#$%^&*()_+qwertyuiop[]\\QWERTYUIOP{}|asdfghjkl;'ASDFGHJKL:\"zxcvbnm,./ZXCVBNM<>?"
2020-03-21 22:19:10 +00:00
data = {}
2020-03-21 20:22:27 +00:00
def main(stdscr):
2020-03-22 10:37:00 +00:00
stdscr.erase()
2020-03-21 20:22:27 +00:00
stdscr.refresh()
2020-03-21 22:37:02 +00:00
k='NOU'
2020-03-21 21:35:38 +00:00
height, width = stdscr.getmaxyx()
vx = vy = tx = ty = nl = 0
2020-03-21 21:35:38 +00:00
cx = width // 2
cy = height // 2
2020-03-21 21:35:38 +00:00
stdscr.move(cy,cx)
while True:
cx = width // 2
cy = height // 2
2020-03-21 21:35:38 +00:00
stdscr.clear()
2020-03-21 20:22:27 +00:00
height, width = stdscr.getmaxyx()
2020-07-22 13:51:33 +00:00
2020-07-24 17:13:53 +00:00
if k == '\x1b':
k=""
for i in range(5):
k += stdscr.getkey()
2020-07-22 13:51:33 +00:00
2020-03-21 21:35:38 +00:00
# detect where to move cursor
2020-03-21 20:22:27 +00:00
if k == 'KEY_UP':
vy += -1
nl = vx
2020-03-21 21:35:38 +00:00
if k == 'KEY_DOWN':
vy += 1
nl = vx
2020-03-21 21:35:38 +00:00
if k == 'KEY_LEFT':
vx += -1
nl = vx
2020-03-21 21:35:38 +00:00
if k == 'KEY_RIGHT':
vx += 1
nl = vx
2020-07-24 17:13:53 +00:00
if k == '[1;5A' or k == 'kUP5':
2020-07-24 17:13:53 +00:00
vy += 0-(height//2)
nl = vx
if k == '[1;5B' or k == 'kDN5':
2020-07-24 17:13:53 +00:00
vy += height//2
nl = vx
if k == '[1;5D' or k == 'kLFT5':
2020-07-24 17:13:53 +00:00
vx += 0-(width//2)
nl = vx
if k == '[1;5C' or k == 'kRIT5':
2020-07-24 17:13:53 +00:00
vx += width//2
nl = vx
if k == '\n':
vx = nl
vy += 1
if k == 'KEY_BACKSPACE' or k == '\x7f':
vx += -1
2020-03-21 21:35:38 +00:00
# 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)
2020-03-21 21:51:42 +00:00
cy = min(height-2, cy)
2020-03-21 21:35:38 +00:00
# calculate true position
tx = cx + (vx) - width // 2
ty = cy + (vy) - (height-1) // 2
2020-03-21 21:51:42 +00:00
# get json data
2020-03-21 22:19:10 +00:00
with open(dataPath, 'r') as openfile:
data = json.load(openfile)
2020-03-21 21:51:42 +00:00
2020-03-21 22:19:10 +00:00
# if valid key pressed, write it
if k in allowedChars:
2020-03-21 22:19:10 +00:00
data[str((ty,tx))] = k
vx = vx+1
time.sleep(0)
2020-03-21 22:19:10 +00:00
with open(dataPath, 'w') as outfile:
json.dump(data, outfile)
tx = cx + (vx) - width // 2
2020-03-21 21:51:42 +00:00
# draw the screen
stdscr.move(0, 0)
for y in range(height-1):
for x in range(width):
2020-03-21 22:26:21 +00:00
stdscr.move(y,x)
2020-03-21 22:19:10 +00:00
stdscr.addstr(data.get(str((ty - cy + y,tx - cx + x)), ' '))
2020-03-21 21:51:42 +00:00
2020-03-21 21:35:38 +00:00
# display some info
2020-07-25 00:59:17 +00:00
stdscr.addstr(height-1, 0, 'x: {}, y: {}, arrow keys to move, ctrl to go faster'.format(tx, 1-ty)[:width-1])
2020-03-21 21:35:38 +00:00
# move the cursor where its actually supposed to be
stdscr.move(cy,cx)
2020-07-22 13:51:33 +00:00
#print(str(k)) # debug keycodes
2020-03-21 21:35:38 +00:00
2020-03-21 21:51:42 +00:00
k = stdscr.getkey()
time.sleep(0)
2020-03-21 20:22:27 +00:00
if __name__ == "__main__":
2020-03-22 10:37:00 +00:00
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.
2020-07-22 13:51:33 +00:00
use the arrow keys to move arount
2020-03-22 10:37:00 +00:00
once you are in the place you want, you can start typing!
2020-07-25 00:59:17 +00:00
hold ctrl while pressing the arrow keys to go faster
please report any bugs to ~xfnw on IRC, anywhere you can find him, or
on the tildegit page, https://tildegit.org/xfnw/nboard
2020-03-22 10:37:00 +00:00
""")
else:
try:
curses.wrapper(main)
except KeyboardInterrupt:
print('bye!')
2020-03-21 20:22:27 +00:00