linux-tools/modules/vim/module.py
2020-01-11 02:38:46 +01:00

118 lines
4.0 KiB
Python

import json
import os
import subprocess
from pathlib import Path
from PyInquirer import prompt
from AbstractModule import AbstractModule
def get_vim_root():
# return Path().home().joinpath(".vim")
return Path(os.getcwd()).joinpath("devenv", ".vim")
class VimModule(AbstractModule):
def __init__(self):
super().__init__()
self.json_config = {}
with open("modules/vim/vimrc_conf.json") as plugins:
self.json_config = json.load(plugins)
def create_folder_structure(self):
# Create necessary folder structure for vim
os.makedirs(str(get_vim_root().joinpath("autoload")), exist_ok=True)
os.makedirs(str(get_vim_root().joinpath("plugged")), exist_ok=True)
def get_vimplug_file(self):
vimplug_filepath = Path(str(get_vim_root().joinpath("autoload")))
print(vimplug_filepath)
if os.path.isfile(str(get_vim_root().joinpath("autoload", "plug.vim"))):
print("Vimplug already installed!")
else:
curl_request = "curl https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim " + \
"-o " + \
str(get_vim_root().joinpath("autoload", "plug.vim"))
subprocess.call(curl_request, shell=True)
def create_plugin_section(self):
vimrc_content = []
mapped_list = []
plugins = self.json_config['plugins']
for choice in plugins.keys():
mapped_list.append({
"name": choice
})
print("Available Plugins:")
plugin_selection = [
{
'type': 'checkbox',
'message': 'Select plugins',
'name': 'modules',
'choices': mapped_list
}
]
selected_plugins = prompt(plugin_selection)['modules']
print("\033[4mYour selection:\033[0m")
[print(x) for x in selected_plugins]
try:
index = vimrc_content.index("call plug#begin('~/.vim/plugged')\n") + 1
for plugin in selected_plugins:
vimrc_content.insert(index, plugins[plugin] + "\n")
except ValueError:
vimrc_content.append("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(self):
vimrc_content = []
settings = self.json_config['settings']
default_settings = list(settings.values())
print("\n\033[4mDefault settings:\033[0m")
[print(i) for i in default_settings]
default_settings.reverse()
for setting in default_settings:
vimrc_content.insert(0, setting + "\n")
return vimrc_content
def get_vimfile_working_copy(self):
vimrc_path = str(get_vim_root().joinpath(".vimrc"))
try:
with open(vimrc_path, "r") as vimrc_file:
vimrc_content = vimrc_file.readlines()
except FileNotFoundError:
vimrc_content = []
return vimrc_content
def write_vimfile(self, 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))]
vimrc_path = str(get_vim_root().joinpath(".vimrc"))
with open(vimrc_path, "w") as vimrc_file:
for line in vimrc_content:
vimrc_file.write(line)
def exec_plugin_manager(self):
install_plugins = "vim +PlugInstall +qall +silent"
subprocess.call(install_plugins, shell=True)
def run(self):
self.create_folder_structure()
self.get_vimplug_file()
plugin_section = self.create_plugin_section()
settings_section = self.create_setting_section()
self.write_vimfile(settings_section + plugin_section)
# Uncomment when in production
# self.exec_plugin_manager()