2021-02-21 00:12:40 +03:00
import io
import json
import sys
2022-11-23 00:14:47 +03:00
import os
from ruamel . yaml import YAML # use ruamel.yaml to preserve comments in config
2021-02-21 00:12:40 +03:00
2022-11-23 00:14:47 +03:00
def get_conf_path ( ) :
# Determine system
# When we are in some Java world do extra checks
if sys . platform . startswith ( ' java ' ) :
import platform
os_name = platform . java_ver ( ) [ 3 ] [ 0 ]
if os_name . startswith ( ' Windows ' ) : # "Windows XP", "Windows 7", etc.
system = ' win32 '
else : # anything that isn't windows ("darwin", "Linux", "SunOS", "FreeBSD", "Arch", etc.)
system = ' linux2 '
else :
system = sys . platform
if system == ' win32 ' :
# In windows alacritty config can only exist in one place
alacritty_path = os . path . expandvars ( r ' % APPDATA % \ alacritty \ alacritty.yml ' )
if os . path . exists ( alacritty_path ) :
return alacritty_path
else :
# If it is not win32 it can exists in only a few other places
xdg_config_home = os . getenv ( ' XDG_CONFIG_HOME ' )
if xdg_config_home is not None and os . path . exists ( xdg_config_home + ' /alacritty/alacritty.yml ' ) :
return xdg_config_home + " /alacritty/alacritty.yml "
if xdg_config_home is not None and os . path . exists ( xdg_config_home + " /alacritty.yml " ) :
return xdg_config_home + " /alacritty.yml "
home = os . getenv ( ' HOME ' )
if home is not None and os . path . exists ( home + ' /.config/alacritty/alacritty.yml ' ) :
return home + " /.config/alacritty/alacritty.yml "
2023-05-05 06:36:09 +03:00
if home is not None and os . path . exists ( home + ' /.config/alacritty/alacritty.yml ' ) :
return home + " /.config/alacritty/alacritty.yml "
if home is not None and os . path . exists ( home + ' /.alacritty.yml ' ) :
return home + " /.alacritty.yml "
2022-11-23 00:14:47 +03:00
print ( " Could not find alacritty config file \n Please make sure you have a file in one of the paths specified on \n https://github.com/alacritty/alacritty#configuration " )
sys . exit ( 1 )
# end
conf_path = get_conf_path ( )
2021-02-21 00:12:40 +03:00
yaml = YAML ( )
2022-11-23 00:14:47 +03:00
# Read & parse alacritty config
with open ( conf_path , ' r ' ) as stream :
2021-02-21 00:12:40 +03:00
data_loaded = yaml . load ( stream )
2022-11-23 00:14:47 +03:00
# parse new colors
2021-02-21 00:12:40 +03:00
js = json . loads ( sys . argv [ 1 ] )
2022-11-23 00:14:47 +03:00
# Update yaml file
try :
# Use update to not remove existing comments
data_loaded [ ' colors ' ] [ ' primary ' ] . update ( js [ ' colors ' ] [ ' primary ' ] )
data_loaded [ ' colors ' ] [ ' normal ' ] . update ( js [ ' colors ' ] [ ' normal ' ] )
data_loaded [ ' colors ' ] [ ' bright ' ] . update ( js [ ' colors ' ] [ ' bright ' ] )
except KeyError :
print ( " Could not find existing ' colors ' settings in your alacritty.yml file \n please make sure to uncomment \n ' colors ' , as well as ' primary ' , ' normal ' and ' bright ' " )
print ( " Check the example config at \n https://github.com/alacritty/alacritty/blob/master/alacritty.yml for more information " )
sys . exit ( 1 )
# make sure the user is okay with having their config changed
answer = input ( " This script will update your alacritty config at: \n " +
conf_path + " \n It is reccomended to make a copy of this file before proceeding. \n Are you sure you want to continue? (Y/N) \n " )
if not answer . lower ( ) in [ ' y ' , ' yes ' ] :
print ( " Aborted " )
sys . exit ( 1 )
2021-02-21 00:12:40 +03:00
# Write alacritty config
2022-11-23 00:14:47 +03:00
with io . open ( conf_path , ' w ' , encoding = ' utf8 ' ) as outfile :
2021-02-21 00:12:40 +03:00
yaml . dump ( data_loaded , outfile )