Created
January 7, 2025 10:47
-
-
Save SavingFrame/98f7009912912a3c06208785821ff79a to your computer and use it in GitHub Desktop.
Convert .itermcolors file to the ghostty theme.
This file contains hidden or 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 python3 | |
# | |
# Convert .itermcolors files to ghostty terminal color settings. | |
# Modded from https://gist.github.com/unglitched/379858c221885a62987b74e39a6c292b | |
# You can pass url link to the raw file via --url or filepath via --file. | |
# Example of usage: | |
# python3 iterm2ghostty.py --file One\ Dark.itermcolors > onedark | |
# python3 iterm2ghostty.py --url https://raw.githubusercontent.com/nathanbuchar/atom-one-dark-terminal/refs/heads/master/scheme/iterm/One%20Dark.itermcolors > onedark | |
import xml.etree.ElementTree as ET | |
import argparse | |
import urllib.request | |
import urllib.error | |
def rgb_to_hex(rgb): | |
return "%02x%02x%02x" % rgb | |
def download_file(url): | |
try: | |
with urllib.request.urlopen(url) as response: | |
file_data = response.read() | |
return file_data.decode("utf-8") | |
print(f"File successfully downloaded! Size: {len(file_data)} bytes") | |
except urllib.error.URLError as e: | |
print(f"Failed to download file: {e}") | |
def convert_tree(tree: ET.ElementTree): | |
keys = tree.findall("./dict/key") | |
dicts = tree.findall("./dict/dict") | |
conversion_table = { | |
"Background Color": "background", | |
"Cursor Color": "cursor-color", | |
"Foreground Color": "foreground", | |
"Selected Text Color": "selection-foreground", | |
"Selection Color": "selection-background", | |
} | |
for i in range(len(keys)): | |
b = int(float(dicts[i][3].text) * 255.0) | |
g = int(float(dicts[i][7].text) * 255.0) | |
r = int(float(dicts[i][9].text) * 255.0) | |
if keys[i].text.split()[1].isdigit(): | |
print( | |
"palette = {}=#{}".format( | |
keys[i].text.split()[1], rgb_to_hex((r, g, b)) | |
) | |
) | |
elif keys[i].text in conversion_table: | |
print( | |
"{} = {}".format(conversion_table[keys[i].text], rgb_to_hex((r, g, b))) | |
) | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--url", help="URL to download itermcolors file from") | |
parser.add_argument("--file", help="File to download itermcolors file from") | |
args = parser.parse_args() | |
if args.url: | |
file_str = download_file(args.url) | |
root = ET.fromstring(file_str) | |
elif args.file: | |
root = ET.parse(args.file) | |
else: | |
print("Please provide a URL or file argument") | |
exit(1) | |
convert_tree(root) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment