Skip to content

Commit bedcfdb

Browse files
author
Dave Wyatt
committed
Minor code cleanup
1 parent ef692c8 commit bedcfdb

3 files changed

Lines changed: 24 additions & 45 deletions

File tree

Commands/Cmdlets.cs

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ public class AddLogFileCommand : PSCmdlet
2121
public string Path
2222
{
2323
get { return _path; }
24-
25-
set
26-
{
27-
_path = System.IO.Path.IsPathRooted(value) ? value : System.IO.Path.Combine(SessionState.Path.CurrentLocation.Path, value);
28-
}
24+
set { _path = System.IO.Path.IsPathRooted(value) ? value : System.IO.Path.Combine(SessionState.Path.CurrentLocation.Path, value); }
2925
}
3026

3127
[Parameter(ParameterSetName = "New")]
@@ -56,8 +52,6 @@ public LogFile InputObject
5652

5753
protected override void EndProcessing()
5854
{
59-
HostIoInterceptor interceptor = HostIoInterceptor.GetInterceptor();
60-
6155
LogFile logFile;
6256

6357
if (ParameterSetName == "New")
@@ -70,7 +64,7 @@ protected override void EndProcessing()
7064
logFile = _inputObject;
7165
}
7266

73-
interceptor.AddSubscriber(logFile);
67+
HostIoInterceptor.GetInterceptor().AddSubscriber(logFile);
7468
}
7569
} // End AddLogFileCommand class
7670

@@ -95,9 +89,7 @@ public string Path
9589

9690
protected override void EndProcessing()
9791
{
98-
HostIoInterceptor interceptor = HostIoInterceptor.GetInterceptor();
99-
100-
foreach (IHostIoSubscriber subscriber in interceptor.Subscribers)
92+
foreach (IHostIoSubscriber subscriber in HostIoInterceptor.GetInterceptor().Subscribers)
10193
{
10294
var logFile = subscriber as LogFile;
10395

@@ -119,8 +111,7 @@ public class DisableLogFileCommand : PSCmdlet
119111

120112
protected override void EndProcessing()
121113
{
122-
HostIoInterceptor interceptor = HostIoInterceptor.GetInterceptor();
123-
interceptor.RemoveSubscriber(InputObject);
114+
HostIoInterceptor.GetInterceptor().RemoveSubscriber(InputObject);
124115
}
125116
} // End DisableLogFileCommand class
126117

@@ -129,8 +120,7 @@ public class SuspendLoggingCommand : PSCmdlet
129120
{
130121
protected override void EndProcessing()
131122
{
132-
HostIoInterceptor interceptor = HostIoInterceptor.GetInterceptor();
133-
interceptor.Paused = true;
123+
HostIoInterceptor.GetInterceptor().Paused = true;
134124
}
135125
} // End SuspendLoggingCommand class
136126

@@ -139,8 +129,7 @@ public class ResumeLoggingCommand : PSCmdlet
139129
{
140130
protected override void EndProcessing()
141131
{
142-
HostIoInterceptor interceptor = HostIoInterceptor.GetInterceptor();
143-
interceptor.Paused = false;
132+
HostIoInterceptor.GetInterceptor().Paused = false;
144133
}
145134
} // End ResumeLoggingCommand class
146135

@@ -204,8 +193,6 @@ public ScriptBlockOutputSubscriber InputObject
204193

205194
protected override void EndProcessing()
206195
{
207-
HostIoInterceptor interceptor = HostIoInterceptor.GetInterceptor();
208-
209196
ScriptBlockOutputSubscriber subscriber;
210197

211198
if (ParameterSetName == "New")
@@ -218,7 +205,7 @@ protected override void EndProcessing()
218205
subscriber = _inputObject;
219206
}
220207

221-
interceptor.AddSubscriber(subscriber);
208+
HostIoInterceptor.GetInterceptor().AddSubscriber(subscriber);
222209
}
223210
} // End AddOutputSubscriberCommand class
224211

@@ -227,9 +214,7 @@ public class GetOutputSubscriberCommand : PSCmdlet
227214
{
228215
protected override void EndProcessing()
229216
{
230-
HostIoInterceptor interceptor = HostIoInterceptor.GetInterceptor();
231-
232-
foreach (IHostIoSubscriber subscriber in interceptor.Subscribers)
217+
foreach (IHostIoSubscriber subscriber in HostIoInterceptor.GetInterceptor().Subscribers)
233218
{
234219
var scriptBlockSubscriber = subscriber as ScriptBlockOutputSubscriber;
235220
if (scriptBlockSubscriber != null) WriteObject(scriptBlockSubscriber);
@@ -247,8 +232,7 @@ public class DisableOutputSubscriberCommand : PSCmdlet
247232

248233
protected override void EndProcessing()
249234
{
250-
HostIoInterceptor interceptor = HostIoInterceptor.GetInterceptor();
251-
interceptor.RemoveSubscriber(InputObject);
235+
HostIoInterceptor.GetInterceptor().RemoveSubscriber(InputObject);
252236
}
253237
} // End DisableOutputSubscriberCommand class
254238
}

HostIOInterceptor.cs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ public static HostIoInterceptor GetInterceptor()
3232
#region Fields
3333

3434
private PSHostUserInterface _psInterface;
35-
3635
private readonly List<WeakReference> _subscribers;
3736
private readonly StringBuilder _writeCache;
38-
3937
private bool _paused;
4038

4139
#endregion
@@ -47,6 +45,7 @@ private HostIoInterceptor()
4745
_psInterface = null;
4846
_subscribers = new List<WeakReference>();
4947
_writeCache = new StringBuilder();
48+
_paused = false;
5049
}
5150

