Skip to content

Commit

Permalink
Special-case MOTD in showMessage procedures.
Browse files Browse the repository at this point in the history
For a MOTD it is natural to use ASCII art but since messages in the chat
box are added using QT's HTML-like text fragments, multiple spaces are
merged together and the default variable-width font destroys alignment.

Therefore, when showing a MOTD we escape spaces with ` ` and wrap
everything in `<code>` tags to select a monospace font.

One problem is that MOTD is also used for showing warnings to the user,
such as the "new-syncplay-available-motd-message" message. These will
also be changed accordingly. If undesired, these warning should be sent
in a separate field.
  • Loading branch information
addap committed Dec 31, 2024
1 parent d6de53e commit 549426c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
4 changes: 2 additions & 2 deletions syncplay/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1687,10 +1687,10 @@ def showChatMessage(self, username, userMessage):
def setSSLMode(self, sslMode, sslInformation=""):
self.__ui.setSSLMode(sslMode, sslInformation)

def showMessage(self, message, noPlayer=False, noTimestamp=False, OSDType=constants.OSD_NOTIFICATION, mood=constants.MESSAGE_NEUTRAL):
def showMessage(self, message, noPlayer=False, noTimestamp=False, OSDType=constants.OSD_NOTIFICATION, mood=constants.MESSAGE_NEUTRAL, isMotd=False):
if not noPlayer:
self.showOSDMessage(message, duration=constants.OSD_DURATION, OSDType=OSDType, mood=mood)
self.__ui.showMessage(message, noTimestamp)
self.__ui.showMessage(message, noTimestamp=noTimestamp, isMotd=isMotd)

def updateAutoPlayState(self, newState):
self.__ui.updateAutoPlayState(newState)
Expand Down
2 changes: 1 addition & 1 deletion syncplay/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def handleHello(self, hello):
motd += "\n\n"
motd += getMessage("persistent-rooms-notice")
if motd:
self._client.ui.showMessage(motd, True, True)
self._client.ui.showMessage(motd, noPlayer=True, noTimestamp=True, isMotd=True)
self._client.ui.showMessage(getMessage("connected-successful-notification"))
self._client.connected()
self._client.sendFile()
Expand Down
2 changes: 1 addition & 1 deletion syncplay/ui/consoleUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def fileSwitchFoundFiles(self):
def setFeatures(self, featureList):
pass

def showMessage(self, message, noTimestamp=False):
def showMessage(self, message, noTimestamp=False, isMotd=False):
message = message.encode(sys.stdout.encoding, 'replace')
try:
message = message.decode('utf-8')
Expand Down
10 changes: 7 additions & 3 deletions syncplay/ui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@


class ConsoleInGUI(ConsoleUI):
def showMessage(self, message, noTimestamp=False):
self._syncplayClient.ui.showMessage(message, True)
def showMessage(self, message, noTimestamp=False, isMotd=False):
self._syncplayClient.ui.showMessage(message, noTimestamp=True, isMotd=isMotd)

def showDebugMessage(self, message):
self._syncplayClient.ui.showDebugMessage(message)
Expand Down Expand Up @@ -542,7 +542,7 @@ def setSSLMode(self, sslMode, sslInformation):
def getSSLInformation(self):
return self.sslInformation

def showMessage(self, message, noTimestamp=False):
def showMessage(self, message, noTimestamp=False, isMotd=False):
message = str(message)
username = None
messageWithUsername = re.match(constants.MESSAGE_WITH_USERNAME_REGEX, message, re.UNICODE)
Expand All @@ -552,6 +552,10 @@ def showMessage(self, message, noTimestamp=False):
message = message.replace("&", "&amp;").replace('"', "&quot;").replace("<", "&lt;").replace(">", "&gt;")
if username:
message = constants.STYLE_USER_MESSAGE.format(constants.STYLE_USERNAME, username, message)
# When showing a MOTD, escape spaces and use a monospace font to preserve the look of ASCII art.
if isMotd:
message = message.replace(" ", "&nbsp;")
message = "<code>{}</code>".format(message)
message = message.replace("\n", "<br />")
if noTimestamp:
self.newMessage("{}<br />".format(message))
Expand Down

0 comments on commit 549426c

Please sign in to comment.