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

how to scroll with scroll wheel in terminal

Started by Duster, 30 March 2016 - 08:15 PM
Duster #1
Posted 30 March 2016 - 10:15 PM
i want to make it so i can scroll up in a terminal while my AI program is running - to see past results - so is there an easy way to do so
Bomb Bloke #2
Posted 30 March 2016 - 11:19 PM
"Easy" is such a relative term.

You'll need to store the old text somewhere (eg a table), then rig up a system for printing different sets of table entries as you work the scroll bar. Things get more complex if the AI is outputting multiple lines per string, and more complex again if you want to be able to type back to it as well.
Duster #3
Posted 31 March 2016 - 10:48 PM
cant i just use term.scroll(a)
a = os.pullevent("mouse_Scroll")
Duster #4
Posted 31 March 2016 - 11:37 PM
"Easy" is such a relative term.

You'll need to store the old text somewhere (eg a table), then rig up a system for printing different sets of table entries as you work the scroll bar. Things get more complex if the AI is outputting multiple lines per string, and more complex again if you want to be able to type back to it as well.

well is there a way to rig os.pullevent("mouse_Scroll") to term.scroll so that your scroll wheel will scroll the text in screen?
valithor #5
Posted 31 March 2016 - 11:38 PM
cant i just use term.scroll(a)
a = os.pullevent("mouse_Scroll")

All term.scroll does is move all the text vertically. It would leave blank spaces at either the top or bottom depending on which direction you scrolled.

As Bomb said, you would need to store what each line had in a table, and display certain parts of the table at a time. Come to think of it, something similar actually happens in the edit program if you want to go look at that as an example. In that program it loads each line of a file to a table, and at the bottom of the script it has variables that keep track of what set of lines is being displayed from that table.
Edited on 31 March 2016 - 09:39 PM
Duster #6
Posted 31 March 2016 - 11:40 PM
soooooo no easy way. i would have to add tons more to the program
cant i just use term.scroll(a)
a = os.pullevent("mouse_Scroll")

All term.scroll does is move all the text vertically. It would leave blank spaces at either the top or bottom depending on which direction you scrolled.

As Bomb said, you would need to store what each line had in a table, and display certain parts of the table at a time. Come to think of it, something similar actually happens in the edit program if you want to go look at that as an example. In that program it loads each line of a file to a table, and at the bottom of the script it has variables that keep track of what set of lines is being displayed from that table.
valithor #7
Posted 31 March 2016 - 11:44 PM
soooooo no easy way. i would have to add tons more to the program
cant i just use term.scroll(a)
a = os.pullevent("mouse_Scroll")

All term.scroll does is move all the text vertically. It would leave blank spaces at either the top or bottom depending on which direction you scrolled.

As Bomb said, you would need to store what each line had in a table, and display certain parts of the table at a time. Come to think of it, something similar actually happens in the edit program if you want to go look at that as an example. In that program it loads each line of a file to a table, and at the bottom of the script it has variables that keep track of what set of lines is being displayed from that table.

It could be done in just a few lines if you know what you are doing, and depending on what exactly your AI is doing. For example, if your AI only writes to the screen, and there is no user input it is extremely easy, and can probably be done in 10-15 lines at max. If it requires user input it is just a few more.

If you want help with the code, either try to do what we are suggesting and post any where you get stuck, or provide the code to your AI so we can give more program specific help.
Duster #8
Posted 31 March 2016 - 11:47 PM
ok so i have this program
how would i rig it up to scroll

local methods = peripheral.getMethods("back")
for k, v in pairs(methods) do
  print(k .. " : " .. v)
end
while true do
  term.scroll()
end
valithor #9
Posted 31 March 2016 - 11:56 PM
-snip



local methods = peripheral.getMethods("back")
local scrollBuffer = {}
local _,maxY = term.getSize() --# getting max screen size, so we only print to the screen

for k, v in pairs(methods) do
  print(k .. " : " .. v)
  table.insert(scrollBuffer,k.." : ".. v) --# storing all of the things printed to the screen into a table
end

local y = 0

while true do
  local _, direction = os.pullEvent("mouse_scroll") --# listening for mouse_scroll events
  if direction == 1 then --# direction 1 is when you scroll the wheel one way
	if  y < #scrollBuffer-maxY then --# don't want to be able to scroll outside of the range of the table
	  y=y+1
	end
	term.clear() --# clearing screen so we can print the new scrolled version
	term.setCursorPos(1,1) --# setting to 1,1
	for i = 1, maxY-1 do --# looping through the screen coords
	  if not scrollBuffer[i+y] then break end --# making sure the table location isnt nil
	  print(scrollBuffer[i+y]) --# printing the table entry, i+y is the table entry
	end
	term.write(scrollBuffer[i+y])
  elseif direction == -1 then --# -1 is the only direction
	if y~=0 then --# not going to explain anymore, as it all repeats from here
	  y=y-1
	end
	term.clear()
	term.setCursorPos(1,1)
	for i = 1, maxY-1 do
	  if not scrollBuffer[i+y] then break end
	  print(scrollBuffer[i+y])
	end
	term.write(scrollBuffer[i+y])
  end
end

That is untested, but I think it should work. It might look long, but half of it is literally copy and pasted with a very slight change.
Edited on 31 March 2016 - 10:01 PM
Duster #10
Posted 31 March 2016 - 11:58 PM
could you pastebin it
-snip



