Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- colors = require("colors")
- term = require("term")
- component = require("component")
- gpu = component.gpu
- local viewX, viewY = gpu.getViewport()
- local barsLeft = 3
- local barWidth = 5
- local barHeight = 10
- local colourHeight = barHeight - 2
- local barSpace = 5
- local barsTop = viewY - barHeight
- local barOutline, barHighlight = 0xFFFFFF, 0x8888FF
- local itemList = {}
- local bars = {}
- local lastUsed = nil
- -- List
- ListItem = { ItemContent = nil,
- ListAssociations = nil,
- Completed = false,
- Value = 1 }
- function ListItem:new (itemContent, listAssociations, completed, value)
- local self = setmetatable({}, self)
- self.__index = self
- self.ItemContent = itemContent
- self.ListAssociations = listAssociations
- self.Completed = completed
- self.Value = value
- return self
- end
- -- Bars
- Bar = { Left = barsLeft,
- Level = 1,
- Max = 8,
- Name = null,
- Number = 1 }
- function Bar:new (left, level, max, name, number)
- local self = setmetatable({}, self)
- self.__index = self
- self.Left = left
- self.Level = level
- self.Max = max
- self.Name = name
- self.Number = number
- return self
- end
- -- Setters and Getters
- function getBarName(bar)
- if type(bar) == "table" then return bar.Name end
- end
- function setBarName(bar, newName)
- if type(bar) == "table" and type(newName) == "string" then
- bar.Name = newName
- end
- end
- function getBarLevel(bar)
- if type(bar) == "table" then
- return bar.Level
- end
- end
- function setBarLevel(bar, newLevel)
- if type(bar) == "table" and type(newLevel) == "number" then
- if newLevel >= 1 and newLevel <= bar.Max then
- bar.Level = newLevel
- end
- end
- drawBars()
- end
- function getBarMax(bar)
- return bar.Max
- end
- function setBarMax(bar, newMax)
- if type(bar) == "table" and newMax < 1000 then
- bar.Max = newMax
- end
- end
- function getBarFill(bar)
- current, max = getBarLevel(bar), getBarMax(bar)
- level = math.floor((current / max) * colourHeight)
- if level < 1 then return 1 end
- if level > colourHeight then return colourHeight end
- return level
- end
- function completeItem(item, bar)
- item.Completed = "true"
- --setBarLevel(bar, bar.Level + item.Value)
- for i=1,8 do -- TODO: UNHARDCODE 8
- associationCheck = tonumber(string.sub(item.ListAssociations, i, i))
- print(associationCheck) -- THIS IS WHERE YOU ARE NOW got getitem done yay :3
- end
- end
- function addListItem(content, associatedBar)
- local currentListItem = nil
- if type(content) == "string" then
- currentListItem = ListItem:new(content, "00000000", "false", 1)
- else
- return nil
- end
- local listAssociationNumber = associatedBar.Number
- currentListItem.ListAssociations = string.sub(currentListItem.ListAssociations, 1, listAssociationNumber - 1) .. 1 .. string.sub(currentListItem.ListAssociations, listAssociationNumber + 1, string.len(currentListItem.ListAssociations))
- print(currentListItem.ItemContent .. " " .. currentListItem.ListAssociations)
- table.insert(itemList, currentListItem)
- end
- function printItemList()
- for k,v in pairs(itemList) do
- if v.Completed == "false" then
- print(k .. ": " .. v.ItemContent .. ", " .. v.ListAssociations .. ", " .. v.Completed .. ", " .. v.Value)
- end
- end
- end
- function clearSaveFile(filename)
- local saveFile = io.open(filename, "w")
- saveFile:write("")
- saveFile:close()
- end
- function loadDate()
- local readFile = io.open("./data/lastRun", "r")
- for line in io.lines("./data/lastRun") do
- print(line)
- end
- end
- function loadItems()
- local readFile = io.open("./data/items", "r")
- lines = {}
- loadedItems = {}
- if readFile == nil then return end
- for line in io.lines("./data/items") do
- table.insert(lines, line)
- end
- local lineIndex = 0
- local listAssociationString = '%[%"ListAssociations%"%] %= '
- local itemContentString = '%[%"ItemContent%"%] %= '
- local completedString = '%[%"Completed%"%] %= '
- local valueString = '%[%"Value%"%] %= '
- local loadAssociation, loadContent, loadCompleted, loadValue = nil, nil, nil, nil
- for k,v in pairs(lines) do
- local lineString = '%[' .. lineIndex + 1 .. '%] %= %{'
- local stringEnd, _ = string.find(v, ",")
- if string.find(v, lineString) then
- lineIndex = lineIndex + 1
- end
- if string.find(v, listAssociationString) then
- _, loadAssociation = string.find(v, listAssociationString)
- loadAssociation = string.sub(v, loadAssociation + 2, stringEnd - 2)
- --print("Ding! loadAssociation = '" .. loadAssociation .. "'")
- elseif string.find(v, itemContentString) then
- _, loadContent = string.find(v, itemContentString)
- loadContent = string.sub(v, loadContent + 2, stringEnd - 2)
- --print("Ding! loadContent = '" .. loadContent .. "'")
- elseif string.find(v, completedString) then
- _, loadCompleted = string.find(v, completedString)
- loadCompleted = string.sub(v, loadCompleted + 2, stringEnd - 2)
- elseif string.find(v, valueString) then
- _, loadValue = string.find(v, valueString)
- loadValue = tonumber(string.sub(v, loadValue + 1, stringEnd - 1))
- end
- if loadAssociation ~= nil and loadContent ~= nil and loadCompleted ~= nil and loadValue ~= nil then
- table.insert(itemList, ListItem:new(loadContent, loadAssociation, loadCompleted, loadValue))
- loadAssociation, loadContent, loadCompleted, loadValue = nil, nil, nil, nil
- end
- end
- end
- function loadBars()
- local readFile = io.open("./data/barData", "r")
- lines = {}
- loadedBars = {}
- if readFile == nil then
- --Load default bars
- for i=1, 8 do -- TODO : replace '8'
- if i==1 then
- currentLeft = barsLeft
- else
- currentLeft = currentLeft + barWidth + barSpace
- end
- currentBar = Bar:new(currentLeft, i, 8, "Bar " .. i, i)
- table.insert(loadedBars, currentBar)
- end
- else
- --Load saved bars
- barIndex = 0
- for line in io.lines("./data/barData") do
- table.insert(lines, line)
- end
- local numString = '%[%"Number%"%] %= '
- local levString = '%[%"Level%"%] %= '
- local maxString = '%[%"Max%"%] %= '
- local namString = '%[%"Name%"%] %= '
- local lefString = '%[%"Left%"%] %= '
- local loadNum, loadLev, loadMax, loadNam, loadLef = nil, nil, nil, nil, nil
- for k,v in pairs(lines) do
- local barString = '%[' .. barIndex + 1 .. '%] %= %{'
- local stringEnd, _ = string.find(v, ",")
- if string.find(v, barString) then
- barIndex = barIndex + 1
- end
- if string.find(v, numString) then
- _, loadNum = string.find(v, numString)
- loadNum = tonumber(string.sub(v, loadNum + 1, stringEnd - 1))
- elseif string.find(v, levString) then
- _, loadLev = string.find(v, levString)
- loadLev = tonumber(string.sub(v, loadLev + 1, stringEnd - 1))
- elseif string.find(v, maxString) then
- _, loadMax = string.find(v, maxString)
- loadMax = tonumber(string.sub(v, loadMax + 1, stringEnd - 1))
- elseif string.find(v, namString) then
- _, loadNam = string.find(v, namString)
- loadNam = string.sub(v, loadNam + 1, stringEnd - 1)
- loadNam = string.sub(loadNam, 2, string.len(loadNam)-1)
- elseif string.find(v, lefString) then
- _, loadLef = string.find(v, lefString)
- loadLef = tonumber(string.sub(v, loadLef + 1, stringEnd - 1))
- end
- if loadNum ~= nil and loadLev ~= nil and loadMax ~= nil and loadNam ~= nil and loadLef ~= nil then
- table.insert(loadedBars, Bar:new(loadLef, loadLev, loadMax, loadNam, loadNum))
- loadNum, loadLev, loadMax, loadNam, loadLef = nil, nil, nil, nil
- end
- --os.sleep(0.05)
- end
- end
- if readFile ~= nil then readFile:close() end
- bars = loadedBars
- end
- function loadAll()
- loadDate()
- loadBars()
- loadItems()
- end
- function saveBars(b)
- barDataFileName = "./data/barData"
- clearSaveFile(barDataFileName)
- local saveFile = io.open(barDataFileName, "a")
- writeTable(b, saveFile)
- saveFile:close(saveFile)
- end
- function saveDate()
- dateFileName = "./data/date"
- tempFileName = "./data/tmp"
- clearSaveFile(dateFileName)
- clearSaveFile(tempFileName)
- local saveFile = io.open(dateFileName, "a")
- --local tempFile = io.open(tempFileName, "r")
- --os.date("%d") something something
- saveFile:close(saveFile)
- end
- function saveList(il)
- itemListFileName = "./data/items"
- clearSaveFile(itemListFileName)
- local saveFile = io.open(itemListFileName, "a")
- writeTable(il, saveFile)
- saveFile:close(saveFile)
- end
- function writeTable (o, f)
- if type(o) == "number" then
- f:write(o)
- elseif type(o) == "string" then
- f:write(string.format("%q", o))
- elseif type(o) == "table" then
- f:write("{\n")
- for k,v in pairs(o) do
- if (k ~= "__index") then
- f:write(" [")
- writeTable(k, f)
- f:write("] = ")
- writeTable(v, f)
- f:write(",\n")
- end
- end
- f:write(" }\n")
- else
- error("cannot serialize a " .. type(o))
- end
- end
- local barColors = { 0xFF0000,
- 0xDD2200,
- 0xBB4400,
- 0x996600,
- 0x669900,
- 0x44BB00,
- 0x22DD00,
- 0x00FF00 }
- function drawRectangle(left, top, width, height, colorFG, colorBG)
- gpu.setForeground(colorFG)
- gpu.setBackground(colorBG)
- gpu.fill(left, top, width, height, " ")
- end
- function drawBar(bar, highlightColor) -- BARMAX UPDATES REQUIRED
- --Box, then black, then bar
- if highlightColor == nil then
- drawRectangle(bar.Left, barsTop, barWidth, barHeight, 0x000000, barOutline)
- else
- drawRectangle(bar.Left, barsTop, barWidth, barHeight, 0x000000, highlightColor)
- end
- drawRectangle(bar.Left+1, barsTop+1, barWidth-2, barHeight-2, 0x000000, 0x000000)
- if bar.Level >= 1 and getBarFill(bar) <= 8 then
- drawRectangle(bar.Left+1, barsTop+1+(colourHeight - getBarFill(bar)), barWidth-2, getBarFill(bar), 0x000000, barColors[getBarFill(bar)])
- end
- end
- function highlightBar(bar)
- drawBar(bar, barHighlight)
- end
- function drawBars()
- for i = 1, 8 do -- TODO: Update 8 here
- drawBar(bars[i])
- end
- setDefaultColours()
- end
- function drawAll()
- drawBars()
- drawRectangle(1, barsTop-2, viewX, 1, 0x000000, 0xFFFFFF)
- end
- function grooveBars() -- USE GETTER/SETTER INSTEAD OF MANUAL SET
- for i = 1, 100 do
- local randBar = math.random(8)
- local randSign = math.random(2)
- local curBar = bars[randBar]
- curBar.Level = curBar.Level + (randSign == 1 and -1 or 1)
- if curBar.Level == -1 then curBar.Level = 0 end
- if curBar.Level == 9 then curBar.Level = 8 end
- drawBars()
- os.sleep(0.005)
- end
- end
- function clearAll(draw)
- draw = draw or ""
- io.flush()
- gpu.setBackground(0x000000)
- gpu.setForeground(0xFFFFFF)
- term.clear()
- if draw ~= "clear" then drawAll() end
- setDefaultColours()
- end
- function setDefaultColours()
- gpu.setBackground(0x000000)
- gpu.setForeground(0xFFFFFF)
- end
- function waitSelectBar()
- local input
- local continueLoop = true
- local help = false
- local inputReturnValue = nil
- clearAll()
- while(true) do
- print("Select a bar, or try 'help' or 'exit'")
- if inputReturnValue ~= nil then print(inputReturnValue) end
- inputReturnValue = parseInput(string.lower(io.read()))
- if type(inputReturnValue) == "table" then return inputReturnValue end
- end
- return nil
- end
- function parseInput(input)
- clearAll()
- for i, v in ipairs(bars) do
- if tonumber(input) == v.Number or input == string.lower(v.Name) then
- return v
- end
- end
- if input == "exit" then
- clearAll("clear")
- exitBars()
- elseif input == "help" then
- printHelp()
- elseif input == "g r o o v e" then
- grooveBars()
- elseif input == "print list" then
- printItemList()
- elseif input == "raise all" then
- raiseAllBars(1)
- elseif input == "lower all" then
- lowerAllBars(1)
- elseif input == "bars" then
- printBars()
- elseif input == "debugmax" then
- getBarFill(getBar(1))
- elseif string.match(string.sub(input, 1, 4), "add ") then
- parseAddListItem(parseMultipleWords(input))
- elseif string.match(string.sub(input, 1, 4), "try ") then
- print(parseMultipleWords(input))
- elseif string.match(string.sub(input, 1, 7), "rename ") then
- local _, barToRename, newName, _ = parseMultipleWords(input)
- barToRename = getBar(barToRename)
- setBarName(barToRename, newName)
- elseif string.match(string.sub(input, 1, 9), "complete ") then
- local _, relevantBar, relevantItem, _ = parseMultipleWords(input)
- relevantBar = getBar(relevantBar)
- relevantItem = getItem(tonumber(relevantItem), relevantBar)
- print(relevantItem.ItemContent .. "marked as complete!")
- os.sleep(3)
- else
- return input .. " is not a valid input."
- end
- end
- function getItem(itemNumber, bar)
- local index = 0
- for i, v in ipairs(itemList) do
- associated = tonumber(string.sub(v.ListAssociations, bar.Number, bar.Number))
- if (associated == 1) then
- index = index + 1
- if (index == itemNumber) then
- return v end
- end
- end
- end
- function getBar(barName)
- for i, v in ipairs(bars) do
- if tonumber(barName) == v.Number or barName == string.lower(v.Name) then
- return v
- end
- end
- end
- function parseMultipleWords(input)
- local remainingInput = input
- local firstSpace = string.find(remainingInput, " ")
- if firstSpace == nil then return input end
- local firstWord = string.sub(remainingInput, 1, firstSpace - 1)
- remainingInput = string.sub(remainingInput, string.len(firstWord) + 2, string.len(remainingInput))
- local secondWord = nil
- if string.sub(remainingInput, 1, 1) ~= '"' then
- local secondSpace = string.find(remainingInput, " ")
- if secondSpace == nil then return firstWord, remainingInput end
- secondWord = string.sub(remainingInput, 1, secondSpace - 1)
- remainingInput = string.sub(remainingInput, string.len(secondWord) + 2, string.len(remainingInput))
- else
- local firstQuote = 1
- local secondQuote = string.find(string.sub(remainingInput, 2, string.len(remainingInput)), '"') + 1
- secondWord = string.sub(remainingInput, firstQuote + 1, secondQuote - 1)
- remainingInput = string.sub(remainingInput, string.len(secondWord) + 4, string.len(remainingInput))
- end
- local thirdWord = nil
- if string.sub(remainingInput, 1, 1) ~= '"' then
- local thirdSpace = string.find(remainingInput, " ")
- if thirdSpace == nil then return firstWord, secondWord, remainingInput end
- thirdWord = string.sub(remainingInput, 1, thirdSpace - 1)
- remainingInput = string.sub(remainingInput, string.len(secondWord) + 2, string.len(remainingInput))
- else
- local firstQuote = 1
- local secondQuote = string.find(string.sub(remainingInput, 2, string.len(remainingInput)), '"') + 1
- thirdWord = string.sub(remainingInput, firstQuote + 1, secondQuote - 1)
- remainingInput = string.sub(remainingInput, string.len(thirdWord) + 4, string.len(remainingInput))
- end
- local fourthWord = string.sub(remainingInput, 1, string.len(remainingInput))
- return firstWord, secondWord, thirdWord, fourthWord
- end
- function parseAddListItem(_, content, addBar)
- local correctUsage = "Correct usage: 'add [text] [bar name/number]'"
- local currentBar = nil
- if content == nil or addBar == nil or type(content) ~= "string" then
- print(correctUsage)
- return nil
- end
- for i,v in ipairs(bars) do
- if tonumber(addBar) == v.Number or addBar == string.lower(v.Name) then
- currentBar = v
- end
- if currentBar ~= nil then break end
- end
- if currentBar == nil then
- print(correctUsage)
- else
- addListItem(content, currentBar)
- end
- end
- function lowerAllBars(amount)
- for k,v in pairs(bars) do
- currentLevel = v.Level
- setBarLevel(v, currentLevel - amount)
- end
- end
- function raiseAllBars(amount)
- for k,v in pairs(bars) do
- currentLevel = v.Level
- setBarLevel(v, currentLevel + amount)
- end
- end
- function printTasksByBar(bar)
- gpu.setForeground(0x00FF00)
- print(" " .. bar.Name)
- gpu.setForeground(0xFFFFFF)
- print(string.rep("=", string.len(bar.Name) + 4))
- for k,v in pairs(itemList) do
- if tonumber(string.sub(v.ListAssociations, bar.Number, bar.Number)) == 1 then
- print(v.ItemContent)
- os.sleep(0.1)
- end
- end
- end
- function printBars()
- for i, v in ipairs(bars) do
- print(i .. ": [" .. v.Name .. "]")
- print(i .. ": [" .. v.Name .. "] - " .. v.Level .. " " .. v.Max)
- end
- end
- function printHelp()
- local otherCommands = "'bars' - Lists all bars\n'print list' - Prints list of tasks\n'add [task] [bar name/number]' - Adds a task to task list\n'rename [bar] [newname]' - Rename a bar\n'raise all / lower all' - debug\n"
- local helpMessage = "Select a bar by entering its name or number\n"
- print(otherCommands)
- print(helpMessage)
- end
- function exitBars()
- print("Exited")
- saveBars(bars)
- saveList(itemList)
- os.exit()
- end
- -- ===== Main =====
- loadAll()
- drawAll()
- while(true) do
- local selectedBar = waitSelectBar()
- printTasksByBar(selectedBar)
- highlightBar(selectedBar)
- os.sleep(1)
- drawBar(selectedBar)
- end
- saveBars(bars)
- saveList(itemList)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement