This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Password needed for Edit?
Started by Grey_M62, 12 July 2012 - 07:09 AMPosted 12 July 2012 - 09:09 AM
Could someone give me a simple way to make the 'edit' command password protected? I can't figure out if there is a way or not.
Posted 12 July 2012 - 10:10 AM
edit the edit file in the server files :9
Posted 12 July 2012 - 11:14 AM
And if you don't have access to the server files do "edit edit" and save now when you use the edit you'll get an error :)/>/> (but praie that the "greifer/hacker/troller" does not know how to do the edit from the LUA thingy)
Hope it helped!
Hope it helped!
Posted 12 July 2012 - 06:16 PM
And if you don't have access to the server files do "edit edit" and save now when you use the edit you'll get an error :)/>/> (but praie that the "greifer/hacker/troller" does not know how to do the edit from the LUA thingy)
Hope it helped!
One would then just use /rom/programs/edit.
Posted 12 July 2012 - 09:03 PM
I'm confused… All i want is to make 'edit' password locked. So basically, i am just asking for a lua code that will edit the argument, if that makes any sense. kind of like for shutdown and reboot, there is os.shutdown() and os.reboot().
Posted 12 July 2012 - 10:28 PM
Well, if you want to put a password lock on the edit program from when you call it you could do something like this:
Here is the original edit code:
And here is some code that will edit the arguments required, the newest of which is a password:
Hope I helped! :)/>/>
Here is the original edit code:
Spoiler
local tArgs = { ... }
if #tArgs == 0 then
print( "Usage: edit <path>" )
return
end
local sPath = shell.resolve( tArgs[1] )
local bReadOnly = fs.isReadOnly( sPath )
if fs.exists( sPath ) and fs.isDir( sPath ) then
print( "Cannot edit a directory" )
return
end
local w,h = term.getSize()
local x,y = 1,1
local scrollX, scrollY = 0,0
local bMenu = false
local nMenuItem = 1
local sStatus = ""
local tLines = {}
local function load()
tLines = {}
if fs.exists( sPath ) then
local file = io.open( sPath, "r" )
local sLine = file:read()
while sLine do
table.insert( tLines, sLine )
sLine = file:read()
end
file:close()
else
table.insert( tLines, "" )
end
end
local function save()
local file = io.open( sPath, "w" )
if file then
for n, sLine in ipairs( tLines ) do
file:write( sLine .. "n" )
end
file:close()
return true
end
return false
end
local function redrawText()
for y=1,h-1 do
term.setCursorPos( 1 - scrollX, y )
term.clearLine()
local sLine = tLines[ y + scrollY ]
if sLine ~= nil then
term.write( sLine )
end
end
term.setCursorPos( x - scrollX, y - scrollY )
end
local function redrawLine()
local sLine = tLines[y]
term.setCursorPos( 1 - scrollX, y - scrollY )
term.clearLine()
term.write( sLine )
term.setCursorPos( x - scrollX, y - scrollY )
end
local tMenuItems = { "Save", "Exit" }
local function redrawMenu()
term.setCursorPos( 1, h )
term.clearLine()
if not bMenu then
term.write( sStatus )
else
for n,sItem in ipairs( tMenuItems ) do
if n == nMenuItem then
term.write( "["..sItem.."]" )
else
term.write( " "..sItem.." " )
end
end
end
term.setCursorPos( x - scrollX, y - scrollY )
end
local function setStatus( _sText )
sStatus = _sText
redrawMenu()
end
local function setCursor( x, y )
local screenX = x - scrollX
local screenY = y - scrollY
local bRedraw = false
if screenX < 1 then
scrollX = x - 1
screenX = 1
bRedraw = true
elseif screenX > w then
scrollX = x - w
screenX = w
bRedraw = true
end
if screenY < 1 then
scrollY = y - 1
screenY = 1
bRedraw = true
elseif screenY > h-1 then
scrollY = y - (h-1)
screenY = h-1
bRedraw = true
end
if bRedraw then
redrawText()
end
term.setCursorPos( screenX, screenY )
end
load()
term.clear()
term.setCursorPos(x,y)
term.setCursorBlink( true )
redrawText()
setStatus( "Press Ctrl to access menu" )
local bRunning = true
local tMenuFunctions = {
["Exit"] = function()
bRunning = false
end,
["Save"] = function()
if save() then
setStatus( "Saved to "..sPath )
else
setStatus( "Access denied" )
end
end,
}
local function doMenuItem( _n )
local fnMenu = tMenuFunctions[ tMenuItems[_n] ]
if fnMenu then
fnMenu()
end
bMenu = false
term.setCursorBlink( true )
redrawMenu()
end
while bRunning do
local sEvent, param = os.pullEvent()
if sEvent == "key" then
if param == 200 then
-- Up
if not bMenu then
-- Move cursor up
if y > 1 then
y = y - 1
x = math.min( x, string.len( tLines[y] ) + 1 )
setCursor( x, y )
end
end
elseif param == 208 then
-- Down
if not bMenu then
-- Move cursor down
if y < #tLines then
y = y + 1
x = math.min( x, string.len( tLines[y] ) + 1 )
setCursor( x, y )
end
end
elseif param == 203 then
-- Left
if not bMenu then
-- Move cursor left
if x > 1 then
x = x - 1
setCursor( x, y )
end
else
-- Move menu left
nMenuItem = nMenuItem - 1
if nMenuItem < 1 then
nMenuItem = #tMenuItems
end
redrawMenu()
end
elseif param == 205 then
-- Right
if not bMenu then
-- Move cursor right
if x < string.len( tLines[y] ) + 1 then
x = x + 1
setCursor( x, y )
end
else
-- Move menu right
nMenuItem = nMenuItem + 1
if nMenuItem > #tMenuItems then
nMenuItem = 1
end
redrawMenu()
end
elseif param == 14 then
-- Backspace
if not bMenu then
if x > 1 then
-- Remove character
local sLine = tLines[y]
tLines[y] = string.sub(sLine,1,x-2) .. string.sub(sLine,x)
redrawLine()
x = x - 1
setCursor( x, y )
elseif y > 1 then
-- Remove newline
local sPrevLen = string.len( tLines[y-1] )
tLines[y-1] = tLines[y-1] .. tLines[y]
table.remove( tLines, y )
redrawText()
x = sPrevLen + 1
y = y - 1
setCursor( x, y )
end
end
elseif param == 28 then
-- Enter
if not bMenu then
-- Newline
local sLine = tLines[y]
tLines[y] = string.sub(sLine,1,x-1)
table.insert( tLines, y+1, string.sub(sLine,x) )
redrawText()
x = 1
y = y + 1
setCursor( x, y )
else
-- Menu selection
doMenuItem( nMenuItem )
end
elseif param == 29 then
-- Menu toggle
bMenu = not bMenu
if bMenu then
term.setCursorBlink( false )
nMenuItem = 1
else
term.setCursorBlink( true )
end
redrawMenu()
end
elseif sEvent == "char" then
if not bMenu then
-- Input text
local sLine = tLines[y]
tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x)
redrawLine()
x = x + string.len( param )
setCursor( x, y )
else
-- Select menu items
for n,sMenuItem in ipairs( tMenuItems ) do
if string.lower(string.sub(sMenuItem,1,1)) == string.lower(param) then
doMenuItem( n )
break
end
end
end
end
end
term.clear()
term.setCursorBlink( false )
term.setCursorPos( 1, 1 )
And here is some code that will edit the arguments required, the newest of which is a password:
Spoiler
local sPassword = "password";
local tArgs = { ... }
if #tArgs == 0 then
print( "Usage: edit <path>" )
return
end
local sPath = shell.resolve( tArgs[1] )
local bReadOnly = fs.isReadOnly( sPath )
if fs.exists( sPath ) and fs.isDir( sPath ) then
print( "Cannot edit a directory" )
return
end
if tArgs[2] == sPassword then
local w,h = term.getSize()
local x,y = 1,1
local scrollX, scrollY = 0,0
local bMenu = false
local nMenuItem = 1
local sStatus = ""
local tLines = {}
local function load()
tLines = {}
if fs.exists( sPath ) then
local file = io.open( sPath, "r" )
local sLine = file:read()
while sLine do
table.insert( tLines, sLine )
sLine = file:read()
end
file:close()
else
table.insert( tLines, "" )
end
end
local function save()
local file = io.open( sPath, "w" )
if file then
for n, sLine in ipairs( tLines ) do
file:write( sLine .. "n" )
end
file:close()
return true
end
return false
end
local function redrawText()
for y=1,h-1 do
term.setCursorPos( 1 - scrollX, y )
term.clearLine()
local sLine = tLines[ y + scrollY ]
if sLine ~= nil then
term.write( sLine )
end
end
term.setCursorPos( x - scrollX, y - scrollY )
end
local function redrawLine()
local sLine = tLines[y]
term.setCursorPos( 1 - scrollX, y - scrollY )
term.clearLine()
term.write( sLine )
term.setCursorPos( x - scrollX, y - scrollY )
end
local tMenuItems = { "Save", "Exit" }
local function redrawMenu()
term.setCursorPos( 1, h )
term.clearLine()
if not bMenu then
term.write( sStatus )
else
for n,sItem in ipairs( tMenuItems ) do
if n == nMenuItem then
term.write( "["..sItem.."]" )
else
term.write( " "..sItem.." " )
end
end
end
term.setCursorPos( x - scrollX, y - scrollY )
end
local function setStatus( _sText )
sStatus = _sText
redrawMenu()
end
local function setCursor( x, y )
local screenX = x - scrollX
local screenY = y - scrollY
local bRedraw = false
if screenX < 1 then
scrollX = x - 1
screenX = 1
bRedraw = true
elseif screenX > w then
scrollX = x - w
screenX = w
bRedraw = true
end
if screenY < 1 then
scrollY = y - 1
screenY = 1
bRedraw = true
elseif screenY > h-1 then
scrollY = y - (h-1)
screenY = h-1
bRedraw = true
end
if bRedraw then
redrawText()
end
term.setCursorPos( screenX, screenY )
end
load()
term.clear()
term.setCursorPos(x,y)
term.setCursorBlink( true )
redrawText()
setStatus( "Press Ctrl to access menu" )
local bRunning = true
local tMenuFunctions = {
["Exit"] = function()
bRunning = false
end,
["Save"] = function()
if save() then
setStatus( "Saved to "..sPath )
else
setStatus( "Access denied" )
end
end,
}
local function doMenuItem( _n )
local fnMenu = tMenuFunctions[ tMenuItems[_n] ]
if fnMenu then
fnMenu()
end
bMenu = false
term.setCursorBlink( true )
redrawMenu()
end
while bRunning do
local sEvent, param = os.pullEvent()
if sEvent == "key" then
if param == 200 then
-- Up
if not bMenu then
-- Move cursor up
if y > 1 then
y = y - 1
x = math.min( x, string.len( tLines[y] ) + 1 )
setCursor( x, y )
end
end
elseif param == 208 then
-- Down
if not bMenu then
-- Move cursor down
if y < #tLines then
y = y + 1
x = math.min( x, string.len( tLines[y] ) + 1 )
setCursor( x, y )
end
end
elseif param == 203 then
-- Left
if not bMenu then
-- Move cursor left
if x > 1 then
x = x - 1
setCursor( x, y )
end
else
-- Move menu left
nMenuItem = nMenuItem - 1
if nMenuItem < 1 then
nMenuItem = #tMenuItems
end
redrawMenu()
end
elseif param == 205 then
-- Right
if not bMenu then
-- Move cursor right
if x < string.len( tLines[y] ) + 1 then
x = x + 1
setCursor( x, y )
end
else
-- Move menu right
nMenuItem = nMenuItem + 1
if nMenuItem > #tMenuItems then
nMenuItem = 1
end
redrawMenu()
end
elseif param == 14 then
-- Backspace
if not bMenu then
if x > 1 then
-- Remove character
local sLine = tLines[y]
tLines[y] = string.sub(sLine,1,x-2) .. string.sub(sLine,x)
redrawLine()
x = x - 1
setCursor( x, y )
elseif y > 1 then
-- Remove newline
local sPrevLen = string.len( tLines[y-1] )
tLines[y-1] = tLines[y-1] .. tLines[y]
table.remove( tLines, y )
redrawText()
x = sPrevLen + 1
y = y - 1
setCursor( x, y )
end
end
elseif param == 28 then
-- Enter
if not bMenu then
-- Newline
local sLine = tLines[y]
tLines[y] = string.sub(sLine,1,x-1)
table.insert( tLines, y+1, string.sub(sLine,x) )
redrawText()
x = 1
y = y + 1
setCursor( x, y )
else
-- Menu selection
doMenuItem( nMenuItem )
end
elseif param == 29 then
-- Menu toggle
bMenu = not bMenu
if bMenu then
term.setCursorBlink( false )
nMenuItem = 1
else
term.setCursorBlink( true )
end
redrawMenu()
end
elseif sEvent == "char" then
if not bMenu then
-- Input text
local sLine = tLines[y]
tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x)
redrawLine()
x = x + string.len( param )
setCursor( x, y )
else
-- Select menu items
for n,sMenuItem in ipairs( tMenuItems ) do
if string.lower(string.sub(sMenuItem,1,1)) == string.lower(param) then
doMenuItem( n )
break
end
end
end
end
end
term.clear()
term.setCursorBlink( false )
term.setCursorPos( 1, 1 )
else
print( "Invalid password" );
return
end
Hope I helped! :)/>/>
Posted 12 July 2012 - 10:40 PM
a bit long, but it works. I was actually thinking that you type 'edit <path>' and then it asks for a password, rather than typing 'edit <path> <password>'.Well, if you want to put a password lock on the edit program from when you call it you could do something like this:
Here is the original edit code:Spoiler
local tArgs = { ... } if #tArgs == 0 then print( "Usage: edit <path>" ) return end local sPath = shell.resolve( tArgs[1] ) local bReadOnly = fs.isReadOnly( sPath ) if fs.exists( sPath ) and fs.isDir( sPath ) then print( "Cannot edit a directory" ) return end local w,h = term.getSize() local x,y = 1,1 local scrollX, scrollY = 0,0 local bMenu = false local nMenuItem = 1 local sStatus = "" local tLines = {} local function load() tLines = {} if fs.exists( sPath ) then local file = io.open( sPath, "r" ) local sLine = file:read() while sLine do table.insert( tLines, sLine ) sLine = file:read() end file:close() else table.insert( tLines, "" ) end end local function save() local file = io.open( sPath, "w" ) if file then for n, sLine in ipairs( tLines ) do file:write( sLine .. "n" ) end file:close() return true end return false end local function redrawText() for y=1,h-1 do term.setCursorPos( 1 - scrollX, y ) term.clearLine() local sLine = tLines[ y + scrollY ] if sLine ~= nil then term.write( sLine ) end end term.setCursorPos( x - scrollX, y - scrollY ) end local function redrawLine() local sLine = tLines[y] term.setCursorPos( 1 - scrollX, y - scrollY ) term.clearLine() term.write( sLine ) term.setCursorPos( x - scrollX, y - scrollY ) end local tMenuItems = { "Save", "Exit" } local function redrawMenu() term.setCursorPos( 1, h ) term.clearLine() if not bMenu then term.write( sStatus ) else for n,sItem in ipairs( tMenuItems ) do if n == nMenuItem then term.write( "["..sItem.."]" ) else term.write( " "..sItem.." " ) end end end term.setCursorPos( x - scrollX, y - scrollY ) end local function setStatus( _sText ) sStatus = _sText redrawMenu() end local function setCursor( x, y ) local screenX = x - scrollX local screenY = y - scrollY local bRedraw = false if screenX < 1 then scrollX = x - 1 screenX = 1 bRedraw = true elseif screenX > w then scrollX = x - w screenX = w bRedraw = true end if screenY < 1 then scrollY = y - 1 screenY = 1 bRedraw = true elseif screenY > h-1 then scrollY = y - (h-1) screenY = h-1 bRedraw = true end if bRedraw then redrawText() end term.setCursorPos( screenX, screenY ) end load() term.clear() term.setCursorPos(x,y) term.setCursorBlink( true ) redrawText() setStatus( "Press Ctrl to access menu" ) local bRunning = true local tMenuFunctions = { ["Exit"] = function() bRunning = false end, ["Save"] = function() if save() then setStatus( "Saved to "..sPath ) else setStatus( "Access denied" ) end end, } local function doMenuItem( _n ) local fnMenu = tMenuFunctions[ tMenuItems[_n] ] if fnMenu then fnMenu() end bMenu = false term.setCursorBlink( true ) redrawMenu() end while bRunning do local sEvent, param = os.pullEvent() if sEvent == "key" then if param == 200 then -- Up if not bMenu then -- Move cursor up if y > 1 then y = y - 1 x = math.min( x, string.len( tLines[y] ) + 1 ) setCursor( x, y ) end end elseif param == 208 then -- Down if not bMenu then -- Move cursor down if y < #tLines then y = y + 1 x = math.min( x, string.len( tLines[y] ) + 1 ) setCursor( x, y ) end end elseif param == 203 then -- Left if not bMenu then -- Move cursor left if x > 1 then x = x - 1 setCursor( x, y ) end else -- Move menu left nMenuItem = nMenuItem - 1 if nMenuItem < 1 then nMenuItem = #tMenuItems end redrawMenu() end elseif param == 205 then -- Right if not bMenu then -- Move cursor right if x < string.len( tLines[y] ) + 1 then x = x + 1 setCursor( x, y ) end else -- Move menu right nMenuItem = nMenuItem + 1 if nMenuItem > #tMenuItems then nMenuItem = 1 end redrawMenu() end elseif param == 14 then -- Backspace if not bMenu then if x > 1 then -- Remove character local sLine = tLines[y] tLines[y] = string.sub(sLine,1,x-2) .. string.sub(sLine,x) redrawLine() x = x - 1 setCursor( x, y ) elseif y > 1 then -- Remove newline local sPrevLen = string.len( tLines[y-1] ) tLines[y-1] = tLines[y-1] .. tLines[y] table.remove( tLines, y ) redrawText() x = sPrevLen + 1 y = y - 1 setCursor( x, y ) end end elseif param == 28 then -- Enter if not bMenu then -- Newline local sLine = tLines[y] tLines[y] = string.sub(sLine,1,x-1) table.insert( tLines, y+1, string.sub(sLine,x) ) redrawText() x = 1 y = y + 1 setCursor( x, y ) else -- Menu selection doMenuItem( nMenuItem ) end elseif param == 29 then -- Menu toggle bMenu = not bMenu if bMenu then term.setCursorBlink( false ) nMenuItem = 1 else term.setCursorBlink( true ) end redrawMenu() end elseif sEvent == "char" then if not bMenu then -- Input text local sLine = tLines[y] tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x) redrawLine() x = x + string.len( param ) setCursor( x, y ) else -- Select menu items for n,sMenuItem in ipairs( tMenuItems ) do if string.lower(string.sub(sMenuItem,1,1)) == string.lower(param) then doMenuItem( n ) break end end end end end term.clear() term.setCursorBlink( false ) term.setCursorPos( 1, 1 )
And here is some code that will edit the arguments required, the newest of which is a password:Spoiler
local sPassword = "password"; local tArgs = { ... } if #tArgs == 0 then print( "Usage: edit <path>" ) return end local sPath = shell.resolve( tArgs[1] ) local bReadOnly = fs.isReadOnly( sPath ) if fs.exists( sPath ) and fs.isDir( sPath ) then print( "Cannot edit a directory" ) return end if tArgs[2] == sPassword then local w,h = term.getSize() local x,y = 1,1 local scrollX, scrollY = 0,0 local bMenu = false local nMenuItem = 1 local sStatus = "" local tLines = {} local function load() tLines = {} if fs.exists( sPath ) then local file = io.open( sPath, "r" ) local sLine = file:read() while sLine do table.insert( tLines, sLine ) sLine = file:read() end file:close() else table.insert( tLines, "" ) end end local function save() local file = io.open( sPath, "w" ) if file then for n, sLine in ipairs( tLines ) do file:write( sLine .. "n" ) end file:close() return true end return false end local function redrawText() for y=1,h-1 do term.setCursorPos( 1 - scrollX, y ) term.clearLine() local sLine = tLines[ y + scrollY ] if sLine ~= nil then term.write( sLine ) end end term.setCursorPos( x - scrollX, y - scrollY ) end local function redrawLine() local sLine = tLines[y] term.setCursorPos( 1 - scrollX, y - scrollY ) term.clearLine() term.write( sLine ) term.setCursorPos( x - scrollX, y - scrollY ) end local tMenuItems = { "Save", "Exit" } local function redrawMenu() term.setCursorPos( 1, h ) term.clearLine() if not bMenu then term.write( sStatus ) else for n,sItem in ipairs( tMenuItems ) do if n == nMenuItem then term.write( "["..sItem.."]" ) else term.write( " "..sItem.." " ) end end end term.setCursorPos( x - scrollX, y - scrollY ) end local function setStatus( _sText ) sStatus = _sText redrawMenu() end local function setCursor( x, y ) local screenX = x - scrollX local screenY = y - scrollY local bRedraw = false if screenX < 1 then scrollX = x - 1 screenX = 1 bRedraw = true elseif screenX > w then scrollX = x - w screenX = w bRedraw = true end if screenY < 1 then scrollY = y - 1 screenY = 1 bRedraw = true elseif screenY > h-1 then scrollY = y - (h-1) screenY = h-1 bRedraw = true end if bRedraw then redrawText() end term.setCursorPos( screenX, screenY ) end load() term.clear() term.setCursorPos(x,y) term.setCursorBlink( true ) redrawText() setStatus( "Press Ctrl to access menu" ) local bRunning = true local tMenuFunctions = { ["Exit"] = function() bRunning = false end, ["Save"] = function() if save() then setStatus( "Saved to "..sPath ) else setStatus( "Access denied" ) end end, } local function doMenuItem( _n ) local fnMenu = tMenuFunctions[ tMenuItems[_n] ] if fnMenu then fnMenu() end bMenu = false term.setCursorBlink( true ) redrawMenu() end while bRunning do local sEvent, param = os.pullEvent() if sEvent == "key" then if param == 200 then -- Up if not bMenu then -- Move cursor up if y > 1 then y = y - 1 x = math.min( x, string.len( tLines[y] ) + 1 ) setCursor( x, y ) end end elseif param == 208 then -- Down if not bMenu then -- Move cursor down if y < #tLines then y = y + 1 x = math.min( x, string.len( tLines[y] ) + 1 ) setCursor( x, y ) end end elseif param == 203 then -- Left if not bMenu then -- Move cursor left if x > 1 then x = x - 1 setCursor( x, y ) end else -- Move menu left nMenuItem = nMenuItem - 1 if nMenuItem < 1 then nMenuItem = #tMenuItems end redrawMenu() end elseif param == 205 then -- Right if not bMenu then -- Move cursor right if x < string.len( tLines[y] ) + 1 then x = x + 1 setCursor( x, y ) end else -- Move menu right nMenuItem = nMenuItem + 1 if nMenuItem > #tMenuItems then nMenuItem = 1 end redrawMenu() end elseif param == 14 then -- Backspace if not bMenu then if x > 1 then -- Remove character local sLine = tLines[y] tLines[y] = string.sub(sLine,1,x-2) .. string.sub(sLine,x) redrawLine() x = x - 1 setCursor( x, y ) elseif y > 1 then -- Remove newline local sPrevLen = string.len( tLines[y-1] ) tLines[y-1] = tLines[y-1] .. tLines[y] table.remove( tLines, y ) redrawText() x = sPrevLen + 1 y = y - 1 setCursor( x, y ) end end elseif param == 28 then -- Enter if not bMenu then -- Newline local sLine = tLines[y] tLines[y] = string.sub(sLine,1,x-1) table.insert( tLines, y+1, string.sub(sLine,x) ) redrawText() x = 1 y = y + 1 setCursor( x, y ) else -- Menu selection doMenuItem( nMenuItem ) end elseif param == 29 then -- Menu toggle bMenu = not bMenu if bMenu then term.setCursorBlink( false ) nMenuItem = 1 else term.setCursorBlink( true ) end redrawMenu() end elseif sEvent == "char" then if not bMenu then -- Input text local sLine = tLines[y] tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x) redrawLine() x = x + string.len( param ) setCursor( x, y ) else -- Select menu items for n,sMenuItem in ipairs( tMenuItems ) do if string.lower(string.sub(sMenuItem,1,1)) == string.lower(param) then doMenuItem( n ) break end end end end end term.clear() term.setCursorBlink( false ) term.setCursorPos( 1, 1 ) else print( "Invalid password" ); return end
Hope I helped! :)/>/>
Posted 12 July 2012 - 11:21 PM
Well there is only a very little changes but here is a version that more suits your idea:
Spoiler
local sPassword = "password";
local sInput = "";
print( "Enter password: " );
sInput = read("*");
if sInput == sPassword then
local tArgs = { ... }
if #tArgs == 0 then
print( "Usage: edit <path>" )
return
end
local sPath = shell.resolve( tArgs[1] )
local bReadOnly = fs.isReadOnly( sPath )
if fs.exists( sPath ) and fs.isDir( sPath ) then
print( "Cannot edit a directory" )
return
end
local w,h = term.getSize()
local x,y = 1,1
local scrollX, scrollY = 0,0
local bMenu = false
local nMenuItem = 1
local sStatus = ""
local tLines = {}
local function load()
tLines = {}
if fs.exists( sPath ) then
local file = io.open( sPath, "r" )
local sLine = file:read()
while sLine do
table.insert( tLines, sLine )
sLine = file:read()
end
file:close()
else
table.insert( tLines, "" )
end
end
local function save()
local file = io.open( sPath, "w" )
if file then
for n, sLine in ipairs( tLines ) do
file:write( sLine .. "n" )
end
file:close()
return true
end
return false
end
local function redrawText()
for y=1,h-1 do
term.setCursorPos( 1 - scrollX, y )
term.clearLine()
local sLine = tLines[ y + scrollY ]
if sLine ~= nil then
term.write( sLine )
end
end
term.setCursorPos( x - scrollX, y - scrollY )
end
local function redrawLine()
local sLine = tLines[y]
term.setCursorPos( 1 - scrollX, y - scrollY )
term.clearLine()
term.write( sLine )
term.setCursorPos( x - scrollX, y - scrollY )
end
local tMenuItems = { "Save", "Exit" }
local function redrawMenu()
term.setCursorPos( 1, h )
term.clearLine()
if not bMenu then
term.write( sStatus )
else
for n,sItem in ipairs( tMenuItems ) do
if n == nMenuItem then
term.write( "["..sItem.."]" )
else
term.write( " "..sItem.." " )
end
end
end
term.setCursorPos( x - scrollX, y - scrollY )
end
local function setStatus( _sText )
sStatus = _sText
redrawMenu()
end
local function setCursor( x, y )
local screenX = x - scrollX
local screenY = y - scrollY
local bRedraw = false
if screenX < 1 then
scrollX = x - 1
screenX = 1
bRedraw = true
elseif screenX > w then
scrollX = x - w
screenX = w
bRedraw = true
end
if screenY < 1 then
scrollY = y - 1
screenY = 1
bRedraw = true
elseif screenY > h-1 then
scrollY = y - (h-1)
screenY = h-1
bRedraw = true
end
if bRedraw then
redrawText()
end
term.setCursorPos( screenX, screenY )
end
load()
term.clear()
term.setCursorPos(x,y)
term.setCursorBlink( true )
redrawText()
setStatus( "Press Ctrl to access menu" )
local bRunning = true
local tMenuFunctions = {
["Exit"] = function()
bRunning = false
end,
["Save"] = function()
if save() then
setStatus( "Saved to "..sPath )
else
setStatus( "Access denied" )
end
end,
}
local function doMenuItem( _n )
local fnMenu = tMenuFunctions[ tMenuItems[_n] ]
if fnMenu then
fnMenu()
end
bMenu = false
term.setCursorBlink( true )
redrawMenu()
end
while bRunning do
local sEvent, param = os.pullEvent()
if sEvent == "key" then
if param == 200 then
-- Up
if not bMenu then
-- Move cursor up
if y > 1 then
y = y - 1
x = math.min( x, string.len( tLines[y] ) + 1 )
setCursor( x, y )
end
end
elseif param == 208 then
-- Down
if not bMenu then
-- Move cursor down
if y < #tLines then
y = y + 1
x = math.min( x, string.len( tLines[y] ) + 1 )
setCursor( x, y )
end
end
elseif param == 203 then
-- Left
if not bMenu then
-- Move cursor left
if x > 1 then
x = x - 1
setCursor( x, y )
end
else
-- Move menu left
nMenuItem = nMenuItem - 1
if nMenuItem < 1 then
nMenuItem = #tMenuItems
end
redrawMenu()
end
elseif param == 205 then
-- Right
if not bMenu then
-- Move cursor right
if x < string.len( tLines[y] ) + 1 then
x = x + 1
setCursor( x, y )
end
else
-- Move menu right
nMenuItem = nMenuItem + 1
if nMenuItem > #tMenuItems then
nMenuItem = 1
end
redrawMenu()
end
elseif param == 14 then
-- Backspace
if not bMenu then
if x > 1 then
-- Remove character
local sLine = tLines[y]
tLines[y] = string.sub(sLine,1,x-2) .. string.sub(sLine,x)
redrawLine()
x = x - 1
setCursor( x, y )
elseif y > 1 then
-- Remove newline
local sPrevLen = string.len( tLines[y-1] )
tLines[y-1] = tLines[y-1] .. tLines[y]
table.remove( tLines, y )
redrawText()
x = sPrevLen + 1
y = y - 1
setCursor( x, y )
end
end
elseif param == 28 then
-- Enter
if not bMenu then
-- Newline
local sLine = tLines[y]
tLines[y] = string.sub(sLine,1,x-1)
table.insert( tLines, y+1, string.sub(sLine,x) )
redrawText()
x = 1
y = y + 1
setCursor( x, y )
else
-- Menu selection
doMenuItem( nMenuItem )
end
elseif param == 29 then
-- Menu toggle
bMenu = not bMenu
if bMenu then
term.setCursorBlink( false )
nMenuItem = 1
else
term.setCursorBlink( true )
end
redrawMenu()
end
elseif sEvent == "char" then
if not bMenu then
-- Input text
local sLine = tLines[y]
tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x)
redrawLine()
x = x + string.len( param )
setCursor( x, y )
else
-- Select menu items
for n,sMenuItem in ipairs( tMenuItems ) do
if string.lower(string.sub(sMenuItem,1,1)) == string.lower(param) then
doMenuItem( n )
break
end
end
end
end
end
term.clear()
term.setCursorBlink( false )
term.setCursorPos( 1, 1 )
else
print( "Invalid Passowrd." );
return;
end
Posted 12 July 2012 - 11:44 PM
Well there is only a very little changes but here is a version that more suits your idea:Spoiler
local sPassword = "password"; local sInput = ""; print( "Enter password: " ); sInput = read("*"); if sInput == sPassword then local tArgs = { ... } if #tArgs == 0 then print( "Usage: edit <path>" ) return end local sPath = shell.resolve( tArgs[1] ) local bReadOnly = fs.isReadOnly( sPath ) if fs.exists( sPath ) and fs.isDir( sPath ) then print( "Cannot edit a directory" ) return end local w,h = term.getSize() local x,y = 1,1 local scrollX, scrollY = 0,0 local bMenu = false local nMenuItem = 1 local sStatus = "" local tLines = {} local function load() tLines = {} if fs.exists( sPath ) then local file = io.open( sPath, "r" ) local sLine = file:read() while sLine do table.insert( tLines, sLine ) sLine = file:read() end file:close() else table.insert( tLines, "" ) end end local function save() local file = io.open( sPath, "w" ) if file then for n, sLine in ipairs( tLines ) do file:write( sLine .. "n" ) end file:close() return true end return false end local function redrawText() for y=1,h-1 do term.setCursorPos( 1 - scrollX, y ) term.clearLine() local sLine = tLines[ y + scrollY ] if sLine ~= nil then term.write( sLine ) end end term.setCursorPos( x - scrollX, y - scrollY ) end local function redrawLine() local sLine = tLines[y] term.setCursorPos( 1 - scrollX, y - scrollY ) term.clearLine() term.write( sLine ) term.setCursorPos( x - scrollX, y - scrollY ) end local tMenuItems = { "Save", "Exit" } local function redrawMenu() term.setCursorPos( 1, h ) term.clearLine() if not bMenu then term.write( sStatus ) else for n,sItem in ipairs( tMenuItems ) do if n == nMenuItem then term.write( "["..sItem.."]" ) else term.write( " "..sItem.." " ) end end end term.setCursorPos( x - scrollX, y - scrollY ) end local function setStatus( _sText ) sStatus = _sText redrawMenu() end local function setCursor( x, y ) local screenX = x - scrollX local screenY = y - scrollY local bRedraw = false if screenX < 1 then scrollX = x - 1 screenX = 1 bRedraw = true elseif screenX > w then scrollX = x - w screenX = w bRedraw = true end if screenY < 1 then scrollY = y - 1 screenY = 1 bRedraw = true elseif screenY > h-1 then scrollY = y - (h-1) screenY = h-1 bRedraw = true end if bRedraw then redrawText() end term.setCursorPos( screenX, screenY ) end load() term.clear() term.setCursorPos(x,y) term.setCursorBlink( true ) redrawText() setStatus( "Press Ctrl to access menu" ) local bRunning = true local tMenuFunctions = { ["Exit"] = function() bRunning = false end, ["Save"] = function() if save() then setStatus( "Saved to "..sPath ) else setStatus( "Access denied" ) end end, } local function doMenuItem( _n ) local fnMenu = tMenuFunctions[ tMenuItems[_n] ] if fnMenu then fnMenu() end bMenu = false term.setCursorBlink( true ) redrawMenu() end while bRunning do local sEvent, param = os.pullEvent() if sEvent == "key" then if param == 200 then -- Up if not bMenu then -- Move cursor up if y > 1 then y = y - 1 x = math.min( x, string.len( tLines[y] ) + 1 ) setCursor( x, y ) end end elseif param == 208 then -- Down if not bMenu then -- Move cursor down if y < #tLines then y = y + 1 x = math.min( x, string.len( tLines[y] ) + 1 ) setCursor( x, y ) end end elseif param == 203 then -- Left if not bMenu then -- Move cursor left if x > 1 then x = x - 1 setCursor( x, y ) end else -- Move menu left nMenuItem = nMenuItem - 1 if nMenuItem < 1 then nMenuItem = #tMenuItems end redrawMenu() end elseif param == 205 then -- Right if not bMenu then -- Move cursor right if x < string.len( tLines[y] ) + 1 then x = x + 1 setCursor( x, y ) end else -- Move menu right nMenuItem = nMenuItem + 1 if nMenuItem > #tMenuItems then nMenuItem = 1 end redrawMenu() end elseif param == 14 then -- Backspace if not bMenu then if x > 1 then -- Remove character local sLine = tLines[y] tLines[y] = string.sub(sLine,1,x-2) .. string.sub(sLine,x) redrawLine() x = x - 1 setCursor( x, y ) elseif y > 1 then -- Remove newline local sPrevLen = string.len( tLines[y-1] ) tLines[y-1] = tLines[y-1] .. tLines[y] table.remove( tLines, y ) redrawText() x = sPrevLen + 1 y = y - 1 setCursor( x, y ) end end elseif param == 28 then -- Enter if not bMenu then -- Newline local sLine = tLines[y] tLines[y] = string.sub(sLine,1,x-1) table.insert( tLines, y+1, string.sub(sLine,x) ) redrawText() x = 1 y = y + 1 setCursor( x, y ) else -- Menu selection doMenuItem( nMenuItem ) end elseif param == 29 then -- Menu toggle bMenu = not bMenu if bMenu then term.setCursorBlink( false ) nMenuItem = 1 else term.setCursorBlink( true ) end redrawMenu() end elseif sEvent == "char" then if not bMenu then -- Input text local sLine = tLines[y] tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x) redrawLine() x = x + string.len( param ) setCursor( x, y ) else -- Select menu items for n,sMenuItem in ipairs( tMenuItems ) do if string.lower(string.sub(sMenuItem,1,1)) == string.lower(param) then doMenuItem( n ) break end end end end end term.clear() term.setCursorBlink( false ) term.setCursorPos( 1, 1 ) else print( "Invalid Passowrd." ); return; end
Perfect :)/>/>
Posted 13 July 2012 - 12:23 AM
Well there is only a very little changes but here is a version that more suits your idea:Spoiler
local sPassword = "password"; local sInput = ""; print( "Enter password: " ); sInput = read("*"); if sInput == sPassword then local tArgs = { ... } if #tArgs == 0 then print( "Usage: edit <path>" ) return end local sPath = shell.resolve( tArgs[1] ) local bReadOnly = fs.isReadOnly( sPath ) if fs.exists( sPath ) and fs.isDir( sPath ) then print( "Cannot edit a directory" ) return end local w,h = term.getSize() local x,y = 1,1 local scrollX, scrollY = 0,0 local bMenu = false local nMenuItem = 1 local sStatus = "" local tLines = {} local function load() tLines = {} if fs.exists( sPath ) then local file = io.open( sPath, "r" ) local sLine = file:read() while sLine do table.insert( tLines, sLine ) sLine = file:read() end file:close() else table.insert( tLines, "" ) end end local function save() local file = io.open( sPath, "w" ) if file then for n, sLine in ipairs( tLines ) do file:write( sLine .. "n" ) end file:close() return true end return false end local function redrawText() for y=1,h-1 do term.setCursorPos( 1 - scrollX, y ) term.clearLine() local sLine = tLines[ y + scrollY ] if sLine ~= nil then term.write( sLine ) end end term.setCursorPos( x - scrollX, y - scrollY ) end local function redrawLine() local sLine = tLines[y] term.setCursorPos( 1 - scrollX, y - scrollY ) term.clearLine() term.write( sLine ) term.setCursorPos( x - scrollX, y - scrollY ) end local tMenuItems = { "Save", "Exit" } local function redrawMenu() term.setCursorPos( 1, h ) term.clearLine() if not bMenu then term.write( sStatus ) else for n,sItem in ipairs( tMenuItems ) do if n == nMenuItem then term.write( "["..sItem.."]" ) else term.write( " "..sItem.." " ) end end end term.setCursorPos( x - scrollX, y - scrollY ) end local function setStatus( _sText ) sStatus = _sText redrawMenu() end local function setCursor( x, y ) local screenX = x - scrollX local screenY = y - scrollY local bRedraw = false if screenX < 1 then scrollX = x - 1 screenX = 1 bRedraw = true elseif screenX > w then scrollX = x - w screenX = w bRedraw = true end if screenY < 1 then scrollY = y - 1 screenY = 1 bRedraw = true elseif screenY > h-1 then scrollY = y - (h-1) screenY = h-1 bRedraw = true end if bRedraw then redrawText() end term.setCursorPos( screenX, screenY ) end load() term.clear() term.setCursorPos(x,y) term.setCursorBlink( true ) redrawText() setStatus( "Press Ctrl to access menu" ) local bRunning = true local tMenuFunctions = { ["Exit"] = function() bRunning = false end, ["Save"] = function() if save() then setStatus( "Saved to "..sPath ) else setStatus( "Access denied" ) end end, } local function doMenuItem( _n ) local fnMenu = tMenuFunctions[ tMenuItems[_n] ] if fnMenu then fnMenu() end bMenu = false term.setCursorBlink( true ) redrawMenu() end while bRunning do local sEvent, param = os.pullEvent() if sEvent == "key" then if param == 200 then -- Up if not bMenu then -- Move cursor up if y > 1 then y = y - 1 x = math.min( x, string.len( tLines[y] ) + 1 ) setCursor( x, y ) end end elseif param == 208 then -- Down if not bMenu then -- Move cursor down if y < #tLines then y = y + 1 x = math.min( x, string.len( tLines[y] ) + 1 ) setCursor( x, y ) end end elseif param == 203 then -- Left if not bMenu then -- Move cursor left if x > 1 then x = x - 1 setCursor( x, y ) end else -- Move menu left nMenuItem = nMenuItem - 1 if nMenuItem < 1 then nMenuItem = #tMenuItems end redrawMenu() end elseif param == 205 then -- Right if not bMenu then -- Move cursor right if x < string.len( tLines[y] ) + 1 then x = x + 1 setCursor( x, y ) end else -- Move menu right nMenuItem = nMenuItem + 1 if nMenuItem > #tMenuItems then nMenuItem = 1 end redrawMenu() end elseif param == 14 then -- Backspace if not bMenu then if x > 1 then -- Remove character local sLine = tLines[y] tLines[y] = string.sub(sLine,1,x-2) .. string.sub(sLine,x) redrawLine() x = x - 1 setCursor( x, y ) elseif y > 1 then -- Remove newline local sPrevLen = string.len( tLines[y-1] ) tLines[y-1] = tLines[y-1] .. tLines[y] table.remove( tLines, y ) redrawText() x = sPrevLen + 1 y = y - 1 setCursor( x, y ) end end elseif param == 28 then -- Enter if not bMenu then -- Newline local sLine = tLines[y] tLines[y] = string.sub(sLine,1,x-1) table.insert( tLines, y+1, string.sub(sLine,x) ) redrawText() x = 1 y = y + 1 setCursor( x, y ) else -- Menu selection doMenuItem( nMenuItem ) end elseif param == 29 then -- Menu toggle bMenu = not bMenu if bMenu then term.setCursorBlink( false ) nMenuItem = 1 else term.setCursorBlink( true ) end redrawMenu() end elseif sEvent == "char" then if not bMenu then -- Input text local sLine = tLines[y] tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x) redrawLine() x = x + string.len( param ) setCursor( x, y ) else -- Select menu items for n,sMenuItem in ipairs( tMenuItems ) do if string.lower(string.sub(sMenuItem,1,1)) == string.lower(param) then doMenuItem( n ) break end end end end end term.clear() term.setCursorBlink( false ) term.setCursorPos( 1, 1 ) else print( "Invalid Passowrd." ); return; end
Could you also do this for the 'delete' command?
Posted 13 July 2012 - 01:10 AM
Well there is only a very little changes but here is a version that more suits your idea:Spoiler
local sPassword = "password"; local sInput = ""; print( "Enter password: " ); sInput = read("*"); if sInput == sPassword then local tArgs = { ... } if #tArgs == 0 then print( "Usage: edit <path>" ) return end local sPath = shell.resolve( tArgs[1] ) local bReadOnly = fs.isReadOnly( sPath ) if fs.exists( sPath ) and fs.isDir( sPath ) then print( "Cannot edit a directory" ) return end local w,h = term.getSize() local x,y = 1,1 local scrollX, scrollY = 0,0 local bMenu = false local nMenuItem = 1 local sStatus = "" local tLines = {} local function load() tLines = {} if fs.exists( sPath ) then local file = io.open( sPath, "r" ) local sLine = file:read() while sLine do table.insert( tLines, sLine ) sLine = file:read() end file:close() else table.insert( tLines, "" ) end end local function save() local file = io.open( sPath, "w" ) if file then for n, sLine in ipairs( tLines ) do file:write( sLine .. "n" ) end file:close() return true end return false end local function redrawText() for y=1,h-1 do term.setCursorPos( 1 - scrollX, y ) term.clearLine() local sLine = tLines[ y + scrollY ] if sLine ~= nil then term.write( sLine ) end end term.setCursorPos( x - scrollX, y - scrollY ) end local function redrawLine() local sLine = tLines[y] term.setCursorPos( 1 - scrollX, y - scrollY ) term.clearLine() term.write( sLine ) term.setCursorPos( x - scrollX, y - scrollY ) end local tMenuItems = { "Save", "Exit" } local function redrawMenu() term.setCursorPos( 1, h ) term.clearLine() if not bMenu then term.write( sStatus ) else for n,sItem in ipairs( tMenuItems ) do if n == nMenuItem then term.write( "["..sItem.."]" ) else term.write( " "..sItem.." " ) end end end term.setCursorPos( x - scrollX, y - scrollY ) end local function setStatus( _sText ) sStatus = _sText redrawMenu() end local function setCursor( x, y ) local screenX = x - scrollX local screenY = y - scrollY local bRedraw = false if screenX < 1 then scrollX = x - 1 screenX = 1 bRedraw = true elseif screenX > w then scrollX = x - w screenX = w bRedraw = true end if screenY < 1 then scrollY = y - 1 screenY = 1 bRedraw = true elseif screenY > h-1 then scrollY = y - (h-1) screenY = h-1 bRedraw = true end if bRedraw then redrawText() end term.setCursorPos( screenX, screenY ) end load() term.clear() term.setCursorPos(x,y) term.setCursorBlink( true ) redrawText() setStatus( "Press Ctrl to access menu" ) local bRunning = true local tMenuFunctions = { ["Exit"] = function() bRunning = false end, ["Save"] = function() if save() then setStatus( "Saved to "..sPath ) else setStatus( "Access denied" ) end end, } local function doMenuItem( _n ) local fnMenu = tMenuFunctions[ tMenuItems[_n] ] if fnMenu then fnMenu() end bMenu = false term.setCursorBlink( true ) redrawMenu() end while bRunning do local sEvent, param = os.pullEvent() if sEvent == "key" then if param == 200 then -- Up if not bMenu then -- Move cursor up if y > 1 then y = y - 1 x = math.min( x, string.len( tLines[y] ) + 1 ) setCursor( x, y ) end end elseif param == 208 then -- Down if not bMenu then -- Move cursor down if y < #tLines then y = y + 1 x = math.min( x, string.len( tLines[y] ) + 1 ) setCursor( x, y ) end end elseif param == 203 then -- Left if not bMenu then -- Move cursor left if x > 1 then x = x - 1 setCursor( x, y ) end else -- Move menu left nMenuItem = nMenuItem - 1 if nMenuItem < 1 then nMenuItem = #tMenuItems end redrawMenu() end elseif param == 205 then -- Right if not bMenu then -- Move cursor right if x < string.len( tLines[y] ) + 1 then x = x + 1 setCursor( x, y ) end else -- Move menu right nMenuItem = nMenuItem + 1 if nMenuItem > #tMenuItems then nMenuItem = 1 end redrawMenu() end elseif param == 14 then -- Backspace if not bMenu then if x > 1 then -- Remove character local sLine = tLines[y] tLines[y] = string.sub(sLine,1,x-2) .. string.sub(sLine,x) redrawLine() x = x - 1 setCursor( x, y ) elseif y > 1 then -- Remove newline local sPrevLen = string.len( tLines[y-1] ) tLines[y-1] = tLines[y-1] .. tLines[y] table.remove( tLines, y ) redrawText() x = sPrevLen + 1 y = y - 1 setCursor( x, y ) end end elseif param == 28 then -- Enter if not bMenu then -- Newline local sLine = tLines[y] tLines[y] = string.sub(sLine,1,x-1) table.insert( tLines, y+1, string.sub(sLine,x) ) redrawText() x = 1 y = y + 1 setCursor( x, y ) else -- Menu selection doMenuItem( nMenuItem ) end elseif param == 29 then -- Menu toggle bMenu = not bMenu if bMenu then term.setCursorBlink( false ) nMenuItem = 1 else term.setCursorBlink( true ) end redrawMenu() end elseif sEvent == "char" then if not bMenu then -- Input text local sLine = tLines[y] tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x) redrawLine() x = x + string.len( param ) setCursor( x, y ) else -- Select menu items for n,sMenuItem in ipairs( tMenuItems ) do if string.lower(string.sub(sMenuItem,1,1)) == string.lower(param) then doMenuItem( n ) break end end end end end term.clear() term.setCursorBlink( false ) term.setCursorPos( 1, 1 ) else print( "Invalid Passowrd." ); return; end
Could you also do this for the 'delete' command?
Might as well ask for move, copy, lua and rename while you're at it. It really should be a simple matter of seeing what was changed and implementing those changes in the other programs.
Posted 13 July 2012 - 01:15 AM
Actually, could you do what Lyqyd said and do the same for move, copy, lua, rename and delete? sorry if that makes things difficult for you.Well there is only a very little changes but here is a version that more suits your idea:Spoiler
local sPassword = "password"; local sInput = ""; print( "Enter password: " ); sInput = read("*"); if sInput == sPassword then local tArgs = { ... } if #tArgs == 0 then print( "Usage: edit <path>" ) return end local sPath = shell.resolve( tArgs[1] ) local bReadOnly = fs.isReadOnly( sPath ) if fs.exists( sPath ) and fs.isDir( sPath ) then print( "Cannot edit a directory" ) return end local w,h = term.getSize() local x,y = 1,1 local scrollX, scrollY = 0,0 local bMenu = false local nMenuItem = 1 local sStatus = "" local tLines = {} local function load() tLines = {} if fs.exists( sPath ) then local file = io.open( sPath, "r" ) local sLine = file:read() while sLine do table.insert( tLines, sLine ) sLine = file:read() end file:close() else table.insert( tLines, "" ) end end local function save() local file = io.open( sPath, "w" ) if file then for n, sLine in ipairs( tLines ) do file:write( sLine .. "n" ) end file:close() return true end return false end local function redrawText() for y=1,h-1 do term.setCursorPos( 1 - scrollX, y ) term.clearLine() local sLine = tLines[ y + scrollY ] if sLine ~= nil then term.write( sLine ) end end term.setCursorPos( x - scrollX, y - scrollY ) end local function redrawLine() local sLine = tLines[y] term.setCursorPos( 1 - scrollX, y - scrollY ) term.clearLine() term.write( sLine ) term.setCursorPos( x - scrollX, y - scrollY ) end local tMenuItems = { "Save", "Exit" } local function redrawMenu() term.setCursorPos( 1, h ) term.clearLine() if not bMenu then term.write( sStatus ) else for n,sItem in ipairs( tMenuItems ) do if n == nMenuItem then term.write( "["..sItem.."]" ) else term.write( " "..sItem.." " ) end end end term.setCursorPos( x - scrollX, y - scrollY ) end local function setStatus( _sText ) sStatus = _sText redrawMenu() end local function setCursor( x, y ) local screenX = x - scrollX local screenY = y - scrollY local bRedraw = false if screenX < 1 then scrollX = x - 1 screenX = 1 bRedraw = true elseif screenX > w then scrollX = x - w screenX = w bRedraw = true end if screenY < 1 then scrollY = y - 1 screenY = 1 bRedraw = true elseif screenY > h-1 then scrollY = y - (h-1) screenY = h-1 bRedraw = true end if bRedraw then redrawText() end term.setCursorPos( screenX, screenY ) end load() term.clear() term.setCursorPos(x,y) term.setCursorBlink( true ) redrawText() setStatus( "Press Ctrl to access menu" ) local bRunning = true local tMenuFunctions = { ["Exit"] = function() bRunning = false end, ["Save"] = function() if save() then setStatus( "Saved to "..sPath ) else setStatus( "Access denied" ) end end, } local function doMenuItem( _n ) local fnMenu = tMenuFunctions[ tMenuItems[_n] ] if fnMenu then fnMenu() end bMenu = false term.setCursorBlink( true ) redrawMenu() end while bRunning do local sEvent, param = os.pullEvent() if sEvent == "key" then if param == 200 then -- Up if not bMenu then -- Move cursor up if y > 1 then y = y - 1 x = math.min( x, string.len( tLines[y] ) + 1 ) setCursor( x, y ) end end elseif param == 208 then -- Down if not bMenu then -- Move cursor down if y < #tLines then y = y + 1 x = math.min( x, string.len( tLines[y] ) + 1 ) setCursor( x, y ) end end elseif param == 203 then -- Left if not bMenu then -- Move cursor left if x > 1 then x = x - 1 setCursor( x, y ) end else -- Move menu left nMenuItem = nMenuItem - 1 if nMenuItem < 1 then nMenuItem = #tMenuItems end redrawMenu() end elseif param == 205 then -- Right if not bMenu then -- Move cursor right if x < string.len( tLines[y] ) + 1 then x = x + 1 setCursor( x, y ) end else -- Move menu right nMenuItem = nMenuItem + 1 if nMenuItem > #tMenuItems then nMenuItem = 1 end redrawMenu() end elseif param == 14 then -- Backspace if not bMenu then if x > 1 then -- Remove character local sLine = tLines[y] tLines[y] = string.sub(sLine,1,x-2) .. string.sub(sLine,x) redrawLine() x = x - 1 setCursor( x, y ) elseif y > 1 then -- Remove newline local sPrevLen = string.len( tLines[y-1] ) tLines[y-1] = tLines[y-1] .. tLines[y] table.remove( tLines, y ) redrawText() x = sPrevLen + 1 y = y - 1 setCursor( x, y ) end end elseif param == 28 then -- Enter if not bMenu then -- Newline local sLine = tLines[y] tLines[y] = string.sub(sLine,1,x-1) table.insert( tLines, y+1, string.sub(sLine,x) ) redrawText() x = 1 y = y + 1 setCursor( x, y ) else -- Menu selection doMenuItem( nMenuItem ) end elseif param == 29 then -- Menu toggle bMenu = not bMenu if bMenu then term.setCursorBlink( false ) nMenuItem = 1 else term.setCursorBlink( true ) end redrawMenu() end elseif sEvent == "char" then if not bMenu then -- Input text local sLine = tLines[y] tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x) redrawLine() x = x + string.len( param ) setCursor( x, y ) else -- Select menu items for n,sMenuItem in ipairs( tMenuItems ) do if string.lower(string.sub(sMenuItem,1,1)) == string.lower(param) then doMenuItem( n ) break end end end end end term.clear() term.setCursorBlink( false ) term.setCursorPos( 1, 1 ) else print( "Invalid Passowrd." ); return; end
Posted 13 July 2012 - 01:19 AM
Actually, could you do what Lyqyd said and do the same for move, copy, lua, rename and delete? sorry if that makes things difficult for you.Well there is only a very little changes but here is a version that more suits your idea:Spoiler
local sPassword = "password"; local sInput = ""; print( "Enter password: " ); sInput = read("*"); if sInput == sPassword then local tArgs = { ... } if #tArgs == 0 then print( "Usage: edit <path>" ) return end local sPath = shell.resolve( tArgs[1] ) local bReadOnly = fs.isReadOnly( sPath ) if fs.exists( sPath ) and fs.isDir( sPath ) then print( "Cannot edit a directory" ) return end local w,h = term.getSize() local x,y = 1,1 local scrollX, scrollY = 0,0 local bMenu = false local nMenuItem = 1 local sStatus = "" local tLines = {} local function load() tLines = {} if fs.exists( sPath ) then local file = io.open( sPath, "r" ) local sLine = file:read() while sLine do table.insert( tLines, sLine ) sLine = file:read() end file:close() else table.insert( tLines, "" ) end end local function save() local file = io.open( sPath, "w" ) if file then for n, sLine in ipairs( tLines ) do file:write( sLine .. "n" ) end file:close() return true end return false end local function redrawText() for y=1,h-1 do term.setCursorPos( 1 - scrollX, y ) term.clearLine() local sLine = tLines[ y + scrollY ] if sLine ~= nil then term.write( sLine ) end end term.setCursorPos( x - scrollX, y - scrollY ) end local function redrawLine() local sLine = tLines[y] term.setCursorPos( 1 - scrollX, y - scrollY ) term.clearLine() term.write( sLine ) term.setCursorPos( x - scrollX, y - scrollY ) end local tMenuItems = { "Save", "Exit" } local function redrawMenu() term.setCursorPos( 1, h ) term.clearLine() if not bMenu then term.write( sStatus ) else for n,sItem in ipairs( tMenuItems ) do if n == nMenuItem then term.write( "["..sItem.."]" ) else term.write( " "..sItem.." " ) end end end term.setCursorPos( x - scrollX, y - scrollY ) end local function setStatus( _sText ) sStatus = _sText redrawMenu() end local function setCursor( x, y ) local screenX = x - scrollX local screenY = y - scrollY local bRedraw = false if screenX < 1 then scrollX = x - 1 screenX = 1 bRedraw = true elseif screenX > w then scrollX = x - w screenX = w bRedraw = true end if screenY < 1 then scrollY = y - 1 screenY = 1 bRedraw = true elseif screenY > h-1 then scrollY = y - (h-1) screenY = h-1 bRedraw = true end if bRedraw then redrawText() end term.setCursorPos( screenX, screenY ) end load() term.clear() term.setCursorPos(x,y) term.setCursorBlink( true ) redrawText() setStatus( "Press Ctrl to access menu" ) local bRunning = true local tMenuFunctions = { ["Exit"] = function() bRunning = false end, ["Save"] = function() if save() then setStatus( "Saved to "..sPath ) else setStatus( "Access denied" ) end end, } local function doMenuItem( _n ) local fnMenu = tMenuFunctions[ tMenuItems[_n] ] if fnMenu then fnMenu() end bMenu = false term.setCursorBlink( true ) redrawMenu() end while bRunning do local sEvent, param = os.pullEvent() if sEvent == "key" then if param == 200 then -- Up if not bMenu then -- Move cursor up if y > 1 then y = y - 1 x = math.min( x, string.len( tLines[y] ) + 1 ) setCursor( x, y ) end end elseif param == 208 then -- Down if not bMenu then -- Move cursor down if y < #tLines then y = y + 1 x = math.min( x, string.len( tLines[y] ) + 1 ) setCursor( x, y ) end end elseif param == 203 then -- Left if not bMenu then -- Move cursor left if x > 1 then x = x - 1 setCursor( x, y ) end else -- Move menu left nMenuItem = nMenuItem - 1 if nMenuItem < 1 then nMenuItem = #tMenuItems end redrawMenu() end elseif param == 205 then -- Right if not bMenu then -- Move cursor right if x < string.len( tLines[y] ) + 1 then x = x + 1 setCursor( x, y ) end else -- Move menu right nMenuItem = nMenuItem + 1 if nMenuItem > #tMenuItems then nMenuItem = 1 end redrawMenu() end elseif param == 14 then -- Backspace if not bMenu then if x > 1 then -- Remove character local sLine = tLines[y] tLines[y] = string.sub(sLine,1,x-2) .. string.sub(sLine,x) redrawLine() x = x - 1 setCursor( x, y ) elseif y > 1 then -- Remove newline local sPrevLen = string.len( tLines[y-1] ) tLines[y-1] = tLines[y-1] .. tLines[y] table.remove( tLines, y ) redrawText() x = sPrevLen + 1 y = y - 1 setCursor( x, y ) end end elseif param == 28 then -- Enter if not bMenu then -- Newline local sLine = tLines[y] tLines[y] = string.sub(sLine,1,x-1) table.insert( tLines, y+1, string.sub(sLine,x) ) redrawText() x = 1 y = y + 1 setCursor( x, y ) else -- Menu selection doMenuItem( nMenuItem ) end elseif param == 29 then -- Menu toggle bMenu = not bMenu if bMenu then term.setCursorBlink( false ) nMenuItem = 1 else term.setCursorBlink( true ) end redrawMenu() end elseif sEvent == "char" then if not bMenu then -- Input text local sLine = tLines[y] tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x) redrawLine() x = x + string.len( param ) setCursor( x, y ) else -- Select menu items for n,sMenuItem in ipairs( tMenuItems ) do if string.lower(string.sub(sMenuItem,1,1)) == string.lower(param) then doMenuItem( n ) break end end end end end term.clear() term.setCursorBlink( false ) term.setCursorPos( 1, 1 ) else print( "Invalid Passowrd." ); return; end
No, I'm saying that you, Grey_M62, should do it. You have the code to make it happen, so copy/paste it over to the other files.
Posted 13 July 2012 - 01:20 AM
Actually, what Lyqyd said is that you can make it yourself, by looking at the changes made in the edit program. Wich is just adding this:Actually, could you do what Lyqyd said and do the same for move, copy, lua, rename and delete? sorry if that makes things difficult for you.
local sPassword = "password"
local sInput = ""
print( "Enter password: " )
sInput = read("*")
if sInput == sPassword then
to the top of the program, and an "end" at the bottom.Posted 13 July 2012 - 01:37 AM
But the thing is, i can't find the code for move, copy, lua, rename, and delete.Actually, what Lyqyd said is that you can make it yourself, by looking at the changes made in the edit program. Wich is just adding this:Actually, could you do what Lyqyd said and do the same for move, copy, lua, rename and delete? sorry if that makes things difficult for you.to the top of the program, and an "end" at the bottom.local sPassword = "password" local sInput = "" print( "Enter password: " ) sInput = read("*") if sInput == sPassword then
Posted 13 July 2012 - 01:58 AM
The files will be located in "C:Users<Yourname>AppDataRoaming.minecraftmodsComputerCraftluaromprograms", if you're on Windows.
Hope I helped! :)/>/>
Hope I helped! :)/>/>
Posted 13 July 2012 - 03:47 AM
Thanks.The files will be located in "C:Users<Yourname>AppDataRoaming.minecraftmodsComputerCraftluaromprograms", if you're on Windows.
Hope I helped! :)/>/>