utils.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. local M = {}
  2. local uv = vim.loop
  3. -- recursive Print (structure, limit, separator)
  4. local function r_inspect_settings(structure, limit, separator)
  5. limit = limit or 100 -- default item limit
  6. separator = separator or "." -- indent string
  7. if limit < 1 then
  8. print "ERROR: Item limit reached."
  9. return limit - 1
  10. end
  11. if structure == nil then
  12. io.write("-- O", separator:sub(2), " = nil\n")
  13. return limit - 1
  14. end
  15. local ts = type(structure)
  16. if ts == "table" then
  17. for k, v in pairs(structure) do
  18. -- replace non alpha keys with ["key"]
  19. if tostring(k):match "[^%a_]" then
  20. k = '["' .. tostring(k) .. '"]'
  21. end
  22. limit = r_inspect_settings(v, limit, separator .. "." .. tostring(k))
  23. if limit < 0 then
  24. break
  25. end
  26. end
  27. return limit
  28. end
  29. if ts == "string" then
  30. -- escape sequences
  31. structure = string.format("%q", structure)
  32. end
  33. separator = separator:gsub("%.%[", "%[")
  34. if type(structure) == "function" then
  35. -- don't print functions
  36. io.write("-- lvim", separator:sub(2), " = function ()\n")
  37. else
  38. io.write("lvim", separator:sub(2), " = ", tostring(structure), "\n")
  39. end
  40. return limit - 1
  41. end
  42. function M.generate_settings()
  43. -- Opens a file in append mode
  44. local file = io.open("lv-settings.lua", "w")
  45. -- sets the default output file as test.lua
  46. io.output(file)
  47. -- write all `lvim` related settings to `lv-settings.lua` file
  48. r_inspect_settings(lvim, 10000, ".")
  49. -- closes the open file
  50. io.close(file)
  51. end
  52. --- Returns a table with the default values that are missing.
  53. --- either parameter can be empty.
  54. --@param config (table) table containing entries that take priority over defaults
  55. --@param default_config (table) table contatining default values if found
  56. function M.apply_defaults(config, default_config)
  57. config = config or {}
  58. default_config = default_config or {}
  59. local new_config = vim.tbl_deep_extend("keep", vim.empty_dict(), config)
  60. new_config = vim.tbl_deep_extend("keep", new_config, default_config)
  61. return new_config
  62. end
  63. --- Checks whether a given path exists and is a file.
  64. --@param path (string) path to check
  65. --@returns (bool)
  66. function M.is_file(path)
  67. local stat = uv.fs_stat(path)
  68. return stat and stat.type == "file" or false
  69. end
  70. --- Checks whether a given path exists and is a directory
  71. --@param path (string) path to check
  72. --@returns (bool)
  73. function M.is_directory(path)
  74. local stat = uv.fs_stat(path)
  75. return stat and stat.type == "directory" or false
  76. end
  77. M.join_paths = _G.join_paths
  78. ---Write data to a file
  79. ---@param path string can be full or relative to `cwd`
  80. ---@param txt string|table text to be written, uses `vim.inspect` internally for tables
  81. ---@param flag string used to determine access mode, common flags: "w" for `overwrite` or "a" for `append`
  82. function M.write_file(path, txt, flag)
  83. local data = type(txt) == "string" and txt or vim.inspect(txt)
  84. uv.fs_open(path, flag, 438, function(open_err, fd)
  85. assert(not open_err, open_err)
  86. uv.fs_write(fd, data, -1, function(write_err)
  87. assert(not write_err, write_err)
  88. uv.fs_close(fd, function(close_err)
  89. assert(not close_err, close_err)
  90. end)
  91. end)
  92. end)
  93. end
  94. ---Copies a file or directory recursively
  95. ---@param source string
  96. ---@param destination string
  97. function M.fs_copy(source, destination)
  98. local source_stats = assert(vim.loop.fs_stat(source))
  99. if source_stats.type == "file" then
  100. assert(vim.loop.fs_copyfile(source, destination))
  101. return
  102. elseif source_stats.type == "directory" then
  103. local handle = assert(vim.loop.fs_scandir(source))
  104. assert(vim.loop.fs_mkdir(destination, source_stats.mode))
  105. while true do
  106. local name = vim.loop.fs_scandir_next(handle)
  107. if not name then
  108. break
  109. end
  110. M.fs_copy(M.join_paths(source, name), M.join_paths(destination, name))
  111. end
  112. end
  113. end
  114. return M