linux-tools/modules/vim/module.py

85 lines
3.2 KiB
Python
Raw Normal View History

from AbstractModule import AbstractModule
2019-12-16 12:08:22 +01:00
from subprocess import call
from PyInquirer import prompt
2019-12-16 12:08:22 +01:00
import os
import shutil
class VimModule(AbstractModule):
def __init__(self):
2019-12-14 05:47:33 +01:00
super().__init__()
self.plugins = {
"Colorschemes": "Plug 'https://github.com/rafi/awesome-vim-colorschemes.git'",
"Unix Operations": "Plug 'https://github.com/tpope/vim-eunuch.git'",
"Emmet": "Plug 'https://github.com/mattn/emmet-vim.git'",
"Meta5 Theme": "Plug 'https://github.com/christophermca/meta5.git'",
"Nerdtree": "Plug 'https://github.com/scrooloose/nerdtree'",
"Lightline": "Plug 'https://github.com/itchyny/lightline.vim'"
}
def do_module_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)
def run(self):
selected_modules = self.do_module_selection()['modules']
# print out selected plugins
[print(x) for x in selected_modules]
# Create vimrc file
vimrc_exists = os.path.isfile("testfile")
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')
# Merge duplicates to first ocassion with awk
sorted_file = "awk -i inplace ' !x[$0]++' ~/Gitlab/linux-tools/testfile"
call(sorted_file, shell=True)
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/")
# Create necessary folder structure for Plugin Manager
if autoloaddir_exists == False:
if vimdir_exists:
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!")