Using yoshi in yab
#1
WHAT!? why would you want to do that when you can make all your windows and gadgets in yab itself?

1. For the same reason you don't laboriously make your own ALERT and FILEPANEL widgets. You could, you know. But someone already did all the heavy lifting here in terms of layout, so why bother?
2. Because using yoshi lets you tweak the dialogue OUTSIDE yab.

I can think of a number of ways to do this. The easiest is to construct the yoshi dialogue in a separate file and just call it. But I have a fondness for shipping apps with as few files as possible (It goes back to my BeOS days) and constructing secondary files on the fly, so that's what I will do here.

Code:
#!/bin/env yab

restore yoshi
open "cmds.yoshi" for writing as #1
    while(line$ <> "end_of_data")
        read line$
        print #1 line$
    wend
close #1
result$ = system$("yoshi cmds.yoshi")
if instr(result$, "chk=OFF") <> 0 then
    alert "You turned off the checkbox. Thank you for following instructions", "OK", "none"
else
    alert "You did not turn off the checkbox. Try again", "OK", "none"
endif
system("rm -f cmds.yoshi")
end

label yoshi
data "*.title = Example window title"
data "*.bgcolor = 255-,232,181"
data "txt.type = text"
data "txt.default = yoshi in yab demo"
data "txt.lines = 2"
data "txt.align = centered"
data "txt.scrollbar = 1"
data "txt.bg = white"
data "chk.type = checkbox"
data "chk.default = 1"
data "chk.label = Turn off this checkbox, click OK and see what happens"
data "end_of_data"

The INSTR command in yab makes it a lot easier to retrieve yoshi's output than it is in bash. Here, all I wanted to know was whether a specific action had been taken. For more complex yoshi scripts, you will have to use all of yab's string-splitting tricks to parse the output (e.g TOKEN, SPLIT, LEFT$ and RIGHT$).
Reply
#2
bas is a BASIC interpreter available on my repo. So I just had to see how hard it would be to port the above program to this different dialect of the language.

Not hard at all. bas uses SHELL rather than SYSTEM and it has no way of getting data back from a SHELL command. So I had to pipe the results into a separate file and read it back in. Also, RESTORE in bas uses a line number rather than a label, and we have to use Haiku's alerts rather than yab's.

But while the yab example admittedly showed a different way of doing something we could have done in yab itself, we are now doing something that would be impossible in bas if we did not have yoshi. So, if for whatever reason I decided that I wanted my app to be in bas rather than yab, I can do it. Or the other way round.

Code:
#!/bin/env bas

restore 1000
open "cmds.yoshi" for output as #1
    while(line$ <> "end_of_data")
        read line$
        print #1 line$
    wend
close #1
shell "yoshi cmds.yoshi > yoshi.out"
open "yoshi.out" for input as #1
    while not EOF(1)
        line input #1, a$
        result$ = result$ + a$
    wend
close #1

if instr(result$, "chk=OFF") <> 0 then
    shell "alert --empty 'You turned off the checkbox. Thank you for following instructions', OK"
else
    shell "alert --empty 'You did not turn off the checkbox. Try again', OK"
endif
shell "rm -f cmds.yoshi"
shell "rm -f yoshi.out"
end

1000 data "*.title = Example window title"
data "*.bgcolor = 255-,232,181"
data "txt.type = text"
data "txt.default = yoshi in yab demo"
data "txt.lines = 2"
data "txt.align = centered"
data "txt.scrollbar = 1"
data "txt.bg = white"
data "chk.type = checkbox"
data "chk.default = 1"
data "chk.label = Turn off this checkbox, click OK and see what happens"
data "end_of_data"
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)
Free Web Hosting