From 3ef59300b54c08de6fd8c70e49420669c1abc961 Mon Sep 17 00:00:00 2001 From: Kyle Seongwoo Jun Date: Sat, 2 May 2020 05:38:05 +0900 Subject: [PATCH] Support Windows Terminal (#183) * Add Windows Terminal adapter * [Windows Terminal] Get defaultProfile on globals * [Windows Terminal] Update with latest version - profiles.json to settings.json https://github.com/microsoft/terminal/pull/5199 - Change current profile https://github.com/microsoft/terminal/pull/4852 * [Windows Terminal] Rename adapter to pass tests * [Windows Terminal] Update with default profile settings https://github.com/microsoft/terminal/blob/master/doc/user-docs/UsingJsonSettings.md#default-settings --- .../terminal/adapters/windowsterminal.py | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 pokemonterminal/terminal/adapters/windowsterminal.py diff --git a/pokemonterminal/terminal/adapters/windowsterminal.py b/pokemonterminal/terminal/adapters/windowsterminal.py new file mode 100644 index 0000000..b6e2d5d --- /dev/null +++ b/pokemonterminal/terminal/adapters/windowsterminal.py @@ -0,0 +1,60 @@ +import json +import os +import re + +from . import TerminalProvider as _TProv + +class WindowsTerminalProvider(_TProv): + + def set_background_image(path: str): + profiles_path = os.environ['LOCALAPPDATA'] + '\\Packages\\Microsoft.WindowsTerminal_8wekyb3d8bbwe\\LocalState\\settings.json' + with open(profiles_path, 'r+') as json_file: + # read profiles.json + # remove comments from json to load + data = json.loads(WindowsTerminalProvider.comment_remover(json_file.read())) + + # migrate to defaults profile settings + profiles = data['profiles'] + if (isinstance(profiles, list)): + data['profiles'] = profiles = { + 'defaults': {}, + 'list': profiles + } + + # update defaults profile + profile = profiles['defaults'] + if (path is None): + del profile['backgroundImage'] + else: + profile['backgroundImage'] = path + + # write to file + # it lost orignal indent, comment, ... + json_file.seek(0) + json.dump(data, json_file, indent=4) + json_file.truncate() + + def comment_remover(text: str) -> str: + def replacer(match: re.Match): + s = match.group(0) + if s.startswith('/'): + return " " # note: a space and not an empty string + else: + return s + pattern = re.compile( + r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"', + re.DOTALL | re.MULTILINE + ) + return re.sub(pattern, replacer, text) + + def is_compatible() -> bool: + return "WT_SESSION" in os.environ + + def change_terminal(path: str): + WindowsTerminalProvider.set_background_image(path) + + def clear(): + WindowsTerminalProvider.set_background_image(None) + + def __str__(): + return "Windows Terminal" \ No newline at end of file