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

Reboot and Shutdown

Started by DeclipZ, 02 April 2015 - 01:26 PM
DeclipZ #1
Posted 02 April 2015 - 03:26 PM
Hi! I've got a question: can I change string "Goodbye." in Shutdown and Reboot function?
KingofGamesYami #2
Posted 02 April 2015 - 03:45 PM
There is no string in os.shutdown nor os.reboot, however the "shutdown" and "reboot" programs (which are not normally editable). You could create your own shutdown or reboot program very easily, something like this:


print( "Goodbye." )
sleep( 5 )
os.shutdown()
DeclipZ #3
Posted 02 April 2015 - 04:20 PM
For not spamming a new topics I write here.
How can I do special field for text? Like a registration.
If it is possible write a example code :)/>
Creator #4
Posted 02 April 2015 - 04:29 PM
What do you mean? Something like password? If yes then use this

PassInput = read()

If passInpit == standartpass then
Do something
End
DannySMc #5
Posted 02 April 2015 - 04:53 PM
For not spamming a new topics I write here.
How can I do special field for text? Like a registration.
If it is possible write a example code :)/>/>

Try this: (this will make username visible and password be *):

print("Username:")
local username = read()
print("Password:")
local password = read("*")
Creator #6
Posted 02 April 2015 - 06:30 PM
I think danny managed to explain in a better way what I was trying to explain.
DeclipZ #7
Posted 02 April 2015 - 06:38 PM
Srry for my bad english. I did not mean that you recommend for me.
Something like this, but in CC:

When I left-click on "field", I can write a text, password or username. I see something like that in CC, but I don't see a code
Edited on 02 April 2015 - 04:38 PM
Creator #8
Posted 02 April 2015 - 06:54 PM
I have a program that does this, here is a link. However, you should use this api with it.

Be sure to add this line of code in the beggining of the program:


os.loadAPI("GUI")

Enjoy!

Toy around with the variables
KingofGamesYami #9
Posted 02 April 2015 - 07:01 PM
To give a better explanation, you have to code that directly into your script. You can pull mouse_click events to check if the field is selected or not, key events to check for tab/return, and char events for letters/numbers/symbols. Then it's a simple issue of keeping track of what they've typed in a string, rendering appropriate bits of the string in the text field, and moving the cursor.

A good way to figure out some of this is to simply read the source code for "read()"

Spoiler

