CoolisTheName007

Scroller

Apr 1st, 2013
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.79 KB | None | 0 0
  1. --By CoolisTheName007
  2.  
  3. local mon=peripheral.wrap'back'
  4. mon.setTextScale(1)
  5. local delay=1
  6. local textPath='text'
  7.  
  8. local function getText()
  9.     local f=fs.open(textPath,'r')
  10.     local text=f.readAll()
  11.     f.close()
  12.     return text
  13. end
  14.  
  15. local function write( sText)
  16.     local term=mon
  17.     local w,h = term.getSize()     
  18.     local x,y = term.getCursorPos()
  19.    
  20.     local nLinesPrinted = 0
  21.     local function newLine()
  22.         if y + 1 <= h then
  23.             term.setCursorPos(1, y + 1)
  24.         else
  25.             term.setCursorPos(1, h)
  26.             term.scroll(1)
  27.         end
  28.         x, y = term.getCursorPos()
  29.         nLinesPrinted = nLinesPrinted + 1
  30.     end
  31.    
  32.     -- Print the line with proper word wrapping
  33.     while string.len(sText) > 0 do
  34.         local whitespace = string.match( sText, "^[ \t]+" )
  35.         if whitespace then
  36.             -- Print whitespace
  37.             term.write( whitespace )
  38.             x,y = term.getCursorPos()
  39.             sText = string.sub( sText, string.len(whitespace) + 1 )
  40.         end
  41.        
  42.         local newline = string.match( sText, "^\n" )
  43.         if newline then
  44.             -- Print newlines
  45.             newLine()
  46.             sText = string.sub( sText, 2 )
  47.         end
  48.        
  49.         local text = string.match( sText, "^[^ \t\n]+" )
  50.         if text then
  51.             sText = string.sub( sText, string.len(text) + 1 )
  52.             if string.len(text) > w then
  53.                 -- Print a multiline word              
  54.                 while string.len( text ) > 0 do
  55.                     if x > w then
  56.                         newLine()
  57.                     end
  58.                     term.write( text )
  59.                     text = string.sub( text, (w-x) + 2 )
  60.                     x,y = term.getCursorPos()
  61.                 end
  62.             else
  63.                 -- Print a word normally
  64.                 if x + string.len(text) - 1 > w then
  65.                     newLine()
  66.                 end
  67.                 term.write( text )
  68.                 x,y = term.getCursorPos()
  69.             end
  70.         end
  71.     end
  72.    
  73.     return nLinesPrinted
  74. end
  75.  
  76. local function wrap(text, limit) --from Kingdaroo
  77.         local lines = {''}
  78.         for word, space in text:gmatch('(%S+)(%s*)') do
  79.                 local temp = lines[#lines] .. word .. space:gsub('\n','')
  80.                 if #temp > limit then
  81.                         table.insert(lines, '')
  82.                 end
  83.                 if space:find('\n') then
  84.                         lines[#lines] = lines[#lines] .. word
  85.                        
  86.                         space = space:gsub('\n', function()
  87.                                 table.insert(lines, '')
  88.                                 return ''
  89.                         end)
  90.                 else
  91.                         lines[#lines] = lines[#lines] .. word .. space
  92.                 end
  93.         end
  94.         return lines
  95. end
  96.  
  97. local function extract(t,s,l)
  98.     local c={}
  99.     for k=s,s+l-1 do
  100.         c[k-s+1]=t[k%(#t)+1]
  101.     end
  102.     return c
  103. end
  104.  
  105. local function print_screen(t)
  106.     mon.clear()
  107.     mon.setCursorPos(1,1)
  108.     local x,y=mon.getSize()
  109.    
  110.    
  111.     for i,line in ipairs(t) do
  112.         if (line:sub(1,1)=='#') then
  113.             local tc=colors[line:match'^#([^-#]+)']
  114.             mon.setTextColor(tc or colors.white)
  115.             local bc=colors[line:match'^#[^-#]+%-([^#]+)#']
  116.             mon.setBackgroundColor(bc or colors.black)
  117.             local l=line:match'^#[^#]+#'
  118.             line=line:sub(l:len()+1)
  119.         end
  120.         c=' '
  121.         if (line:sub(-2,-2)=='&') then
  122.             c=line:sub(-1,-1)
  123.             line=line:sub(1,-3)
  124.         end
  125.         line=line..string.rep(c,math.max(x-#line,0))
  126.         write(line..(i~=#t and '\n' or ''))
  127.     end
  128. end
  129.  
  130. local function main()
  131.     local x,y=mon.getSize()
  132.     local text
  133.     local c=-1
  134.     repeat
  135.        
  136.         local ok,r=pcall(getText)
  137.         if ok then text=r else error(r) end
  138.         local tText=wrap(text,x)
  139.         c=(c+1)%(#tText)
  140.         mon.setTextColor(colors.white)
  141.         mon.setBackgroundColor(colors.black)
  142.         local t=tText
  143.         for i=1,(c+1) do
  144.             local line=t[i]
  145.             if (line:sub(1,1)=='#') then
  146.                 local tc=colors[line:match'^#([^-#]+)']
  147.                 mon.setTextColor(tc or colors.white)
  148.                 local bc=colors[line:match'^#[^-#]+%-([^#]+)#']
  149.                 mon.setBackgroundColor(bc or colors.black)
  150.             end
  151.         end
  152.         print_screen(extract(tText,c,y))
  153.        
  154.         sleep(delay)
  155.     until false
  156. end
  157. parallel.waitForAny(main,function() shell.run'shell' end)
Advertisement
Add Comment
Please, Sign In to add comment