Created
March 30, 2024 23:23
-
-
Save Ferdi265/f1710cff553375216536a83adcbcbb1d to your computer and use it in GitHub Desktop.
Convert Konsole colorscheme to VS Code settings
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
#!/usr/bin/env python | |
import sys | |
from typing import Optional, TypeAlias | |
Color: TypeAlias = tuple[int, int, int] | |
def parse_rgbcolor(color: str) -> Color: | |
r, g, b = color.split(",") | |
return int(r), int(g), int(b) | |
def format_hexcolor(color: Color) -> str: | |
return "#" + "".join(f"{c:02x}" for c in color) | |
def parse_konsole_colorscheme(file: str) -> dict[str, Color]: | |
with open(file, "r") as f: | |
data = f.read() | |
colorscheme = {} | |
section = None | |
for line in data.split("\n"): | |
# parse section headers | |
if line.startswith("["): | |
section = line[1:-1] | |
continue | |
# ignore values before a section | |
if section is None: | |
continue | |
# ignore lines without key value pairs | |
if "=" not in line: | |
continue | |
key, value = line.split("=", maxsplit = 1) | |
if key == "Color": | |
colorscheme[section] = parse_rgbcolor(value) | |
return colorscheme | |
COLORNAMES = [ | |
"Black", "Red", "Green", "Yellow", "Blue", "Magenta", "Cyan", "White" | |
] | |
def convert_colorname_to_vscode(name: str) -> Optional[str]: | |
faint = "Faint" in name | |
bright = "Intense" in name | |
name = name.replace("Faint", "").replace("Intense", "").replace("Color", "").lower() | |
# handle background and foreground colors | |
if name == "background" or name == "foreground": | |
if faint or bright: | |
return None | |
else: | |
return name | |
if faint: | |
return None | |
else: | |
index = int(name) | |
return ("ansiBright" if bright else "ansi") + COLORNAMES[index] | |
def print_vscode_colorscheme(colorscheme: dict[str, Color]): | |
for key, value in colorscheme.items(): | |
name = convert_colorname_to_vscode(key) | |
if name is None: | |
continue | |
print(f'"terminal.{name}": "{format_hexcolor(value)}",') | |
def main(): | |
args = sys.argv[1:] | |
if len(args) != 1: | |
print("usage: konsole2vscode <theme.colorscheme> [out.json]") | |
sys.exit(1) | |
colorscheme = parse_konsole_colorscheme(args[0]) | |
print_vscode_colorscheme(colorscheme) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment