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

Panel, Terminal Redirection To Parts Of Screen

Started by McLeopold, 22 August 2013 - 06:03 PM
McLeopold #1
Posted 22 August 2013 - 08:03 PM
Panel

Terminal redirection to parts of the screen. Easilly create scrolling sidebars, status bars and other screen areas.
  • Create sidebar text that scrolls independently of other panels
  • Works seamlessly with write() and print() since it uses term.redirect()

You can see in the screen shot that the edit program is contained inside the new main panel and doesn't overwrite the sidebar text.

Install


pastebin get aibwRhj1 panel

http://pastebin.com/aibwRhj1

GitHub repo:

https://github.com/Forte40/panel

Usage

Load the API with


os.loadAPI("panel")

Create a new panel with specified left, top, width and height:


new_panel = panel.new(5, 5, 10, 10)
-- or --
new_panel = panel.new{x=5, y=5, w=10, h=10}
-- or with default values
new_panel = panel.new{x=1, w=20} -- y will be 1 and height will fill the screen

Update: Or use relative coordinates so you don't have to know the size of the screen. A negative number will use the width or height and count back.


-- panel to occupy 20 characters on the right
-- y and height are defaulted to 1 and height of screen
new_panel = panel.new{x=-20, w=20}

New panels should be activated. Activating switches from one panel to the other. write() and print() will display to the active panel.


new_panel:redirect() -- notice the colon operator
-- or --
panel.redirect(new_panel)

Panel redirects don't stack (unlike term.redirect). Restoring always returns you to the terminal or monitor panel you were on. Restore the normal input with:


term.restore()
-- or --
panel.restore()

This will work with monitors as well:

Spoiler

mon = peripheral.wrap("left")
term.redirect(mon)

sidebar = panel.new(1, 20, 20, 20)
sidebar:redirect()

-- remove panel redirect from monitor
panel.restore()

-- remove panel redirect from monitor
term.restore()

Here is the example script in the screen shot:

SpoilerUpdate: I changed the new function syntax slightly, so this code no longer matches the screenshot.

os.loadAPI("panel")

-- create area for info
sidebar = panel.new{x=-12, w=12}
sidebar:redirect()
for k in pairs(term) do
  print(k)
end

-- create new main area
main = panel.new{x=1, w=-13}
panel.redirect(main)
Zudo #2
Posted 23 August 2013 - 05:05 AM
I like this!
theoriginalbit #3
Posted 23 August 2013 - 05:11 AM
Nice… Good idea, fairly efficient code at a quick glance over, one of the better GUI type APIs I've seen in a while, good job! :)/>
jay5476 #4
Posted 23 August 2013 - 07:12 AM
this looks… awesome very good code and works great a lot of potential use with this
DarkEspeon #5
Posted 23 August 2013 - 11:21 AM
Wow… I never would have thought this was possible…. seems i am wrong though…. good job, not many words, still in shock that this is remotely possible in cc
theoriginalbit #6
Posted 23 August 2013 - 12:07 PM
I never would have thought this was possible…. -snip- still in shock that this is remotely possible in cc
Virtually anything is possible, just requires someone willing to code it…
Shaun #7
Posted 25 August 2013 - 06:27 AM
Virtually anything is possible […]
at zombocom.


>.> Loving this api, must use in future projects!
theoriginalbit #8
Posted 25 August 2013 - 06:32 AM
Virtually anything is possible […]
at zombocom.
I don't get it, what is that site?
McLeopold #9
Posted 27 August 2013 - 02:49 PM
A new version is on pastebin now. Panels will remember the text color and background color that was set for them.

Also, if I create a full screen panel, I've noticed that the edit program doesn't scroll the screen to the left or right when on a long line. What functionality do you suppose I missed?


os.loadAPI("panel")
main = panel.new(1,1,-1,-1)
main:redirect()
Grim Reaper #10
Posted 27 August 2013 - 03:23 PM
You probably missed the way that term.write will still write to the screen when the cursor is out of bounds. You'll need to still move the cursor when this happens, but just not write text to the screen.
McLeopold #11
Posted 27 August 2013 - 07:39 PM
You probably missed the way that term.write will still write to the screen when the cursor is out of bounds. You'll need to still move the cursor when this happens, but just not write text to the screen.

I'm actually calling the native write function, then the native getCusorPos function. I don't think this is the issue.
theoriginalbit #12
Posted 28 August 2013 - 01:08 AM
Also, if I create a full screen panel, I've noticed that the edit program doesn't scroll the screen to the left or right when on a long line. What functionality do you suppose I missed?
Without looking at your code again I'd assume you've missed a term.getSize override for the panel, and you'll probably find it doesn't scroll with smaller panels too. Since the scrolling in the edit program is dependant on the width of the screen, if you've missed term.getSize it'll think it has the whole screen, not just the sub-panel. It really seems like it'd be the only cause of the problem.
Mitchfizz05 #13
Posted 28 August 2013 - 02:59 AM
I could imagine this API would be rather useful when making windowed CC OS's.
McLeopold #14
Posted 28 August 2013 - 12:01 PM
Also, if I create a full screen panel, I've noticed that the edit program doesn't scroll the screen to the left or right when on a long line. What functionality do you suppose I missed?
Without looking at your code again I'd assume you've missed a term.getSize override for the panel, and you'll probably find it doesn't scroll with smaller panels too. Since the scrolling in the edit program is dependant on the width of the screen, if you've missed term.getSize it'll think it has the whole screen, not just the sub-panel. It really seems like it'd be the only cause of the problem.

I got it. My clearLine override was setting the cursor position back to 1. Now I remember the position and restore it.

I think the only other feature I may want is to scroll up the buffer.
theoriginalbit #15
Posted 28 August 2013 - 12:10 PM
I got it. My clearLine override was setting the cursor position back to 1. Now I remember the position and restore it.
Ahh that'd do it… haha…

I think the only other feature I may want is to scroll up the buffer.
As in adding term.scroll support? If so, then I definitely think you should, programs do use it from time to time.
Lyqyd #16
Posted 28 August 2013 - 01:30 PM
Wow… I never would have thought this was possible…. seems i am wrong though…. good job, not many words, still in shock that this is remotely possible in cc

If this put you into shock, be sure never to look at LyqydOS; the surprise and amazement would surely kill you. :P/>
theoriginalbit #17
Posted 28 August 2013 - 01:48 PM
If this put you into shock, be sure never to look at LyqydOS; the surprise and amazement would surely kill you. :P/>
Hahahaha! Well put! :P/>
McLeopold #18
Posted 28 August 2013 - 07:35 PM
As in adding term.scroll support? If so, then I definitely think you should, programs do use it from time to time.

I've already got term.scroll support. But it scrolls up and loses the text above. I'm thinking more like scrolling up the console to see previous messages and have been scrolled past, like [ctrl]+[pageup] in linux.