Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local scSide = "left"
- local stoSide = "right"
- local sto = peripheral.wrap(stoSide)
- local sc = peripheral.wrap(scSide)
- local mw = 51
- local mh = 16
- local topBar = window.create(term.current(),1 ,1 ,mw,1)
- local win = window.create(term.current(), 1,2,mw,mh)
- local pageIndex = 0
- local inPageIndex = 0
- local pageCount = 0
- local itemCount = 0
- local listSize = 0
- -- [name] = {[index] = {},[count] = ...}
- local winList = {}
- local nList = {}
- local searchStateFlag = false
- local searchPos = 1
- local winNormColor = {
- font = colors.black,
- bg = colors.lime
- }
- local winFocusColor = {
- font = colors.gray,
- bg = colors.pink
- }
- topBar.setBackgroundColor(colors.orange)
- topBar.setTextColor(colors.white)
- win.setBackgroundColor(winNormColor.bg)
- win.setTextColor(winNormColor.font)
- local function LDebug(str,col)
- term.setCursorPos(1,col)
- term.setTextColor(colors.orange)
- term.setBackgroundColor(colors.white)
- term.clearLine()
- term.write(str)
- end
- local function renewTopBar()
- local str = string.format("%3d/%3d size:%d",pageIndex,pageCount,itemCount)
- topBar.clear()
- topBar.setCursorPos(1,1)
- topBar.write(str)
- end
- local function renewWindow()
- win.clear()
- local s = 0
- local nPos = 1
- local str = ""
- for i,j in pairs(winList) do
- for o,k in pairs(j) do
- -- LDebug("o : "..o,18)
- win.setCursorPos(1,nPos)
- if(nPos == inPageIndex) then
- win.setBackgroundColor(winFocusColor.bg)
- win.setTextColor(winFocusColor.font)
- win.clearLine()
- else
- win.setBackgroundColor(winNormColor.bg)
- win.setTextColor(winNormColor.font)
- end
- s = string.find(o,":")
- win.write(string.sub(o,s+1))
- if(k.count > 10000) then
- str = string.format("%3dK",(k.count-k.count%1000)/1000)
- else
- str = string.format("%4d",k.count)
- end
- end
- win.setCursorPos(mw - 4,nPos)
- win.write(str)
- -- LDebug(j.count,18)
- nPos = nPos + 1
- end
- win.setBackgroundColor(winNormColor.bg)
- win.setTextColor(winNormColor.font)
- end
- local function re_listCount()
- -- name ,table
- for i,j in pairs(nList) do
- j.count = 0
- for o,k in pairs(j.index) do
- j.count = j.count + tList[k].count
- end
- end
- end
- local function re_pageCount()
- itemCount = table.getn(nList)
- pageCount = (itemCount - itemCount % mh)/mh
- if(itemCount % mh ~= 0) then
- pageCount = pageCount +1
- end
- end
- local function re_scan()
- tList = sc.list()
- itemCount = 0
- nList = {}
- -- name={index,count}
- for i,j in pairs(tList) do
- if(nList[j.name] == nil) then
- nList[j.name] = {}
- nList[j.name]["index"] = {}
- table.insert(nList[j.name]["index"],i)
- nList[j.name]["count"] = 0
- itemCount = itemCount +1
- else
- table.insert(nList[j.name]["index"],i)
- end
- end
- re_listCount()
- pageCount = (itemCount - itemCount % mh)/mh
- if(itemCount % mh ~= 0) then
- pageCount = pageCount +1
- end
- -- for i,j in pairs(nList) do
- -- if(j[2] == nil ) then
- -- table.remove(nList,i)
- -- end
- -- end
- -- re_pageCount()
- end
- local function sub_scan()
- end
- local function pageChange()
- winList = {}
- local tMod = itemCount % mh
- if (pageIndex == pageCount and tMod ~= 0) then
- listSize = tMod
- else listSize = mh end
- -- init pos
- local startPos = (pageIndex - 1) * mh
- local endPos = startPos + listSize
- local nPos = 1
- -- LDebug("listSize "..listSize.."itemCOunt "..itemCount, 19)
- for i,j in pairs(nList) do
- if (nPos > startPos) then
- if(nPos > endPos) then
- break
- end
- -- winList[i] = j
- local d = {}
- d[i]=j
- table.insert(winList,d)
- end
- nPos = nPos + 1
- end
- -- LDebug("endPos "..pos,18)
- if(inPageIndex > listSize)then
- inPageIndex = listSize
- end
- renewTopBar()
- renewWindow()
- end
- local function List_move()
- local nPos = 1
- local index = 0
- for i,j in pairs(winList) do
- if(nPos == inPageIndex) then
- for o,k in pairs(j) do
- index = table.remove(nList[o].index)
- sc.pushItems(stoSide,index)
- if(table.getn(nList[o].index) == 0) then
- nList[o].index = nil
- end
- break
- end
- end
- nPos = nPos + 1
- end
- -- table.remove(nList,pageIndex * mh + inPageIndex)
- re_scan()
- table.sort(nList)
- re_listCount()
- if(pageIndex >= pageCount) then
- pageIndex = pageCount
- if(inPageIndex >= itemCount % mh) then
- inPageIndex = itemCount % mh
- end
- end
- pageChange()
- end
- local function init()
- pageIndex = 1
- inPageIndex = 1
- re_scan()
- -- gen winList
- pageChange()
- end
- local function preParse(cmdStr)
- local sPos,ePos
- local cmdArgsPack = {}
- while true do
- sPos,ePos = string.find(cmdStr, " ")
- if(sPos == nil) then
- table.insert(cmdArgsPack,cmdStr)
- break
- end
- table.insert(cmdArgsPack,string.sub(cmdStr,1,sPos-1))
- cmdStr = string.sub(cmdStr, ePos+1)
- end
- return cmdArgsPack
- end
- local function paraseCMD(cmdStr)
- local tArgs = preParse(cmdStr)
- if(tArgs[1] == "s") then
- -- search
- if(searchStateFlag == false) then
- searchStateFlag = true
- searchPos = 0
- end
- local nPos = 1
- table.sort(nList)
- for i,j in pairs(nList) do
- if(nPos > searchPos) then
- if( string.find(i,tArgs[2]) ~= nil) then
- -- find
- -- LDebug("str "..nPos , 19)
- inPageIndex = nPos % mh
- if(inPageIndex == 0 ) then
- inPageIndex = mh
- end
- pageIndex = (nPos - nPos % mh) / mh + 1
- searchPos = nPos
- break
- end
- end
- nPos = nPos +1
- end
- if(nPos > itemCount) then
- term.setCursorPos(1,19)
- term.clearLine()
- term.write("not found")
- end
- pageChange()
- else
- -- remove
- searchStateFlag = false
- List_move()
- term.setCursorPos(1,18)
- term.clearLine()
- -- LDebug("args "..tArgs[1].." "..tArgs,18)
- end
- end
- local function mainloop()
- term.clear()
- init()
- while true do
- term.setCursorPos(1,18)
- term.setTextColour(colors.white)
- -- re_scan()
- -- pageChange()
- local cmd = io.read()
- -- LDebug("nowPos :"..(pageIndex-1)*mh + inPageIndex, 18)
- paraseCMD(cmd)
- end
- end
- local keyMap = {
- ["end"]=(function ()
- if(inPageIndex < mh and inPageIndex < listSize) then
- inPageIndex = inPageIndex + 1
- -- LDebug("nowPos "..(pageIndex-1) * mh + inPageIndex,18)
- renewWindow()
- end
- end),
- ["home"]=(function ()
- if(inPageIndex > 1) then
- inPageIndex = inPageIndex - 1
- -- LDebug("nowPos "..(pageIndex-1) * mh + inPageIndex,18)
- renewWindow()
- end
- end),
- ["pageUp"]=(function ()
- if(pageIndex > 1) then
- inPageIndex = 1
- pageIndex = pageIndex - 1
- pageChange()
- end
- end),
- ["pageDown"]=(function ()
- if(pageIndex < pageCount) then
- inPageIndex = 1
- pageIndex = pageIndex + 1
- pageChange()
- end
- end),
- }
- local function key_check()
- while true do
- local event, key, isHeld = os.pullEvent("key")
- local kvalue = keys.getName(key)
- if(keyMap[kvalue]~= nil) then
- keyMap[kvalue]()
- term.setTextColour(colors.white)
- term.setCursorPos(1,18)
- end
- end
- end
- parallel.waitForAny(mainloop,key_check)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement