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

Add new option 'all' to output all matches #32

Open
wants to merge 3 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
8 changes: 7 additions & 1 deletion src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ static const char *usage_str =
""
"Usage: fzy [OPTION]...\n"
" -l, --lines=LINES Specify how many lines of results to show (default 10)\n"
" -a, --all Print all matched lines\n"

Choose a reason for hiding this comment

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

Indentation?

Copy link
Author

Choose a reason for hiding this comment

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

My mistake, vim inserted a tab and I didn't notice the other lines were spaces.

Choose a reason for hiding this comment

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

:set smarttab in your vim config.

" -p, --prompt=PROMPT Input prompt (default '> ')\n"
" -q, --query=QUERY Use QUERY as the initial search string\n"
" -e, --show-matches=QUERY Output the sorted matches of QUERY\n"
Expand All @@ -26,6 +27,7 @@ static void usage(const char *argv0) {
static struct option longopts[] = {{"show-matches", required_argument, NULL, 'e'},
{"query", required_argument, NULL, 'q'},
{"lines", required_argument, NULL, 'l'},
{"all", no_argument, NULL, 'a'},
{"tty", required_argument, NULL, 't'},
{"prompt", required_argument, NULL, 'p'},
{"show-scores", no_argument, NULL, 's'},
Expand All @@ -43,6 +45,7 @@ void options_init(options_t *options) {
options->tty_filename = "/dev/tty";
options->show_scores = 0;
options->num_lines = 10;
options->output_all = 0;
options->scrolloff = 1;
options->prompt = "> ";
options->workers = 0;
Expand All @@ -52,7 +55,7 @@ void options_parse(options_t *options, int argc, char *argv[]) {
options_init(options);

int c;
while ((c = getopt_long(argc, argv, "vhse:q:l:t:p:j:", longopts, NULL)) != -1) {
while ((c = getopt_long(argc, argv, "vhsae:q:l:t:p:j:", longopts, NULL)) != -1) {
switch (c) {
case 'v':
printf("%s " VERSION " (c) 2014 John Hawthorn\n", argv[0]);
Expand Down Expand Up @@ -88,6 +91,9 @@ void options_parse(options_t *options, int argc, char *argv[]) {
exit(EXIT_FAILURE);
}
break;
case 'a':
options->output_all = 1;
break;
case 'l': {
int l;
if (!strcmp(optarg, "max")) {
Expand Down
1 change: 1 addition & 0 deletions src/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ typedef struct {
const char *tty_filename;
int show_scores;
unsigned int num_lines;
unsigned int output_all;
unsigned int scrolloff;
const char *prompt;
unsigned int workers;
Expand Down
19 changes: 12 additions & 7 deletions src/tty_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,18 @@ static void action_emit(tty_interface_t *state) {
/* 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);
} else {
/* No match, output the query instead */
printf("%s\n", state->search);
unsigned int all = state->options->output_all;
size_t start = all ? 0 : state->choices->selection;
size_t end = all ? choices_available(state->choices) : start + 1;

Choose a reason for hiding this comment

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

The rest of the code doesn't use whitespace to line up the = like that. Do you have an editor plugin that does it for you or do you have to do it manually? I'd be way too lazy for the latter :)

Copy link
Author

Choose a reason for hiding this comment

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

I use the easyalign-plugin for vim. The above alignment would be done in 3 keystrokes (after selecting the region of interest).

Choose a reason for hiding this comment

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

It sure is prettier, but I think it's bad. Say you add a fourth statement with a longer type and variable name, now you have to update 4 lines of code. If you just always use = then a single line of code won't involve changing others, keeping git diffs more readable. Yep, bad idea.

Choose a reason for hiding this comment

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

Plus having to select code and run the macro is also too much hassle, for it to be worth it (ignoring the source control issues) it would have to automatically detect consecutive assignments and update them all without needing any user selection.

Copy link
Author

Choose a reason for hiding this comment

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

Right. It's not important to me at all, I'd change that in a heart beat if it would stand in the way of the patch being accepted.

for (size_t i = start; i < end; i++) {
const char *selection = choices_get(state->choices, i);
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;
Expand Down