summaryrefslogtreecommitdiff
path: root/curses/cui.py
diff options
context:
space:
mode:
authorLivio Recchia <recchialivio@libero.it>2020-02-10 23:06:34 +0100
committerLivio Recchia <recchialivio@libero.it>2020-02-10 23:06:34 +0100
commit9a13903a2f7d3a65fdf15a65fb59cccd622e2066 (patch)
tree9403b7dff39eb5e5d7fa0f79efb69b496add4c4b /curses/cui.py
parent11cc316b74d5f3f283413a33e7693b314741aa4a (diff)
downloadmanachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.tar.gz
manachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.tar.bz2
manachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.tar.xz
manachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.zip
Initial commit
Diffstat (limited to 'curses/cui.py')
-rw-r--r--curses/cui.py78
1 files changed, 78 insertions, 0 deletions
diff --git a/curses/cui.py b/curses/cui.py
new file mode 100644
index 0000000..5405765
--- /dev/null
+++ b/curses/cui.py
@@ -0,0 +1,78 @@
+#-*- coding: utf-8 -*-
+"""
+Curses-based console user interface for TMW chat client.
+"""
+
+import curses
+from curses.textpad import Textbox
+
+stdscr = None
+chatlog_win = None
+input_win = None
+players_win = None
+input_textbox = None
+
+
+def init():
+ global stdscr, chatlog_win, input_win, players_win, input_textbox
+
+ stdscr = curses.initscr()
+ curses.cbreak()
+ curses.noecho()
+ stdscr.keypad(1)
+
+ h, w = stdscr.getmaxyx()
+ PNW = 20 # player name width
+ INH = 4 # input window height
+
+ stdscr.vline(0, w - PNW - 1, curses.ACS_VLINE, h)
+ stdscr.hline(h - INH - 1, 0, curses.ACS_HLINE, w - PNW - 1)
+
+ chatlog_win = curses.newwin(h - INH - 1, w - PNW - 1, 0, 0)
+ input_win = curses.newwin(INH, w - PNW - 1, h - INH, 0)
+ players_win = curses.newwin(h, PNW, 0, w - PNW)
+
+ chatlog_win.idlok(1)
+ chatlog_win.scrollok(1)
+
+ players_win.idlok(1)
+ players_win.scrollok(1)
+
+ input_textbox = Textbox(input_win)
+ input_textbox.stripspaces = True
+
+ stdscr.noutrefresh()
+ input_win.noutrefresh()
+ players_win.noutrefresh()
+ chatlog_win.noutrefresh()
+
+ curses.doupdate()
+
+
+def chatlog_append(line):
+ if line[-1] != "\n":
+ line = line + "\n"
+ chatlog_win.addstr(line)
+ chatlog_win.refresh()
+
+
+def input_loop(callback):
+ def v(ch):
+ # chatlog_append(curses.keyname(ch))
+ if ch in (curses.KEY_ENTER, curses.ascii.NL):
+ return curses.ascii.BEL
+ return ch
+
+ cmd = ''
+ while cmd not in ('/exit', '/quit'):
+ cmd = input_textbox.edit(v).strip()
+ callback(cmd)
+ input_win.clear()
+ input_win.move(0, 0)
+
+
+def finalize():
+ stdscr.keypad(0)
+ curses.echo()
+ curses.nocbreak()
+ curses.endwin()