Skip to content

Commit

Permalink
Add new conversion tool
Browse files Browse the repository at this point in the history
  • Loading branch information
tehaleph committed Aug 6, 2022
1 parent 8d3c591 commit c1da868
Show file tree
Hide file tree
Showing 27 changed files with 226 additions and 1,652 deletions.
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.10.5
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Jinja2~=3.1.2
rich~=12.5.1
141 changes: 141 additions & 0 deletions tools/converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import logging
import os
import plistlib
import re
import sys

from jinja2 import Environment
from rich.progress import Progress

iterm_re = re.compile("(.+)\.itermcolors$")
iterm_ext = '.itermcolors'
xrdb_ext = '.xrdb'
hex_format = '%02x%02x%02x'

red_comp = 'Red Component'
green_comp = 'Green Component'
blue_comp = 'Blue Component'


class Converter(object):
def __init__(
self,
schemes: list,
templates: list,
loader: Environment,
bar: Progress,
path_to_iterm_schemes: str,
path_to_xrdb: str,
output_dir: str
):
self.bar = bar
self.loader = loader
self.iterm_dir = path_to_iterm_schemes
self.xrdb_dir = path_to_xrdb
self.out_dir = output_dir

self.templates = self.get_all_templates(templates)

self.schemes = schemes
if self.schemes is None:
self.schemes = self.get_all_schemes()

def get_all_schemes(self):
schemes = []

for name in os.listdir(self.iterm_dir):
schemes.append(iterm_re.match(name).group(1))

return schemes

def get_all_templates(self, templates_arg):
templates = {}

for template in self.loader.list_templates():
name, ext = os.path.splitext(template)

if templates_arg is None or name in templates_arg:
templates.update({name: template})

return templates

def parse_scheme(self, scheme):
colors_dict = {}

iterm_path = self.iterm_dir + scheme + iterm_ext
xrdb_path = self.xrdb_dir + scheme + xrdb_ext

if not os.path.isfile(iterm_path):
logging.error('Scheme ' + iterm_path + ' doesn\'t exist')
sys.exit(1)

with open(iterm_path, 'rb') as f:
plist = plistlib.load(f)

for color_name in plist:
color_components = plist[color_name]
color_hex, rgb = self.calculate_color(color_components)
colors_dict[color_name.replace(' ', '_')] = {
'hex': color_hex,
'rgb': ','.join(map(lambda x: str(x), rgb))
}

f.close()

# xrdb files were used to generate themes, now it is not needed
# but in case someone uses them for other purposes, these files continue to be saved
if not os.path.isfile(xrdb_path):
self.generate_xrdb_file(xrdb_path, colors_dict)

return colors_dict

def calculate_color(self, color_components):
r = round(color_components[red_comp] * 255)
g = round(color_components[green_comp] * 255)
b = round(color_components[blue_comp] * 255)
rgb = (r, g, b)
color_hex = hex_format % rgb

return color_hex, rgb

def generate_xrdb_file(self, xrdb_path, hex_dict):
with open(xrdb_path, 'w') as f:
for color_name in hex_dict:
color_hex = hex_dict[color_name]
f.write('#define ' + color_name.replace(' ', '_') + ' #' + color_hex['hex'] + '\n')

f.close()

def generate_from_template(self, task_id, colors, template):
self.bar.update(task_id, total=len(self.schemes))

t = self.loader.get_template(self.templates[template])
name, ext = os.path.splitext(self.templates[template])

self.bar.start_task(task_id)

for scheme in colors:
data = colors[scheme]
data['scheme_name'] = scheme
result = t.render(data)
destination = self.out_dir + template + '/' + scheme + ext
f = open(destination, 'w')
f.write(result)
f.close()
self.bar.update(task_id, advance=1)

def run(self):
colors = {}

with self.bar:
task_id = self.bar.add_task("process", template="Parse iTerm schemes", start=False)
self.bar.update(task_id, total=len(self.schemes))
self.bar.start_task(task_id)

for scheme in self.schemes:
colors[scheme] = self.parse_scheme(scheme)
self.bar.update(task_id, advance=1)

for template in self.templates:
task_id_tmp = self.bar.add_task("process", template="Generating " + template, start=False)
self.generate_from_template(task_id_tmp, colors, template)
82 changes: 82 additions & 0 deletions tools/gen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env python
"""iTerm2 Schemes Converter Tool
usage: gen.py [-h] [-s [SCHEME [SCHEME ...]]] [-t [TEMPLATE [TEMPLATE ...]]]
A script for generating themes based on a set of schemes and templates.
If run with no arguments, everything will be re-generated.
optional arguments:
-h, --help show this help message and exit
-s [SCHEME [SCHEME ...]], --scheme [SCHEME [SCHEME ...]]
list of schemes for which themes will be generated, default: all
-t [TEMPLATE [TEMPLATE ...]], --template [TEMPLATE [TEMPLATE ...]]
list of templates for which themes will be generated, default: all
Author: Mikhail Shlyakhov <mike@aleph.space>
Based on code by:
Alexey Ten <alexeyten@gmail.com>
Antenore Gatta <antenore@simbiosi.org>
Caesar Kabalan <caesar.kabalan@gmail.com>
gBopHuk <gbophuk_alt@mail.ru>
hhchung <hhchung@users.noreply.github.com>
Jonathan Couldridge <beforan86@gmail.com>
Justin Grote <JustinGrote@users.noreply.github.com>
Leiser Fernández Gallo <leiserfg@gmail.com>
Mike Wallio <miwalli@microsoft.com>
Nicolas Cornette <nicolas.cornette@gmail.com>
Stéphane Travostino <steph@combo.cc>
Suraj N. Kurapati <sunaku@gmail.com>tig
Thomas Sarboni <max-k@post.com>
Tobias Kortkamp <t6@users.noreply.github.com>
Wez Furlong <wez@wezfurlong.org>
Xabier Larrakoetxea <slok69@gmail.com>
"""

import argparse

from jinja2 import Environment, FileSystemLoader
from rich.progress import Progress, TextColumn, BarColumn

from converter import Converter

if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='A script for generating themes based on a set of schemes and templates. ' +
'If run with no arguments, everything will be re-generated.')

parser.add_argument('-s', '--scheme', nargs='*', dest='scheme',
help='list of schemes for which themes will be generated, default: all')
parser.add_argument('-t', '--template', nargs='*', dest='template',
help='list of templates for which themes will be generated, default: all')

arguments = vars(parser.parse_args())

scheme_arg = arguments['scheme']
template = arguments['template']

file_system_loader = Environment(
loader=FileSystemLoader('templates/'),
trim_blocks=True,
lstrip_blocks=True,
keep_trailing_newline=True
)

progress_bar = Progress(
TextColumn('[bold green]{task.fields[template]}', justify='left'),
BarColumn(),
'[progress.percentage]{task.percentage:>3.1f}%'
)

converter = Converter(
schemes=arguments['scheme'],
templates=arguments['template'],
loader=file_system_loader,
bar=progress_bar,
path_to_iterm_schemes='../schemes/',
path_to_xrdb='../xrdb/',
output_dir='../'
)

converter.run()
16 changes: 0 additions & 16 deletions tools/iterm2xrdb

This file was deleted.

76 changes: 0 additions & 76 deletions tools/update_all.py

This file was deleted.

34 changes: 0 additions & 34 deletions tools/windowsterminal2vscode.ps1

This file was deleted.

30 changes: 0 additions & 30 deletions tools/windowsterminalinstaller.ps1

This file was deleted.

Loading

0 comments on commit c1da868

Please sign in to comment.