forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisualEditing.vi.cs
More file actions
111 lines (97 loc) · 3.67 KB
/
VisualEditing.vi.cs
File metadata and controls
111 lines (97 loc) · 3.67 KB
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
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Microsoft.PowerShell
{
public partial class PSConsoleReadLine
{
private string _visualEditTemporaryFilename = null;
private Func<string, bool> _savedAddToHistoryHandler = null;
/// <summary>
/// Edit the command line in a text editor specified by $env:EDITOR or $env:VISUAL
/// </summary>
public static void ViEditVisually(ConsoleKeyInfo? key = null, object arg = null)
{
string editorOfChoice = GetPreferredEditor();
if (string.IsNullOrWhiteSpace(editorOfChoice))
{
Ding();
return;
}
_singleton._visualEditTemporaryFilename = GetTemporaryPowerShellFile();
using (FileStream fs = File.OpenWrite(_singleton._visualEditTemporaryFilename))
{
using (TextWriter tw = new StreamWriter(fs))
{
tw.Write(_singleton._buffer.ToString());
}
}
_singleton._savedAddToHistoryHandler = _singleton.Options.AddToHistoryHandler;
_singleton.Options.AddToHistoryHandler = ((string s) =>
{
return false;
});
_singleton._buffer.Clear();
_singleton._current = 0;
_singleton.Render();
_singleton._buffer.Append(editorOfChoice + " \'" + _singleton._visualEditTemporaryFilename + "\'");
AcceptLine();
}
private static string GetTemporaryPowerShellFile()
{
string filename;
do
{
filename = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".ps1");
} while (File.Exists(filename) || Directory.Exists(filename));
return filename;
}
private void ProcessViVisualEditing()
{
if (_visualEditTemporaryFilename == null)
{
return;
}
Options.AddToHistoryHandler = _savedAddToHistoryHandler;
_savedAddToHistoryHandler = null;
string editedCommand = null;
using (TextReader tr = File.OpenText(_visualEditTemporaryFilename))
{
editedCommand = tr.ReadToEnd();
}
File.Delete(_visualEditTemporaryFilename);
_visualEditTemporaryFilename = null;
if (!string.IsNullOrWhiteSpace(editedCommand))
{
while (editedCommand.Length > 0 && char.IsWhiteSpace(editedCommand[editedCommand.Length - 1]))
{
editedCommand = editedCommand.Substring(0, editedCommand.Length - 1);
}
editedCommand = editedCommand.Replace(Environment.NewLine, "\n");
_buffer.Clear();
_buffer.Append(editedCommand);
_current = _buffer.Length - 1;
Render();
//_queuedKeys.Enqueue(Keys.Enter);
}
}
private static string GetPreferredEditor()
{
string[] names = {"VISUAL", "EDITOR"};
foreach (string name in names)
{
string editor = Environment.GetEnvironmentVariable(name);
if (!string.IsNullOrWhiteSpace(editor))
{
return editor;
}
}
return null;
}
}
}