1

I can use NSTask to grab the output of stdout, create a string and put it into an NSTextField without issue, although how would I make the output behave like print "\r$i"?

This perl script demonstrates the output:

$|++;

for ($i=0; $i<1000000; $i++) {
    print "\r$i";
}
print "\n";

Is it possible to take stdout from an NSTask and output in Objective-C like this?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • You could use `scanf()` in a loop. `scanf("%d", &i);`. The form of the input would have to be very predictable. – ryyker Jul 28 '14 at 00:47
  • @ryyker, I would be worried about a buffer overflow with `scanf()` possibly. Does `fgets` exist in objective-c? – Richard Hoffman Jul 28 '14 at 00:57
  • It appears to be: ***[see here](http://stackoverflow.com/q/4208552/645128)***. – ryyker Jul 28 '14 at 01:01
  • Provide an example of a few lines of the input you are getting. fgets in a loop is sometimes used to read from stdin – ryyker Jul 28 '14 at 01:07
  • @ryyker, Interesting, thanks for that. An example would be any console app that overwrites it's output on the same line. So if you had "Hello", the next line would overwrite it with "World" (using \r instead of \n). Still not clear on how to take the stdout and loop it though as you mentioned to an `NSTextField` so any new output "overwrites" the same line. – Richard Hoffman Jul 28 '14 at 01:08
  • I think I misunderstood the real question. Let me try again: I think what you are asking is How do you 1) clear the terminal, 2) place the cursor position to the top left corner of the terminal, and 3) start writing your next line. In short, controlling the position of the terminal cursor. If this is correct, then have you looked at ***[ncurses](http://www.gnu.org/software/ncurses/)***? (not the easy answer, but most often referenced for cursor control questions by others) – ryyker Jul 28 '14 at 02:39
  • @ryyker, Yes precisely. I realize it's not "overwriting" the line but more a matter of manipulating the cursor in a tty session. So the question is can that behavior be called upon in a Cocoa View? I installed ncurses, although I'm needing to have a native solution - as not everyone would be willing or capable to compile and install it. – Richard Hoffman Jul 29 '14 at 00:19

0 Answers0