Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-6335 - Script quick launcher fixups

dragonmacher2f6f9a938ad6
diff --git a/Ghidra/Features/Base/src/main/help/help/topics/GhidraScriptMgrPlugin/GhidraScriptMgrPlugin.htm b/Ghidra/Features/Base/src/main/help/help/topics/GhidraScriptMgrPlugin/GhidraScriptMgrPlugin.htm
index 2e6e0f3194..244f99b529 100644
--- a/Ghidra/Features/Base/src/main/help/help/topics/GhidraScriptMgrPlugin/GhidraScriptMgrPlugin.htm
+++ b/Ghidra/Features/Base/src/main/help/help/topics/GhidraScriptMgrPlugin/GhidraScriptMgrPlugin.htm
@@ -147,9 +147,9 @@
<LI><B>All Scripts</B> - Shows all available scripts in alphabetical order.</LI>
</UL>
- <P>Type any part of a script name in the filter field to narrow down the list. The filter
- supports wildcard matching using * (any characters) and ? (single character). Use the
- up/down arrow keys or mouse to select a script, then press Enter or click OK to run it.</P>
+ <P>Type any part of a script name or description in the filter field to narrow down the
+ list. The filter wildcard matching using * (any characters) and ? (single character). Use
+ the down arrow keys or mouse to select a script, then press Enter or click OK to run it.</P>
<P>The detail pane on the right displays information about the currently selected script,
including its description, author, category, and assigned keybinding (if any).</P>
diff --git a/Ghidra/Features/Base/src/main/help/help/topics/GhidraScriptMgrPlugin/images/ScriptQuickLaunchDialog.png b/Ghidra/Features/Base/src/main/help/help/topics/GhidraScriptMgrPlugin/images/ScriptQuickLaunchDialog.png
index d87ba1191a..644611cdf7 100644
Binary files a/Ghidra/Features/Base/src/main/help/help/topics/GhidraScriptMgrPlugin/images/ScriptQuickLaunchDialog.png and b/Ghidra/Features/Base/src/main/help/help/topics/GhidraScriptMgrPlugin/images/ScriptQuickLaunchDialog.png differ
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptActionManager.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptActionManager.java
index d1aea472a7..cc15b2a3aa 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptActionManager.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptActionManager.java
@@ -293,9 +293,9 @@ class GhidraScriptActionManager {
initialScript = lastRunScript.getName();
}
- ScriptSelectionDialog dialog =
- new ScriptSelectionDialog(plugin, scriptInfos, provider.getRecentScripts(),
- initialScript);
+ List<String> recentScripts = provider.getRecentScripts();
+ ScriptSelectionDialog dialog = new ScriptSelectionDialog(plugin, scriptInfos, recentScripts,
+ initialScript);
dialog.show();
ScriptInfo chosenInfo = dialog.getUserChoice();
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptComponentProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptComponentProvider.java
index 97af8176e8..485682e381 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptComponentProvider.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptComponentProvider.java
@@ -54,8 +54,7 @@ import ghidra.framework.options.SaveState;
import ghidra.framework.plugintool.ComponentProviderAdapter;
import ghidra.program.model.listing.Program;
import ghidra.util.*;
-import ghidra.util.datastruct.WeakDataStructureFactory;
-import ghidra.util.datastruct.WeakSet;
+import ghidra.util.datastruct.*;
import ghidra.util.table.GhidraTableFilterPanel;
import ghidra.util.task.*;
import util.CollectionUtils;
@@ -90,7 +89,7 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
private String[] previousCategory;
private ResourceFile lastRunScript;
- private LinkedList<String> recentScripts = new LinkedList<>();
+ private LRUSet<String> recentScripts = new LRUSet<String>(MAX_RECENT_SCRIPTS);
private WeakSet<RunScriptTask> runningScriptTaskSet =
WeakDataStructureFactory.createCopyOnReadWeakSet();
private TaskListener cleanupTaskSetListener = new TaskListener() {
@@ -342,7 +341,7 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
String filterText = tableFilterPanel.getFilterText();
saveState.putString(FILTER_TEXT, filterText);
- String[] scripts = recentScripts.toArray(new String[0]);
+ String[] scripts = recentScripts.toList().toArray(new String[0]);
saveState.putStrings(RECENT_SCRIPTS, scripts);
}
@@ -376,8 +375,8 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
return editorMap;
}
- LinkedList<String> getRecentScripts() {
- return recentScripts;
+ List<String> getRecentScripts() {
+ return recentScripts.toList();
}
void assignKeyBinding() {
@@ -657,7 +656,8 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
}
}
catch (IOException e) {
- Msg.showError(this, getComponent(), getName(), e.getMessage(), e);
+ Msg.showError(this, getComponent(), getName(), "Unexpected exception loading script",
+ e);
}
}
@@ -683,12 +683,7 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
// Update recent scripts list
String scriptName = scriptFile.getName();
- recentScripts.remove(scriptName);
- recentScripts.addFirst(scriptName); // Add to front (most recent)
-
- while (recentScripts.size() > MAX_RECENT_SCRIPTS) {
- recentScripts.removeLast();
- }
+ recentScripts.add(scriptName);
GhidraScript script = doGetScriptInstance(scriptFile);
if (script != null) {
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/ScriptSelectionDialog.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/ScriptSelectionDialog.java
index 5947f509fc..e92cfbb60b 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/ScriptSelectionDialog.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/ScriptSelectionDialog.java
@@ -16,9 +16,9 @@
package ghidra.app.plugin.core.script;
import java.awt.*;
-import java.util.LinkedList;
import java.util.List;
import java.util.function.BiPredicate;
+import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.*;
@@ -26,17 +26,17 @@ import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.text.html.HTMLEditorKit;
+import org.apache.commons.lang3.StringUtils;
+
import docking.DialogComponentProvider;
import docking.widgets.list.GListCellRenderer;
import docking.widgets.searchlist.SearchList;
import docking.widgets.searchlist.SearchListEntry;
+import generic.theme.GColor;
import generic.theme.GThemeDefaults.Colors.Palette;
import ghidra.app.script.ScriptInfo;
import ghidra.framework.plugintool.PluginTool;
-import ghidra.util.HelpLocation;
-import ghidra.util.HTMLUtilities;
-import ghidra.util.Swing;
-import ghidra.util.UserSearchUtils;
+import ghidra.util.*;
/**
* A dialog that prompts the user to select a script from a searchable list
@@ -46,14 +46,14 @@ public class ScriptSelectionDialog extends DialogComponentProvider {
private PluginTool tool;
private List<ScriptInfo> scriptInfos;
- private LinkedList<String> recentScripts;
+ private List<String> recentScripts;
private String initialScript;
private ScriptInfo userChoice;
private SearchList<ScriptInfo> searchList;
private JTextPane detailPane;
ScriptSelectionDialog(GhidraScriptMgrPlugin plugin, List<ScriptInfo> scriptInfos,
- LinkedList<String> recentScripts, String initialScript) {
+ List<String> recentScripts, String initialScript) {
super("Run Script", true, true, true, false);
this.tool = plugin.getTool();
this.scriptInfos = scriptInfos;
@@ -69,17 +69,15 @@ public class ScriptSelectionDialog extends DialogComponentProvider {
private JComponent buildMainPanel() {
ScriptsModel model = new ScriptsModel(scriptInfos, recentScripts);
- searchList = new SearchList<ScriptInfo>(model, (script, category) -> scriptChosen(script)) {
+ searchList = new SearchList<>(model, (script, category) -> scriptChosen(script)) {
@Override
protected BiPredicate<ScriptInfo, String> createFilter(String text) {
- Pattern pattern = UserSearchUtils.createContainsPattern(text, true, Pattern.CASE_INSENSITIVE);
- return (script, category) -> pattern.matcher(script.getName()).matches();
+ return new ScriptFilter(text);
}
};
searchList.setItemRenderer(new ScriptRenderer());
searchList.setDisplayNameFunction((script, category) -> script.getName());
- // Add selection listener to update detail pane
searchList.setSelectionCallback(this::updateDetailPane);
// Add model listener to reset selection when filter changes
@@ -100,17 +98,7 @@ public class ScriptSelectionDialog extends DialogComponentProvider {
}
});
- // Pre-select the initial script if provided
- if (initialScript != null && !initialScript.isEmpty()) {
- Swing.runLater(() -> {
- for (ScriptInfo info : scriptInfos) {
- if (info.getName().equals(initialScript)) {
- searchList.setSelectedItem(info);
- break;
- }
- }
- });
- }
+ selectInitialScript();
JComponent detailPaneComponent = buildDetailPane();
@@ -128,6 +116,21 @@ public class ScriptSelectionDialog extends DialogComponentProvider {
return panel;
}
+ private void selectInitialScript() {
+ if (StringUtils.isBlank(initialScript)) {
+ return;
+ }
+
+ Swing.runLater(() -> {
+ for (ScriptInfo info : scriptInfos) {
+ if (info.getName().equals(initialScript)) {
+ searchList.setSelectedItem(info);
+ break;
+ }
+ }
+ });
+ }
+
private JComponent buildDetailPane() {
detailPane = new JTextPane();
detailPane.setEditable(false);
@@ -144,14 +147,14 @@ public class ScriptSelectionDialog extends DialogComponentProvider {
private void updateDetailPane(ScriptInfo script) {
String text = (script != null) ? script.getToolTipText() : "";
- SwingUtilities.invokeLater(() -> {
+ Swing.runLater(() -> {
detailPane.setText(text);
detailPane.setCaretPosition(0);
});
}
private void resetSelectionToFirst() {
- SwingUtilities.invokeLater(() -> {
+ Swing.runLater(() -> {
ScriptsModel model = (ScriptsModel) searchList.getModel();
if (model.getSize() > 0) {
ScriptInfo firstScript = model.getElementAt(0).value();
@@ -161,10 +164,8 @@ public class ScriptSelectionDialog extends DialogComponentProvider {
}
private void scriptChosen(ScriptInfo script) {
- if (script != null) {
- userChoice = script;
- close();
- }
+ userChoice = script;
+ close();
}
public void show() {
@@ -204,45 +205,64 @@ public class ScriptSelectionDialog extends DialogComponentProvider {
// Inner Classes
//=================================================================================================
- /**
- * Custom renderer for script entries in the search list.
- */
+ private class ScriptFilter implements BiPredicate<ScriptInfo, String> {
+
+ private Pattern pattern;
+
+ ScriptFilter(String filterText) {
+ pattern =
+ UserSearchUtils.createContainsPattern(filterText, true, Pattern.CASE_INSENSITIVE);
+ }
+
+ @Override
+ public boolean test(ScriptInfo info, String category) {
+
+ Matcher matcher = pattern.matcher(info.getName());
+ if (matcher.matches()) {
+ return true;
+ }
+
+ matcher = pattern.matcher(info.getDescription());
+ return matcher.matches();
+ }
+ }
+
private class ScriptRenderer extends GListCellRenderer<SearchListEntry<ScriptInfo>> {
{
setHTMLRenderingEnabled(true);
}
@Override
- public Component getListCellRendererComponent(JList<? extends SearchListEntry<ScriptInfo>> list,
- SearchListEntry<ScriptInfo> entry, int index, boolean isSelected, boolean cellHasFocus) {
+ public Component getListCellRendererComponent(
+ JList<? extends SearchListEntry<ScriptInfo>> list,
+ SearchListEntry<ScriptInfo> entry, int index, boolean isSelected,
+ boolean cellHasFocus) {
super.getListCellRendererComponent(list, entry, index, isSelected, cellHasFocus);
- if (entry == null) {
- return this;
- }
-
ScriptInfo script = entry.value();
StringBuilder html = new StringBuilder("<html>");
html.append("<b>").append(HTMLUtilities.escapeHTML(script.getName())).append("</b>");
+ GColor altColor = isSelected ? (GColor) getForeground() : Palette.GRAY;
+
KeyStroke keyBinding = script.getKeyBinding();
if (keyBinding != null) {
html.append(" <font color=\"")
- .append(Palette.GRAY.toHexString())
- .append("\"><i>(")
- .append(keyBinding.toString())
- .append(")</i></font>");
+ .append(altColor.toHexString())
+ .append("\"><i>(")
+ .append(keyBinding.toString())
+ .append(")</i></font>");
}
String description = script.getDescription();
- if (description != null && !description.isEmpty()) {
+ if (!StringUtils.isBlank(description)) {
html.append("<br><font color=\"")
- .append(Palette.GRAY.toHexString())
- .append("\">")
- .append(HTMLUtilities.escapeHTML(truncateDescription(description)))
- .append("</font>");
+ .append(altColor.toHexString())
+ .append("\">")
+ .append(HTMLUtilities.escapeHTML(truncateDescription(description)))
+ .append("</font>");
}
html.append("</html>");
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/ScriptsModel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/ScriptsModel.java
index edac061113..9f89b82f00 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/ScriptsModel.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/ScriptsModel.java
@@ -27,9 +27,9 @@ import ghidra.app.script.ScriptInfo;
public class ScriptsModel extends DefaultSearchListModel<ScriptInfo> {
private List<ScriptInfo> allScripts;
- private LinkedList<String> recentScriptNames;
+ private List<String> recentScriptNames;
- public ScriptsModel(List<ScriptInfo> allScripts, LinkedList<String> recentScriptNames) {
+ public ScriptsModel(List<ScriptInfo> allScripts, List<String> recentScriptNames) {
this.allScripts = allScripts;
this.recentScriptNames = recentScriptNames != null ? recentScriptNames : new LinkedList<>();
populateModel();
@@ -42,7 +42,7 @@ public class ScriptsModel extends DefaultSearchListModel<ScriptInfo> {
scriptMap.put(script.getName(), script);
}
- // Add recent scripts first (in MRU order)
+ // Add recent scripts first (these should be sorted by most recently used)
List<ScriptInfo> recentScripts = new ArrayList<>();
Set<String> addedScripts = new HashSet<>();
for (String recentName : recentScriptNames) {
diff --git a/Ghidra/Framework/Docking/src/main/java/docking/widgets/searchlist/DefaultSearchListModel.java b/Ghidra/Framework/Docking/src/main/java/docking/widgets/searchlist/DefaultSearchListModel.java
index d38d9b1381..a3d41dfd74 100644
--- a/Ghidra/Framework/Docking/src/main/java/docking/widgets/searchlist/DefaultSearchListModel.java
+++ b/Ghidra/Framework/Docking/src/main/java/docking/widgets/searchlist/DefaultSearchListModel.java
@@ -53,7 +53,7 @@ public class DefaultSearchListModel<T> extends AbstractListModel<SearchListEntry
/**
* Adds the list of items to the given category. If the category already exists, these items
- * will be added to any items already associated with that cateogry.
+ * will be added to any items already associated with that category.
* @param category the category to add the items to
* @param items the list of items to add to and be associated with the given category
*/
diff --git a/Ghidra/Test/IntegrationTest/src/screen/java/help/screenshot/GhidraScriptMgrPluginScreenShots.java b/Ghidra/Test/IntegrationTest/src/screen/java/help/screenshot/GhidraScriptMgrPluginScreenShots.java
index f7ccf0cc71..88bdb08cd2 100644
--- a/Ghidra/Test/IntegrationTest/src/screen/java/help/screenshot/GhidraScriptMgrPluginScreenShots.java
+++ b/Ghidra/Test/IntegrationTest/src/screen/java/help/screenshot/GhidraScriptMgrPluginScreenShots.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -15,10 +15,9 @@
*/
package help.screenshot;
-import java.awt.Component;
-import java.awt.Window;
import java.io.*;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.List;
import javax.swing.*;
@@ -38,6 +37,7 @@ import ghidra.app.script.*;
import ghidra.app.services.ConsoleService;
import ghidra.jython.JythonScriptProvider;
import ghidra.util.HelpLocation;
+import ghidra.util.datastruct.LRUSet;
public class GhidraScriptMgrPluginScreenShots extends GhidraScreenShotGenerator {
@@ -234,24 +234,17 @@ public class GhidraScriptMgrPluginScreenShots extends GhidraScreenShotGenerator
@Test
public void testScriptQuickLaunchDialog() {
+ addRecentScripts("HelloWorldScript.java");
+
DockingActionIf action = getAction(tool, "Script Quick Launch");
performAction(action, false);
ScriptSelectionDialog dialog = waitForDialogComponent(ScriptSelectionDialog.class);
JTextField textField = findComponent(dialog.getComponent(), JTextField.class);
- triggerText(textField, "Hello*Pop");
-
- // note: textField is an instance of DropDownSelectionTextField
- waitFor(() -> (Window) getInstanceField("matchingWindow", textField));
-
- JComponent component = dialog.getComponent();
- Window dataTypeDialog = windowForComponent(component);
- Window[] popUpWindows = dataTypeDialog.getOwnedWindows();
-
- List<Component> dataTypeWindows = new ArrayList<>(Arrays.asList(popUpWindows));
- dataTypeWindows.add(dataTypeDialog);
+ triggerText(textField, "Hello");
- captureComponents(dataTypeWindows);
+ waitForSwing();
+ captureDialog(dialog);
closeAllWindows();
}
@@ -259,6 +252,16 @@ public class GhidraScriptMgrPluginScreenShots extends GhidraScreenShotGenerator
// Private Methods
//==================================================================================================
+ private void addRecentScripts(String... names) {
+ GhidraScriptComponentProvider provider = getProvider(GhidraScriptComponentProvider.class);
+ @SuppressWarnings("unchecked")
+ LRUSet<String> recents = (LRUSet<String>) getInstanceField("recentScripts", provider);
+ for (String name : names) {
+ recents.add(name);
+ }
+ waitForSwing(); // flush the thread cache
+ }
+
private void collapse(final GTree tree, final String nodeName) {
runSwing(() -> {
GTreeNode rootNode = tree.getViewRoot();