Skip to content

Commit

Permalink
fuzzy search output also going to stdout (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
moretticb authored Mar 31, 2023
1 parent 58e4ef5 commit 59e28af
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions SDAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ - (void) tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableC
/* Filtering! */
/******************************************************************************/

- (void) runQuery:(NSString*)query {
- (void) doQuery:(NSString*)query {
query = [query lowercaseString];

self.filteredSortedChoices = [self.choices mutableCopy];
Expand All @@ -440,6 +440,11 @@ - (void) runQuery:(NSString*)query {
}];

}
}


- (void) runQuery:(NSString*)query {
[self doQuery: query];

// render remainder
for (SDChoice* choice in self.filteredSortedChoices)
Expand Down Expand Up @@ -671,6 +676,17 @@ static void usage(const char* name) {
printf(" -u disable underline and use background for matched string\n");
printf(" -m return the query string in case it doesn't match any item\n");
printf(" -p defines a prompt to be displayed when query field is empty\n");
printf(" -o given a query, outputs results to standard output\n");
exit(0);
}

static void queryStdout(SDAppDelegate* delegate, const char* query) {
delegate.choices = [delegate choicesFromInputItems: [delegate getInputItems]];
[delegate doQuery: [NSString stringWithUTF8String: query]];

for (SDChoice* choice in delegate.filteredSortedChoices)
printf("%s\n", [choice.raw UTF8String]);

exit(0);
}

Expand All @@ -694,7 +710,7 @@ int main(int argc, const char * argv[]) {
[NSApp setDelegate: delegate];

int ch;
while ((ch = getopt(argc, (char**)argv, "lvf:s:r:c:b:n:w:p:hium")) != -1) {
while ((ch = getopt(argc, (char**)argv, "lvf:s:r:c:b:n:w:p:o:hium")) != -1) {
switch (ch) {
case 'i': SDReturnsIndex = YES; break;
case 'f': queryFontName = optarg; break;
Expand All @@ -707,6 +723,7 @@ int main(int argc, const char * argv[]) {
case 'u': SDUnderlineDisabled = YES; break;
case 'm': SDReturnStringOnMismatch = YES; break;
case 'p': queryPromptString = optarg; break;
case 'o': queryStdout(delegate, optarg); break;
case '?':
case 'h':
default:
Expand Down

0 comments on commit 59e28af

Please sign in to comment.