Compare commits

..

No commits in common. "main" and "1505c9ecb8a55ba389d63919c135c841a5fe3158" have entirely different histories.

3 changed files with 59 additions and 93 deletions

View file

@ -2,7 +2,7 @@
import json
with open("nboard.json", "r") as file:
with open('nboard.json', 'r') as file:
data = json.load(file)
print(len(data))
@ -11,5 +11,6 @@ data = {k: v for k, v in data.items() if v is not " "}
print(len(data))
with open("nboard.json", "w") as file:
with open('nboard.json', 'w') as file:
json.dump(data, file)

View file

@ -1,17 +1,20 @@
#!/usr/bin/env python3
import curses
def main(stdscr):
while True:
k = stdscr.getkey()
if k == "\x1b":
if k == '\x1b':
k=""
for i in range(5):
k += stdscr.getkey()
print(repr(k))
curses.wrapper(main)

98
nboard
View file

@ -3,19 +3,18 @@
import curses,time,json,sys,os,subprocess
dataPath = os.path.expanduser("~") + "/.nboard/nboard.json"
dataPath = os.path.expanduser('~')+'/.nboard/nboard.json'
allowedChars = " `~1234567890-=!@#$%^&*()_+qwertyuiop[]\\QWERTYUIOP{}|asdfghjkl;'ASDFGHJKL:\"zxcvbnm,./ZXCVBNM<>?"
data = {}
def main(stdscr):
curses.halfdelay(5)
curses.use_default_colors()
stdscr.erase()
stdscr.refresh()
k = "NOU"
k='NOU'
height, width = stdscr.getmaxyx()
vx = vy = tx = ty = nl = 0
@ -32,40 +31,40 @@ def main(stdscr):
height, width = stdscr.getmaxyx()
if k == "\x0c":
if k == '\x0c':
stdscr.clear()
# detect where to move cursor
if k == "KEY_UP":
if k == 'KEY_UP':
vy += -1
nl = vx
if k == "KEY_DOWN":
if k == 'KEY_DOWN':
vy += 1
nl = vx
if k == "KEY_LEFT":
if k == 'KEY_LEFT':
vx += -1
nl = vx
if k == "KEY_RIGHT":
if k == 'KEY_RIGHT':
vx += 1
nl = vx
if k == "[1;5A" or k == "kUP5":
if k == '[1;5A' or k == 'kUP5':
vy += 0-(height//2)
nl = vx
if k == "[1;5B" or k == "kDN5":
if k == '[1;5B' or k == 'kDN5':
vy += height//2
nl = vx
if k == "[1;5D" or k == "kLFT5":
if k == '[1;5D' or k == 'kLFT5':
vx += 0-(width//2)
nl = vx
if k == "[1;5C" or k == "kRIT5":
if k == '[1;5C' or k == 'kRIT5':
vx += width//2
nl = vx
if k == "\n":
if k == '\n':
vx = nl
vy += 1
if k == "KEY_BACKSPACE" or k == "\x7f":
if k == 'KEY_BACKSPACE' or k == '\x7f':
vx += -1
# make sure the cursor is on the screen
@ -81,17 +80,17 @@ def main(stdscr):
ty = cy + (vy) - (height-1) // 2
# get json data
with open(dataPath, "r") as openfile:
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
if k == " ":
if k == ' ':
data.pop(str((ty,tx)))
vx = vx+1
time.sleep(0)
with open(dataPath, "w") as outfile:
with open(dataPath, 'w') as outfile:
json.dump(data, outfile, indent=1)
tx = cx + (vx) - width // 2
@ -100,16 +99,12 @@ def main(stdscr):
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)), " "))
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, ctrl to go faster ".format(
tx, 1 - ty
)[: width - 1],
)
stdscr.addstr(height-1, 0, 'x: {}, y: {}, arrow keys to move, ctrl to go faster '.format(tx, 1-ty)[:width-1])
# move the cursor where its actually supposed to be
stdscr.move(cy,cx)
@ -119,7 +114,7 @@ def main(stdscr):
try:
k = stdscr.getkey()
except curses.error:
k = "NOU"
k = 'NOU'
time.sleep(0)
except json.decoder.JSONDecodeError:
time.sleep(0.1)
@ -128,19 +123,10 @@ def main(stdscr):
if __name__ == "__main__":
try:
dExists = os.path.isfile(dataPath)
if not dExists:
with open(dataPath, "w") as f:
f.write("{}")
dExists = True
except:
dExists = False
if (
not dExists
or len(sys.argv) > 1
and (sys.argv[1] == "--help" or sys.argv[1] == "help")
):
print(
"""
if not dExists or 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.
how to get started:
@ -162,39 +148,15 @@ hold ctrl while pressing the arrow keys to go faster
bugs and stuff:
please report any bugs to ~xfnw on IRC, anywhere you can find him, or
on the tildegit page, https://tildegit.org/xfnw/nboard
"""
)
elif len(sys.argv) > 1 and sys.argv[1] == "pull":
branches = (
subprocess.run(
[
"git",
"--git-dir=" + os.path.expanduser("~") + "/.nboard/.git",
"for-each-ref",
"refs/remotes",
"--format",
"%(refname)",
],
capture_output=True,
)
.stdout.decode()
.splitlines()
)
subprocess.run(
[
"git",
"--git-dir=" + os.path.expanduser("~") + "/.nboard/.git",
"fetch",
"--all",
]
)
subprocess.run(
["git", "--git-dir=" + os.path.expanduser("~") + "/.nboard/.git", "merge"]
+ branches
)
""")
elif len(sys.argv) > 1 and sys.argv[1] == 'pull':
branches=subprocess.run(['git','--git-dir='+os.path.expanduser('~')+'/.nboard/.git','for-each-ref','refs/remotes','--format','%(refname)'], capture_output=True).stdout.decode().splitlines()
subprocess.run(['git','--git-dir='+os.path.expanduser('~')+'/.nboard/.git','fetch','--all'])
subprocess.run(['git','--git-dir='+os.path.expanduser('~')+'/.nboard/.git','merge']+branches)
print("pulled all remotes!")
else:
try:
curses.wrapper(main)
except KeyboardInterrupt:
print("bye!")
print('bye!')