Example #1
0
        /// <summary>
        ///
        /// Outputs the object to the host console, with optional newline
        ///
        /// </summary>
        protected override void ProcessRecord()
        {
            string result = ProcessObject(Object) ?? "";

            HostInformationMessage informationMessage = new HostInformationMessage();
            informationMessage.Message = result;
            informationMessage.NoNewLine = NoNewline.IsPresent;

            try
            {
                informationMessage.ForegroundColor = ForegroundColor;
                informationMessage.BackgroundColor = BackgroundColor;
            }
            catch (System.Management.Automation.Host.HostException)
            {
                // Expected if the host is not interactive, or doesn't have Foreground / Background
                // colours.
            }

            this.WriteInformation(informationMessage, new string[] { "PSHOST" });
            this.Host.UI.TranscribeResult(result);
        }
        public void WriteJsonMessage(JsonMessage message)
        {
            if (message.Error != null)
            {
                var error = new ErrorRecord(new Exception(message.Error.Message), null, ErrorCategory.OperationStopped, null);
                _cmdlet.WriteError(error);
            }
            else if (message.Progress != null)
            {
                var id = message.ID ?? "";
                int activity;
                if (!_idToActivity.TryGetValue(id, out activity))
                {
                    activity = _nextActivity;
                    _nextActivity++;
                    _idToActivity.Add(id, activity);
                }

                var activityName = new StringBuilder(id);
                if (activityName.Length == 0)
                {
                    activityName.Append("Operation");
                }

                if (message.From != null)
                {
                    activityName.AppendFormat("(from {0})", message.From);
                }

                var record = new ProgressRecord(activity, activityName.ToString(), message.Status ?? "Processing");

                var progress = message.Progress;
                if (progress.Total > 0)
                {
                    record.PercentComplete = (int)(progress.Current * 100 / progress.Total);
                }

                if (progress.Current > 0)
                {
                    record.CurrentOperation = string.Format(" ({0} bytes)", progress.Current);
                }

                _cmdlet.WriteProgress(record);
            }
            else
            {
                var info = new StringBuilder();
                if (message.ID != null)
                {
                    info.Append(message.ID);
                    info.Append(": ");
                }

                if (message.From != null)
                {
                    info.AppendFormat("(from {0})", message.From);
                }

                var infoRecord = new HostInformationMessage();
                if (message.Stream != null)
                {
                    info.Append(message.Stream);
                    infoRecord.NoNewLine = true;
                }
                else
                {
                    info.Append(message.Status);
                }

                infoRecord.Message = info.ToString();
                _cmdlet.WriteInformation(infoRecord, new string[] { "PSHOST" });
            }
        }