Skip to content

Input fixes #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: rewrite
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Discline.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

# Set terminal X11 window title
print('\33]0;Discline\a', end='', flush=True)
# Set the timeout for the ESC key to 25ms
os.environ.setdefault('ESCDELAY', '25')

gc.initClient()

Expand Down
31 changes: 11 additions & 20 deletions input/input_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,18 @@ def key_input():
editWin = gc.ui.editWin
call = (ui.draw_edit_win, True)
gc.ui_thread.funcs.append(call)
while call in gc.ui_thread.funcs or \
call[0].__name__ in gc.ui_thread.locks:
time.sleep(0.01)
while not gc.doExit:
prompt = gc.client.prompt
ch = editWin.getch()
if ch == -1 or not gc.ui.displayPanel.hidden():
ch = editWin.get_wch()
if not gc.ui.displayPanel.hidden():
time.sleep(0.01)
continue

if chr(ch) != '\n' and len(gc.ui.messageEdit.inputBuffer) > 0 and \
gc.ui.messageEdit.inputBuffer[0] != ord('/'):
if ch != '\n' and len(gc.ui.messageEdit.inputBuffer) > 0 and \
gc.ui.messageEdit.inputBuffer[0] != '/':
gc.typingBeingHandled = True
# prevents crashes when enter is hit and input buf is empty
if chr(ch) == '\n' and not gc.ui.messageEdit.inputBuffer:
if ch == '\n' and not gc.ui.messageEdit.inputBuffer:
continue
if ch == curses.KEY_PPAGE:
gc.ui.channel_log_offset -= settings["scroll_lines"]
Expand All @@ -49,25 +46,17 @@ def key_input():
ui.draw_screen()
continue
# if ESC is pressed, clear messageEdit buffer
elif ch == 27:
ch = editWin.getch()
if ch in (0x7f, ord('\b'), curses.KEY_BACKSPACE):
gc.ui.messageEdit.reset()
call = (ui.draw_edit_win, True)
gc.ui_thread.funcs.append(call)
while call in gc.ui_thread.funcs or \
call[0].__name__ in gc.ui_thread.locks:
time.sleep(0.01)
elif ch == chr(27):
gc.ui.messageEdit.reset()
call = (ui.draw_edit_win, True)
gc.ui_thread.funcs.append(call)
continue
ret = gc.ui.messageEdit.addKey(ch)
if ret is not None:
input_handler(ret)
gc.ui.messageEdit.reset()
call = (ui.draw_edit_win, True)
gc.ui_thread.funcs.append(call)
while not gc.doExit and (call in gc.ui_thread.funcs or \
call[0].__name__ in gc.ui_thread.locks):
time.sleep(0.01)
log("key_input finished")
gc.tasksExited += 1

Expand Down Expand Up @@ -152,6 +141,8 @@ def parseCommand(command, arg=None):
elif command in ("quit", "exit"):
try: gc.exit_thread.start()
except SystemExit: pass
while not gc.doExit:
time.sleep(0.01)
elif command in ("help", 'h'): ui.draw_help()
elif command in ("guilds", "glds", "servers", "servs"): ui.draw_guildlist()
elif command in ("channels", "chans"): ui.draw_channellist()
Expand Down
24 changes: 16 additions & 8 deletions input/messageEdit.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import curses
from utils.log import log

class MessageEdit:
def __init__(self, termWidth):
self.curPos = 0
self.startPos = 0 # relative to len(inputBuffer)
self.termWidth = self.width = termWidth
self.inputBuffer = []
self.inputBuffer = ""

def reset(self):
self.curPos = 0
self.startPos = 0
del(self.inputBuffer[:])
self.inputBuffer = ""

def resize(self):
pass
Expand All @@ -19,7 +20,7 @@ def setPrompt(self, prompt):
self.width = self.termWidth - (len(prompt) + 5) - 1

def getCurrentData(self):
return (bytearray(self.inputBuffer).decode("utf-8"), self.curPos, self.startPos)
return (self.inputBuffer, self.curPos, self.startPos)

