better persistace
This commit is contained in:
		| @@ -1,5 +1,7 @@ | ||||
| ## dates and their corresponding seconds been here :) | ||||
| [24-10-08] | ||||
| u80864958_at_u80864958 = 425 | ||||
| [24-10-07] | ||||
| u80864958_at_u80864958 = 432 | ||||
| [24-10-08] | ||||
| u80864958_at_u80864958 = 372 | ||||
| [24-10-11] | ||||
| u80864958_at_u80864958 = 5497 | ||||
|   | ||||
| @@ -1,4 +1,4 @@ | ||||
| function trim(s) | ||||
| local function trim(s) | ||||
|   return (s:gsub("^%s*(.-)%s*$", "%1")) | ||||
| end | ||||
|  | ||||
| @@ -107,6 +107,7 @@ local function log_total_time() | ||||
|     local history = require("toml").parse(file:read("*a")) | ||||
|     file:close() | ||||
|  | ||||
|     print("Time for:", filename) | ||||
|  | ||||
|     for _, day in pairs(history) do | ||||
|       for user, time in pairs(day) do | ||||
| @@ -144,17 +145,43 @@ local function focus_lost() | ||||
|   }) | ||||
| end | ||||
|  | ||||
| local function next_lowest_time_file() | ||||
|   local file_path = filename | ||||
|  | ||||
| local function persist() | ||||
|   for _ = 1, 10, 1 do | ||||
|     local f = io.open(file_path) | ||||
|  | ||||
|     if f ~= nil then | ||||
|       return file_path | ||||
|     end | ||||
|  | ||||
|     file_path = "../" .. file_path | ||||
|   end | ||||
|  | ||||
|   return "" | ||||
| end | ||||
|  | ||||
| local function persist(force) | ||||
|   return function() | ||||
|     local TOML = require("toml") | ||||
|     local today = os.date("%y-%m-%d", os.time()) | ||||
|     local table = {} | ||||
|     local file_path = filename | ||||
|  | ||||
|   local file = io.open(filename, "r") | ||||
|     local file = io.open(file_path, "r") | ||||
|     -- if file exists read | ||||
|     if file ~= nil then | ||||
|       table = TOML.parse(file:read("*a")) | ||||
|       file:close() | ||||
|     else | ||||
|       if not force then | ||||
|         file_path = next_lowest_time_file() | ||||
|         if file_path == "" then | ||||
|           print("No save file found. To create a new save file run :TimerSave") | ||||
|           return | ||||
|         end | ||||
|         print("Save file found:", file_path) | ||||
|       end | ||||
|     end | ||||
|  | ||||
|     -- write time to table | ||||
| @@ -168,29 +195,31 @@ local function persist() | ||||
|     end | ||||
|  | ||||
|     -- write table to file | ||||
|   file = io.open(filename, "w") | ||||
|     file = io.open(file_path, "w") | ||||
|     if file == nil then | ||||
|       error("can't write to .timer file") | ||||
|       return | ||||
|     end | ||||
|  | ||||
|     local tml = TOML.encode(table) | ||||
|   print(tml) | ||||
|  | ||||
|     file:write("## dates and their corresponding seconds been here :)\n", tml, "\n") | ||||
|     file:close() | ||||
|  | ||||
|     print("Timer saved successfully :-)") | ||||
|  | ||||
|     --reset timer | ||||
|     clean_to = 1 | ||||
|     focus_events = {} | ||||
|     focus_gained() | ||||
|   end | ||||
| end | ||||
|  | ||||
| local function setup() | ||||
|   vim.api.nvim_create_autocmd("VimEnter", | ||||
|     { group = augroup, desc = "Start Timer", once = true, callback = focus_gained }) | ||||
|   vim.api.nvim_create_autocmd("ExitPre", | ||||
|     { group = augroup, desc = "Persist timer", once = true, callback = persist }) | ||||
|     { group = augroup, desc = "Persist timer", once = true, callback = persist(false) }) | ||||
|   vim.api.nvim_create_autocmd("FocusGained", { group = augroup, callback = focus_gained }) | ||||
|   vim.api.nvim_create_autocmd("FocusLost", { group = augroup, callback = focus_lost }) | ||||
|  | ||||
| @@ -206,7 +235,7 @@ local function setup() | ||||
|   ) | ||||
|   vim.api.nvim_create_user_command( | ||||
|     'TimerSave', | ||||
|     persist, | ||||
|     persist(true), | ||||
|     { nargs = 0 } -- No arguments for this command (you can configure this later) | ||||
|   ) | ||||
| end | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 u80864958
					u80864958