capture cli output

Find quick help here to get you started with Hollywood
Post Reply
User avatar
emeck
Posts: 169
Joined: Fri Apr 03, 2015 3:17 pm

capture cli output

Post by emeck »

Hello,

I want to run a cli program from a Hollywood app, and to capture any output from the ran cli program. What I'm doing now is use a temporary file in T:, like this this:

Code: Select all

Local today$

Execute("date > T:today")

todayfile=OpenFile(Nil, "T:today")
today$=ReadLine(todayfile)
CloseFile(todayfile)
DeleteFile("T:today")

moai.Set("second", "text", today$)  ;send the output to the status bar
Ithis the best way or can I capture cli output without the temporary file?
PowerBook 5.2 MorphOS 3.15
PowerBook 5.8 MorphOS 3.15
Amiga 1200 BPPC/BVision AOS4.1 FE
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: capture cli output

Post by airsoftsoftwair »

No, I think what you're doing is the best way to capture CLI output with Hollywood.
User avatar
lazi
Posts: 625
Joined: Thu Feb 24, 2011 11:08 pm

Re: capture cli output

Post by lazi »

There is a way to avoid temp files.
However it has a problem if the executed command does not send anything to the named pipe:

Code: Select all

Execute("date >pipe:day")

todayfile=OpenFile(Nil, "pipe:day")
pos,today$=-1,""
While FilePos(todayfile)<>pos
	pos=FilePos(todayfile)
	today$=today$..ReadLine(todayfile)
Wend
CloseFile(todayfile)

NPrint(today$)
WaitLeftMouse           
User avatar
jPV
Posts: 603
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: capture cli output

Post by jPV »

airsoftsoftwair wrote: Sat Apr 01, 2017 3:47 pm No, I think what you're doing is the best way to capture CLI output with Hollywood.
Would it be possible to implement an event handler that would get triggered always when a program started with the Run() command would output something? RunOutput or such... it would be handy when creating GUIs for shell programs.
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: capture cli output

Post by airsoftsoftwair »

Hmm, maybe ;)
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: capture cli output

Post by airsoftsoftwair »

Unfortunately, this is technically not possible because it would have to be implemented through pipes but when redirecting a program's output to a pipe, the internal buffering routines won't flush on a line break but only when a certain platform-dependent buffer threshold is exceeded (e.g. 4096 bytes). This makes it impossible to capture the output of console programs in realtime. It's a little sad because that certainly would have been a nice feature but AFAICS there's no way to implement it, except maybe on Linux by using pseudoterminals.
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: capture cli output

Post by airsoftsoftwair »

Reflecting on it further, I now think that even though stdio buffering might prohibit real-time output capturing, it's still a good idea to have such a feature because most programs will just continually output text to stdout and this of course can be easily captured. There'll only be problems in case an external program pauses outputting text at some point for whatever reason. In that case, not all output might make its way to Hollywood instantly because of stdio buffering. But it will reach Hollywood as soon as the program continues output to stdout. But since most programs don't behave that way, it's probably not much of an issue, so the feature is now implemented:

Code: Select all

- New: Added "RunOutput" event handler; this can be used to capture the output of programs started using
  Run(); note that output will normally not be delivered in real-time because of stdio buffering but
  if the external program continually outputs text it will arrive pretty instantly at your "RunOutput"
  callback; note that due to OS limitations, this is currently unsupported on AmigaOS 3.x; MorphOS
  users require at least MorphOS 3.12
Note that it's not possible to support it on OS 3.x because the queue handler on OS 3.x is pretty much broken. But all the other Amigaoids will work (MorphOS requires 3.12, though).
User avatar
jPV
Posts: 603
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: capture cli output

Post by jPV »

Great work \o/
User avatar
emeck
Posts: 169
Joined: Fri Apr 03, 2015 3:17 pm

Re: capture cli output

Post by emeck »

Thank you!
PowerBook 5.2 MorphOS 3.15
PowerBook 5.8 MorphOS 3.15
Amiga 1200 BPPC/BVision AOS4.1 FE
Post Reply