Merge branch '8-set-up-vim-correctly' into 'master'

Resolve "Set up vim correctly"

Closes #8

See merge request icaotix/linux-tools!7
This commit is contained in:
Marcel Schwarz 2020-01-12 18:07:03 +00:00
commit d7531412cf
5 changed files with 152 additions and 0 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
__pycache__
.vscode
devenv

View File

@ -0,0 +1,24 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Main Script" type="PythonConfigurationType" factoryName="Python">
<module name="linux-tools" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/main.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="true" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
</component>

5
modules/vim/__init__.py Normal file
View File

@ -0,0 +1,5 @@
from .module import VimModule
def get_module():
return VimModule()

102
modules/vim/module.py Normal file
View File

@ -0,0 +1,102 @@
import json
import os
import subprocess
from pathlib import Path
from PyInquirer import prompt
from AbstractModule import AbstractModule
def get_vim_root():
# return Path(os.getcwd()).joinpath("devenv", ".vim")
return Path().home().joinpath(".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):
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 {} -o {}".format(
self.json_config['pluginmanager_url'],
str(get_vim_root().joinpath("autoload", "plug.vim"))
)
subprocess.call(curl_request, shell=True)
def create_plugin_section(self):
vimrc_content = []
plugins = self.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']
if len(selected_plugins) > 0:
self.get_vimplug_file()
print("\033[4mYour selection:\033[0m")
[print(x) for x in selected_plugins]
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):
default_settings = list(self.json_config['settings'].values())
print("\n\033[4mDefault settings:\033[0m")
[print(i) for i in default_settings]
vimrc_content = list(map(lambda x: x + "\n", default_settings))
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()
plugin_section = self.create_plugin_section()
settings_section = self.create_setting_section()
self.write_vimfile(settings_section + plugin_section)
self.exec_plugin_manager()

View File

@ -0,0 +1,20 @@
{
"pluginmanager_url": "https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim",
"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'"
},
"settings": {
"tabstop": "set tabstop=2",
"shiftwidth": "set shiftwidth=2",
"expand": "set noexpandtab",
"highlighting": "syntax on",
"linenumbers": "set number",
"status": "set laststatus=2",
"noshowmode": "set noshowmode"
}
}