File size: 5,007 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Management.Automation;
using System.Text;
using Microsoft.PowerShell.Commands.Internal.Format;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// Implementation for the out-string command.
/// </summary>
[Cmdlet(VerbsData.Out, "String", DefaultParameterSetName = "NoNewLineFormatting", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2097024", RemotingCapability = RemotingCapability.None)]
[OutputType(typeof(string))]
public class OutStringCommand : FrontEndCommandBase
{
#region Command Line Parameters
/// <summary>
/// Optional, non positional parameter to specify the streaming behavior.
/// FALSE: accumulate all the data, then write a single string.
/// TRUE: write one line at the time.
/// </summary>
[Parameter(ParameterSetName = "StreamFormatting")]
public SwitchParameter Stream
{
get { return _stream; }
set { _stream = value; }
}
private bool _stream;
/// <summary>
/// Optional, number of columns to use when writing to device.
/// </summary>
[ValidateRange(2, int.MaxValue)]
[Parameter]
public int Width
{
get { return (_width != null) ? _width.Value : 0; }
set { _width = value; }
}
private int? _width = null;
/// <summary>
/// False to add a newline to the end of the output string, true if not.
/// </summary>
[Parameter(ParameterSetName = "NoNewLineFormatting")]
public SwitchParameter NoNewline
{
get { return _noNewLine; }
set { _noNewLine = value; }
}
private bool _noNewLine = false;
#endregion
/// <summary>
/// Initializes a new instance of the <see cref="OutStringCommand"/> class
/// and sets the inner command.
/// </summary>
public OutStringCommand()
{
this.implementation = new OutputManagerInner();
}
/// <summary>
/// Read command line parameters.
/// </summary>
protected override void BeginProcessing()
{
// set up the LineOutput interface
OutputManagerInner outInner = (OutputManagerInner)this.implementation;
outInner.LineOutput = InstantiateLineOutputInterface();
// finally call the base class for general hookup
base.BeginProcessing();
}
/// <summary>
/// One-time initialization: acquire a screen host interface
/// by creating one on top of a stream.
/// </summary>
private LineOutput InstantiateLineOutputInterface()
{
// set up the streaming text writer
StreamingTextWriter.WriteLineCallback callback = new(this.OnWriteLine);
_writer = new StreamingTextWriter(callback, Host.CurrentCulture);
// compute the # of columns available
int computedWidth = int.MaxValue;
if (_width != null)
{
// use the value from the command line
computedWidth = _width.Value;
}
// use it to create and initialize the Line Output writer
TextWriterLineOutput twlo = new(_writer, computedWidth);
// finally have the LineOutput interface extracted
return (LineOutput)twlo;
}
/// <summary>
/// Callback to add lines to the buffer or to write them to the output stream.
/// </summary>
/// <param name="s"></param>
private void OnWriteLine(string s)
{
if (_stream)
{
this.WriteObject(s);
}
else
{
if (_noNewLine)
{
_buffer.Append(s);
}
else
{
_buffer.AppendLine(s);
}
}
}
/// <summary>
/// Execution entry point.
/// </summary>
protected override void ProcessRecord()
{
base.ProcessRecord();
_writer.Flush();
}
/// <summary>
/// Execution entry point.
/// </summary>
protected override void EndProcessing()
{
base.EndProcessing();
// close the writer
_writer.Flush();
_writer.Dispose();
if (!_stream)
this.WriteObject(_buffer.ToString());
}
/// <summary>
/// Writer used by the LineOutput.
/// </summary>
private StreamingTextWriter _writer = null;
/// <summary>
/// Buffer used when buffering until the end.
/// </summary>
private readonly StringBuilder _buffer = new();
}
}
|