-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Windows Terminal adapter * [Windows Terminal] Get defaultProfile on globals * [Windows Terminal] Update with latest version - profiles.json to settings.json microsoft/terminal#5199 - Change current profile microsoft/terminal#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
- Loading branch information
1 parent
d7bba22
commit 3ef5930
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |