Fixed several bugs

This commit is contained in:
Carlos Sanchez 2023-05-01 17:23:53 -04:00
parent 63a8f84017
commit 4f3c0fe8cd

24
main.py
View File

@ -5,6 +5,7 @@ import logging
import getpass
import textwrap
import threading
import platform
import re
import toml
import readchar
@ -17,9 +18,11 @@ from colorama import Fore, Back, Style, init as colorama_init
import contentapi
import myutils
VERSION="0.1.0" # Arbitrary but whatever
CONFIGFILE="config.toml"
MAXTITLE=25
MAXMESSAGEWIDTH=80 # May instead check the console window
MSGPREFIX=Back.GREEN + " " + Back.RESET + " "
# The entire config object with all defaults
config = {
@ -30,6 +33,7 @@ config = {
"default_markup" : "plaintext",
"expire_seconds" : 31536000, # 365 days in seconds, expiration for token
"appear_in_global" : False,
"print_status_after_insert" : True,
"tokenfile" : ".qcstoken"
}
@ -47,7 +51,7 @@ commands = OrderedDict([
def main():
print("Program start")
print("Program start: %s" % VERSION)
win_unicode_console.enable()
colorama_init() # colorama init
@ -105,6 +109,8 @@ def ws_onopen(ws):
# The infinite input loop! Or something!
while True:
ws.pause_output = False # Allow arbitrary output again. Do this BEFORE other stuff for race conditions
# We can be certain that at this point, there is no user input (because this loop is all there is)
# As such, just dump the buffer
for output in ws.output_buffer:
@ -116,8 +122,13 @@ def ws_onopen(ws):
print_statusline(ws)
printstatus = False # Assume we are not printing the status every time (it's kinda annoying)
ws.pause_output = False # Allow arbitrary output again
key = readchar.readkey()
if platform.system().startswith("Windows"):
import msvcrt
logging.debug("on windows, using msvcrt.getch")
key = msvcrt.getch().decode("utf-8")
else:
key = readchar.readkey()
ws.pause_output = True # Disable output for the duration of input handling
@ -144,7 +155,7 @@ def ws_onopen(ws):
if message:
ws.context.post_message(contentapi.comment_builder(message,
ws.current_room, ws.main_config["default_markup"], ws.user_info["avatar"]))
printstatus = True
printstatus = ws.main_config["print_status_after_insert"]
elif key == "t":
print(" -- Ignored WS Data (normal) --")
for key,value in ws.ignored.items():
@ -198,10 +209,10 @@ def ws_onmessage(ws, message):
if message and message["contentId"] == ws.current_room:
# OK we're DEFINITELY displaying it now
user = contentapi.get_user_or_default(objects["user"], message["createUserId"])
ws_print(ws, Fore.CYAN + Style.BRIGHT + user["username"] + " " + Style.DIM + "#%d" % user["id"] +
ws_print(ws, MSGPREFIX + Fore.CYAN + Style.BRIGHT + user["username"] + " " + Style.DIM + "#%d" % user["id"] +
Fore.MAGENTA + " " + message["createDate"] + " [%d]" % message["id"])
for t in textwrap.wrap(message["text"], width = MAXMESSAGEWIDTH):
ws_print(ws, t)
ws_print(ws, MSGPREFIX + t)
# Track ignored data
if result["type"] not in ws.ignored:
@ -266,6 +277,7 @@ def search(ws):
if match:
roomid = int(match.group(1))
set_current_room(ws, roomid)
return
elif searchterm:
# Go search for rooms and display them
result = ws.context.basic_search(searchterm)["objects"]["content"]