|
#!/usr/bin/python3 |
|
|
|
# Where to store kitty configuration |
|
color_file = '/home/kristijan/.cache/main_colorscheme.conf' |
|
# Hash with dark/light colorschemes, background type => vim colorscheme nane |
|
colorschemes = { |
|
'dark': 'gruvbox', |
|
'light': 'xcodelight', |
|
} |
|
|
|
import pynvim |
|
import argparse |
|
import os |
|
import sys |
|
from pathlib import Path |
|
|
|
parser = argparse.ArgumentParser() |
|
parser.add_argument('bg', nargs='?', default=None, choices=['dark', 'light', 'toggle']) |
|
|
|
args = parser.parse_args() |
|
bg = args.bg |
|
current_bg = os.getenv('NVIM_COLORSCHEME_BG') |
|
|
|
def set_env(background): |
|
os.system('kitty @ set-colors --all --configured ' + color_file) |
|
cmd = ['export NVIM_COLORSCHEME_BG=' + background] |
|
cmd += ['export NVIM_COLORSCHEME=' + colorschemes[background]] |
|
return cmd |
|
|
|
if Path(color_file).exists(): |
|
cfile = open(color_file, 'r') |
|
first_line = cfile.readline().rstrip() |
|
current_bg = first_line.split('=')[1] |
|
elif not bg: |
|
print('You must provide background type argument on first run', file=sys.stderr) |
|
exit() |
|
|
|
if not bg: |
|
print(';'.join(set_env(current_bg))) |
|
exit() |
|
|
|
if bg == 'toggle': |
|
bg = 'dark' if current_bg == 'light' else 'light' |
|
|
|
os.environ['NVIM_COLORSCHEME_BG'] = bg |
|
os.environ['NVIM_COLORSCHEME'] = colorschemes[bg] |
|
nvim = pynvim.attach('child', argv=["/usr/bin/env", "nvim", "--embed", "--headless"]) |
|
|
|
def get_hl_color(name): |
|
hl_output = nvim.call('execute', 'hi ' + name) |
|
bgcolor = None |
|
fgcolor = None |
|
for item in hl_output.split('\n')[1].split(): |
|
if item.startswith('guifg'): |
|
fgcolor = item.split('=')[1] |
|
if item.startswith('guibg'): |
|
bgcolor = item.split('=')[1] |
|
return fgcolor or '', bgcolor or '' |
|
|
|
normal_fg, normal_bg = get_hl_color('Normal') |
|
cursor_fg, cursor_bg = get_hl_color('Cursor') |
|
selection_fg, selection_bg = get_hl_color('Visual') |
|
|
|
command = [ |
|
'env NVIM_COLORSCHEME_BG=' + bg, |
|
'env NVIM_COLORSCHEME=' + colorschemes[bg], |
|
'foreground ' + normal_fg, |
|
'background ' + normal_bg, |
|
'cursor ' + (cursor_bg.startswith('#') and cursor_bg or normal_fg), |
|
'cursor_text_color ' + (cursor_fg.startswith('#') and cursor_fg or 'background'), |
|
'selection_foreground ' + (selection_fg.startswith('#') and selection_fg or 'none'), |
|
'selection_background ' + (selection_bg.startswith('#') and selection_bg or normal_fg) |
|
] |
|
|
|
for i in range(0, 16): |
|
command.append('color' + str(i) + ' ' + nvim.eval('g:terminal_color_' + str(i))) |
|
|
|
nvim.close() |
|
|
|
file = open(color_file, 'w') |
|
file.write('\n'.join(command)) |
|
file.close() |
|
|
|
cmd = set_env(bg) |
|
is_bg_changed = bg != current_bg |
|
if is_bg_changed: |
|
# Do some additional work if needed |
|
|
|
print(';'.join(cmd)) |