function read( _sReplaceChar, _tHistory, _fnComplete )
    term.setCursorBlink( true )

    local sLine = ""
    local nHistoryPos
    local nPos = 0
    if _sReplaceChar then
        _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
    end

    local tCompletions
    local nCompletion
    local function recomplete()
        if _fnComplete and nPos == string.len(sLine) then
            tCompletions = _fnComplete( sLine )
            if tCompletions and #tCompletions > 0 then
                nCompletion = 1
            else
                nCompletion = nil
            end
        else
            tCompletions = nil
            nCompletion = nil
        end
    end

    local w = term.getSize()
    local sx = term.getCursorPos()

    local function redraw( _bClear )
        local nScroll = 0
        if sx + nPos >= w then
            nScroll = (sx + nPos) - w
        end

        local cx,cy = term.getCursorPos()
        term.setCursorPos( sx, cy )
        local sReplace = (_bClear and " ") or _sReplaceChar
        if sReplace then
            term.write( string.rep( sReplace, math.max( string.len(sLine) - nScroll, 0 ) ) )
        else
            term.write( string.sub( sLine, nScroll + 1 ) )
        end

        if nCompletion then
            local sCompletion = tCompletions[ nCompletion ]
            local oldText, oldBg
            if not _bClear then
                oldText = term.getTextColor()
                oldBg = term.getBackgroundColor()
                term.setTextColor( colours.white )
                term.setBackgroundColor( colours.grey )
            end
            if sReplace then
                term.write( string.rep( sReplace, string.len( sCompletion ) ) )
            else
                term.write( sCompletion )
            end
            if not _bClear then
                term.setTextColor( oldText )
                term.setBackgroundColor( oldBg )
            end
        end

        term.setCursorPos( sx + nPos - nScroll, cy )
    end

    local function clear()
        redraw( true )
    end

    recomplete()
    redraw()

    local function acceptCompletion()
        if nCompletion then
            -- Clear
            clear()

            -- Find the common prefix of all the other suggestions which start with the same letter as the current one
            local sCompletion = tCompletions[ nCompletion ]
            local sFirstLetter = string.sub( sCompletion, 1, 1 )
            local sCommonPrefix = sCompletion
            for n=1,#tCompletions do
                local sResult = tCompletions[n]
                if n ~= nCompletion and string.find( sResult, sFirstLetter, 1, true ) == 1 then
                    while #sCommonPrefix > 1 do
                        if string.find( sResult, sCommonPrefix, 1, true ) == 1 then
                            break
                        else
                            sCommonPrefix = string.sub( sCommonPrefix, 1, #sCommonPrefix - 1 )
                        end
                    end
                end
            end

            -- Append this string
            sLine = sLine .. sCommonPrefix
            nPos = string.len( sLine )
            recomplete()
            redraw()
        end
    end
    while true do
        local sEvent, param = os.pullEvent()
        if sEvent == "char" then
            -- Typed key
            clear()
            sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
            nPos = nPos + 1
            recomplete()
            redraw()

        elseif sEvent == "paste" then
            -- Pasted text
            clear()
            sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
            nPos = nPos + string.len( param )
            recomplete()
            redraw()

        elseif sEvent == "key" then
            if param == keys.enter then
                -- Enter
                if nCompletion then
                    clear()
                    tCompletions = nil
                    nCompletion = nil
                    redraw()
                end
                break

            elseif param == keys.left then
                -- Left
                if nPos > 0 then
                    clear()
                    nPos = nPos - 1
                    recomplete()
                    redraw()
                end

            elseif param == keys.right then
                -- Right                
                if nPos < string.len(sLine) then
                    -- Move right
                    clear()
                    nPos = nPos + 1
                    recomplete()
                    redraw()
                else
                    -- Accept autocomplete
                    acceptCompletion()
                end

            elseif param == keys.up or param == keys.down then
                -- Up or down
                if nCompletion then
                    -- Cycle completions
                    clear()
                    if param == keys.up then
                        nCompletion = nCompletion - 1
                        if nCompletion < 1 then
                            nCompletion = #tCompletions
                        end
                    elseif param == keys.down then
                        nCompletion = nCompletion + 1
                        if nCompletion > #tCompletions then
                            nCompletion = 1
                        end
                    end
                    redraw()

                elseif _tHistory then
                    -- Cycle history
                    clear()
                    if param == keys.up then
                        -- Up
                        if nHistoryPos == nil then
                            if #_tHistory > 0 then
                                nHistoryPos = #_tHistory
                            end
                        elseif nHistoryPos > 1 then
                            nHistoryPos = nHistoryPos - 1
                        end
                    else
                        -- Down
                        if nHistoryPos == #_tHistory then
                            nHistoryPos = nil
                        elseif nHistoryPos ~= nil then
                            nHistoryPos = nHistoryPos + 1
                        end                        
                    end
                    if nHistoryPos then
                        sLine = _tHistory[nHistoryPos]
                        nPos = string.len( sLine ) 
                    else
                        sLine = ""
                        nPos = 0
                    end
                    recomplete()
                    redraw()

                end

            elseif param == keys.backspace then
                -- Backspace
                if nPos > 0 then
                    clear()
                    sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
                    nPos = nPos - 1
                    recomplete()
                    redraw()
                end

            elseif param == keys.home then
                -- Home
                if nPos > 0 then
                    clear()
                    nPos = 0
                    recomplete()
                    redraw()
                end

            elseif param == keys.delete then
                -- Delete
                if nPos < string.len(sLine) then
                    clear()
                    sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )                
                    recomplete()
                    redraw()
                end

            elseif param == keys["end"] then
                -- End
                if nPos < string.len(sLine ) then
                    clear()
                    nPos = string.len(sLine)
                    recomplete()
                    redraw()
                end

            elseif param == keys.tab then
                -- Tab (accept autocomplete)
                acceptCompletion()

            end

        elseif sEvent == "term_resize" then
            -- Terminal resized
            w = term.getSize()
            redraw()

        end
    end

    local cx, cy = term.getCursorPos()
    term.setCursorBlink( false )
    term.setCursorPos( w + 1, cy )
    print()

    return sLine
end

Note: I wouldn't recommend writing this from scratch if you don't have a good knowledge of Lua or CC. It may be easier to use an API written by someone else.
DeclipZ #10
Posted 02 April 2015 - 07:21 PM
Thank you for helping. Another question: what function sets label for terminal?
KingofGamesYami #11
Posted 02 April 2015 - 07:57 PM
What do you mean? If you mean the


CraftOS #.##

That's not settable.

If you mean the computer label, os.setComputerLabel.

http://computercraft.info/wiki/OS_(API)
DeclipZ #12
Posted 02 April 2015 - 08:13 PM
If you mean the computer label
Yeah, i did. Thank you. Can you find my mistake in program? It crashed with message: "bios:367: [string "install"]:88: ')' expected"
Spoiler
--Variables--
local XSize, YSize = term.getSize()
local Selector = 1

--Functions--
local function pastebin(paste, fileName)
  local file = http.get("http://pastebin.com/raw.php?i="..paste)
  if file then
    file = file.readAll()
    h = fs.open(fileName, "a")
    h.write(file)
    h.close()
  else
    error("Pastebin server is not avaible.")
  end
end

