utils.lua 5.0 KB

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