From 659f410b9ea400277a444813f10ce7b747a025d1 Mon Sep 17 00:00:00 2001 From: jdhao Date: Thu, 15 Aug 2024 23:37:35 +0200 Subject: [PATCH] move version check into function --- init.lua | 22 +++------------------- lua/utils.lua | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/init.lua b/init.lua index 9fda2f9..5199cd7 100644 --- a/init.lua +++ b/init.lua @@ -11,26 +11,10 @@ -- StackOverflow: https://stackoverflow.com/users/6064933/jdhao vim.loader.enable() -local version = vim.version +local utils = require("utils") --- check if we have the latest stable version of nvim -local expected_ver_str = "0.10.1" -local expect_ver = version.parse(expected_ver_str) -local actual_ver = vim.version() - -if expect_ver == nil then - local msg = string.format("Unsupported version string: %s", expected_ver_str) - vim.api.nvim_err_writeln(msg) - return -end - -local result = version.cmp(expect_ver, actual_ver) - -if result ~= 0 then - local _ver = string.format("%s.%s.%s", actual_ver.major, actual_ver.minor, actual_ver.patch) - local msg = string.format("Expect nvim %s, but got %s instead. Use at your own risk!", expected_ver_str, _ver) - vim.api.nvim_err_writeln(msg) -end +local expected_version = "0.10.1" +utils.is_compatible_version(expected_version) local core_conf_files = { "globals.lua", -- some global settings diff --git a/lua/utils.lua b/lua/utils.lua index 6b279d6..86a97d0 100644 --- a/lua/utils.lua +++ b/lua/utils.lua @@ -1,4 +1,5 @@ local fn = vim.fn +local version = vim.version local M = {} @@ -50,4 +51,32 @@ function M.rand_element(seq) return seq[idx] end +--- check if the current nvim version is compatible with the allowed version +--- @param expected_version string +--- @return boolean +function M.is_compatible_version(expected_version) + -- check if we have the latest stable version of nvim + local expect_ver = version.parse(expected_version) + local actual_ver = vim.version() + + if expect_ver == nil then + local msg = string.format("Unsupported version string: %s", expected_version) + vim.api.nvim_err_writeln(msg) + return false + end + + local result = version.cmp(expect_ver, actual_ver) + if result ~= 0 then + local _ver = string.format("%s.%s.%s", actual_ver.major, actual_ver.minor, actual_ver.patch) + local msg = string.format( + "Expect nvim version %s, but your current nvim version is %s. Use at your own risk!", + expected_version, + _ver + ) + vim.api.nvim_err_writeln(msg) + end + + return true +end + return M