Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- page = {}
- local g_window_size = { w = 0, h = 0 }
- page.mPageIndex = 0
- page.mRowIndex = 0
- page.mPageCount = 0
- page.mItemCount = 0
- page.mNowMaxPageIndex = 0
- page.topBar = nil
- page.body = nil
- local g_entry_list = {}
- local winNormColor = {
- font = colors.black,
- bg = colors.lime
- }
- local winFocusColor = {
- font = colors.gray,
- bg = colors.pink
- }
- -- renewTopBar and Body
- local function flushTopBar()
- local str = string.format("%3d/%3d size:%d",page.mPageIndex,page.mPageCount,page.mItemCount)
- page.topBar.clear()
- page.topBar.setCursorPos(1,1)
- page.topBar.write(str)
- end
- local function flushBody()
- page.body.clear()
- local nPos = 1
- local startPos = (page.mPageIndex - 1) * g_window_size.h + 1
- local endPos = startPos + page.mNowMaxPageIndex - 1
- for index = startPos, endPos, 1 do
- page.body.setCursorPos(1,nPos)
- if(nPos == page.mRowIndex) then -- select backGround Color
- page.body.setBackgroundColor(winFocusColor.bg)
- page.body.setTextColor(winFocusColor.font)
- page.body.clearLine()
- else
- page.body.setBackgroundColor(winNormColor.bg)
- page.body.setTextColor(winNormColor.font)
- end
- page.body.write(g_entry_list[index])
- nPos = nPos + 1
- end
- page.body.setBackgroundColor(winNormColor.bg)
- page.body.setTextColor(winNormColor.font)
- end
- -- switch the page
- local function pageChange(offset)
- local targetPageIndex = page.mPageIndex + offset
- -- page.LDebug(page.mPageIndex,19)
- targetPageIndex = targetPageIndex > 0 and targetPageIndex or 1
- targetPageIndex = targetPageIndex <= page.mPageCount and targetPageIndex or page.mPageCount
- page.mPageIndex = targetPageIndex
- local remainder = page.mItemCount % g_window_size.h
- if (targetPageIndex == page.mPageCount and remainder ~= 0) then
- -- have remainder
- page.mNowMaxPageIndex = remainder
- else
- -- common page
- page.mNowMaxPageIndex = g_window_size.h
- end
- flushTopBar()
- flushBody()
- end
- --[[
- the key method for use
- ]]
- -- debug in window
- function page.LDebug(str,col)
- term.setCursorPos(1,col)
- term.setTextColor(colors.orange)
- term.setBackgroundColor(colors.white)
- term.clearLine()
- term.write(str)
- end
- -- init
- function page.init(list,width,height)
- g_window_size.w = width
- g_window_size.h = height - 3
- page.topBar = window.create(term.current(), 1, 1, g_window_size.w, 1)
- page.body = window.create(term.current(), 1, 2, g_window_size.w, g_window_size.h)
- page.topBar.setBackgroundColor(colors.orange)
- page.topBar.setTextColor(colors.white)
- page.body.setBackgroundColor(winNormColor.bg)
- page.body.setTextColor(winNormColor.font)
- page.mItemCount = table.getn(list)
- page.mPageCount = (page.mItemCount - 1 - (page.mItemCount - 1) % g_window_size.h) / g_window_size.h + 1
- page.mPageIndex = 1
- page.mRowIndex = 1
- g_entry_list = list
- term.clear()
- term.setTextColor(colors.white)
- pageChange(0)
- end
- -- get current select
- function page.getSelect()
- local index = (page.mPageIndex -1) * g_window_size.h + page.mRowIndex
- return index
- end
- local keyMap = {
- ["up"]=(function ()
- if(page.mRowIndex > 1) then
- page.mRowIndex = page.mRowIndex - 1
- -- LDebug("nowPos "..(pageIndex-1) * mh + page.mRowIndex,18)
- flushBody()
- end
- end),
- ["down"]=(function ()
- if(page.mRowIndex < g_window_size.h and page.mRowIndex < page.mNowMaxPageIndex) then
- page.mRowIndex = page.mRowIndex + 1
- -- LDebug("nowPos "..(pageIndex-1) * mh + page.mRowIndex,18)
- flushBody()
- end
- end),
- ["left"]=(function ()
- if(page.mPageIndex > 1) then
- page.mRowIndex = 1
- pageChange(-1)
- end
- end),
- ["right"]=(function ()
- if(page.mPageIndex < page.mPageCount) then
- page.mRowIndex = 1
- pageChange(1)
- end
- end),
- }
- -- control the page
- function page.control(cmd)
- local cb = keyMap[cmd]
- if cb ~= nil then
- keyMap[cmd]()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement