diff --git a/modules/vim/module.py b/modules/vim/module.py index 384ec16..e305b84 100644 --- a/modules/vim/module.py +++ b/modules/vim/module.py @@ -2,7 +2,6 @@ from AbstractModule import AbstractModule from subprocess import call from PyInquirer import prompt import os -import shutil import json @@ -13,7 +12,7 @@ class VimModule(AbstractModule): with open("modules/vim/vim_plugins.json") as plugins: self.plugins = json.load(plugins)['plugins'] - def do_module_selection(self): + def do_plugin_selection(self): mapped_list = [] for choice in self.plugins.keys(): mapped_list.append({ @@ -33,42 +32,40 @@ class VimModule(AbstractModule): def run(self): - selected_modules = self.do_module_selection() + selected_plugins = self.do_plugin_selection() # print out selected plugins - [print(x) for x in selected_modules] + [print(x) for x in selected_plugins] # Create vimrc file - vimrc_exists = os.path.isfile("testfile") + try: + with open("vim_testfile", "r") as vim_file: + vimrc_content = vim_file.readlines() + except FileNotFoundError: + vimrc_content = [] - if vimrc_exists: - temp_file = open('temp', 'w') - with open("testfile", "r") as vimrc_out_file: - for line in vimrc_out_file: - if line.startswith("call plug#begin('~/.vim/plugged')"): - for element in selected_modules: - line = line.strip() + "\n" + self.plugins[element] + "\n" - temp_file.write(line) - temp_file.close() - shutil.move('temp', 'testfile') + 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") - # Merge duplicates to first ocassion with awk - sorted_file = "awk -i inplace ' !x[$0]++' ~/Gitlab/linux-tools/testfile" - call(sorted_file, shell=True) + seen = set() + seen_add = seen.add + vimrc_content = [x1 for x1 in vimrc_content if not (x1 in seen or seen_add(x1))] - else: - with open("testfile", "w+") as vimrc_file: - vimrc_file.write("\n" + "call plug#begin('~/.vim/plugged')\n") - for element in selected_modules: - vimrc_file.write(self.plugins[element] + "\n") - vimrc_file.write("call plug#end()\n") - - vimdir_exists = os.path.isdir("/home/clay/Gitlab/linux-tools/testdirectory/") - autoloaddir_exists = os.path.isdir("/home/clay/Gitlab/linux-tools/testdirectory/autoload/") + 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 autoloaddir_exists: - if vimdir_exists: + 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 " \