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

Added option "--exec CMD" #158

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions fzy.1
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,8 @@ List running processes, kill the selected process
.TP
.BR "git checkout $(git branch | cut -c 3- | fzy)"
Same as above, but switching git branches.
.TP
.BR "find . | fzy -x less
Select files and open them for reading.
.SH AUTHOR
John Hawthorn <[email protected]>
11 changes: 10 additions & 1 deletion src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ static const char *usage_str =
" -0, --read-null Read input delimited by ASCII NUL characters\n"
" -j, --workers NUM Use NUM workers for searching. (default is # of CPUs)\n"
" -i, --show-info Show selection info line\n"
" -x, --exec=PROG Execute prog on selection. Don't exit.\n"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indentation is different here, than in the surrounding lines

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, ;) I literally didn't see it. Fixed it, and trying to push it again.

" -h, --help Display this help and exit\n"
" -v, --version Output version information and exit\n";

Expand All @@ -29,6 +30,7 @@ static void usage(const char *argv0) {

static struct option longopts[] = {{"show-matches", required_argument, NULL, 'e'},
{"query", required_argument, NULL, 'q'},
{"exec", required_argument, NULL, 'x'},
{"lines", required_argument, NULL, 'l'},
{"tty", required_argument, NULL, 't'},
{"prompt", required_argument, NULL, 'p'},
Expand All @@ -45,6 +47,7 @@ void options_init(options_t *options) {
/* set defaults */
options->benchmark = 0;
options->filter = NULL;
options->exec = NULL;
options->init_search = NULL;
options->show_scores = 0;
options->scrolloff = 1;
Expand All @@ -60,7 +63,7 @@ void options_parse(options_t *options, int argc, char *argv[]) {
options_init(options);

int c;
while ((c = getopt_long(argc, argv, "vhs0e:q:l:t:p:j:i", longopts, NULL)) != -1) {
while ((c = getopt_long(argc, argv, "vhs0e:q:l:t:p:j:ix:", longopts, NULL)) != -1) {
switch (c) {
case 'v':
printf("%s " VERSION " © 2014-2018 John Hawthorn\n", argv[0]);
Expand All @@ -74,6 +77,12 @@ void options_parse(options_t *options, int argc, char *argv[]) {
case 'q':
options->init_search = optarg;
break;
case 'x':
options->exec = malloc(MAXEXECLEN);
options->p_exec = stpcpy(options->exec,optarg);
*options->p_exec = ' ';
options->p_exec++;
break;
case 'e':
options->filter = optarg;
break;
Expand Down
4 changes: 4 additions & 0 deletions src/options.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef OPTIONS_H
#define OPTIONS_H OPTIONS_H

#define MAXEXECLEN 256

typedef struct {
int benchmark;
const char *filter;
Expand All @@ -10,6 +12,8 @@ typedef struct {
unsigned int num_lines;
unsigned int scrolloff;
const char *prompt;
char *exec;
char* p_exec;
unsigned int workers;
char input_delimiter;
int show_info;
Expand Down
33 changes: 21 additions & 12 deletions src/tty_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,31 @@ static void update_state(tty_interface_t *state) {
static void action_emit(tty_interface_t *state) {
update_state(state);

/* Reset the tty as close as possible to the previous state */
clear(state);
const char *selection = choices_get(state->choices, state->choices->selection);
if ( state->options->exec == NULL ){
/* Reset the tty as close as possible to the previous state */
clear(state);

/* ttyout should be flushed before outputting on stdout */
tty_close(state->tty);
/* ttyout should be flushed before outputting on stdout */
tty_close(state->tty);

const char *selection = choices_get(state->choices, state->choices->selection);
if (selection) {
/* output the selected result */
printf("%s\n", selection);
if (selection) {
/* output the selected result */
printf("%s\n", selection);
} else {
/* No match, output the query instead */
printf("%s\n", state->search);
}

state->exit = EXIT_SUCCESS;
} else {
/* No match, output the query instead */
printf("%s\n", state->search);
}

state->exit = EXIT_SUCCESS;
if (selection) {
strcpy(state->options->p_exec,selection);
system(state->options->exec);
}

}
}

static void action_del_char(tty_interface_t *state) {
Expand Down