diff options
Diffstat (limited to 'config/essentials/vis/backup.lua')
-rw-r--r-- | config/essentials/vis/backup.lua | 48 |
1 files changed, 30 insertions, 18 deletions
diff --git a/config/essentials/vis/backup.lua b/config/essentials/vis/backup.lua index f48895f..7c44c60 100644 --- a/config/essentials/vis/backup.lua +++ b/config/essentials/vis/backup.lua @@ -1,36 +1,48 @@ +--[[ +Based on https://github.com/roguh/vis-backup +Changes made: +- stylua +- file path +--]] + local backup = {} -- Return the backup path concatenated with the filename, replace / with % -backup.entire_path_with_double_percentage_signs = - function(backup_dir, path) - return backup_dir .. "/" .. string.gsub(path, "/", "%%") - end +backup.entire_path_with_double_percentage_signs = function(backup_dir, path) + return backup_dir .. "/" .. string.gsub(path, "/", "%%") +end -- Return the backup path concatenated with the filename, replace / with % -- and append the current time using time_format -backup.entire_path_with_double_percentage_signs_and_timestamp = function( - backup_dir, path) - return backup_dir .. "/" .. os.date(backup.time_format) .. - string.gsub(path, "/", "%%") +backup.entire_path_with_double_percentage_signs_and_timestamp = function(backup_dir, path) + return backup_dir .. "/" .. os.date(backup.time_format) .. string.gsub(path, "/", "%%") end -- Before saving the file, copy the current contents of the file to a backup file vis.events.subscribe(vis.events.FILE_SAVE_PRE, function(file, path) - if file.size > backup.byte_limit then return end + if file.size > backup.byte_limit then + return + end - -- E.g. when editing stdin as an interactive filter - -- `vis -` - if path == nil then return end + -- E.g. when editing stdin as an interactive filter + -- `vis -` + if path == nil then + return + end - local backup_path = backup.get_fname(backup.directory, path) + local backup_path = backup.get_fname(backup.directory, path) - local backup_file = io.open(backup_path, "w") - local current_file = io.open(path) - if backup_file == nil or current_file == nil then return end + local backup_file = io.open(backup_path, "w") + local current_file = io.open(path) + if backup_file == nil or current_file == nil then + return + end - for line in current_file:lines() do backup_file:write(line .. "\n") end + for line in current_file:lines() do + backup_file:write(line .. "\n") + end - backup_file:close() + backup_file:close() end) -- Set defaults |