TextSearch: add a simple file filter.

It relies on grep's "--include <glob>" option.

GUI layout code courtesy of Humdinger (thanks!).

Change-Id: I825abdaafaecdb750f2d27e510b310d20d3b461c
Reviewed-on: https://review.haiku-os.org/c/haiku/+/9285
Tested-by: Commit checker robot <[email protected]>
Reviewed-by: waddlesplash <[email protected]>
Reviewed-by: humdinger humdinger <[email protected]>
This commit is contained in:
Oscar Lesta
2025-05-21 17:48:14 +00:00
committed by waddlesplash
parent 2de2e7aec3
commit 30e1b0862f
5 changed files with 53 additions and 9 deletions
+36 -7
View File
@@ -109,6 +109,7 @@ GrepWindow::GrepWindow(BMessage* message)
fGrepper(NULL),
fOldPattern(""),
fIncludeFilesGlob(""),
fModel(new (nothrow) Model()),
fLastNodeMonitorEvent(system_time()),
fChangesIterator(NULL),
@@ -235,6 +236,10 @@ void GrepWindow::MessageReceived(BMessage* message)
_OnSearchText();
break;
case MSG_SEARCH_GLOB_FILTER:
_OnGlobFilterChange();
break;
case MSG_SELECT_HISTORY:
_OnHistoryItem(message);
break;
@@ -547,6 +552,14 @@ GrepWindow::_CreateViews()
fSearchText->TextView()->SetMaxBytes(1000);
fSearchText->SetModificationMessage(new BMessage(MSG_SEARCH_TEXT));
fGlobText = new BTextControl(
"GlobText", B_TRANSLATE("File filter:"), NULL, NULL,
B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE | B_NAVIGABLE);
fGlobText->TextView()->SetMaxBytes(20);
fGlobText->SetModificationMessage(new BMessage(MSG_SEARCH_GLOB_FILTER));
fGlobText->SetToolTip(B_TRANSLATE("Only search files matching the given pattern, e.g. \"*.h\"."));
fButton = new BButton(
"Button", B_TRANSLATE("Search"),
new BMessage(MSG_START_CANCEL));
@@ -573,13 +586,16 @@ GrepWindow::_LayoutViews()
BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
.SetInsets(0, 0, -1, -1)
.Add(fMenuBar)
.AddGrid(B_USE_HALF_ITEM_INSETS)
.AddGroup(B_VERTICAL, B_USE_SMALL_SPACING)
.SetInsets(B_USE_WINDOW_SPACING, B_USE_WINDOW_SPACING,
B_USE_WINDOW_SPACING, B_USE_DEFAULT_SPACING)
.Add(fSearchText, 0, 0, 3)
.Add(fShowLinesCheckbox, 0, 1)
.Add(BSpaceLayoutItem::CreateGlue(), 1, 1)
.Add(fButton, 2, 1)
.Add(fSearchText)
.AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING)
.Add(fGlobText, 1.0)
.Add(fShowLinesCheckbox)
.AddGlue(0.5)
.Add(fButton)
.End()
.End()
.AddGroup(B_VERTICAL, 0)
.SetInsets(-2, 0, -1, -1)
@@ -737,6 +753,7 @@ GrepWindow::_OnStartCancel()
// displaying the names of the files we are grepping.
fSearchText->SetModificationMessage(NULL);
fGlobText->SetModificationMessage(NULL);
fFileMenu->SetEnabled(false);
fActionMenu->SetEnabled(false);
@@ -745,6 +762,7 @@ GrepWindow::_OnStartCancel()
fEncodingMenu->SetEnabled(false);
fSearchText->SetEnabled(false);
fGlobText->SetEnabled(false);
fButton->MakeFocus(true);
fButton->SetLabel(B_TRANSLATE("Cancel"));
@@ -762,7 +780,7 @@ GrepWindow::_OnStartCancel()
_SetWindowTitle();
FileIterator* iterator = new (nothrow) InitialIterator(fModel);
fGrepper = new (nothrow) Grepper(fOldPattern.String(), fModel,
fGrepper = new (nothrow) Grepper(fOldPattern.String(), fIncludeFilesGlob.String(), fModel,
this, iterator);
if (fGrepper != NULL && fGrepper->IsValid())
fGrepper->Start();
@@ -810,6 +828,8 @@ GrepWindow::_OnSearchFinished()
fSearchText->SetText(fOldPattern.String());
fSearchText->TextView()->SelectAll();
fSearchText->SetModificationMessage(new BMessage(MSG_SEARCH_TEXT));
fGlobText->SetEnabled(true);
fGlobText->SetModificationMessage(new BMessage(MSG_SEARCH_GLOB_FILTER));
PostMessage(MSG_START_NODE_MONITORING);
}
@@ -968,7 +988,7 @@ GrepWindow::_OnNodeMonitorPulse()
fChangesIterator->PrintToStream();
#endif
fGrepper = new (nothrow) Grepper(fOldPattern.String(), fModel,
fGrepper = new (nothrow) Grepper(fOldPattern.String(), fIncludeFilesGlob.String(), fModel,
this, fChangesIterator);
if (fGrepper != NULL && fGrepper->IsValid()) {
fGrepper->Start();
@@ -1558,6 +1578,15 @@ GrepWindow::_OnSetTargetToParent()
}
void
GrepWindow::_OnGlobFilterChange()
{
CALLED();
fIncludeFilesGlob = fGlobText->Text();
_StopNodeMonitoring();
}
// #pragma mark -
+3
View File
@@ -74,6 +74,7 @@ private:
void _OnSelectAll(BMessage* message);
void _OnNewWindow();
void _OnSetTargetToParent();
void _OnGlobFilterChange();
void _ModelChanged();
bool _OpenInEditor(const entry_ref& ref, int32 lineNum);
@@ -85,6 +86,7 @@ private:
private:
BTextControl* fSearchText;
BTextControl* fGlobText;
GrepListView* fSearchResults;
BMenuBar* fMenuBar;
@@ -121,6 +123,7 @@ private:
Grepper* fGrepper;
BString fOldPattern;
BString fIncludeFilesGlob;
Model* fModel;
bigtime_t fLastNodeMonitorEvent;
ChangesIterator* fChangesIterator;
+11 -1
View File
@@ -88,7 +88,7 @@ strdup_from_utf8(uint32 encode, const char* src, int32 length)
}
Grepper::Grepper(const char* pattern, const Model* model,
Grepper::Grepper(const char* pattern, const char* glob, const Model* model,
const BHandler* target, FileIterator* iterator)
: fPattern(NULL),
fTarget(target),
@@ -107,6 +107,8 @@ Grepper::Grepper(const char* pattern, const Model* model,
free(src);
} else
_SetPattern(pattern);
fGlob = strdup(glob);
}
@@ -114,6 +116,7 @@ Grepper::~Grepper()
{
Cancel();
free(fPattern);
free(fGlob);
delete fIterator;
}
@@ -271,6 +274,13 @@ Grepper::_RunnerThread()
argv[argc++] = "-i";
if (! fRegularExpression)
argv[argc++] = "-F"; // no a regexp: force fixed string,
// Limit included files to fGlob pattern, if one was given.
if (strlen(fGlob) > 0) {
argv[argc++] = "--include";
argv[argc++] = fGlob;
}
// Add double dash argument to tell grep
// it's the end of commands options
argv[argc++] = "--";
+2 -1
View File
@@ -13,7 +13,7 @@ class Model;
// Executes "grep" in a background thread.
class Grepper {
public:
Grepper(const char* pattern, const Model* model,
Grepper(const char* pattern, const char* glob, const Model* model,
const BHandler* target,
FileIterator* iterator);
virtual ~Grepper();
@@ -43,6 +43,7 @@ private:
private:
// The (escaped) search pattern.
char* fPattern;
char* fGlob;
// The settings from the model.
BMessenger fTarget;
+1
View File
@@ -28,6 +28,7 @@ enum {
MSG_INVOKE_EDITOR,
MSG_CHECKBOX_SHOW_LINES,
MSG_SEARCH_TEXT,
MSG_SEARCH_GLOB_FILTER,
MSG_INVOKE_ITEM,
MSG_SELECT_HISTORY,
MSG_NODE_MONITOR_PULSE,