from AbstractModule import AbstractModule from subprocess import call from PyInquirer import prompt import os import json class VimModule(AbstractModule): def __init__(self): super().__init__() self.plugins = {} with open("modules/vim/vim_plugins.json") as plugins: self.plugins = json.load(plugins)['plugins'] def do_plugin_selection(self): mapped_list = [] for choice in self.plugins.keys(): mapped_list.append({ "name": choice }) print("Available Plugins:") module_selection = [ { 'type': 'checkbox', 'message': 'Select plugins', 'name': 'modules', 'choices': mapped_list } ] return prompt(module_selection)['modules'] def run(self): selected_plugins = self.do_plugin_selection() # print out selected plugins [print(x) for x in selected_plugins] # Create vimrc file try: with open("vim_testfile", "r") as vim_file: vimrc_content = vim_file.readlines() except FileNotFoundError: vimrc_content = [] try: index = vimrc_content.index("call plug#begin('~/.vim/plugged')\n") + 1 print("Index of plugman " + str(index)) for plugin in selected_plugins: vimrc_content.insert(index, self.plugins[plugin] + "\n") except ValueError: vimrc_content.append("call plug#begin('~/.vim/plugged')\n") for element in selected_plugins: vimrc_content.append(self.plugins[element] + "\n") vimrc_content.append("call plug#end()\n") 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("vim_testfile", "w") as vimrc_file: for line in vimrc_content: vimrc_file.write(line) # Create necessary folder structure for Plugin Manager if not os.path.isdir("/home/clay/Gitlab/linux-tools/testdirectory/autoload/"): if os.path.isdir("/home/clay/Gitlab/linux-tools/testdirectory/"): install_vimplug = "cd ~/Gitlab/linux-tools/testdirectory/ " \ "&& mkdir autoload " \ "&& cd autoload " \ "&& touch test.txt" call(install_vimplug, shell=True) else: make_vimdir = "cd ~/Gitlab/linux-tools/ " \ "&& mkdir testdirectory " \ "&& cd testdirectory " \ "&& mkdir autoload " \ "&& cd autoload " \ "&& touch test.txt" call(make_vimdir, shell=True) else: print("Vim-Plug already installed!")