https://www.reddit.com/r/PowerSh ... _hidden/?tl=zh-hant
我在上面贴子发现了下面的代码, 原来powershell ISE是可以自定义的,
现在我有一个功能需求, 就是在按下F5后, 先执行一下cls把输出窗口中的已前的输出先清理一下, 怎样修改呢?- Add-Type -TypeDefinition @"
- using System;
- using System.Windows.Threading;
- using System.Reflection;
- namespace ISECustomization
- {
- public class EditorCustomization
- {
- private MethodInfo methodToInvoke;
- private object editorOptions;
- private object[] optionsToSet;
- public static void SetEditorOptions(object editor, object[] optionsToSet)
- {
- BindingFlags nonPublicFlags = BindingFlags.NonPublic | BindingFlags.Instance;
- Type editorType = editor.GetType();
- object editorOperations = editorType.GetProperty("EditorOperations", nonPublicFlags).GetMethod.Invoke(editor,null);
- object editorOptions = editorOperations.GetType().GetProperty("Options").GetValue(editorOperations);
- object editorViewHost = editorType.GetProperty("EditorViewHost",nonPublicFlags).GetMethod.Invoke(editor,null);
- object dispatcher = editorViewHost.GetType().GetProperty("Dispatcher").GetValue(editorViewHost);
- var setterInstance = new EditorCustomization()
- {
- methodToInvoke = editorOptions.GetType().GetMethod("SetOptionValue", new Type[] {typeof(string), typeof(object) }),
- editorOptions = editorOptions,
- optionsToSet = optionsToSet
- };
- DispatcherFrame frame = new DispatcherFrame();
- (dispatcher as Dispatcher).BeginInvoke(DispatcherPriority.Normal, new DispatcherOperationCallback(setterInstance.ExitFrames),frame);
- Dispatcher.PushFrame(frame);
- }
- private object ExitFrames(object f)
- {
- DispatcherFrame df = ((DispatcherFrame)f);
- foreach (object[] args in optionsToSet)
- {
- methodToInvoke.Invoke(editorOptions, args);
- }
- df.Continue = false;
- return null;
- }
- }
- }
- "@ -ReferencedAssemblies windowsbase
- $Settings=@(
- ,@('Adornments/HighlightCurrentLine/Enable',$true)
- ,@('TextViewHost/ChangeTracking',$true)
- ,@('TextView/WordWrapStyle',[Microsoft.VisualStudio.Text.Editor.WordWrapStyles]7)
- ,@('TextView/UseVisibleWhitespace',$true)
- ,@('TextView/UseVirtualSpace',$true)
- )
- [ISECustomization.EditorCustomization]::SetEditorOptions($psISE.CurrentFile.Editor,$Settings)
复制代码 |