forked from PowerShell/PSReadLine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisualEditing.vi.cs
More file actions
105 lines (95 loc) · 3.5 KB
/
Copy pathVisualEditing.vi.cs
File metadata and controls
105 lines (95 loc) · 3.5 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
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System;
using System.Diagnostics;
using System.IO;
using System.Management.Automation;
namespace Microsoft.PowerShell
{
public partial class PSConsoleReadLine
{
/// <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 editor = GetPreferredEditor();
if (string.IsNullOrWhiteSpace(editor))
{
Ding();
return;
}
if (!(_singleton._engineIntrinsics?.InvokeCommand.GetCommand(editor, CommandTypes.Application) is
ApplicationInfo editorCommand))
{
Ding();
return;
}
var tempPowerShellFile = GetTemporaryPowerShellFile();
using (FileStream fs = File.OpenWrite(tempPowerShellFile))
{
using (TextWriter tw = new StreamWriter(fs))
{
tw.Write(_singleton._buffer.ToString());
}
}
editor = editorCommand.Path;
var si = new ProcessStartInfo(editor, $"\"{tempPowerShellFile}\"")
{
UseShellExecute = false,
RedirectStandardError = false,
RedirectStandardInput = false,
RedirectStandardOutput = false
};
var pi = _singleton.CallPossibleExternalApplication(() => Process.Start(si));
if (pi != null)
{
pi.WaitForExit();
InvokePrompt();
_singleton.ProcessViVisualEditing(tempPowerShellFile);
}
else
{
Ding();
}
}
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(string tempFileName)
{
string editedCommand;
using (TextReader tr = File.OpenText(tempFileName))
{
editedCommand = tr.ReadToEnd();
}
File.Delete(tempFileName);
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");
Replace(0, _buffer.Length, editedCommand);
_current = _buffer.Length;
if (_options.EditMode == EditMode.Vi) _current -= 1;
Render();
}
}
private static string GetPreferredEditor()
{
var editor = Environment.GetEnvironmentVariable("VISUAL");
return !string.IsNullOrWhiteSpace(editor)
? editor
: Environment.GetEnvironmentVariable("EDITOR");
}
}
}