Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --install blittle
- if not fs.exists( "stitch" ) then
- local h = http.get( 'https://pastebin.com/raw/CTrVdFK9' )
- if not h then
- error( "Failure to install stitch from 'CTrVdFK9'", 0 )
- end
- local data = h.readAll()
- h.close()
- local file = fs.open( "stitch", "w" )
- file.write( data )
- file.close()
- end
- --install statemachine
- if not fs.exists( "statemachine" ) then
- local h = http.get( "https://pastebin.com/raw/3qe1iUQc" )
- if not h then
- error( "Failure to install state machine from '3qe1iUQc'", 0 )
- end
- local data = h.readAll()
- h.close()
- local file = fs.open( "statemachine", "w" )
- file.write( data )
- file.close()
- end
- --load statemachine
- if not statemachine then
- os.loadAPI( "statemachine" )
- end
- --menu stuff
- function menu( title, ... )
- --edit the background color to make the GUI more unique
- term.setBackgroundColor( colors.black )
- local debug = false --#turn to true to see what it's thinking
- local tArgs = { ... }
- local pages = {[1]={}}
- for i = 1, #tArgs, 1 do
- if #pages[ #pages ] == 7 then
- pages[ #pages + 1 ] = {}
- end
- pages[ #pages ][ #pages[#pages] + 1 ] = tArgs[ i ]
- end
- local maxLen = 0
- for k, v in ipairs( tArgs ) do
- if #v > maxLen then maxLen = #v end
- end
- local maxx, maxy = term.getSize()
- if maxLen > maxx - 20 then
- error( 'largest string is too large', 2 )
- end
- local centerx = math.ceil( maxx/2 )
- local centery
- local yValue = {}
- local page = 1
- local selected = math.ceil( #pages[ page ] / 2 )
- local function render()
- local tbl = pages[ page ]
- centery = (maxy/2) - #tbl/2
- term.clear()
- term.setCursorPos( centerx - #title/2 + 1, 2 )
- term.write( title )
- for i = 1, #tbl do
- term.setCursorPos( centerx - (#tbl[i]/2), centery + i )
- yValue[ i ] = centery + i
- term.write( tbl[ i ] )
- end
- if pages[ page - 1 ] then
- term.setCursorPos( 3, centery + #tbl/2 + 1 )
- term.write( "previous" )
- end
- if pages[ page + 1 ] then
- term.setCursorPos( maxx - 5, centery + #tbl/2 + 1 )
- term.write( "next" )
- end
- local str = "(" .. page .. "/" .. #pages .. ")"
- term.setCursorPos( centerx - (#str/2), maxy )
- term.write( str )
- end
- while true do
- render()
- if debug then
- term.setCursorPos( 1, 1 )
- term.write( selected )
- term.setCursorPos( 1, 2 )
- term.write( page )
- end
- if term.isColor() then
- term.setTextColor( colors.yellow )
- end
- if selected == "previous" and not pages[ page - 1 ] then
- selected = math.ceil( #pages[ page ]/2 )
- elseif selected == "next" and not pages[ page + 1 ] then
- selected = math.ceil( #pages[ page ]/2 )
- end
- if type( selected ) == "number" then
- term.setCursorPos( centerx - (#pages[page][selected]/2) - 3, yValue[ selected ] )
- term.write( "[" )
- term.setCursorPos( centerx + (#pages[page][selected]/2) + 2, yValue[ selected ] )
- term.write( "]" )
- elseif selected == "previous" then
- term.setCursorPos( 1, centery + #pages[ page ]/2 + 1 )
- term.write( "[" )
- term.setCursorPos( 12, centery + #pages[ page ]/2 + 1 )
- term.write( "]" )
- elseif selected == "next" then
- term.setCursorPos( maxx - 7, centery + #pages[ page ]/2 + 1 )
- term.write( "[" )
- term.setCursorPos( maxx, centery + #pages[ page ]/2 + 1 )
- term.write( "]" )
- end
- if term.isColor() then
- term.setTextColor( colors.white )
- end
- local event, key = os.pullEvent( "key" )
- local old = selected
- if key == 200 and type( selected ) == "number" and pages[ page ][ selected - 1 ] then
- selected = selected - 1
- elseif key == 208 and type( selected ) == "number" and pages[ page ][ selected + 1 ] then
- selected = selected + 1
- elseif key == 28 then
- if type( selected ) == "number" then
- return pages[ page ][ selected ]
- elseif selected == "next" and pages[ page + 1 ] then
- page = page + 1
- elseif selected == "previous" and pages[ page - 1 ] then
- page = page - 1
- end
- elseif key == 203 then
- if selected == "next" then
- selected = math.ceil( #pages[ page ]/2 )
- elseif type( selected ) == "number" and pages[ page - 1 ] then
- selected = "previous"
- end
- elseif key == 205 then
- if selected == "previous" then
- selected = math.ceil( #pages[ page ]/2 )
- elseif type( selected ) == "number" and pages[ page + 1 ] then
- selected = "next"
- end
- end
- end
- end
- umenu = statemachine.unblock( menu )
- local tPrograms = {}
- local function cleanNils(t)
- local ans = {}
- for _,v in pairs(t) do
- ans[ #ans+1 ] = v
- end
- return ans
- end
- local function addProgram( window, program, monitorName )
- local prgm = {}
- prgm.func = statemachine.unblock( loadfile( program ) )
- prgm.win = window
- prgm.monName = monitorName
- tPrograms[ #tPrograms + 1 ] = prgm
- end
- local tMonitors = {}
- peripheral.find( "monitor", function( name, object )
- tMonitors[ #tMonitors + 1 ] = name
- end )
- local dofile = function( str )
- local fn = loadfile( str, getfenv() )
- return fn()
- end
- local function extendHorizantal( name1, name2 )
- local stitch = dofile( "stitch" )
- local list = {
- { name1, name2 }
- }
- stitch.setMonitors( list )
- return stitch
- end
- local function extendVertical( name1, name2 )
- local stitch = dofile( "stitch" )
- local list = {
- {name1},
- {name2}
- }
- stitch.setMonitors( list )
- return stitch
- end
- local curTerm = term.current()
- local focus = 1
- local shift = false
- local control = false
- local in_window_menu = false
- local to_merge = nil
- local input_events = {
- ["char"] = true,
- ["key"] = true,
- ["key_up"] = true,
- ["paste"] = true,
- ["terminate"] = true,
- }
- local function runMenu()
- local complete, option
- if tPrograms[focus].monName then
- complete, option = umenu( "Options", "Merge Vertically", "Merge Horizantally", "Split", "Exit" )
- else
- complete, option = umenu( "Options", "Split", "Exit" )
- end
- if complete then
- if option == "Split" then
- local w, h = tPrograms[focus].win.getSize()
- w = math.floor( w / 2 )
- h = math.floor( h / 2 )
- local win = window.create(tPrograms[focus].win, 1, 1, w, h )
- addProgram( win, "rom/programs/shell.lua" )
- local win = window.create(tPrograms[focus].win, 1, 1 + h, w, h )
- addProgram( win, "rom/programs/shell.lua" )
- local win = window.create( tPrograms[focus].win, 1 + w, 1, w, h )
- addProgram( win, "rom/programs/shell.lua" )
- local win = window.create( tPrograms[focus].win, 1 + w, 1 + h, w, h )
- addProgram( win, "rom/programs/shell.lua" )
- end
- if option == "Merge Vertically" and not to_merge then
- to_merge = tPrograms[ focus ]
- elseif option == "Merge Vertically" and to_merge then
- local win = extendVertical( to_merge.monName, tPrograms[ focus ].monName )
- addProgram( win, "rom/programs/shell.lua" )
- to_merge = nil
- end
- if option == "Merge Horizantally" and not to_merge then
- to_merge = tPrograms[ focus ]
- elseif option == "Merge Horizantally" and to_merge then
- local win = extendHorizantal( to_merge.monName, tPrograms[ focus].monName )
- addProgram( win, "rom/programs/shell.lua" )
- to_merge = nil
- end
- tPrograms[focus] = nil
- in_window_menu = false
- end
- end
- statemachine.setLoop( function()
- local event, key = statemachine.pullEvent()
- if event == "terminate" then
- return true
- end
- if key == keys.rightShift or key == keys.leftShift then
- if event == "key" then
- shift = true
- elseif event == "key_up" then
- shift = false
- end
- end
- if key == keys.rightCtrl or key == keys.leftCtrl then
- if event == "key" then
- control = true
- elseif event == "key_up" then
- control = false
- end
- end
- if event == "key" and key == keys.tab then
- if shift then
- --change process
- focus = focus + 1
- if not tPrograms[ focus ] then
- focus = 1
- end
- --print( "focus is now " .. focus )
- elseif control then
- -- open window control menu
- in_window_menu = true
- end
- end
- if #tPrograms == 0 then
- addProgram( curTerm, "rom/programs/shell.lua" )
- end
- if input_events[ event ] then
- term.redirect( tPrograms[ focus ].win )
- if in_window_menu then
- runMenu()
- else
- local complete = tPrograms[ focus ].func()
- if complete then
- tPrograms[ focus ].func = loadfile( "rom/programs/shell.lua" )
- end
- end
- else
- for i = 1, #tPrograms do
- term.redirect( tPrograms[i].win )
- local complete = tPrograms[i].func()
- if complete then
- tPrograms[ i ].func = loadfile( "rom/programs/shell.lua" )
- end
- end
- end
- tPrograms = cleanNils( tPrograms )
- if not tPrograms[ focus ] then
- focus = 1
- end
- term.redirect( curTerm )
- end)
- addProgram( curTerm, "rom/programs/shell.lua" )
- peripheral.find( "monitor", function( k, v )
- v.clear()
- v.setCursorPos( 1, 1 )
- addProgram( v, "rom/programs/shell.lua", k )
- end )
- statemachine.run()
Advertisement
Add Comment
Please, Sign In to add comment