This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
hybrid1969's profile picture

Re-read File And Display Results On Monitor

Started by hybrid1969, 18 November 2013 - 06:59 PM
hybrid1969 #1
Posted 18 November 2013 - 07:59 PM
im trying to read a file, print to screen and do this every few minutes.

also when the result has more lines than the screen has, the results slowly scroll p.

im thinking I should be using arrays but have no idea where to start to be honest.

so far I have -


local cPrint = function(text)
		local x2,y2 = term.getCursorPos()
		local x,y = term.getSize()
  term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
		print(text)
end
local mon = peripheral.wrap("left") -- get the monitor
mon.setTextScale(1.2) -- set the text scale
term.redirect(mon) -- redirect output to the monitor
repeat
local time = os.time()
time = textutils.formatTime(time, true)

local f = io.open("disk/listings.yml", "r")

if fs.exists("disk/listings.yml") == false then
  print("File not found!")
  return
end
--term.clear()
cPrint ("Auction Listings \n")
local lines = {}while true do		  
  local line = f:read()  
  if line == nil then			  
   print("\n updated: "..time)			
   break	  
  end	  
  
  local result = string.gsub(line, " ", "") -- remove line breaks  
  if (string.match(result,"type")) ~= nil then
   type = result
  end
  if (string.match(result,"amount")) ~= nil then
   amount = result
  end
  if (string.match(result,"seller")) ~= nil then
   seller = result
  end
  if (string.match(result,"price")) ~= nil then
   price = result
  end
  if (string.match(result,"time")) ~= nil then
   cPrint("Item "..type.." -- "..amount.." -- "..seller.." -- Sell "..price)
   cPrint("----------------------------------------------------------------------------------")
  end
  
  lines[#lines+1] = line
end

until os.pullEvent() ~= 'key'
term.restore()

any help would be appreciated
Bomb Bloke #2
Posted 19 November 2013 - 07:13 AM
Best not to try to overwrite "type". That's an in-built function, used to identify objects (eg, "type(paintutils)" returns "table") - even if you're not using that function directly, you never know when you might want to call something that needs it intact.

Your line-break remover appears to remove spaces?

string.match doesn't do whatever it is you want it to:

  if (string.match(result,"amount")) ~= nil then    -- "If the string 'amount' exists in 'result', then...
   amount = result                                  -- ... let the variable 'amount' equal the ENTIRE string in 'result'.
  end

Can't really comment on fixing that stuff without seeing at least an example of what'd be in your "listings.yml" file (speaking of which, it's probably best to see if that exists before you try to open it, and don't forget to close that file handle when you've reached the end of its text!). In the meantime I can at least recommend reviewing this page - you've probably already seen it, but it's worth reading again. All of it, not just the functions you think you need.

Anyway, in regards to your actual question: There's no need for any tables, at least as far as I can see. In your cPrint function you just have to have to throw in a line like this:

if y==y2 then sleep(2) end

It'll print stuff to the screen flat out until it reaches the bottom of the page, then slowly let the last remaining lines push the first lines out the top. When you DO reach the end of the file, throw in a sleep(180) or somesuch, clear the screen, move the cursor back up to the top, then repeat the process anew.

Things do get a bit more complex then that if you want to scroll both ways (eg have the text bounce up/down).