Advertisement
HandieAndy

library

Sep 17th, 2019
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.55 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local term = require("term")
  4. local internet = require("internet")
  5. local fs = require("filesystem")
  6. local serial = require("serialization")
  7.  
  8. -- The directory where all entries are stored.
  9. local ENTRIES_DIRECTORY = "/home/entries"
  10.  
  11. -- Global state variable.
  12. local RUNNING = true
  13.  
  14. -- Fetches the real-world date and time from the interwebs.
  15. local function getRealWorldTime()
  16.   local response = internet.request("http://worldtimeapi.org/api/ip")
  17.   if response == nil then
  18.     return nil
  19.   end
  20.  
  21.   local content = response()
  22.   local start_idx, end_idx = string.find(content, "\"datetime\":\"")
  23.   local time_string = string.sub(content, end_idx + 1, end_idx + 19)
  24.   local time = {}
  25.   time.year = tonumber(string.sub(time_string, 1, 4))
  26.   time.month = tonumber(string.sub(time_string, 6, 7))
  27.   time.day = tonumber(string.sub(time_string, 9, 10))
  28.   time.hour = tonumber(string.sub(time_string, 12, 13))
  29.   time.minute = tonumber(string.sub(time_string, 15, 16))
  30.   time.second = tonumber(string.sub(time_string, 18, 19))
  31.   time.string = time_string
  32.   return time
  33. end
  34.  
  35. -- Checks if a directory with the given name exists, and makes it if it doesn't.
  36. local function makeDirIfNotExists(name)
  37.   if not fs.exists(name) then
  38.     if not fs.makeDirectory(name) then
  39.       io.stderr:write("Could not make directory: "..name.."\n")
  40.       return false
  41.     end
  42.   end
  43.   return true
  44. end
  45.  
  46. -- Saves the given text into a file in the entries directory.
  47. local function saveEntry(entry_text, time)
  48.   if not makeDirIfNotExists(ENTRIES_DIRECTORY) then
  49.     return false
  50.   end
  51.   local year_str = tostring(time.year)
  52.   local month_str = tostring(time.month)
  53.   --local filename = time.hour .. "_" .. time.minute .. "_" .. time.second .. ".txt"
  54.   local filename = time.string .. ".txt"
  55.   local full_path = fs.concat(ENTRIES_DIRECTORY, year_str, month_str, filename)
  56.   if not makeDirIfNotExists(fs.concat(ENTRIES_DIRECTORY, year_str)) then
  57.     return false
  58.   end
  59.   if not makeDirIfNotExists(fs.concat(ENTRIES_DIRECTORY, year_str, month_str)) then
  60.     return false
  61.   end
  62.   local f = io.open(full_path, "w")
  63.   if f == nil then
  64.     io.stderr:write("Could not open file for writing: "..full_path.."\n")
  65.     return false
  66.   end
  67.   f:write(entry_text)
  68.   f:close()
  69.   return true
  70. end
  71.  
  72. -- Creates a new entry with the given user input.
  73. local function createEntry()
  74.   print("Enter some text below.")
  75.   local input = io.read()
  76.   if input == nil or #input == 0 then
  77.     print("No valid input given, cancelling this entry.")
  78.     return
  79.   end
  80.   print("Getting real-world time...")
  81.   local time = getRealWorldTime()
  82.   print("Saving entry for timestamp " .. time.string .. "...")
  83.   if saveEntry(input, time) then
  84.     print("Entry saved successfully!")
  85.   else
  86.     print("An error occurred, and the entry could not be saved.")
  87.   end
  88. end
  89.  
  90. -- Shows the main menu to the user.
  91. local function mainMenu()
  92.   print("[Main Menu]")
  93.   print("What would you like to do?")
  94.   print("1. Create a new log entry.")
  95.   print("2. View a list of log entries.")
  96.   print("3. Quit the program.")
  97.  
  98.   local choice = tonumber(io.read())
  99.   while choice == nil or choice < 1 or choice > 3 do
  100.     print("Invalid choice. Please enter an integer corresponding to one of the options.")
  101.     choice = tonumber(io.read())
  102.   end
  103.  
  104.   if choice == 1 then
  105.     createEntry()
  106.   elseif choice == 2 then
  107.     -- view entries list
  108.   else
  109.     -- quit
  110.     print("Quitting the program...")
  111.     RUNNING = false
  112.   end
  113. end
  114.  
  115. while RUNNING do
  116.   mainMenu()
  117. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement