import encodings.aliases from helpers import HButtonSelect, spawnDialog, spawnSimplePopup, errorTxt from prompt_toolkit.application.current import get_app from prompt_toolkit.layout import FormattedTextControl from prompt_toolkit.layout.containers import HSplit, Window from prompt_toolkit.layout.dimension import Dimension as D from prompt_toolkit.widgets import Box, Button, Dialog, Label, RadioList, ValidationToolbar class InterfaceSettings: def __init__(self,intset,guestLogin,restart,server,floats): self.intset = intset self.guestLogin = guestLogin self.server = server self.floats = floats self.el = HSplit([ Window(FormattedTextControl(text=self.showISettings,focusable=False),dont_extend_height=True), Box(HButtonSelect([("Change color depth",self.changecolor),("Change text encoding",self.changeenc),("Apply changes",restart)]),padding=0), Label("\n\nYou will need to restart the interface or relog to apply any new changes."), ]) def showISettings(self): color = ({1: "1 bit (Monochrome)",4: "4 bits (ANSI/RGBI)",8: "8 bits",24: "24 bits (True Color)"})[self.intset["color"]] encoding = self.intset["encoding"] if encoding=="utf8": encoding="UTF-8 (Unicode)" if encoding=="ascii": encoding="ASCII (7-bit)" if encoding=="cp437": encoding="CP437 (Extended ASCII)" if encoding=="latin_1": encoding="Latin-1/ISO-8859-1 (Ext. ASCII)" return [ ("","Color depth: "),("bold",color),("","\n"), ("","Text encoding: "),("bold",encoding),("","\n") ] async def _updateisettings(self): if self.guestLogin: return res = await self.server.setisettings("basic",self.intset) if res["type"]=="error": if res["server"]: await spawnSimplePopup("Error",f"{errorTxt(res)}\nYour changes might not have been sent to the internal server. Please try again.",self.floats) else: await spawnSimplePopup("Error",errorTxt(res),self.floats) def changecolor(self): def dlgclose(): despawn() self.intset["color"] = colorselect.current_value get_app().create_background_task(self._updateisettings()) ok_button = Button(text="OK", handler=dlgclose, width=16) colorselect = RadioList([(24,"24 bits (True Color)"),(8,"8 bits (Paletted)"),(4,"4 bits (ANSI/RGBI)"),(1,"1 bit (Monochrome)")],default=self.intset["color"]) dlg = Dialog( title="Change color depth", body=HSplit( [ Label(text="Please select a color depth supported by this terminal:", dont_extend_height=True), colorselect, Label(text="Use 1 or 4 bits for compatibility with older terminals, otherwise 8 or 24 bits.", dont_extend_height=True), Label(text="The more bits in the color depth, the more colors there can be displayed on-screen."), ValidationToolbar(), ], padding=D(preferred=1, max=1), ), buttons=[ok_button], with_background=True, ) despawn = spawnDialog(dlg,self.floats) def changeencother(self): def dlgclose(): despawn() self.intset["encoding"] = encselect.current_value get_app().create_background_task(self._updateisettings()) ok_button = Button(text="OK", handler=dlgclose, width=16) encodingList = sorted(set(encodings.aliases.aliases.values())) for e in encodingList.copy(): try: ("test").encode(e,errors="replace") except LookupError: encodingList.remove(e) encselect = RadioList([(e,e) for e in encodingList],default=self.intset["encoding"]) dlg = Dialog( title="Change text encoding", body=HSplit( [ Label(text="Please select the encoding supported by this terminal:", dont_extend_height=True), encselect, Label(text="For more information about every supported encoding, open this webpage:\nhttps://docs.python.org/3.14/library/codecs.html#standard-encodings",dont_extend_height=True), ValidationToolbar(), ], padding=D(preferred=1, max=1), ), buttons=[ok_button], with_background=True, ) despawn = spawnDialog(dlg,self.floats) def changeenc(self): def dlgclose(): despawn() v = encselect.current_value if v=="other": self.changeencother() else: self.intset["encoding"] = v get_app().create_background_task(self._updateisettings()) ok_button = Button(text="OK", handler=dlgclose, width=16) default = self.intset["encoding"] if not default in ["utf8","ascii","latin_1","cp437"]: default = "other" encselect = RadioList([("utf8","UTF-8 (Unicode)"),("ascii","ASCII (7-bit)"),("latin_1","Latin-1/ISO-8859-1 (Ext. ASCII)"),("cp437","CP437 (Extended ASCII)"),("other","Other (Python-supported)")],default=default) dlg = Dialog( title="Change text encoding", body=HSplit( [ Label(text="Please select the encoding supported by this terminal:", dont_extend_height=True), encselect, Label(text="UTF-8 is generally reccomended for a normal modern terminal, and supports everything from Unicode.",dont_extend_height=True), Label(text="ASCII is very barebones but is supported by every terminal. Please use it if all else fails.", dont_extend_height=True), Label(text="You may see garbled text if you chose the wrong text encoding.",dont_extend_height=True), ValidationToolbar(), ], padding=D(preferred=1, max=1), ), buttons=[ok_button], with_background=True, ) despawn = spawnDialog(dlg,self.floats)