diff options
author | ben <ben@nagy.contact> | 2025-01-03 18:09:08 -0800 |
---|---|---|
committer | ben <ben@nagy.contact> | 2025-01-03 18:09:08 -0800 |
commit | 18fbe9a0a1e600c1b79a1bfea702cdfe158e2776 (patch) | |
tree | 78f4adfcaae93f3a4eb1a8ac70a2173c36b4f3ae /lua/sleep.lua | |
parent | 7bb4de5c8023f2512645f20c1d28a5954e3fdecf (diff) |
simplified json parsing logic
Diffstat (limited to 'lua/sleep.lua')
-rw-r--r-- | lua/sleep.lua | 103 |
1 files changed, 28 insertions, 75 deletions
diff --git a/lua/sleep.lua b/lua/sleep.lua index a47f87e..931c21e 100644 --- a/lua/sleep.lua +++ b/lua/sleep.lua @@ -1,4 +1,4 @@ -local config_file = "../sleep.json" -- TODO: update path to . +local config_file = "../sleep.json" -- TODO: update path to . (since sleep.json should be in is.xyz.mpv) local time = { minutes = 25, @@ -22,85 +22,31 @@ local gesture_state = { --[[ todo: -perhaps instead of using a json config_file, we simply write the state attributes at the top of this config_file. +perhaps instead of using a json file, we simply write the state attributes at the top of sleep.lua. then have test.lua edit itself as need be. ]] ---[[ -TODO: merge `read_default_time()` and `read_prev_state` - into `read_jsonkey_value(file_str, block_key, value_key)` - then have called by `reinstate_prevstate()` to retrieve keys (timestamp, last_updated, etc.) - and also wherever read_default_time shoudl be called, will retrieve default_time in config block - - (parsing logic is the same, so we should reduce the code) -]] - --------------------------- -- JSON PARSER -- --------------------------- +local function read_jsonkey_value(file_str, json_obj, obj_key) + local b_obj = string.find(file_str, json_obj) + local e_obj = string.find(file_str, "}", b_obj) -local function read_default_time(file_str) - local config_block_i = string.find(file_str, "\"config\"") - if not config_block_i then - print("\"config\" not found in " .. config_file) - return nil - end - - local end_config_i = string.find(file_str, "}", config_block_i) - if not end_config_i then - print("formatting error in " .. config_file) + if not (b_obj or e_obj) then + print(json_obj .. "does not exist or " .. config_file "is formatted incorrectly") return nil end - local substr = string.sub(file_str, config_block_i, end_config_i) - local def_time = string.match(substr, "\"default_time\"%s*:%s*(%d+)") + local substr = string.sub(file_str, b_obj, e_obj) + local key_val = string.match(substr, obj_key) - if not def_time then - print("could not extract \"default_time\" value in \"config\" key from " .. config_file) + if not key_val then + print("could not extract \"" .. string.match(obj_key, "\"([^\"]+)\"") .. "\"" .. " from " .. file) return nil end - return tonumber(def_time) -end - -local function read_prev_state(file_str) - -- we want to extract under previous_state: - -- timestamp - -- last_updated - -- was_active - -- last_update - - local prevstate_block_i = string.find(file_str, "\"previous_state\"") - if not prevstate_block_i then - print("\"previous_state\" block not found in " .. config_file) - return nil - end - - local end_prevstate_i = string.find(file_str, "}", prevstate_block_i) - if not end_prevstate_i then - print("formatting error in " .. config_file) - return nil - end - - local substr = string.sub(file_str, prevstate_block_i, end_prevstate_i) - local tstamp = string.match(substr, "\"timestamp\"%s*:%s*\"(.-)\"") - local lastup = string.match(substr, "\"last_updated\"%s*:%s*\"(.-)\"") - local was_active = string.match(substr, "\"was_active\"%s*:%s*([a-zA-Z]+)") - - if not tstamp then - print("could not extract \"timestamp\" value in \"previous_state\" key from " .. config_file) - return nil - end - - if not lastup then - print("could not extract \"last_updated\" value in \"previous_state\" key from " .. config_file) - return nil - end - - if not was_active then - print("could not extract \"was_active\" value in \"previous_state\" key from " .. config_file) - return nil - end + return key_val end local function read_config() @@ -117,16 +63,26 @@ local function read_config() print("failed to read config_file into string or config_file is empty") end - read_default_time(file_str) - read_prev_state(file_str) + local default_time = read_jsonkey_value(file_str, "\"config\"", "\"default_time\"%s*:%s*(%d+)") + local prevstate_tstamp = read_jsonkey_value(file_str, "\"previous_state\"", "\"timestamp\"%s*:%s*\"(.-)\"") + local prevstate_lastup = read_jsonkey_value(file_str, "\"previous_state\"", "\"last_updated\"%s*:%s*\"(.-)\"") + local prevstate_active = read_jsonkey_value(file_str, "\"previous_state\"", "\"was_active\"%s*:%s*([a-zA-Z]+)") + + if (default_time and prevstate_tstamp and prevstate_lastup and prevstate_active) then + --[[ print("default_time: " .. default_time) + print("prevstate_tstamp: " .. prevstate_tstamp) + print("prevstate_lastup: " .. prevstate_lastup) + print("prevstate_active: " .. prevstate_active) ]] + else + -- print("something went wrong") + end print("closing", config_file .. ".") io.close(openf) end -local function reinstate_tstamp() - -- call read_config and grab the exported time stamp, then seek to that position in the config_file - -- config_file is, timer +local function reinstate_tstamp(time) + -- according to a gesture, this should be called, and seek to @param in file end local function export_time() @@ -140,10 +96,6 @@ end local function reset_timer() end -local function format_time(seconds) - -- return string format hours minutes seconds -end - local function set_timer() timer.minutes = read_config() or timer.minutes timer.active = true @@ -158,6 +110,7 @@ local function handle_gesture() mp.osd_message("Gesture Received.", 3) set_timer() + -- TODO: -- the firs time handle_gestures() is called, timer should be set -- if handle_gestures() is called a second time within a short period, then we should cycle timer off -- if handle_gestures() is called again a third time within a short period, then we should reinstate the last time export |