local function clr(color,string,textColor,lineColor)
  term.setBackgroundColor(color)
  term.clear()
  term.setCursorPos(1,1)
  if string ~= nil then
    if textColor ~= nil then
	  term.setTextColor(textColor)
    end
    print(string)
    if lineColor ~= nil then
	  term.setTextColor(lineColor)
    end
    for i=1,51 do
	  if i == 51 then
	    print("-")
	  else
	    write("-")
	  end
    end
  end
end

local function fadeIn(color1,color2,color3,wait)
  term.setBackgroundColor(color1)
  term.clear()
  sleep(0)
  term.setBackgroundColor(color2)
  term.clear()
  sleep(0)
  term.setBackgroundColor(color3)
  term.clear()
  if wait ~= nil then
    sleep(wait)
  end
end

local function fadeOut(color1,color2,wait)
  term.setBackgroundColor(color1)
  term.clear()
  sleep(0)
  term.setBackgroundColor(color2)
  term.clear()
  if wait ~= nil then
    sleep(wait)
  end
  clr(colors.black)
  term.setTextColor(colors.white)
end

local function centerText(type,text,coord,color)
  if color ~= nil then
    term.setTextColor(color)
  end

  if type == "x" then
    local pos = (XSize/2) - (string.len(text))
    term.setCursorPos(pos,coord)
    print(text)
   elseif type == "y" then
    local pos = YSize/2
    term.setCursorPos(coord,pos)
    print(text)
   elseif type == "xy" then
    local pos = (XSize/2) - (string.len(text))
    term.setCursorPos(pos,YSize/2)
    print(text)
  end
end

local function horisontalBar(x,y,width,color)
  for i=x,(x+width,-1) do
    paintutils.drawPixel(i,y,color)
  end
end

local function drawSelection(y,text,id)
  if id == Selector then
    horisontalBar(XSize/2-8,y,16,colors.lightGray)
    centerText("x",y,text,colors.white,colors.lightGray)
  else
    horisontalBar(XSize/2-8,y,16,colors.white)
    centerText("x",y,text,colors.lightGray,colors.white)
  end
end

local function gui()
  local startingY = YSize/2-2
  centerText("x",startingY,"DeclipZ installer",colors.gray,color.white)

  drawSelection(startingY+2,"Install OS",1)
  drawSelection(startingY+3,"Download API",2)
  drawSelection(startingY+4,"Exit",3)
end

--Main--

fadeIn(colors.gray,colors.lightGray,colors.white)
gui()

while true do
  local event, key = os.pullEvent("key")
  if key == keys.up then
    Selector = Selector-1
    if Selector<1 then Selector = 1 end
    gui()
  elseif key == keys.down then
    Selector = Selector+1
    if Selector>3 then Selector = 3 end
    gui()
  elseif key == keys.enter and Selector == 1 then
  --OS
  elseif key == keys.enter and Selector == 2 then
  --APIs
  elseif key == keys.enter and Selector == 3 then
    break
  end
end

fadeOut(colors.lightGray,colors.gray)
clr(colors.black)
SquidDev #13
Posted 02 April 2015 - 09:28 PM
On line 88 you have
for i=x,(x+width,-1) do
You don't want those brackets there.
Edited on 02 April 2015 - 07:29 PM
KingofGamesYami #14
Posted 02 April 2015 - 09:28 PM
On line 88 you have:

  for i=x,(x+width,-1) do
Which should be

  for i=x, x+width,-1 do
Dog #15
Posted 02 April 2015 - 09:42 PM
Shouldn't line 88 be

for i=x,x+width-1 do
otherwise, you're dealing with an increasing value (x to x + width) but decrementing the counter…or am I missing something?
DeclipZ #16
Posted 03 April 2015 - 02:17 PM
Thank you. Another crash:
window:248:bad argument: number expected, got string
Bomb Bloke #17
Posted 03 April 2015 - 02:32 PM
Errors in window are generally triggered by mis-used term functions.

I notice you're doing this sort of thing in drawSelection():

centerText("x",y,text,colors.white,colors.lightGray)

But centerText() is declared as:

local function centerText(type,text,coord,color)

You've got text / coord mixed up, so you end up term.setCursorPos()'ing your text. There may be similar errors elsewhere in the script, but you should now know what to look for if so.
DeclipZ #18
Posted 03 April 2015 - 02:47 PM
Oh, sorry, I forgot about it :lol:/>
Thank you very much!
Edited on 03 April 2015 - 05:21 PM
DeclipZ #19
Posted 03 April 2015 - 07:21 PM
Guys, help me. I have a problem with exit in my installer. Computer is crashed, when I press button "Exit".
Only blackscreen and nothing else
This is installer

It is a code of menu and red rectangle "Exit" button:
Edited on 03 April 2015 - 05:42 PM
KingofGamesYami #20
Posted 03 April 2015 - 08:45 PM
What do you mean by "Computer is crashed"? I ran the installer, selected exit, and it simply cleared the screen and returned to the shell, as I expected.
DeclipZ #21
Posted 03 April 2015 - 09:28 PM
Oh, yeah, I don't know what is happend, but it worked now