summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortroido <troido@hotmail.com>2018-04-18 23:59:40 +0200
committertroido <troido@hotmail.com>2018-04-18 23:59:40 +0200
commit1bf981b4836dc7d48535fcfcdf0d1a4e6d8ffb84 (patch)
treecc52a29decf1721002aa27c4b9d144ab40a0b7a0
parent757ad925b47d24b1d0714671b8f04147a5748480 (diff)
client can now read strings from chat input
-rw-r--r--asciifarm/client/commandhandler.py2
-rw-r--r--asciifarm/client/display/display.py5
-rw-r--r--asciifarm/client/display/textinput.py29
-rw-r--r--asciifarm/client/display/window.py4
-rw-r--r--asciifarm/client/inputhandler.py17
5 files changed, 33 insertions, 24 deletions
diff --git a/asciifarm/client/commandhandler.py b/asciifarm/client/commandhandler.py
index 7eefe5b..ebb773e 100644
--- a/asciifarm/client/commandhandler.py
+++ b/asciifarm/client/commandhandler.py
@@ -62,7 +62,7 @@ class CommandHandler:
self.execute(action)
def runInput(self):
- self.client.inputHandler.typing = True
+ self.client.inputHandler.startTyping()
def select(self, widget, value, relative=False, modular=False):
self.client.display.getWidget(widget).select(value, relative, modular)
diff --git a/asciifarm/client/display/display.py b/asciifarm/client/display/display.py
index 8056a14..85ee4b1 100644
--- a/asciifarm/client/display/display.py
+++ b/asciifarm/client/display/display.py
@@ -126,9 +126,8 @@ class Display:
"""This returns the character belonging to some spritename. This does not read a character"""
return self.characters.get(sprite, self.defaultChar)
- def getString(self):
- """This does actually read input"""
- return str(self.getWidget("textinput").getString(), "utf-8")
+ def setInputString(self, string, cursor):
+ self.getWidget("textinput").setText(string, cursor)
def update(self):
changed = False
diff --git a/asciifarm/client/display/textinput.py b/asciifarm/client/display/textinput.py
index 621214e..38d69ed 100644
--- a/asciifarm/client/display/textinput.py
+++ b/asciifarm/client/display/textinput.py
@@ -4,28 +4,23 @@ import curses
class TextInput:
def __init__(self):
- self.reading = False
self.widget = None
+ self.text = ""
+ self.cursor = 0
def setWidget(self, widget):
self.widget = widget
- def getString(self):
+ def setText(self, text, cursor):
+ self.text = text
+ self.cursor = cursor
+ self.widget.change()
+
+ def update(self):
win = self.widget.getWin()
- if not win:
- return None
- self.reading = True
- curses.echo()
- curses.nocbreak()
- win.addLine((0, 0), ">")
- string = win.getStr((2,0))
- curses.noecho()
- curses.cbreak()
- self.reading = False
+ width, height = win.getSize()
win.erase()
+ win.addLine((0, 0), self.text)
+ if self.cursor >= 0 and self.cursor <= len(self.text):
+ win.setAttr((self.cursor, 0), curses.A_REVERSE)
win.noutrefresh()
- self.widget.doUpdate()
- return string
-
- def update(self):
- pass
diff --git a/asciifarm/client/display/window.py b/asciifarm/client/display/window.py
index f54fee3..7e19942 100644
--- a/asciifarm/client/display/window.py
+++ b/asciifarm/client/display/window.py
@@ -71,5 +71,9 @@ class Window:
x, y = pos
return self.win.getch(y, x)
+ def setAttr(self, pos, attr, num=1):
+ x, y = pos
+ self.win.chgat(y, x, num, attr)
+
diff --git a/asciifarm/client/inputhandler.py b/asciifarm/client/inputhandler.py
index 1d907e9..5296f9d 100644
--- a/asciifarm/client/inputhandler.py
+++ b/asciifarm/client/inputhandler.py
@@ -1,8 +1,11 @@
-from commandhandler import CommandHandler
-from .keynames import nameFromKey
import curses
import curses.ascii
+import shlex
+
+from commandhandler import CommandHandler, InvalidCommandException
+from .keynames import nameFromKey
+
class InputHandler:
@@ -38,6 +41,12 @@ class InputHandler:
else:
self.commandHandler.chat(message)
+ def startTyping(self):
+ self.typing = True
+ self.showString()
+
+ def showString(self):
+ self.client.display.setInputString(self.string, self.cursor if self.typing else -1)
def addKey(self, key):
if curses.ascii.isprint(key):
@@ -47,7 +56,7 @@ class InputHandler:
self.string = self.string[:self.cursor-1] + self.string[self.cursor:]
self.cursor = max(self.cursor - 1, 0)
elif key == curses.KEY_RIGHT:
- self.cursor = max(self.cursor + 1, len(self.string))
+ self.cursor = min(self.cursor + 1, len(self.string))
elif key == curses.KEY_LEFT:
self.cursor = max(self.cursor - 1, 0)
elif key == curses.KEY_DC:
@@ -68,6 +77,8 @@ class InputHandler:
self.typing = False
self.processString(message)
+ self.showString()
+