def addKey(self, ch):
# check if character is function character
Expand All @@ -31,6 +32,8 @@ def addKey(self, ch):
# if inputBuffer fits into line
self.curPos = len(self.inputBuffer)
self.startPos = self.curPos - self.width
if self.startPos < 0:
self.startPos = 0
elif ch == curses.KEY_LEFT:
# curPos is greater than 0
if self.curPos > 0:
Expand All @@ -44,17 +47,22 @@ def addKey(self, ch):
# TODO: Implement edit last message on KEY_UP
elif ch == curses.KEY_UP or ch == curses.KEY_DOWN:
pass
elif ch in (0x7f, ord('\b'), curses.KEY_BACKSPACE):
elif ch in (chr(0x7f), '\b', curses.KEY_BACKSPACE):
if self.curPos > 0:
self.inputBuffer.pop(self.curPos-1)
self.inputBuffer = self.inputBuffer[:self.curPos - 1] + self.inputBuffer[self.curPos:]
self.curPos -= 1
if self.startPos > 0 and self.curPos == self.startPos:
self.startPos -= 1
elif ch == ord('\n'):
return bytearray(self.inputBuffer).decode("utf-8")
elif ch == curses.KEY_DC:
if self.curPos < len(self.inputBuffer):
self.inputBuffer = self.inputBuffer[:self.curPos] + self.inputBuffer[self.curPos + 1:]
elif ch == '\n':
return self.inputBuffer
elif not isinstance(ch, str):
log("Unknown key " + str(ch))
# Normal text
else:
self.inputBuffer.insert(self.curPos, ch)
self.inputBuffer = self.inputBuffer[:self.curPos] + ch + self.inputBuffer[self.curPos:]
self.curPos += 1
if self.curPos-self.startPos > self.width:
self.startPos += 1
61 changes: 24 additions & 37 deletions ui/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ def makeBottomWin(self, resize=False):
return
content = curses.newwin(1,self.max_x, self.max_y-1,0)
content.keypad(True)
content.nodelay(True)
self.editWin = content

self.contentWins.append(content)
Expand Down Expand Up @@ -356,10 +355,9 @@ def set_display(string, attrs=0):
display.addstr("(press q to quit this dialog)")
display.refresh()
while True:
ch = display.getch()
if ch == ord('q'):
ch = display.get_wch()
if ch == 'q':
break
time.sleep(0.1)
display.clear()
gc.ui.toggleDisplay()
draw_screen()
Expand Down Expand Up @@ -504,10 +502,9 @@ def draw_guildlist():
if len(gc.client.guilds) == 0:
display.addstr("Error: You are not in any guilds.", gc.ui.colors["red"])
while True:
ch = display.getch()
if ch == ord('q'):
ch = display.get_wch()
if ch == 'q':
break
time.sleep(0.1)
display.clear()
gc.ui.toggleDisplay()
return
Expand Down Expand Up @@ -554,8 +551,8 @@ def draw_guildlist():
color = serv[1]
display.addstr(2+serv_id,0, serv[0], color)
display.addstr(2+serv_id+2,0, "(press q to quit this dialog)", gc.ui.colors["green"])
ch = display.getch()
if ch == ord('q'):
ch = display.get_wch()
if ch == 'q':
break
if len(buf) > (gc.ui.max_y-5):
if ch == curses.KEY_UP:
Expand All @@ -566,7 +563,6 @@ def draw_guildlist():
line_offset = 0
elif len(buf) > (gc.ui.max_y-5) and line_offset > (len(buf)-(gc.ui.max_y-5)):
line_offset = len(buf)-(gc.ui.max_y-5)
time.sleep(0.01)
gc.ui.toggleDisplay()
gc.ui.refreshAll()
draw_screen()
Expand All @@ -578,21 +574,19 @@ def draw_channellist():
if len(gc.client.guilds) == 0:
display.addstr("Error: You are not in any guilds.", gc.ui.colors["red"])
while True:
ch = display.getch()
if ch == ord('q'):
ch = display.get_wch()
if ch == 'q':
break
time.sleep(0.1)
display.clear()
gc.ui.toggleDisplay()
return

