Advertisement
Lanzr

mass_sto_broswer

Nov 15th, 2023 (edited)
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.31 KB | None | 0 0
  1. local scSide = "left"
  2. local stoSide = "right"
  3.  
  4. local sto = peripheral.wrap(stoSide)
  5. local sc = peripheral.wrap(scSide)
  6. local mw = 51
  7. local mh = 16
  8.  
  9. local topBar = window.create(term.current(),1 ,1 ,mw,1)
  10. local win = window.create(term.current(), 1,2,mw,mh)
  11.  
  12. local pageIndex = 0
  13. local inPageIndex = 0
  14. local pageCount = 0
  15. local itemCount = 0
  16. local listSize = 0
  17. -- [name] = {[index] = {},[count] = ...}
  18. local winList = {}
  19. local nList = {}
  20. local searchStateFlag = false
  21. local searchPos = 1
  22. local winNormColor = {
  23. font = colors.black,
  24. bg = colors.lime
  25. }
  26.  
  27. local winFocusColor = {
  28. font = colors.gray,
  29. bg = colors.pink
  30. }
  31.  
  32. topBar.setBackgroundColor(colors.orange)
  33. topBar.setTextColor(colors.white)
  34. win.setBackgroundColor(winNormColor.bg)
  35. win.setTextColor(winNormColor.font)
  36.  
  37.  
  38. local function LDebug(str,col)
  39. term.setCursorPos(1,col)
  40. term.setTextColor(colors.orange)
  41. term.setBackgroundColor(colors.white)
  42. term.clearLine()
  43. term.write(str)
  44. end
  45.  
  46. local function renewTopBar()
  47. local str = string.format("%3d/%3d size:%d",pageIndex,pageCount,itemCount)
  48. topBar.clear()
  49. topBar.setCursorPos(1,1)
  50. topBar.write(str)
  51. end
  52.  
  53. local function renewWindow()
  54. win.clear()
  55. local s = 0
  56. local nPos = 1
  57. local str = ""
  58. for i,j in pairs(winList) do
  59. for o,k in pairs(j) do
  60. -- LDebug("o : "..o,18)
  61.  
  62. win.setCursorPos(1,nPos)
  63. if(nPos == inPageIndex) then
  64. win.setBackgroundColor(winFocusColor.bg)
  65. win.setTextColor(winFocusColor.font)
  66. win.clearLine()
  67. else
  68. win.setBackgroundColor(winNormColor.bg)
  69. win.setTextColor(winNormColor.font)
  70. end
  71. s = string.find(o,":")
  72. win.write(string.sub(o,s+1))
  73. if(k.count > 10000) then
  74. str = string.format("%3dK",(k.count-k.count%1000)/1000)
  75. else
  76. str = string.format("%4d",k.count)
  77. end
  78. end
  79. win.setCursorPos(mw - 4,nPos)
  80. win.write(str)
  81. -- LDebug(j.count,18)
  82. nPos = nPos + 1
  83. end
  84. win.setBackgroundColor(winNormColor.bg)
  85. win.setTextColor(winNormColor.font)
  86. end
  87. local function re_listCount()
  88. -- name ,table
  89. for i,j in pairs(nList) do
  90. j.count = 0
  91. for o,k in pairs(j.index) do
  92. j.count = j.count + tList[k].count
  93. end
  94. end
  95. end
  96. local function re_pageCount()
  97. itemCount = table.getn(nList)
  98. pageCount = (itemCount - itemCount % mh)/mh
  99. if(itemCount % mh ~= 0) then
  100. pageCount = pageCount +1
  101. end
  102. end
  103.  
  104. local function re_scan()
  105. tList = sc.list()
  106. itemCount = 0
  107. nList = {}
  108. -- name={index,count}
  109. for i,j in pairs(tList) do
  110. if(nList[j.name] == nil) then
  111. nList[j.name] = {}
  112. nList[j.name]["index"] = {}
  113. table.insert(nList[j.name]["index"],i)
  114. nList[j.name]["count"] = 0
  115.  
  116. itemCount = itemCount +1
  117. else
  118. table.insert(nList[j.name]["index"],i)
  119. end
  120. end
  121. re_listCount()
  122. pageCount = (itemCount - itemCount % mh)/mh
  123.  
  124. if(itemCount % mh ~= 0) then
  125. pageCount = pageCount +1
  126. end
  127.  
  128. -- for i,j in pairs(nList) do
  129. -- if(j[2] == nil ) then
  130. -- table.remove(nList,i)
  131. -- end
  132. -- end
  133. -- re_pageCount()
  134. end
  135.  
  136.  
  137. local function sub_scan()
  138.  
  139. end
  140.  
  141.  
  142. local function pageChange()
  143. winList = {}
  144. local tMod = itemCount % mh
  145. if (pageIndex == pageCount and tMod ~= 0) then
  146. listSize = tMod
  147. else listSize = mh end
  148. -- init pos
  149. local startPos = (pageIndex - 1) * mh
  150. local endPos = startPos + listSize
  151. local nPos = 1
  152. -- LDebug("listSize "..listSize.."itemCOunt "..itemCount, 19)
  153.  
  154. for i,j in pairs(nList) do
  155. if (nPos > startPos) then
  156. if(nPos > endPos) then
  157. break
  158. end
  159. -- winList[i] = j
  160. local d = {}
  161. d[i]=j
  162. table.insert(winList,d)
  163. end
  164. nPos = nPos + 1
  165. end
  166. -- LDebug("endPos "..pos,18)
  167. if(inPageIndex > listSize)then
  168. inPageIndex = listSize
  169. end
  170.  
  171. renewTopBar()
  172. renewWindow()
  173. end
  174.  
  175. local function List_move()
  176. local nPos = 1
  177. local index = 0
  178. for i,j in pairs(winList) do
  179. if(nPos == inPageIndex) then
  180. for o,k in pairs(j) do
  181. index = table.remove(nList[o].index)
  182. sc.pushItems(stoSide,index)
  183. if(table.getn(nList[o].index) == 0) then
  184. nList[o].index = nil
  185. end
  186. break
  187. end
  188. end
  189. nPos = nPos + 1
  190. end
  191.  
  192. -- table.remove(nList,pageIndex * mh + inPageIndex)
  193. re_scan()
  194. table.sort(nList)
  195. re_listCount()
  196. if(pageIndex >= pageCount) then
  197. pageIndex = pageCount
  198. if(inPageIndex >= itemCount % mh) then
  199. inPageIndex = itemCount % mh
  200. end
  201. end
  202. pageChange()
  203. end
  204.  
  205. local function init()
  206.  
  207. pageIndex = 1
  208. inPageIndex = 1
  209. re_scan()
  210. -- gen winList
  211. pageChange()
  212. end
  213.  
  214. local function preParse(cmdStr)
  215. local sPos,ePos
  216. local cmdArgsPack = {}
  217. while true do
  218. sPos,ePos = string.find(cmdStr, " ")
  219. if(sPos == nil) then
  220. table.insert(cmdArgsPack,cmdStr)
  221. break
  222. end
  223. table.insert(cmdArgsPack,string.sub(cmdStr,1,sPos-1))
  224. cmdStr = string.sub(cmdStr, ePos+1)
  225. end
  226. return cmdArgsPack
  227. end
  228.  
  229. local function paraseCMD(cmdStr)
  230. local tArgs = preParse(cmdStr)
  231. if(tArgs[1] == "s") then
  232. -- search
  233. if(searchStateFlag == false) then
  234. searchStateFlag = true
  235. searchPos = 0
  236. end
  237. local nPos = 1
  238. table.sort(nList)
  239.  
  240. for i,j in pairs(nList) do
  241. if(nPos > searchPos) then
  242. if( string.find(i,tArgs[2]) ~= nil) then
  243. -- find
  244. -- LDebug("str "..nPos , 19)
  245. inPageIndex = nPos % mh
  246. if(inPageIndex == 0 ) then
  247. inPageIndex = mh
  248. end
  249.  
  250. pageIndex = (nPos - nPos % mh) / mh + 1
  251. searchPos = nPos
  252. break
  253. end
  254. end
  255. nPos = nPos +1
  256. end
  257. if(nPos > itemCount) then
  258. term.setCursorPos(1,19)
  259. term.clearLine()
  260. term.write("not found")
  261. end
  262. pageChange()
  263. else
  264.  
  265. -- remove
  266. searchStateFlag = false
  267. List_move()
  268. term.setCursorPos(1,18)
  269. term.clearLine()
  270. -- LDebug("args "..tArgs[1].." "..tArgs,18)
  271. end
  272. end
  273.  
  274. local function mainloop()
  275. term.clear()
  276. init()
  277. while true do
  278. term.setCursorPos(1,18)
  279. term.setTextColour(colors.white)
  280. -- re_scan()
  281. -- pageChange()
  282. local cmd = io.read()
  283. -- LDebug("nowPos :"..(pageIndex-1)*mh + inPageIndex, 18)
  284. paraseCMD(cmd)
  285.  
  286. end
  287. end
  288. local keyMap = {
  289. ["end"]=(function ()
  290. if(inPageIndex < mh and inPageIndex < listSize) then
  291. inPageIndex = inPageIndex + 1
  292. -- LDebug("nowPos "..(pageIndex-1) * mh + inPageIndex,18)
  293. renewWindow()
  294. end
  295. end),
  296. ["home"]=(function ()
  297. if(inPageIndex > 1) then
  298. inPageIndex = inPageIndex - 1
  299. -- LDebug("nowPos "..(pageIndex-1) * mh + inPageIndex,18)
  300. renewWindow()
  301. end
  302. end),
  303. ["pageUp"]=(function ()
  304. if(pageIndex > 1) then
  305. inPageIndex = 1
  306. pageIndex = pageIndex - 1
  307. pageChange()
  308. end
  309. end),
  310. ["pageDown"]=(function ()
  311. if(pageIndex < pageCount) then
  312. inPageIndex = 1
  313. pageIndex = pageIndex + 1
  314. pageChange()
  315. end
  316. end),
  317. }
  318.  
  319. local function key_check()
  320. while true do
  321. local event, key, isHeld = os.pullEvent("key")
  322. local kvalue = keys.getName(key)
  323. if(keyMap[kvalue]~= nil) then
  324. keyMap[kvalue]()
  325. term.setTextColour(colors.white)
  326. term.setCursorPos(1,18)
  327. end
  328. end
  329. end
  330. parallel.waitForAny(mainloop,key_check)
  331.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement