View difference between Paste ID: mC2CGgVc and LbVvhrWQ
SHOW: | | - or go back to the newest paste.
1
-------------------Start Settings Section------------------
2
---Note neither w nor h are directly related to Physical screen size
3
---they just control in how many "pixels" the physical space is divided
4
--- I recommend leaving it default and test then change and see what fits your screen
5
---Also the bigger the numbers the smaller the text
6
w = 85 --Screen Width
7-
--- Edit: 05/16/2022  Thanks to Kian369 for pointing out a better way to calculate max capacity
7+
8
9
minStackSize = 500 --- This is the stack size used to Calculate Capacity
10
headerRow = 1 --- Row where the Header row is drawn
11
startRow = 3  --- Row in which itemList starts
12
startCol = 1 --- Col in which ItemList starts
13
14
refreshTime = 60 --- Seconds between refresh of Screen also used to measure gain loss
15
16
mediumThreshold = 50  ---when stock higher then this percentage bar color changes to yellow by default
17
highThreshold = 90  ---when stock higher then this percentage bar color changes to green by default
18
19
--- Here you can change the Colors of some elements
20
--- Backcolors is for Background FrontColors for Foreground
21
--- Colors that are used in the setColor Function need to exist in both tables
22
23
BackColors = {default={0,0,0,0},rowEven={255,0,0,0.1},rowOdd = {0,0,0,1},header={255,0,0,0.1},
24
              lowStockBar={255,0,0,1},mediumStockBar={50,50,0,1},highStockBar={0,255,0,1},nextButton={0,255,0,0.3},
25
              prevButton = {0,255,0,0.3}}
26
27
FrontColors = {default={255,255,255,1},rowEven={255,255,255,1},rowOdd = {255,255,255,1},header={255,255,255,1},nextButton={0,0,0,1},
28
               prevButton = {0,0,0,1}}
29
30
--- In this table you can change the Column attributes
31
--- width is the Columnwidth be aware that these are cut off if text is longer then width
32
--- header is the Text in the Header Column
33
--- offset allows you to move header text left or right
34
--- Note: Changing Header text does not change the content for that you need to adjust the code in the drawRows function
35
Columns = {}
36
Columns[0] = {width = 29,header="Item",offset = 0}
37
Columns[1] = {width = 10,header="Current",offset = 0}
38
Columns[2] = {width = 10,header="Max",offset = 0}
39
Columns[3] = {width = 10,header=" %",offset = 0}
40
Columns[4] = {width = 10,header="Change",offset = -3}
41
Columns[5] = {width = 12,header="Visual",offset = 0}
42
43
buttonText_NextPage = "  Next Page  "
44
buttonText_PrevPage = "  Prev Page  "
45
--- Only change code outside of the settings area if you know what you are doing ;)
46
-------------------End Settings Section------------------
47
48
49
-------------------Start Helper Functions------------------
50
function setBackgroundColor(colorName)
51
    gpu:setBackground(BackColors[colorName][1],BackColors[colorName][2],BackColors[colorName][3],BackColors[colorName][4])
52
end
53
54
function setForegroundColor(colorName)
55
    gpu:setForeground(FrontColors[colorName][1],FrontColors[colorName][2],FrontColors[colorName][3],FrontColors[colorName][4])
56
end
57
58
function setColor(colorName)
59
    setBackgroundColor(colorName)
60
    setForegroundColor(colorName)
61
end
62
63
function setDefaultColor()
64
    setColor("default")
65
end
66
67
function setRowColor(rowNum)
68
    if rowNum % 2 == 0 then
69
        setColor("rowEven")
70
    else
71
        setColor("rowOdd")
72
    end
73
end
74
75
function clearScreen()
76
    setDefaultColor()
77
    gpu:fill(0,0,w,h," ")
78
    gpu:flush()
79
end
80
81
function pairsByKeys (t, f)
82
    local a = {}
83
    for n in pairs(t) do table.insert(a, n) end
84
    table.sort(a, f)
85
    local i = 0
86
    local iter = function ()
87
        i = i + 1
88
        if a[i] == nil then return nil
89
        else return a[i], t[a[i]]
90
        end
91
    end
92
    return iter
93
end
94
95
function fillString(str,len)
96
    local trimmedText = string.sub(str,0,len)
97
    return trimmedText .. string.rep(" ",len-string.len(trimmedText))
98
end
99
100
function format_int(number)
101
102
    local i, j, minus, int, fraction = tostring(number):find('([-]?)(%d+)([.]?%d*)')
103
104
    --- reverse the int-string and append a comma to all blocks of 3 digits
105
    int = int:reverse():gsub("(%d%d%d)", "%1'")
106
107
    --- reverse the int-string back remove an optional comma and put the
108
    --- optional minus and fractional part back
109
    return minus .. int:reverse():gsub("^'", "") .. fraction
110
end
111
112
function getItemsPerPage()
113
    return h-2-startRow
114
end
115
116
function setMaxPages()
117
    maxItemsPerPage = getItemsPerPage()
118
    totalItems = 0
119
    for k,v in pairs(items) do
120
        totalItems = totalItems + 1
121
    end
122
    maxPage = totalItems/maxItemsPerPage
123
end
124
125
----------------------End Helper Functions------------------
126
127
----------------------Start Functions for Display Stuff ------------------
128
129
function drawHeader(row)
130
    local colStart = startCol
131
    setColor("header")
132
    for i = 0,#Columns,1 do
133
        local col = Columns[i]
134
        gpu:setText(colStart+col.offset,headerRow,fillString(col.header,col.width))
135
        colStart = colStart + col.width
136
    end
137
    gpu:flush()
138
end
139
140
function drawButtons()
141
    setColor("prevButton")
142
    gpu:setText(0,h-1,buttonText_PrevPage)
143
    setColor("nextButton")
144
    gpu:setText(w-14,h-1,buttonText_NextPage)
145
    setDefaultColor()
146
end
147
148
function drawBar(x,y,capacityPercent)
149
    setDefaultColor()
150
    gpu:setText(x,y,"|")
151
    if capacityPercent > 0 and capacityPercent <= 50 then
152
        setBackgroundColor("lowStockBar")
153
    elseif capacityPercent > 50 and capacityPercent <= 90 then
154
        setBackgroundColor("mediumStockBar")
155
    else
156
        setBackgroundColor("highStockBar")
157
    end
158
    BarLen =math.floor(capacityPercent/10)
159
    if BarLen < 1 then BarLen = 1 end
160
    gpu:setText(x+1,y,fillString("",BarLen))
161
    setDefaultColor()
162
    gpu:setText(x+11,y,"|")
163
end
164
165
function getItemChange(itemName,itemInfo)
166
    if itemsLast[itemName] ~= nil then
167
        change = itemInfo.count - itemsLast[itemName].count
168
    else
169
        change = "-"
170
    end
171
    return change
172
end
173
174
function drawRows()
175
    local currentRow = 0
176
    local columnValues = {}
177
    local currentIndex = 1
178
    local itemsPerPage = getItemsPerPage()
179
    local maxIndex = itemsPerPage*page
180
    local minIndex = maxIndex - itemsPerPage +1
181
182
    currentRow = currentRow+startRow
183
184
    for itemName,itemInfo in pairsByKeys(items) do
185
        if currentIndex >= minIndex and currentIndex <= maxIndex then
186
            local capacityPercent = math.floor(itemInfo.count/itemInfo.capacity*100)
187
            columnValues[0] = itemName
188
            columnValues[1] = format_int(itemInfo.count)
189
            columnValues[2] = format_int(itemInfo.capacity)
190
            columnValues[3] = capacityPercent
191
            columnValues[4] = getItemChange(itemName,itemInfo)
192
            columnValues[5] = "Graph"
193
194
            local colStart = startCol
195
            for i = 0,#Columns,1 do
196
                setRowColor(currentRow)
197
                local col = Columns[i]
198
                if columnValues[i] == "Graph" then
199
                    drawBar(colStart+col.offset,currentRow,capacityPercent)
200
                else
201
                    gpu:setText(colStart+col.offset,currentRow,fillString(columnValues[i],col.width))
202
                end
203
204
                colStart = colStart + col.width
205
            end
206
            currentRow = currentRow + 1
207
        end
208
        currentIndex = currentIndex +1
209
    end
210
    itemsLast = items
211
end
212
213
function DrawTable()
214
    clearScreen()
215
    drawHeader()
216
    drawRows()
217
    drawButtons()
218
    gpu:flush()
219
end
220
221
----------------------Start Functions for Grabbing Container Info ------------------
222
223
function getContainerInfo(container)
224
    inv = container:getInventories()[1]
225
    inventorySize = inv.size
226
    inv:sort()
227
    maxInventory = minStackSize*inventorySize
228
    containerInfo = {name = "nothing", count = 0,capacity=inventorySize}
229
    for i=0,inventorySize-1,1 do
230
        stack = inv:getStack(i)
231
        if stack.item.type then
232
            itemName = stack.item.type.name
233
            itemCount = inv.itemCount
234
            containerInfo = {name=itemName,count = itemCount,capacity = maxInventory}
235-
            containerInfo = {name=itemName,count = itemCount,capacity = stack.item.type.max*inventorySize}
235+
236
        end
237
    end
238
    return containerInfo
239
end
240
241
function addToItems(containerInfo)
242
    if items[containerInfo.name] == nil then
243
        items[containerInfo.name] = containerInfo
244
    else
245
        items[containerInfo.name].count = items[containerInfo.name].count + containerInfo.count
246
        items[containerInfo.name].capacity = items[containerInfo.name].capacity + containerInfo.capacity
247
    end
248
end
249
250
----------------------End Functions for Grabbing Container Info ------------------
251
252
--- Grabing Components and initializing Global Variables
253
254
Containers = component.proxy(component.findComponent(findClass("FGBuildableStorage")))
255
gpu = computer.getPCIDevices(findClass("GPUT1"))[1]
256
comp = component.findComponent(findClass("Screen"))[1]
257
screen = component.proxy(comp)
258
gpu:bindScreen(screen)
259
gpu:setSize(w,h)
260
event.listen(gpu)
261
262
pages = {}
263
items = {}
264
itemsLast = {}
265
page = 1
266
maxPage = 1
267
lastRefresh = computer.millis()
268
firstStart = true
269
270
---Main Loop
271
---
272
273
while true do
274
    if computer.millis()-lastRefresh >= refreshTime*1000 or firstStart then
275
        firstStart = false
276
        Containers = component.proxy(component.findComponent(findClass("FGBuildableStorage")))
277
        items = {}
278
        for _,v in pairs(Containers) do
279
            CurrentInfo = getContainerInfo(v)
280
            if CurrentInfo.name ~= "nothing" then
281
                addToItems(CurrentInfo)
282
            end
283
        end
284
        setMaxPages()
285
        DrawTable()
286
        itemsLast = items
287
        lastRefresh = computer.millis()
288
    end
289
290
291
    signal, sender, x, y = event.pull(refreshTime)
292
    if signal == "OnMouseDown" then
293
        if x>=0 and x<=string.len(buttonText_PrevPage) and y==h-1 and page > 1 then
294
            page=page-1
295
            DrawTable()
296
        end
297
        if x>=w-string.len(buttonText_NextPage) and x<=w and y==h-1  and page < maxPage then
298
            page=page+1
299
            DrawTable()
300
        end
301
    end
302
end