5251
#endregion
@@ -59,8 +58,7 @@ private void SendToSubscribers(string methodName, params object[] args)
5958
// references in to this method. It uses Reflection to invoke the methods on
6059
// subscriber objects because I'm not sure how to accomplish the same thing using
6160
// delegates (assuming it is even possible), when the target methods have different
62-
// signatures, and the caller doesn't know which objects the target methods will be
63-
// invoked on, if any.
61+
// signatures.
6462

6563
if (_paused) return;
6664

@@ -121,10 +119,7 @@ public IEnumerable<IHostIoSubscriber> Subscribers
121119

122120
public override PSHostRawUserInterface RawUI
123121
{
124-
get
125-
{
126-
return _psInterface.RawUI;
127-
}
122+
get { return _psInterface.RawUI; }
128123
}
129124

130125
#endregion
@@ -163,9 +158,9 @@ public override Dictionary<string, PSObject> Prompt(
163158
string caption, string message, Collection<FieldDescription> descriptions)
164159
{
165160
if (_psInterface == null) throw new InvalidOperationException();
166-
161+
167162
var result = _psInterface.Prompt(caption, message, descriptions);
168-
163+
169164
SendToSubscribers("Prompt", result);
170165

171166
return result;
@@ -227,14 +222,14 @@ public override string ReadLine()
227222
public override SecureString ReadLineAsSecureString()
228223
{
229224
if (_psInterface == null) throw new InvalidOperationException();
230-
225+
231226
return _psInterface.ReadLineAsSecureString();
232227
}
233228

234229
public override void Write(string value)
235230
{
236231
if (_psInterface == null) throw new InvalidOperationException();
237-
232+
238233
_psInterface.Write(value);
239234

240235
if (!_paused)
@@ -246,7 +241,7 @@ public override void Write(string value)
246241
public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
247242
{
248243
if (_psInterface == null) throw new InvalidOperationException();
249-
244+
250245
_psInterface.Write(foregroundColor, backgroundColor, value);
251246

252247
if (!_paused)
@@ -258,8 +253,8 @@ public override void Write(ConsoleColor foregroundColor, ConsoleColor background
258253
public override void WriteDebugLine(string message)
259254
{
260255
if (_psInterface == null) throw new InvalidOperationException();
261-
string[] lines = message.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
262256

257+
string[] lines = message.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
263258
foreach (string line in lines)
264259
{
265260
SendToSubscribers("WriteDebug", line.TrimEnd() + "\r\n");
@@ -284,7 +279,7 @@ public override void WriteErrorLine(string message)
284279
public override void WriteLine()
285280
{
286281
if (_psInterface == null) throw new InvalidOperationException();
287-
282+
288283
string[] lines = _writeCache.ToString().Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
289284
foreach (string line in lines)
290285
{

ScriptBlockOutputSubscriber.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,27 @@ public ScriptBlockOutputSubscriber()
3333

3434
public override void WriteDebug(string message)
3535
{
36-
if (OnWriteDebug != null) OnWriteDebug.Invoke(new object[] { message });
36+
if (OnWriteDebug != null) OnWriteDebug.Invoke(message);
3737
}
3838

3939
public override void WriteOutput(string message)
4040
{
41-
if (OnWriteOutput != null) OnWriteOutput.Invoke(new object[] { message });
41+
if (OnWriteOutput != null) OnWriteOutput.Invoke(message);
4242
}
4343

4444
public override void WriteError(string message)
4545
{
46-
if (OnWriteError != null) OnWriteError.Invoke(new object[] { message });
46+
if (OnWriteError != null) OnWriteError.Invoke(message);
4747
}
4848

4949
public override void WriteVerbose(string message)
5050
{
51-
if (OnWriteVerbose != null) OnWriteVerbose.Invoke(new object[] { message });
51+
if (OnWriteVerbose != null) OnWriteVerbose.Invoke(message);
5252
}
5353

5454
public override void WriteWarning(string message)
5555
{
56-
if (OnWriteWarning != null) OnWriteWarning.Invoke(new object[] { message });
56+
if (OnWriteWarning != null) OnWriteWarning.Invoke(message);
5757
}
5858
}
5959
}

0 commit comments

Comments
 (0)