Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local event = require("event")
- local term = require("term")
- local internet = require("internet")
- local fs = require("filesystem")
- local serial = require("serialization")
- -- The directory where all entries are stored.
- local ENTRIES_DIRECTORY = "/home/entries"
- -- Global state variable.
- local RUNNING = true
- -- Fetches the real-world date and time from the interwebs.
- local function getRealWorldTime()
- local response = internet.request("http://worldtimeapi.org/api/ip")
- if response == nil then
- return nil
- end
- local content = response()
- local start_idx, end_idx = string.find(content, "\"datetime\":\"")
- local time_string = string.sub(content, end_idx + 1, end_idx + 19)
- local time = {}
- time.year = tonumber(string.sub(time_string, 1, 4))
- time.month = tonumber(string.sub(time_string, 6, 7))
- time.day = tonumber(string.sub(time_string, 9, 10))
- time.hour = tonumber(string.sub(time_string, 12, 13))
- time.minute = tonumber(string.sub(time_string, 15, 16))
- time.second = tonumber(string.sub(time_string, 18, 19))
- time.string = time_string
- return time
- end
- -- Checks if a directory with the given name exists, and makes it if it doesn't.
- local function makeDirIfNotExists(name)
- if not fs.exists(name) then
- if not fs.makeDirectory(name) then
- io.stderr:write("Could not make directory: "..name.."\n")
- return false
- end
- end
- return true
- end
- -- Saves the given text into a file in the entries directory.
- local function saveEntry(entry_text, time)
- if not makeDirIfNotExists(ENTRIES_DIRECTORY) then
- return false
- end
- local year_str = tostring(time.year)
- local month_str = tostring(time.month)
- --local filename = time.hour .. "_" .. time.minute .. "_" .. time.second .. ".txt"
- local filename = time.string .. ".txt"
- local full_path = fs.concat(ENTRIES_DIRECTORY, year_str, month_str, filename)
- if not makeDirIfNotExists(fs.concat(ENTRIES_DIRECTORY, year_str)) then
- return false
- end
- if not makeDirIfNotExists(fs.concat(ENTRIES_DIRECTORY, year_str, month_str)) then
- return false
- end
- local f = io.open(full_path, "w")
- if f == nil then
- io.stderr:write("Could not open file for writing: "..full_path.."\n")
- return false
- end
- f:write(entry_text)
- f:close()
- return true
- end
- -- Creates a new entry with the given user input.
- local function createEntry()
- print("Enter some text below.")
- local input = io.read()
- if input == nil or #input == 0 then
- print("No valid input given, cancelling this entry.")
- return
- end
- print("Getting real-world time...")
- local time = getRealWorldTime()
- print("Saving entry for timestamp " .. time.string .. "...")
- if saveEntry(input, time) then
- print("Entry saved successfully!")
- else
- print("An error occurred, and the entry could not be saved.")
- end
- end
- -- Shows the main menu to the user.
- local function mainMenu()
- print("[Main Menu]")
- print("What would you like to do?")
- print("1. Create a new log entry.")
- print("2. View a list of log entries.")
- print("3. Quit the program.")
- local choice = tonumber(io.read())
- while choice == nil or choice < 1 or choice > 3 do
- print("Invalid choice. Please enter an integer corresponding to one of the options.")
- choice = tonumber(io.read())
- end
- if choice == 1 then
- createEntry()
- elseif choice == 2 then
- -- view entries list
- else
- -- quit
- print("Quitting the program...")
- RUNNING = false
- end
- end
- while RUNNING do
- mainMenu()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement