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

Previewon (Display Document On A Screen, With Scrolling!)

Started by BigTwisty, 08 September 2013 - 08:30 PM
BigTwisty #1
Posted 08 September 2013 - 10:30 PM
previewOn
by BigTwisty

Have you ever found some files just too big to comfortably view on a standard computer screen? This simple but handy program allows you to use large monitors to display those big files without having to use the computer to navigate. Simply click the top half of the monitor to scroll up a screen, or the bottom half to scroll down. I found this to work best on a 4x4 monitor: anything larger and the scrolling kind of goofs up. But hey, 4x4 is still WAY better than that tiny computer screen!

download: http://pastebin.com/Ue3MeGhX

pastebin get Ue3MeGhX previewOn

previewOn requires an advanced monitor because it relies on monitor_touch to scroll the text. Because the "preview" program, which this uses for display, does not support "monitor_resize" I had to end the program on such an event.

This was my first attempt at limited multi-threading using coroutine, and as such it is fairly simple. It basically loads the "preview" program in a coroutine, and then translates events to make "preview" do what we want it to.

Code:
Spoiler

args = {...}
if #args < 2 then
  print("Usage: readOn <direction> <filename>")
  return
end

if not fs.exists(shell.resolve(args[2])) then
  print("File does not exist: ",args[2])
  return
end

if peripheral.getType(args[1])~="monitor" then
  print("No monitor in direction: ",args[1])
  return
end

local reader = coroutine.create( function()
  shell.run("preview", args[2])
end )

function sendEvent(...)
  coroutine.resume(reader, ...)
end

local mon = peripheral.wrap(args[1])
if not mon.isColor() then
  print("previewOn requires an advanced monitor.")
  return
end

mon.setTextScale(0.5)
_,height = mon.getSize()
midscreen = height / 2

print("Reading ", args[2], " on ", args[1], " monitor.")
print("Click top half to scroll up, bottom to scroll down.")
print("Press q to quit")

term.redirect(mon)

sendEvent()

while coroutine.status(reader)~="dead" do
  e = { os.pullEventRaw() }
  if e[1] == "char" and e[2] == "q" or e[1] == "monitor_resize" then
	sendEvent("key", 29)
	sendEvent("key", 28)
  elseif e[1] == "monitor_touch" then
	dir = e[4] > midscreen and 1 or -1
	for i=1,height-1 do
	  sendEvent("mouse_scroll",dir, 1,1)
	end
	sendEvent("mouse_click", 1, 1, 1)
  else
	sendEvent(unpack(e))
  end
end

term.restore()

Edit: Any way I can change the title of the post? The program is called "previewOn" in accordance to standard Lua naming convention, not "Previewon" which just looks goofy!
Dave-ee Jones #2
Posted 09 September 2013 - 04:32 AM
Screenies?
BigTwisty #3
Posted 09 September 2013 - 08:16 AM
Screenies?
Kinda pointless. It's just running the "preview" program on another monitor, inserting its own controls.

"Screenie":
Spoiler
Mitchfizz05 #4
Posted 12 September 2013 - 03:25 AM
You should make it possible to specify a text scale in the args.
Good job though!
Dave-ee Jones #5
Posted 12 September 2013 - 03:57 AM
Screenies?
Kinda pointless. It's just running the "preview" program on another monitor, inserting its own controls.

"Screenie":
Spoiler

…You know, you could just type into the terminal: monitor <side> edit/preview <program>

It does the same thing…?
Or can you edit it with the code still visible in the computer, as well as the monitor?
BigTwisty #6
Posted 12 September 2013 - 07:13 AM
The problem with viewing text on a monitor is that to move around in it you have to go back into the computer and type or scroll. This app takes monitor touches and scrolls a whole screen at a time. It is much easier to just right click the monitor you are viewing on on the top or bottom.
Dave-ee Jones #7
Posted 13 September 2013 - 08:18 PM
The problem with viewing text on a monitor is that to move around in it you have to go back into the computer and type or scroll. This app takes monitor touches and scrolls a whole screen at a time. It is much easier to just right click the monitor you are viewing on on the top or bottom.

Ohhhhhh! That makes much more sense!

Sorry :/

But it sounds really good!
Bubba #8
Posted 13 September 2013 - 08:24 PM
Edit: Any way I can change the title of the post? The program is called "previewOn" in accordance to standard Lua naming convention, not "Previewon" which just looks goofy!

Unfortunately, the forum software messes with the capitalization. I don't believe that there is any way to change it. See this topic