|
|
|
|
|
|
|
|
using System; |
|
|
using System.Collections.Generic; |
|
|
using System.Linq; |
|
|
using System.Text; |
|
|
using System.IO; |
|
|
using System.Threading; |
|
|
using Microsoft.Scripting.Hosting.Shell; |
|
|
using System.Windows.Input; |
|
|
using Microsoft.Scripting; |
|
|
using IronPython.Hosting; |
|
|
using IronPython.Runtime; |
|
|
using Microsoft.Scripting.Hosting; |
|
|
using Microsoft.Scripting.Hosting.Providers; |
|
|
using System.Diagnostics; |
|
|
using System.Globalization; |
|
|
using System.Runtime.InteropServices; |
|
|
using System.Threading.Tasks; |
|
|
using System.Windows; |
|
|
using System.Windows.Threading; |
|
|
using System.Windows.Documents; |
|
|
using ICSharpCode.AvalonEdit.Editing; |
|
|
using ICSharpCode.AvalonEdit.Document; |
|
|
using ICSharpCode.AvalonEdit.Highlighting; |
|
|
using ICSharpCode.AvalonEdit.Utils; |
|
|
using Style = Microsoft.Scripting.Hosting.Shell.Style; |
|
|
|
|
|
public class TextEventArgs : EventArgs |
|
|
{ |
|
|
public TextEventArgs(string text) |
|
|
{ |
|
|
Text = text; |
|
|
} |
|
|
|
|
|
public string Text { get; } |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
namespace PythonConsoleControl |
|
|
{ |
|
|
public delegate void ConsoleInitializedEventHandler(object sender, EventArgs e); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class PythonConsole : IConsole, IDisposable |
|
|
{ |
|
|
bool allowFullAutocompletion = true; |
|
|
public bool AllowFullAutocompletion |
|
|
{ |
|
|
get { return allowFullAutocompletion; } |
|
|
set { allowFullAutocompletion = value; } |
|
|
} |
|
|
|
|
|
bool disableAutocompletionForCallables = true; |
|
|
public bool DisableAutocompletionForCallables |
|
|
{ |
|
|
get { return disableAutocompletionForCallables; } |
|
|
set |
|
|
{ |
|
|
if (textEditor.CompletionProvider != null) textEditor.CompletionProvider.ExcludeCallables = value; |
|
|
disableAutocompletionForCallables = value; |
|
|
} |
|
|
} |
|
|
|
|
|
bool allowCtrlSpaceAutocompletion = false; |
|
|
public bool AllowCtrlSpaceAutocompletion |
|
|
{ |
|
|
get { return allowCtrlSpaceAutocompletion; } |
|
|
set { allowCtrlSpaceAutocompletion = value; } |
|
|
} |
|
|
|
|
|
public Action<ScriptScope> UpdateVariables { get; set; } |
|
|
|
|
|
PythonTextEditor textEditor; |
|
|
int lineReceivedEventIndex = 0; |
|
|
ManualResetEvent lineReceivedEvent = new ManualResetEvent(false); |
|
|
ManualResetEvent disposedEvent = new ManualResetEvent(false); |
|
|
AutoResetEvent statementsExecutionRequestedEvent = new AutoResetEvent(false); |
|
|
WaitHandle[] waitHandles; |
|
|
int promptLength = 4; |
|
|
List<string> previousLines = new List<string>(); |
|
|
CommandLine commandLine; |
|
|
CommandLineHistory commandLineHistory = new CommandLineHistory(); |
|
|
|
|
|
volatile bool _executing = false; |
|
|
|
|
|
|
|
|
Thread dispatcherThread; |
|
|
Window dispatcherWindow; |
|
|
Dispatcher dispatcher; |
|
|
|
|
|
bool consoleInitialized = false; |
|
|
string prompt; |
|
|
private DateTime _lastWrite; |
|
|
private Style _lastStyle; |
|
|
|
|
|
public event ConsoleInitializedEventHandler ConsoleInitialized; |
|
|
public event EventHandler<EventArgs> ScriptStarting; |
|
|
public event EventHandler<EventArgs> ScriptFinished; |
|
|
public event EventHandler<TextEventArgs> Error; |
|
|
|
|
|
public ScriptScope ScriptScope |
|
|
{ |
|
|
get { return commandLine.ScriptScope; } |
|
|
} |
|
|
|
|
|
public PythonConsole(PythonTextEditor textEditor, CommandLine commandLine) |
|
|
{ |
|
|
waitHandles = new WaitHandle[] { lineReceivedEvent, disposedEvent }; |
|
|
|
|
|
this.commandLine = commandLine; |
|
|
this.textEditor = textEditor; |
|
|
textEditor.CompletionProvider = new PythonConsoleCompletionDataProvider(commandLine) { ExcludeCallables = disableAutocompletionForCallables }; |
|
|
textEditor.PreviewKeyDown += textEditor_PreviewKeyDown; |
|
|
textEditor.TextEntering += textEditor_TextEntering; |
|
|
dispatcherThread = new Thread(new ThreadStart(DispatcherThreadStartingPoint)); |
|
|
dispatcherThread.SetApartmentState(ApartmentState.STA); |
|
|
dispatcherThread.IsBackground = true; |
|
|
dispatcherThread.Start(); |
|
|
|
|
|
|
|
|
prompt = ">>> "; |
|
|
|
|
|
|
|
|
this.textEditor.textArea.Dispatcher.Invoke(new Action(delegate() |
|
|
{ |
|
|
CommandBinding pasteBinding = null; |
|
|
CommandBinding copyBinding = null; |
|
|
CommandBinding cutBinding = null; |
|
|
CommandBinding undoBinding = null; |
|
|
CommandBinding deleteBinding = null; |
|
|
foreach (CommandBinding commandBinding in (this.textEditor.textArea.CommandBindings)) |
|
|
{ |
|
|
if (commandBinding.Command == ApplicationCommands.Paste) pasteBinding = commandBinding; |
|
|
if (commandBinding.Command == ApplicationCommands.Copy) copyBinding = commandBinding; |
|
|
if (commandBinding.Command == ApplicationCommands.Cut) cutBinding = commandBinding; |
|
|
if (commandBinding.Command == ApplicationCommands.Undo) undoBinding = commandBinding; |
|
|
if (commandBinding.Command == ApplicationCommands.Delete) deleteBinding = commandBinding; |
|
|
} |
|
|
|
|
|
|
|
|
if (pasteBinding != null) this.textEditor.textArea.CommandBindings.Remove(pasteBinding); |
|
|
if (copyBinding != null) this.textEditor.textArea.CommandBindings.Remove(copyBinding); |
|
|
if (cutBinding != null) this.textEditor.textArea.CommandBindings.Remove(cutBinding); |
|
|
if (undoBinding != null) this.textEditor.textArea.CommandBindings.Remove(undoBinding); |
|
|
if (deleteBinding != null) this.textEditor.textArea.CommandBindings.Remove(deleteBinding); |
|
|
this.textEditor.textArea.CommandBindings.Add(new CommandBinding(ApplicationCommands.Paste, OnPaste, CanPaste)); |
|
|
this.textEditor.textArea.CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy, OnCopy, PythonEditingCommandHandler.CanCutOrCopy)); |
|
|
this.textEditor.textArea.CommandBindings.Add(new CommandBinding(ApplicationCommands.Cut, PythonEditingCommandHandler.OnCut, CanCut)); |
|
|
this.textEditor.textArea.CommandBindings.Add(new CommandBinding(ApplicationCommands.Undo, OnUndo, CanUndo)); |
|
|
this.textEditor.textArea.CommandBindings.Add(new CommandBinding(ApplicationCommands.Delete, PythonEditingCommandHandler.OnDelete(ApplicationCommands.NotACommand), CanDeleteCommand)); |
|
|
|
|
|
})); |
|
|
CodeContext codeContext = DefaultContext.Default; |
|
|
|
|
|
ClrModule.SetCommandDispatcher(codeContext, DispatchCommand); |
|
|
} |
|
|
|
|
|
protected void DispatchCommand(Delegate command) |
|
|
{ |
|
|
if (command != null) |
|
|
{ |
|
|
|
|
|
Executing = true; |
|
|
var operation = dispatcher.BeginInvoke(DispatcherPriority.Normal, command); |
|
|
while (Executing) |
|
|
{ |
|
|
if (operation.Status != DispatcherOperationStatus.Completed) |
|
|
operation.Wait(TimeSpan.FromSeconds(1)); |
|
|
if (operation.Status == DispatcherOperationStatus.Completed) |
|
|
Executing = false; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
private void DispatcherThreadStartingPoint() |
|
|
{ |
|
|
dispatcherWindow = new Window(); |
|
|
dispatcher = dispatcherWindow.Dispatcher; |
|
|
while (true) |
|
|
{ |
|
|
try |
|
|
{ |
|
|
System.Windows.Threading.Dispatcher.Run(); |
|
|
} |
|
|
catch (ThreadAbortException tae) |
|
|
{ |
|
|
if (tae.ExceptionState is Microsoft.Scripting.KeyboardInterruptException) |
|
|
{ |
|
|
Thread.ResetAbort(); |
|
|
Executing = false; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
public void SetDispatcher(Dispatcher dispatcher) |
|
|
{ |
|
|
this.dispatcher = dispatcher; |
|
|
} |
|
|
|
|
|
public void Dispose() |
|
|
{ |
|
|
disposedEvent.Set(); |
|
|
textEditor.PreviewKeyDown -= textEditor_PreviewKeyDown; |
|
|
textEditor.TextEntering -= textEditor_TextEntering; |
|
|
} |
|
|
|
|
|
public TextWriter Output |
|
|
{ |
|
|
get { return null; } |
|
|
set { } |
|
|
} |
|
|
|
|
|
public TextWriter ErrorOutput |
|
|
{ |
|
|
get { return null; } |
|
|
set { } |
|
|
} |
|
|
|
|
|
#region CommandHandling |
|
|
protected void CanPaste(object target, CanExecuteRoutedEventArgs args) |
|
|
{ |
|
|
if (IsInReadOnlyRegion) |
|
|
{ |
|
|
args.CanExecute = false; |
|
|
} |
|
|
else |
|
|
args.CanExecute = true; |
|
|
} |
|
|
|
|
|
protected void CanCut(object target, CanExecuteRoutedEventArgs args) |
|
|
{ |
|
|
if (!CanDelete) |
|
|
{ |
|
|
args.CanExecute = false; |
|
|
} |
|
|
else |
|
|
PythonEditingCommandHandler.CanCutOrCopy(target, args); |
|
|
} |
|
|
|
|
|
protected void CanDeleteCommand(object target, CanExecuteRoutedEventArgs args) |
|
|
{ |
|
|
if (!CanDelete) |
|
|
{ |
|
|
args.CanExecute = false; |
|
|
} |
|
|
else |
|
|
PythonEditingCommandHandler.CanDelete(target, args); |
|
|
} |
|
|
|
|
|
protected void CanUndo(object target, CanExecuteRoutedEventArgs args) |
|
|
{ |
|
|
args.CanExecute = false; |
|
|
} |
|
|
|
|
|
protected void OnPaste(object target, ExecutedRoutedEventArgs args) |
|
|
{ |
|
|
if (target != textEditor.textArea) return; |
|
|
TextArea textArea = textEditor.textArea; |
|
|
if (textArea != null && textArea.Document != null) |
|
|
{ |
|
|
Debug.WriteLine(Clipboard.GetText(TextDataFormat.Html)); |
|
|
|
|
|
|
|
|
string newLine = TextUtilities.GetNewLineFromDocument(textArea.Document, textArea.Caret.Line); |
|
|
string text = TextUtilities.NormalizeNewLines(Clipboard.GetText(), newLine); |
|
|
string[] commands = text.Split(new String[] {newLine}, StringSplitOptions.None); |
|
|
string scriptText = ""; |
|
|
if (commands.Length > 1) |
|
|
{ |
|
|
text = newLine; |
|
|
foreach (string command in commands) |
|
|
{ |
|
|
text += "... " + command + newLine; |
|
|
scriptText += command.Replace("\t", " ") + newLine; |
|
|
} |
|
|
} |
|
|
|
|
|
if (!string.IsNullOrEmpty(text)) |
|
|
{ |
|
|
bool fullLine = textArea.Options.CutCopyWholeLine && Clipboard.ContainsData(LineSelectedType); |
|
|
bool rectangular = Clipboard.ContainsData(RectangleSelection.RectangularSelectionDataType); |
|
|
if (fullLine) |
|
|
{ |
|
|
DocumentLine currentLine = textArea.Document.GetLineByNumber(textArea.Caret.Line); |
|
|
if (textArea.ReadOnlySectionProvider.CanInsert(currentLine.Offset)) |
|
|
{ |
|
|
textArea.Document.Insert(currentLine.Offset, text); |
|
|
} |
|
|
} |
|
|
else if (rectangular && textArea.Selection.IsEmpty) |
|
|
{ |
|
|
if (!RectangleSelection.PerformRectangularPaste(textArea, textArea.Caret.Position, text, false)) |
|
|
textEditor.Write(text, false); |
|
|
} |
|
|
else |
|
|
{ |
|
|
textEditor.Write(text, false); |
|
|
} |
|
|
} |
|
|
textArea.Caret.BringCaretToView(); |
|
|
args.Handled = true; |
|
|
|
|
|
if (commands.Length > 1) |
|
|
{ |
|
|
dispatcherWindow.Dispatcher.BeginInvoke(new Action(delegate() { ExecuteStatements(scriptText); })); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
protected void OnCopy(object target, ExecutedRoutedEventArgs args) |
|
|
{ |
|
|
if (target != textEditor.textArea) return; |
|
|
if (textEditor.SelectionLength == 0 && Executing) |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
MoveToHomePosition(); |
|
|
|
|
|
|
|
|
|
|
|
AbortRunningScript(); |
|
|
args.Handled = true; |
|
|
} |
|
|
else PythonEditingCommandHandler.OnCopy(target, args); |
|
|
} |
|
|
|
|
|
public void AbortRunningScript() |
|
|
{ |
|
|
if (Executing) |
|
|
dispatcherThread.Abort(new KeyboardInterruptException("")); |
|
|
} |
|
|
|
|
|
const string LineSelectedType = "MSDEVLineSelect"; |
|
|
|
|
|
protected void OnUndo(object target, ExecutedRoutedEventArgs args) |
|
|
{ |
|
|
} |
|
|
#endregion |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void RunStatements(string statements) |
|
|
{ |
|
|
MoveToHomePosition(); |
|
|
|
|
|
dispatcher.BeginInvoke(new Action(delegate() { ExecuteStatements(statements); })); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void RunStatementsSync(string statements) |
|
|
{ |
|
|
MoveToHomePosition(); |
|
|
|
|
|
using (var evt = new AutoResetEvent(false)) |
|
|
{ |
|
|
|
|
|
|
|
|
dispatcher.BeginInvoke(new Action(delegate() |
|
|
{ |
|
|
try |
|
|
{ |
|
|
ExecuteStatements(statements); |
|
|
} |
|
|
finally |
|
|
{ |
|
|
evt.Set(); |
|
|
} |
|
|
})); |
|
|
|
|
|
while (true) |
|
|
{ |
|
|
if (evt.WaitOne(1)) |
|
|
break; |
|
|
|
|
|
System.Windows.Forms.Application.DoEvents(); |
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ExecuteStatements(string scriptText) |
|
|
{ |
|
|
UpdateVariables?.Invoke(commandLine.ScriptScope); |
|
|
lock (scriptText) |
|
|
{ |
|
|
textEditor.Write("\r\n"); |
|
|
ScriptSource scriptSource = commandLine.ScriptScope.Engine.CreateScriptSourceFromString(scriptText, SourceCodeKind.Statements); |
|
|
string error = ""; |
|
|
try |
|
|
{ |
|
|
Executing = true; |
|
|
scriptSource.Execute(commandLine.ScriptScope); |
|
|
} |
|
|
catch (ThreadAbortException tae) |
|
|
{ |
|
|
if (tae.ExceptionState is Microsoft.Scripting.KeyboardInterruptException) Thread.ResetAbort(); |
|
|
error = "KeyboardInterrupt" + System.Environment.NewLine; |
|
|
} |
|
|
catch (Microsoft.Scripting.SyntaxErrorException exception) |
|
|
{ |
|
|
ExceptionOperations eo; |
|
|
eo = commandLine.ScriptScope.Engine.GetService<ExceptionOperations>(); |
|
|
error = eo.FormatException(exception); |
|
|
} |
|
|
catch (Exception exception) |
|
|
{ |
|
|
ExceptionOperations eo; |
|
|
eo = commandLine.ScriptScope.Engine.GetService<ExceptionOperations>(); |
|
|
error = eo.FormatException(exception) + System.Environment.NewLine; |
|
|
} |
|
|
Executing = false; |
|
|
if (error != "") |
|
|
{ |
|
|
textEditor.Write(error); |
|
|
OnError(new TextEventArgs(error)); |
|
|
} |
|
|
textEditor.Write(prompt); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public string ReadLine(int autoIndentSize) |
|
|
{ |
|
|
string indent = String.Empty; |
|
|
if (autoIndentSize > 0) |
|
|
{ |
|
|
indent = String.Empty.PadLeft(autoIndentSize); |
|
|
Write(indent, Style.Prompt); |
|
|
} |
|
|
|
|
|
string line = ReadLineFromTextEditor(); |
|
|
if (line != null) |
|
|
{ |
|
|
return indent + line; |
|
|
} |
|
|
return null; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Write(string text, Style style) |
|
|
{ |
|
|
textEditor.Write(text); |
|
|
if (style == Style.Prompt) |
|
|
{ |
|
|
promptLength = text.Length; |
|
|
if (!consoleInitialized) |
|
|
{ |
|
|
consoleInitialized = true; |
|
|
if (ConsoleInitialized != null) ConsoleInitialized(this, EventArgs.Empty); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
_lastStyle = style; |
|
|
} |
|
|
|
|
|
private void AddPrompt() |
|
|
{ |
|
|
if ((DateTime.UtcNow - _lastWrite).TotalSeconds > 1) |
|
|
{ |
|
|
|
|
|
_lastWrite = DateTime.UtcNow; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void WriteLine(string text, Style style) |
|
|
{ |
|
|
Write(text + Environment.NewLine, style); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void WriteLine() |
|
|
{ |
|
|
Write(Environment.NewLine, Style.Out); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool IsLineAvailable |
|
|
{ |
|
|
get |
|
|
{ |
|
|
lock (previousLines) |
|
|
{ |
|
|
return previousLines.Count > 0; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public string GetCurrentLine() |
|
|
{ |
|
|
string fullLine = GetLastTextEditorLine(); |
|
|
return fullLine.Substring(promptLength); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public string[] GetUnreadLines() |
|
|
{ |
|
|
return previousLines.ToArray(); |
|
|
} |
|
|
|
|
|
string GetLastTextEditorLine() |
|
|
{ |
|
|
return textEditor.GetLine(textEditor.TotalLines - 1); |
|
|
} |
|
|
|
|
|
string ReadLineFromTextEditor() |
|
|
{ |
|
|
int result = WaitHandle.WaitAny(waitHandles); |
|
|
if (result == lineReceivedEventIndex) |
|
|
{ |
|
|
lock (previousLines) |
|
|
{ |
|
|
string line = previousLines[0]; |
|
|
previousLines.RemoveAt(0); |
|
|
if (previousLines.Count == 0) |
|
|
{ |
|
|
lineReceivedEvent.Reset(); |
|
|
} |
|
|
return line; |
|
|
} |
|
|
} |
|
|
return null; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void textEditor_PreviewKeyDown(object sender, KeyEventArgs e) |
|
|
{ |
|
|
switch (e.Key) |
|
|
{ |
|
|
case Key.Delete: |
|
|
if (!CanDelete) e.Handled = true; |
|
|
return; |
|
|
case Key.Tab: |
|
|
if (IsInReadOnlyRegion) e.Handled = true; |
|
|
return; |
|
|
case Key.Back: |
|
|
if (!CanBackspace) e.Handled = true; |
|
|
return; |
|
|
case Key.Home: |
|
|
MoveToHomePosition(); |
|
|
e.Handled = true; |
|
|
return; |
|
|
case Key.Down: |
|
|
if (!IsInReadOnlyRegion) MoveToNextCommandLine(); |
|
|
e.Handled = true; |
|
|
return; |
|
|
case Key.Up: |
|
|
if (!IsInReadOnlyRegion) MoveToPreviousCommandLine(); |
|
|
e.Handled = true; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void textEditor_TextEntering(object sender, TextCompositionEventArgs e) |
|
|
{ |
|
|
if (e.Text.Length > 0) |
|
|
{ |
|
|
if (!char.IsLetterOrDigit(e.Text[0])) |
|
|
{ |
|
|
|
|
|
|
|
|
textEditor.RequestCompletioninsertion(e); |
|
|
} |
|
|
} |
|
|
|
|
|
if (IsInReadOnlyRegion) |
|
|
{ |
|
|
e.Handled = true; |
|
|
} |
|
|
else |
|
|
{ |
|
|
if (e.Text[0] == '\n') |
|
|
{ |
|
|
UpdateVariables?.Invoke(commandLine.ScriptScope); |
|
|
OnEnterKeyPressed(); |
|
|
} |
|
|
|
|
|
if (e.Text[0] == '.' && allowFullAutocompletion) |
|
|
{ |
|
|
textEditor.ShowCompletionWindow(); |
|
|
} |
|
|
|
|
|
if ((e.Text[0] == ' ') && (Keyboard.Modifiers == ModifierKeys.Control)) |
|
|
{ |
|
|
e.Handled = true; |
|
|
if (allowCtrlSpaceAutocompletion) textEditor.ShowCompletionWindow(); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void OnEnterKeyPressed() |
|
|
{ |
|
|
textEditor.StopCompletion(); |
|
|
if (textEditor.WriteInProgress) return; |
|
|
lock (previousLines) |
|
|
{ |
|
|
|
|
|
textEditor.Column = GetLastTextEditorLine().Length + 1; |
|
|
|
|
|
|
|
|
string currentLine = GetCurrentLine(); |
|
|
previousLines.Add(currentLine); |
|
|
commandLineHistory.Add(currentLine); |
|
|
|
|
|
lineReceivedEvent.Set(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool IsInReadOnlyRegion |
|
|
{ |
|
|
get { return IsCurrentLineReadOnly || IsInPrompt; } |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool IsCurrentLineReadOnly |
|
|
{ |
|
|
get { return textEditor.Line < textEditor.TotalLines; } |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool IsInPrompt |
|
|
{ |
|
|
get { return textEditor.Column - promptLength - 1 < 0; } |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool CanDelete |
|
|
{ |
|
|
get |
|
|
{ |
|
|
if (textEditor.SelectionLength > 0) return SelectionIsDeletable; |
|
|
else return !IsInReadOnlyRegion; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool CanBackspace |
|
|
{ |
|
|
get |
|
|
{ |
|
|
if (textEditor.SelectionLength > 0) return SelectionIsDeletable; |
|
|
else |
|
|
{ |
|
|
int cursorIndex = textEditor.Column - promptLength - 1; |
|
|
return !IsCurrentLineReadOnly && (cursorIndex > 0 || (cursorIndex == 0 && textEditor.SelectionLength > 0)); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
bool SelectionIsDeletable |
|
|
{ |
|
|
get |
|
|
{ |
|
|
return (!textEditor.SelectionIsMultiline |
|
|
&& !IsCurrentLineReadOnly |
|
|
&& (textEditor.SelectionStartColumn - promptLength - 1 >= 0) |
|
|
&& (textEditor.SelectionEndColumn - promptLength - 1 >= 0)); |
|
|
} |
|
|
} |
|
|
|
|
|
public bool Executing |
|
|
{ |
|
|
get { return _executing; } |
|
|
set |
|
|
{ |
|
|
if (value && !_executing && ScriptStarting != null) |
|
|
ScriptStarting(this, null); |
|
|
if (!value && _executing && ScriptFinished != null) |
|
|
ScriptFinished(this, null); |
|
|
_executing = value; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void MoveToHomePosition() |
|
|
{ |
|
|
textEditor.Line = textEditor.TotalLines; |
|
|
textEditor.Column = promptLength + 1; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void MoveToPreviousCommandLine() |
|
|
{ |
|
|
if (commandLineHistory.MovePrevious()) |
|
|
{ |
|
|
ReplaceCurrentLineTextAfterPrompt(commandLineHistory.Current); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void MoveToNextCommandLine() |
|
|
{ |
|
|
textEditor.Line = textEditor.TotalLines; |
|
|
if (commandLineHistory.MoveNext()) |
|
|
{ |
|
|
ReplaceCurrentLineTextAfterPrompt(commandLineHistory.Current); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ReplaceCurrentLineTextAfterPrompt(string text) |
|
|
{ |
|
|
string currentLine = GetCurrentLine(); |
|
|
textEditor.Replace(promptLength, currentLine.Length, text); |
|
|
|
|
|
|
|
|
textEditor.Column = promptLength + text.Length + 1; |
|
|
} |
|
|
|
|
|
public void OutputWritten(object sender, EventArgs e) |
|
|
{ |
|
|
if (!Executing ) |
|
|
{ |
|
|
_lastWrite = DateTime.UtcNow; |
|
|
Task.Run(async () => |
|
|
{ |
|
|
await Task.Delay(1100); |
|
|
await dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(AddPrompt)); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
protected virtual void OnError(TextEventArgs e) |
|
|
{ |
|
|
Error?.Invoke(this, e); |
|
|
} |
|
|
} |
|
|
} |
|
|
|