-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
169 lines (136 loc) · 4.21 KB
/
MainWindow.xaml.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Management.Automation.Runspaces;
using System.Management.Automation;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows.Input;
using System.Windows.Controls;
// TODO: Add a way to submit username and enable exo in interface
namespace mnemonicr
{
public partial class MainWindow : Window
{
private Runspace _rs;
public static bool firstSearch = true;
private static bool clearResults = true;
public MainWindow()
{
InitializeComponent();
_rs = RunspaceFactory.CreateRunspace();
_rs.Open();
}
private List<string> RunScript(string script, string input, bool firstSearch)
{
List<string> output = new List<string>();
using (PowerShell ps = PowerShell.Create())
{
ps.Runspace = _rs;
ps.AddScript(script).AddParameter("InputString", input);
ps.AddParameter("FirstSearch", firstSearch);
Collection<PSObject> result = ps.Invoke();
if (ps.Streams.Error.Count > 0)
{
// Handle errors if needed
foreach (var error in ps.Streams.Error)
{
output.Add(error.ToString());
}
}
if (ps.Streams.Information.Count > 0)
{
// Print debug information
foreach (var info in ps.Streams.Information)
{
Debug.WriteLine(info.MessageData);
}
}
foreach (PSObject obj in result)
{
output.Add(obj.ToString());
}
}
return output;
}
private void SearchButton_Click(object sender, RoutedEventArgs e)
{
List<string> inputList = new List<string>();
List<string> mnemonics = new List<string>();
if (!clearResults && ResultListBox_input.ItemsSource != null)
{
foreach (string item in ResultListBox_input.ItemsSource)
{
inputList.Add(item);
}
foreach (string item in ResultListBox_mnem.ItemsSource)
{
mnemonics.Add(item);
}
}
string input = InputTextBox.Text.Trim();
input = input.Replace("\r\n", ";").Replace(";;", ";");
// Populate input listbox
inputList.AddRange(new List<string>(input.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries)));
ResultListBox_input.ItemsSource = inputList;
string script = File.ReadAllText("script.ps1");
mnemonics.AddRange(RunScript(script, input, firstSearch));
// test for: The term 'Add-PSSnapin' is not recognized as a name of a cmdlet, function, script file, or executable program.
// Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
ResultListBox_mnem.ItemsSource = mnemonics;
firstSearch = false;
}
private void CopyButton_Click(object sender, RoutedEventArgs e)
{
CopySelectedItems();
}
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.C && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{
CopySelectedItems();
}
}
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
CopyButton.IsEnabled = (ResultListBox_input.SelectedItems.Count > 0) || (ResultListBox_mnem.SelectedItems.Count > 0);
}
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
clearResults = true;
}
private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
clearResults = false;
}
private void CopySelectedItems()
{
ListBox? focusedListBox = null;
if (ResultListBox_input.IsFocused || ResultListBox_input.IsKeyboardFocusWithin)
{
focusedListBox = ResultListBox_input;
}
else if (ResultListBox_mnem.IsFocused || ResultListBox_mnem.IsKeyboardFocusWithin)
{
focusedListBox = ResultListBox_mnem;
}
if (focusedListBox != null && focusedListBox.SelectedItems.Count > 0)
{
StringBuilder copyTextBuilder = new StringBuilder();
foreach (var item in focusedListBox.SelectedItems)
{
copyTextBuilder.AppendLine(item.ToString());
}
string copyText = copyTextBuilder.ToString().TrimEnd();
Clipboard.SetText(copyText);
}
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
_rs.Dispose();
}
}
}