Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#47 1,2,3 + readme example #48

Merged
merged 4 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ ls /Applications/ /Applications/Utilities/ /System/Applications/ /System/Applica
xargs -I {} open -a "{}.app"
```

### Use as a snippet manager

Suppose you have some snippets in a text file and you want to quickly search and paste them with choose. Here is a command that you can bind to some shortcut with something like Karabiner:
```bash
cat snippets_separated_with_two_newline_symbols.txt | choose -e -m -x \n\n - | pbcopy - && osascript -e 'tell application "System Events" to keystroke "v" using command down'
```

This will prompt choose, get its output, copy it to pasteboard, and trigger a paste shortcut `command+v`.

For this to work in Karabiner, you need to give it access via Privacy & Security -> Accessibility -> karabiner_console_user_server (typically located at `/Library/Application Support/org.pqrs/Karabiner-Elements/bin/karabiner_console_user_server`), otherwise you will get `System Events got an error: osascript is not allowed to send keystrokes. (1002)`

## License

See [MIT LICENSE](./LICENSE).
28 changes: 24 additions & 4 deletions SDAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
static NSFont* SDQueryFont;
static NSString* PromptText;
static NSString* InitialQuery;
static NSString* Separator;
static int SDNumRows;
static int SDPercentWidth;
static BOOL SDUnderlineDisabled;
static BOOL SDReturnStringOnMismatch;
static BOOL VisualizeWhitespaceCharacters;
static BOOL AllowEmptyInput;

/******************************************************************************/
/* Boilerplate Subclasses */
Expand Down Expand Up @@ -74,7 +77,12 @@ - (id) initWithString:(NSString*)str {
self.raw = str;
self.normalized = [self.raw lowercaseString];
self.indexSet = [NSMutableIndexSet indexSet];
self.displayString = [[NSMutableAttributedString alloc] initWithString:self.raw attributes:nil];

NSString* displayStringRaw = self.raw;
if (VisualizeWhitespaceCharacters) {
displayStringRaw = [[self.raw stringByReplacingOccurrencesOfString:@"\n" withString:@"⏎"] stringByReplacingOccurrencesOfString:@"\t" withString:@"⇥"];
}
self.displayString = [[NSMutableAttributedString alloc] initWithString:displayStringRaw attributes:nil];
}
return self;
}
Expand Down Expand Up @@ -641,10 +649,10 @@ - (NSArray*) getInputItems {
NSData* inputData = [stdinHandle readDataToEndOfFile];
NSString* inputStrings = [[[NSString alloc] initWithData:inputData encoding:NSUTF8StringEncoding] stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];

if ([inputStrings length] == 0)
if ([inputStrings length] == 0 && !AllowEmptyInput)
return nil;

return [inputStrings componentsSeparatedByString:@"\n"];
return [inputStrings componentsSeparatedByString: Separator];

#endif

Expand Down Expand Up @@ -679,6 +687,12 @@ static void usage(const char* name) {
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(" -q defines initial query to start with (empty by default)\n");
printf(" -x defines separator string, a single newline (\\n) by default\n");
printf(" beware of escaping:\n");
printf(" passing -x \\n\\n will work\n");
printf(" passing -x '\\n\\n' will not work\n");
printf(" -y show newline and tab as symbols (⏎ ⇥)\n");
printf(" -e allow empty input (choose will show up even if there are no items to select)\n");
printf(" -o given a query, outputs results to standard output\n");
exit(0);
}
Expand All @@ -697,13 +711,16 @@ int main(int argc, const char * argv[]) {
@autoreleasepool {
[NSApp setActivationPolicy: NSApplicationActivationPolicyAccessory];

VisualizeWhitespaceCharacters = NO;
AllowEmptyInput = NO;
SDReturnsIndex = NO;
SDUnderlineDisabled = NO;
const char* hexColor = HexFromSDColor(NSColor.systemBlueColor);
const char* hexBackgroundColor = HexFromSDColor(NSColor.systemGrayColor);
const char* queryFontName = "Menlo";
const char* queryPromptString = "";
InitialQuery = [NSString stringWithUTF8String: ""];
Separator = [NSString stringWithUTF8String: "\n"];
CGFloat queryFontSize = 26.0;
SDNumRows = 10;
SDReturnStringOnMismatch = NO;
Expand All @@ -714,7 +731,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:q:o:hium")) != -1) {
while ((ch = getopt(argc, (char**)argv, "lvyef:s:r:c:b:n:w:p:q:x:o:hium")) != -1) {
switch (ch) {
case 'i': SDReturnsIndex = YES; break;
case 'f': queryFontName = optarg; break;
Expand All @@ -728,6 +745,9 @@ int main(int argc, const char * argv[]) {
case 'm': SDReturnStringOnMismatch = YES; break;
case 'p': queryPromptString = optarg; break;
case 'q': InitialQuery = [NSString stringWithUTF8String: optarg]; break;
case 'x': Separator = [NSString stringWithUTF8String: optarg]; break;
case 'y': VisualizeWhitespaceCharacters = YES; break;
case 'e': AllowEmptyInput = YES; break;
case 'o': queryStdout(delegate, optarg); break;
case '?':
case 'h':
Expand Down