if len(gc.client.current_guild.channels) == 0:
display.addstr("Error: Does this guild not have any channels?", gc.ui.colors["red"])
while True:
ch = display.getch()
if ch == ord('q'):
ch = display.get_wch()
if ch == 'q':
break
time.sleep(0.1)
display.clear()
gc.ui.toggleDisplay()
return
Expand All @@ -616,8 +610,8 @@ def draw_channellist():
color = chan[1]
display.addstr(2+chan_id,0, chan[0], color)
display.addstr(2+chan_id+2,0, "(press q to quit this dialog)", gc.ui.colors["green"])
ch = display.getch()
if ch == ord('q'):
ch = display.get_wch()
if ch == 'q':
break
if len(buf) > (gc.ui.max_y-5):
if ch == curses.KEY_UP:
Expand All @@ -628,7 +622,6 @@ def draw_channellist():
line_offset = 0
elif len(buf) > (gc.ui.max_y-5) and line_offset > (len(buf)-(gc.ui.max_y-5)):
line_offset = len(buf)-(gc.ui.max_y-5)
time.sleep(0.01)
gc.ui.toggleDisplay()
gc.ui.refreshAll()
draw_screen()
Expand All @@ -640,10 +633,9 @@ def draw_emojilist():
if len(gc.client.guilds) == 0:
display.addstr("Error: You are not in any guilds.", gc.ui.colors["red"])
while True:
ch = display.getch()
if ch == ord('q'):
ch = display.get_wch()
if ch == 'q':
break
time.sleep(0.1)
display.clear()
gc.ui.toggleDisplay()
draw_screen()
Expand Down Expand Up @@ -671,8 +663,8 @@ def draw_emojilist():
color = emoji[1]
display.addstr(2+emoji_id,0, emoji[0], color)
display.addstr(2+emoji_id+2,0, "(press q to quit this dialog)", gc.ui.colors["green"])
ch = display.getch()
if ch == ord('q'):
ch = display.get_wch()
if ch == 'q':
break
if len(emojis) > (gc.ui.max_y-5):
if ch == curses.KEY_UP:
Expand All @@ -683,7 +675,6 @@ def draw_emojilist():
line_offset = 0
elif len(emojis) > (gc.ui.max_y-5) and line_offset > (len(emojis)-(gc.ui.max_y-5)):
line_offset = len(emojis)-(gc.ui.max_y-5)
time.sleep(0.01)
gc.ui.toggleDisplay()
gc.ui.refreshAll()
draw_screen()
Expand All @@ -694,10 +685,9 @@ def draw_userlist():
if len(gc.client.guilds) == 0:
display.addstr("Error: You are not in any guilds.", gc.ui.colors["red"])
while True:
ch = display.getch()
if ch == ord('q'):
ch = display.get_wch()
if ch == 'q':
break
time.sleep(0.1)
display.clear()
gc.ui.toggleDisplay()
draw_screen()
Expand All @@ -706,10 +696,9 @@ def draw_userlist():
if len(gc.client.current_guild.channels) == 0:
display.addstr("Error: Does this guild not have any channels?", gc.ui.colors["red"])
while True:
ch = display.getch()
if ch == ord('q'):
ch = display.get_wch()
if ch == 'q':
break
time.sleep(0.1)
display.clear()
gc.ui.toggleDisplay()
draw_screen()
Expand Down Expand Up @@ -765,8 +754,8 @@ def draw_userlist():
color = user[1]
display.addstr(2+user_id,0, user[0], color)
display.addstr(2+user_id+2,0, "(press q to quit this dialog)", gc.ui.colors["green"])
ch = display.getch()
if ch == ord('q'):
ch = display.get_wch()
if ch == 'q':
break
if len(buf) > (gc.ui.max_y-5):
if ch == curses.KEY_UP:
Expand All @@ -777,7 +766,6 @@ def draw_userlist():
line_offset = 0
elif len(buf) > (gc.ui.max_y-5) and line_offset > (len(buf)-(gc.ui.max_y-5)):
line_offset = len(buf)-(gc.ui.max_y-5)
time.sleep(0.01)
gc.ui.toggleDisplay()
gc.ui.refreshAll()
draw_screen()
Expand Down Expand Up @@ -851,8 +839,8 @@ def draw_help(terminateAfter=False):
display.addstr('-'*45, segment[1])
continue
display.addstr(segment[0] + ' ', segment[1])
ch = display.getch()
if ch == ord('q'):
ch = display.get_wch()
if ch == 'q':
break
if len(buf) > (gc.ui.max_y-5):
if ch == curses.KEY_UP:
Expand All @@ -867,7 +855,6 @@ def draw_help(terminateAfter=False):
line_offset = 0
elif len(buf) > (gc.ui.max_y-5) and line_offset > (len(buf)-(gc.ui.max_y-5)):
line_offset = len(buf)-(gc.ui.max_y-5)
time.sleep(0.01)
if terminateAfter:
raise SystemExit
gc.ui.toggleDisplay()
Expand Down