ayecue / greybel-vs

VSCode extension for GreyScript.
https://marketplace.visualstudio.com/items?itemName=ayecue.greybel-vs
MIT License
16 stars 7 forks source link

`print`/`user_input` semantics do not match the in-game ones. #121

Closed Olipro closed 1 year ago

Olipro commented 1 year ago

I don't know what to say here because frankly, it took me a painstaking amount of effort to properly determine WTF the interaction between print and user_input is (in GreyHack, not Greybel)

But, I figured it out and the code below gives you a skeleton of what basically behaves like an interactive terminal (albeit with a chunk of functionality left to be implemented)

If you run this same code in Greybel, it dumps out one char per line.

prompt = "root@Foo /home % "
chr = ""
prevCmd = "touch foo" //--for where we are faking up completion
inp = prompt
last = inp
ptr = inp.len
buf = ""
while true
    print(buf + inp, true)
    inp = user_input("", false, true) //--returns the entire buffer of the above print + the key, UNLESS it was a special key, or the previous call was the return key.
    if inp == "UpArrow" then
        inp = last + prevCmd
        wait 0.1
    else if inp == "DownArrow" then //just to test input. gets wiped
        tst = user_input("> ")
        inp = last
    else if inp == last then
        s =  "\npretend we ran: " + inp[ptr:] + "\n" //--simulate a command here. In reality, this will end up needing a full print wrapper, but that's my problem to deal with.
        buf = buf + last + s
        inp = prompt
        ptr = inp.len
    end if
    last = inp
end while
Olipro commented 1 year ago

I've been reliably informed that user_input does this weird behaviour when it has an empty prompt.

However, here's a much simpler scenario that also doesn't work in Greybel (and does in GreyHack)

Gsh = {}
Gsh.buf = ""
LF = char(10)

__print = @print
globals.print = function(s, cs = false)
    if not cs then Gsh.buf = Gsh.buf + s + LF
    if cs then Gsh.buf = s + LF
    __print(Gsh.buf, cs)
end function
__user_input = @user_input
globals.user_input = function(s, pwd = false, oneChar = false)
    cmd = ""
    while true
        clear_screen
        chr = __user_input(Gsh.buf + s + cmd, false, true)
        if chr.len == 0 then
            Gsh.buf = Gsh.buf + s + cmd + LF
            break
        end if
        cmd = cmd + chr
    end while
    return cmd
end function

while true
    cmd = user_input("root@foo /home % ", false, true)
    print("running: " + cmd + "\n")
end while
ayecue commented 1 year ago

Yeah seems like the first example doesn't work in Greybel even though I am not sure I really want to support that behaviour as it's already pretty finicky to support every scenario.

Regarding your second example I am not exactly sure I am following. For me it seems to work fine and the characters actual concatenate just fine.

https://s6.gifyu.com/images/S8bBa.gif

ayecue commented 1 year ago

But there is one bug for sure where print with replaceText does not actually remove all the text.

print("abc1")
print("abc1")
print("abc1")
print("abc1")
print("abc1")
print("abc2", 1)

I was always under the impression that it only replaces the last print but it seems like I was wrong about that.

Olipro commented 1 year ago

Sorry that I forgot the globals, fixed my example for posterity. Anyhow, Bearing in mind that I am still on 1.9.3 - Greybel seems to be failing to detect that we should loop and redisplay the prompt.

In Greybel:

root@foo /home % fsafafgdsg
gdgdf
df
df
dfdf

in GreyHack:

root@foo /home % kshdasf asfh saf
running: kshdasf asfh saf

root@foo /home % adfkdsf
running: adfkdsf

root@foo /home % dsaf 
running: dsaf 

root@foo /home % 
ayecue commented 1 year ago

Yeah it seems in Greybel if you press enter on a user_input the returned char length won't be 0 unlike GreyHack. Have to take a look why that is the reason.

ayecue commented 1 year ago

With 1.9.5 - https://github.com/ayecue/greybel-vs/pull/118 enter is now an empty string just like in GreyHack.

Olipro commented 1 year ago

I went looking for this myself yesterday and was extremely confused when I saw that KeyCode.Enter returned '' - then I checked the commit log and realised you'd already fixed it 😂

Anyhow, all good now, thanks!