Jump to content




read() add string into read before writing


6 replies to this topic

#1 TechnicalCoding

  • Members
  • 35 posts

Posted 29 June 2016 - 12:43 AM

I am creating something to an operating system, I also want some kind of "memory" to it, and thats good, but what I am having problems to understand is how I can assign text/string to read() before anyone starts writing to it?

EXAMPLE
---------------------------
I have an username variable which use read, they get 3 attempts trying to figure out right details but I want that after first attempt I want the same username to appear to second so no one needs to rewrite the username.


Any ideas? Thanks

#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 29 June 2016 - 01:02 AM

So basically like a text field that has a description in it before you start typing? It's not exactly easy, but you *can* do it.

#3 TechnicalCoding

  • Members
  • 35 posts

Posted 29 June 2016 - 02:48 AM

View PostKingofGamesYami, on 29 June 2016 - 01:02 AM, said:

So basically like a text field that has a description in it before you start typing? It's not exactly easy, but you *can* do it.

Not exactly an description, but I want it to contain itself, basicly I am using a while loop to make it go over and over again, and for each time it has to repeat I want it to contain the string it had before the it kind of overlap itself, so basicly just using itself (or duplicate it) to make it just contain itself again..

#4 eniallator

  • Members
  • 56 posts

Posted 29 June 2016 - 09:45 AM

View PostTechnicalCoding, on 29 June 2016 - 02:48 AM, said:

View PostKingofGamesYami, on 29 June 2016 - 01:02 AM, said:

So basically like a text field that has a description in it before you start typing? It's not exactly easy, but you *can* do it.

Not exactly an description, but I want it to contain itself, basicly I am using a while loop to make it go over and over again, and for each time it has to repeat I want it to contain the string it had before the it kind of overlap itself, so basicly just using itself (or duplicate it) to make it just contain itself again..

what about you have an option before you get to the login screen where you can quickly login from. for example like a button that has the username that will show an input field where you put in your password instead of both the username and the password.

#5 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 29 June 2016 - 04:51 PM

EDIT: Do check out the answer below this one first. It's way more crafty and simple.

Re-implementing read in a way that it accepts a 4th parameter which defaults to an empty string if not passed is an option. You'd probably end up with more code than sense, though. For example, the history code is completely useless here.

local function read( _sReplaceChar, _tHistory, _fnComplete, initial_sLine ) -- note the new parameter initial_sLine
	term.setCursorBlink( true )

	local sLine = initial_sLine or "" -- empty string used if initial_sLine is not passed (or it's nil/false)

	... -- the rest of read() as seen in bios.lua
end

read(nil, nil, nil, "initial text") -- you'd call it like this

Full code, in case you have never seen bios.lua before

EDIT: You can just call it with the last result it returned if you want it to remember the input.

local last
while ... -- some loop
	last = read(nil, nil, nil, last)
end

Edited by LBPHacker, 29 June 2016 - 06:39 PM.


#6 Anavrins

  • Members
  • 775 posts

Posted 29 June 2016 - 05:58 PM

A simple way to achieve this is queueing every characters as a char event before read()
local init = "hello world"
for c in init:gmatch(".") do os.queueEvent("char", c) end
read() #-- Will start with "hello world" already written
Or in a format similar to your function
local function newread(sReplaceChar, tHistory, fnComplete, initial_sLine)
  for c in (initial_sLine or ""):gmatch(".") do os.queueEvent("char", c) end
  return read(sReplaceChar, tHistory, fnComplete)
end

Edited by Anavrins, 29 June 2016 - 06:18 PM.


#7 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 30 June 2016 - 02:56 AM

You could alternatively use the "history" feature already built into the read function.

http://www.computerc....info/wiki/Read

Eg:

read(nil, {"Tom", "Dick", "Harry"})

The user can then use up/down to go through these entries. This is how CraftOS and the Lua console let you get at previously typed commands.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users