From 681e07edcd1c522d110a81a0fd6a46d17713532e Mon Sep 17 00:00:00 2001 From: Ste Vaidis Date: Wed, 16 Aug 2023 11:20:33 +0300 Subject: [PATCH] vim --- init.lua => vim/init.lua | 0 vim/persistence.lua | 63 ++++++++++++++++++++++++++++++++++++ polish.lua => vim/polish.lua | 0 2.Vim.md => vim/vim.md | 0 4 files changed, 63 insertions(+) rename init.lua => vim/init.lua (100%) create mode 100644 vim/persistence.lua rename polish.lua => vim/polish.lua (100%) rename 2.Vim.md => vim/vim.md (100%) diff --git a/init.lua b/vim/init.lua similarity index 100% rename from init.lua rename to vim/init.lua diff --git a/vim/persistence.lua b/vim/persistence.lua new file mode 100644 index 0000000..bdee281 --- /dev/null +++ b/vim/persistence.lua @@ -0,0 +1,63 @@ +local constants = require("themery.constants") +local config = require("themery.config") +local utils = require("themery.utils") + +local function saveTheme(theme) + local configFilePath = config.getSettings().themeConfigFile + local file = io.open(configFilePath , "r") + + if file == nil then + print(constants.MSG_ERROR.READ_FILE..": "..configFilePath) + return + end + + local content = file:read("*all") + + local start_marker = "-- Themery block" + local end_marker = "-- end themery block" + + local start_pos, end_pos = content:find(start_marker .. "\n(.+)\n" .. end_marker) + + if not start_pos or not end_pos then + print(constants.MSG_ERROR.NO_MARKETS) + return + end + + local beforeCode = "" + local afterCode = "" + + if theme.before then + beforeCode = utils.trimStartSpaces(theme.before).."\n" + end + + if theme.after then + afterCode = "\n"..utils.trimStartSpaces(theme.after) + end + + local configToWrite = "-- This block will be replaced by Themery.\n" + configToWrite = configToWrite..beforeCode + configToWrite = configToWrite.."vim.cmd(\"colorscheme " + configToWrite = configToWrite..theme.colorscheme.."\")\n" + configToWrite = configToWrite..afterCode + +local replaced_content = content:sub(1, start_pos-1) + ..start_marker.."\n" + ..configToWrite + ..end_marker + ..content:sub(end_pos+1) + + local outfile = io.open(configFilePath, "w") + + if outfile == nil then + print(constants.MSG_ERROR.WRITE_FILE..": "..configFilePath) + return + end + + outfile:write(replaced_content) + outfile:close() + print(constants.MSG_INFO.THEME_SAVED) +end + +return { + saveTheme = saveTheme, +} diff --git a/polish.lua b/vim/polish.lua similarity index 100% rename from polish.lua rename to vim/polish.lua diff --git a/2.Vim.md b/vim/vim.md similarity index 100% rename from 2.Vim.md rename to vim/vim.md