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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
/*
* GNU AFFERO GENERAL PUBLIC LICENSE
* Version 3, 19 November 2007
* Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
using System;
using System.IO;
using System.Text;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Markup.Xaml;
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
using UVtools.Core;
using UVtools.WPF.Controls;
namespace UVtools.WPF.Windows
{
public partial class TerminalWindow : WindowEx
{
private static readonly string DefaultTerminalText = $"> Welcome to {About.Software} interactive terminal.\n" +
"> Type in some commands in C# language to inject code.\n" +
"> Example 1: SlicerFile.FirstLayer\n" +
"> Example 2: SlicerFile.ExposureTime = 3\n\n";
private string _terminalText = DefaultTerminalText;
private string _commandText = string.Empty;
private bool _multiLine = true;
private bool _autoScroll = true;
private bool _verbose;
private bool _clearCommandAfterSend = true;
private readonly TextBox _terminalTextBox;
public ScriptState _scriptState;
#region Properties
public string TerminalText
{
get => _terminalText;
set
{
if(!RaiseAndSetIfChanged(ref _terminalText, value)) return;
if(_autoScroll) _terminalTextBox.CaretIndex = _terminalText.Length - 1;
}
}
public string CommandText
{
get => _commandText;
set => RaiseAndSetIfChanged(ref _commandText, value ?? string.Empty);
}
public bool MultiLine
{
get => _multiLine;
set => RaiseAndSetIfChanged(ref _multiLine, value);
}
public bool AutoScroll
{
get => _autoScroll;
set => RaiseAndSetIfChanged(ref _autoScroll, value);
}
public bool Verbose
{
get => _verbose;
set => RaiseAndSetIfChanged(ref _verbose, value);
}
public bool ClearCommandAfterSend
{
get => _clearCommandAfterSend;
set => RaiseAndSetIfChanged(ref _clearCommandAfterSend, value);
}
#endregion
public TerminalWindow()
{
InitializeComponent();
#if DEBUG
this.AttachDevTools();
#endif
_terminalTextBox = this.FindControl<TextBox>("TerminalTextBox");
AddHandler(DragDrop.DropEvent, (sender, e) =>
{
var text = e.Data.GetText();
if (text is not null)
{
CommandText = text;
return;
}
var fileNames = e.Data.GetFileNames();
if (fileNames is not null)
{
var sb = new StringBuilder();
foreach (var fileName in fileNames)
{
try
{
if (!File.Exists(fileName)) continue;
var fi = new FileInfo(fileName);
if(fi.Length > 5000000) continue; // 5Mb only!
sb.AppendLine(File.ReadAllText(fi.FullName));
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
CommandText = sb.ToString();
}
});
DataContext = this;
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
public void Clear()
{
TerminalText = DefaultTerminalText;
}
public async void SendCommand()
{
if (string.IsNullOrWhiteSpace(_commandText))
{
TerminalText += '\n';
return;
}
var output = new StringBuilder(_terminalText);
if (_verbose) output.AppendLine(_commandText);
try
{
if (_scriptState is null)
{
_scriptState = CSharpScript.RunAsync(_commandText,
ScriptOptions.Default
.AddReferences(typeof(About).Assembly)
.AddImports(
"System",
"System.Collections.Generic",
"System.Math",
"System.IO",
"System.Linq",
"System.Threading",
"System.Threading.Tasks",
"UVtools.Core",
"UVtools.Core.Extensions",
"UVtools.Core.FileFormats",
"UVtools.Core.Layers",
"UVtools.Core.Objects",
"UVtools.Core.Operations")
.WithAllowUnsafe(true), this).Result;
}
else
{
_scriptState = await _scriptState.ContinueWithAsync(_commandText);
}
if (_scriptState.ReturnValue is not null)
{
output.AppendLine(_scriptState.ReturnValue.ToString());
}
else if (_scriptState.Exception is not null)
{
output.AppendLine(_scriptState.Exception.ToString());
}
else if(!_verbose)
{
output.AppendLine(_commandText);
}
}
catch (Exception e)
{
output.AppendLine(e.Message);
}
TerminalText = output.ToString();
if (_clearCommandAfterSend) CommandText = string.Empty;
}
}
}
|