added tool to generate Konsole colorschemes

This commit is contained in:
Stéphane Travostino 2015-04-15 02:27:29 +01:00
parent 3d561f0441
commit f467dcb5c6

120
tools/xrdb2konsole.py Executable file
View file

@ -0,0 +1,120 @@
#!/usr/bin/env python
# coding: utf-8
#
# This script converts xrdb (X11) color scheme format to Konsole color
# scheme format
#
# Usage:
# xrdb2konsole.py path/to/xrdb/files -d /konsole/schemes/output
#
# Author: Stéphane Travostino
# Adapted from xrdb2terminator by Xabier Larrakoetxea
import os
import sys
import re
import argparse
# Takes #000A0B and returns (0, 10, 11)
def hex_to_rgb(color):
return (int(color[1:3], 16), int(color[3:5], 16), int(color[5:7], 16))
def build_konsole_color(name, r, g, b):
return "[%s]\nColor=%d,%d,%d\n\n" % (name, r, g, b)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='Translate X color schemes to termiantor format')
parser.add_argument('xrdb_path', type=str, help='path to xrdb files')
parser.add_argument('-d', '--out-directory', type=str, dest='output_path',
help='path where terminator config files will be' +
' created, if not provided then will be printed')
args = parser.parse_args()
# The regexes to match the colors
color_regex = re.compile("#define +Ansi_(\d+)_Color +(#[A-Fa-f0-9]{6})")
bg_regex = re.compile("#define +Background_Color +(#[A-Fa-f0-9]{6})")
fg_regex = re.compile("#define +Foreground_Color +(#[A-Fa-f0-9]{6})")
bold_regex = re.compile("#define +Bold_Color +(#[A-Fa-f0-9]{6})")
cursor_regex = re.compile("#define +Cursor_Color +(#[A-Fa-f0-9]{6})")
# File regex
xrdb_regex = re.compile("(.+)\.[xX][rR][dD][bB]")
for i in filter(lambda x: xrdb_regex.match(x), os.listdir(args.xrdb_path)):
name = xrdb_regex.match(i).group(1)
# Read XRDB file
with open(os.path.join(args.xrdb_path, i)) as f:
xrdb_data = f.read()
# Open output file
output = sys.stdout
if args.output_path:
dest = os.path.join(args.output_path, name)
output = open('{0}.colorscheme'.format(dest), 'w+')
else:
output.write('\n%s:\n' % name)
# Emit header
output.write("[General]\nDescription=%s\nOpacity=1\nWallpaper=\n\n" % name)
# Emit background color
bg_color = hex_to_rgb(bg_regex.search(xrdb_data).group(1))
output.write(build_konsole_color('Background', *bg_color))
output.write(build_konsole_color('BackgroundIntense', *bg_color))
# Emit foreground color
fg_color = hex_to_rgb(fg_regex.search(xrdb_data).group(1))
output.write(build_konsole_color('Foreground', *fg_color))
# Emit bold color, if any
match = bold_regex.search(xrdb_data)
if match:
bold_color = hex_to_rgb(match.group(1))
output.write(build_konsole_color('ForegroundIntense', *bold_color))
else:
output.write(build_konsole_color('ForegroundIntense', *fg_color))
# Emit other colors
for match in color_regex.findall(xrdb_data):
color_index = int(match[0])
color_rgb = hex_to_rgb(match[1])
color_name = 'Color%d' % color_index if color_index < 8 else 'Color%dIntense' % (color_index - 8)
output.write(build_konsole_color(color_name, *color_rgb))
# Search palette
# colors = sorted(filter(lambda x: color_regex.match(x), lines),
# key=lambda x: int(color_regex.match(x).group(1)))
#
# # Create the color string
# colors = ":".join(map(lambda x: color_regex.match(x).group(2), colors))
#
# scheme = """
#[[{name}]]
# palette = "{pl}"
# background_color = "{bg}"
# cursor_color = "{cr}"
# foreground_color = "{fg}"
# background_image = None
#"""
#
# output = scheme.format(name=xrdb_regex.match(i).group(1),
# pl=colors,
# bg=bg_color,
# cr=cursor_color,
# fg=fg_color)
#
# if not args.output_path:
# print(output)
# else:
# dest = os.path.join(args.output_path, xrdb_regex.match(i).group(1))
# with open('{0}.config'.format(dest), 'w+') as f:
# f.write(output)