linux-tools/modules/vim/__init__.py

95 lines
2.6 KiB
Python
Raw Normal View History

2020-08-02 03:50:50 +02:00
import json
import os
import subprocess
from pathlib import Path
from PyInquirer import prompt
2020-08-02 13:58:47 +02:00
from print_helpers import print_gr, print_bold
2020-08-02 03:50:50 +02:00
vim_root = Path().home().joinpath(".vim")
vimrc_path = str(Path().home().joinpath(".vimrc"))
json_config = {}
def create_plugin_section():
plugins = json_config['plugins']
print("Available Plugins:")
plugin_selection = [
{
'type': 'checkbox',
'message': 'Select plugins',
'name': 'modules',
'choices': list(map(lambda x: {"name": x}, plugins.keys()))
}
]
selected_plugins = prompt(plugin_selection)['modules']
2020-08-02 13:58:47 +02:00
print_bold("Your selection:")
2020-08-02 03:50:50 +02:00
[print(x) for x in selected_plugins]
# Install vimplug if plugins selected
vimplug_path = str(vim_root.joinpath("autoload", "plug.vim"))
if selected_plugins and not os.path.isfile(vimplug_path):
curl_request = f"curl {json_config['pluginmanager_url']} -o {vimplug_path}"
subprocess.call(curl_request, shell=True)
2020-08-02 13:58:47 +02:00
print_gr("Pluginmanager was set up")
2020-08-02 03:50:50 +02:00
vimrc_content = ["call plug#begin('~/.vim/plugged')\n"]
for element in selected_plugins:
vimrc_content.append(plugins[element] + "\n")
vimrc_content.append("call plug#end()\n")
return vimrc_content
def create_setting_section():
default_settings = list(json_config['settings'].values())
2020-08-02 13:58:47 +02:00
print_bold("Default settings:")
2020-08-02 03:50:50 +02:00
[print(i) for i in default_settings]
vimrc_content = list(map(lambda x: x + "\n", default_settings))
return vimrc_content
def write_vimfile(vimrc_content):
seen = set()
seen_add = seen.add
vimrc_content = [x1 for x1 in vimrc_content if not (x1 in seen or seen_add(x1))]
with open(vimrc_path, "w") as vimrc_file:
for line in vimrc_content:
vimrc_file.write(line)
# Unused
def _get_vimfile_working_copy():
try:
with open(vimrc_path, "r") as vimrc_file:
vimrc_content = vimrc_file.readlines()
except FileNotFoundError:
vimrc_content = []
return vimrc_content
def run():
global json_config
with open("modules/vim/vimrc_conf.json", "r") as config:
json_config = json.load(config)
# Create folders
os.makedirs(str(vim_root.joinpath("autoload")), exist_ok=True)
os.makedirs(str(vim_root.joinpath("plugged")), exist_ok=True)
plugin_section = create_plugin_section()
settings_section = create_setting_section()
write_vimfile(settings_section + plugin_section)
# Exec plugin manager
install_plugins = "vim +PlugInstall +qall"
subprocess.call(install_plugins, shell=True)
2020-08-02 13:58:47 +02:00
print_gr("Module finished successfully!")
2020-08-02 03:50:50 +02:00
functions = run