local methods = peripheral.getMethods("back")
local scrollBuffer = {}
local _,maxY = term.getSize()

for k, v in pairs(methods) do
  print(k .. " : " .. v)
  table.insert(scrollBuffer,k.." : ".. v)
end

local y = 0

while true do
  local _, direction = os.pullEvent("mouse_scroll")
  if direction == 1 then
	if  y < #scrollBuffer then
	  y=y+1
	end
	term.clear()
	term.setCursorPos(1,1)
	for i = 1, maxY-1 do
	  if not scrollBuffer[i+y] then break end
	  print(scrollBuffer[i+y])
	end
	term.write(scrollBuffer[i+y])
  elseif direction == -1 then
	if y~=0 then
	  y=y-1
	end
	term.clear()
	term.setCursorPos(1,1)
	for i = 1, maxY-1 do
	  if not scrollBuffer[i+y] then break end
	  print(scrollBuffer[i+y])
	end
	term.write(scrollBuffer[i+y])
  end
end

That is untested, but I think it should work. It might look long, but half of it is literally copy and pasted with a very slight change.
valithor #11
Posted 01 April 2016 - 12:05 AM
Since I was posting it to pastebin decided to test it. For anyone looking at this topic in the future here is the fixed code:

local methods = peripheral.getMethods("back")
local scrollBuffer = {}
local _,maxY = term.getSize() --# getting max screen size, so we only print to the screen

for k, v in pairs(methods) do
  print(k .. " : " .. v)
  table.insert(scrollBuffer,k.." : ".. v) --# storing all of the things printed to the screen into a table
end

local y = #scrollBuffer

while true do
  local _, direction = os.pullEvent("mouse_scroll") --# listening for mouse_scroll events
  if direction == 1 then --# direction 1 is when you scroll the wheel one way
		if  y < #scrollBuffer-maxY then --# don't want to be able to scroll outside of the range of the table
		  y=y+1
		end
		term.clear() --# clearing screen so we can print the new scrolled version
		term.setCursorPos(1,1) --# setting to 1,1
		for i = 1, maxY-1 do --# looping through the screen coords
		  if not scrollBuffer[i+y] then break end --# making sure the table location isnt nil
		  print(scrollBuffer[i+y]) --# printing the table entry, i+y is the table entry
		end
		term.write(scrollBuffer[maxY+y])
  elseif direction == -1 then --# -1 is the only direction
		if y~=0 then --# not going to explain anymore, as it all repeats from here
		  y=y-1
		end
		term.clear()
		term.setCursorPos(1,1)
		for i = 1, maxY-1 do
		  if not scrollBuffer[i+y] then break end
		  print(scrollBuffer[i+y])
		end
		term.write(scrollBuffer[maxY+y])
  end
end

pastebin link: http://pastebin.com/Ngq5R1YY

Edit:

Noticed I posted the version I used to test, which had removed the peripheral call due to me testing in a emulator. The code is now fixed, but the pastebin still has the testing version. It is really easy to fix what I added for testing, so I am not updating the pastebin.
Edited on 31 March 2016 - 10:12 PM
Duster #12
Posted 02 April 2016 - 01:00 AM
Since I was posting it to pastebin decided to test it. For anyone looking at this topic in the future here is the fixed code:

local methods = peripheral.getMethods("back")
local scrollBuffer = {}
local _,maxY = term.getSize() --# getting max screen size, so we only print to the screen

for k, v in pairs(methods) do
  print(k .. " : " .. v)
  table.insert(scrollBuffer,k.." : ".. v) --# storing all of the things printed to the screen into a table
end

local y = #scrollBuffer

while true do
  local _, direction = os.pullEvent("mouse_scroll") --# listening for mouse_scroll events
  if direction == 1 then --# direction 1 is when you scroll the wheel one way
		if  y < #scrollBuffer-maxY then --# don't want to be able to scroll outside of the range of the table
		  y=y+1
		end
		term.clear() --# clearing screen so we can print the new scrolled version
		term.setCursorPos(1,1) --# setting to 1,1
		for i = 1, maxY-1 do --# looping through the screen coords
		  if not scrollBuffer[i+y] then break end --# making sure the table location isnt nil
		  print(scrollBuffer[i+y]) --# printing the table entry, i+y is the table entry
		end
		term.write(scrollBuffer[maxY+y])
  elseif direction == -1 then --# -1 is the only direction
		if y~=0 then --# not going to explain anymore, as it all repeats from here
		  y=y-1
		end
		term.clear()
		term.setCursorPos(1,1)
		for i = 1, maxY-1 do
		  if not scrollBuffer[i+y] then break end
		  print(scrollBuffer[i+y])
		end
		term.write(scrollBuffer[maxY+y])
  end
end

pastebin link: http://pastebin.com/Ngq5R1YY

Edit:

Noticed I posted the version I used to test, which had removed the peripheral call due to me testing in a emulator. The code is now fixed, but the pastebin still has the testing version. It is really easy to fix what I added for testing, so I am not updating the pastebin.
why does this not work on regular computers?
Anavrins #13
Posted 02 April 2016 - 01:08 AM
why does this not work on regular computers?
Normal computer aren't compatible with colors and mouse, that include mouse scrolling.