Example #1
0
        public IDisposable Subscribe(IObserver <LineOut> observerLines, IObserver <CharactersOut> observerCharacters)
        {
            var published          = OutStream.Publish();
            var observeLinesFilter = StartArguments.LineOutFilter ?? (l => true);

            if (observerCharacters != null)
            {
                published.Subscribe(observerCharacters);
            }

            var boundaries = published
                             .Where(o => o.EndsWithNewLine || o.StartsWithCarriage || BufferBoundary(_bufferStdOutRemainder, _bufferStdErrRemainder));
            var buffered = published.Buffer(boundaries);
            var newlines = buffered
                           .Select(c =>
            {
                if (c.Count == 0)
                {
                    return(null);
                }
                var line = new string(c.SelectMany(o => o.Characters).ToArray());
                return(new LineOut(c.First().Error, line.TrimEnd(NewlineChars)));
            })
                           .TakeWhile(KeepBufferingLines)
                           .Where(l => l != null)
                           .Where(observeLinesFilter)
                           .Subscribe(
                observerLines.OnNext,
                e =>
            {
                observerLines.OnError(e);
                SetCompletedHandle();
            },
                () =>
            {
                observerLines.OnCompleted();
                SetCompletedHandle();
            });
            var connected = published.Connect();

            return(new CompositeDisposable(newlines, connected));
        }