Skip to content

Commit

Permalink
Merge pull request #8 from loicreynier/feat/no-bind-opt
Browse files Browse the repository at this point in the history
Add a `--no-bind` option for shell
  • Loading branch information
homerours authored Dec 18, 2024
2 parents 94092c6 + a9dc569 commit 3ea0aa8
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
12 changes: 9 additions & 3 deletions src/arguments.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static const char HELP_STRING[] =
" -e, --existing Only print files/directories that exist in the "
"file system.\n"
" -I, --case-insensitive Make the search case-insensitive.\n"
" -S, --case-sensitive Make the search case-sensititive.\n"
" -S, --case-sensitive Make the search case-sensitive.\n"
" -H, --home-tilde Substitute $HOME with ~ when printing "
"results.\n"
" -r, --relative=PATH Outputs relative paths to PATH if\n"
Expand All @@ -48,7 +48,8 @@ static const char HELP_STRING[] =
" -w, --weight=WEIGHT Weight of the visit (default=1.0).\n\n"
"MODE clean: remove entries that do not exist anymore.\n"
"MODE status: print databases' locations and some statistics.\n"
"MODE shell: print setup scripts. ARG has to be bash, zsh or fish.\n";
"MODE shell: print setup scripts. ARG has to be bash, zsh or fish.\n"
" -B, --no-bind Do not bind keys.\n";

static void help(const char *argv0) { printf(HELP_STRING, argv0); }

Expand All @@ -69,6 +70,7 @@ static struct option longopts[] = {{"file", required_argument, NULL, 'f'},
{"orderless", no_argument, NULL, 'o'},
{"existing", no_argument, NULL, 'e'},
{"type", required_argument, NULL, 't'},
{"no-bind", no_argument, NULL, 'B'},
{NULL, 0, NULL, 0}};

static void args_init(Arguments *args) {
Expand All @@ -90,6 +92,7 @@ static void args_init(Arguments *args) {
// score.
args->beta = 1.0;
args->weight = 1.0;
args->no_bind = false;
}

static MODE parse_mode(const char *mode) {
Expand Down Expand Up @@ -242,7 +245,7 @@ Arguments *parse_arguments(int argc, char **argv) {
optind++;
int c = 0;
while (optind < argc && c != -1) {
c = getopt_long(argc, argv, "csoeHISt:f:n:w:b:x:r::", longopts, NULL);
c = getopt_long(argc, argv, "csoeHISt:f:n:w:b:x:r::B", longopts, NULL);
if (c != -1) {
switch (c) {
case 'f':
Expand Down Expand Up @@ -321,6 +324,9 @@ Arguments *parse_arguments(int argc, char **argv) {
exit(EXIT_FAILURE);
}
break;
case 'B':
args->no_bind = true;
break;
default:
help(argv[0]);
exit(EXIT_SUCCESS);
Expand Down
1 change: 1 addition & 0 deletions src/arguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ typedef struct Arguments {
bool home_tilde;
bool orderless;
bool existing;
bool no_bind;
TYPE type;
int n_results;
const char *relative_to;
Expand Down
2 changes: 1 addition & 1 deletion src/jumper.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ int main(int argc, char **argv) {
} else if (args->mode == MODE_clean) {
clean_database(args);
} else if (args->mode == MODE_shell) {
shell_setup(args->key);
shell_setup(args->key, args->no_bind);
}
free(args);
return EXIT_SUCCESS;
Expand Down
24 changes: 15 additions & 9 deletions src/shell.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -136,7 +137,9 @@ static const char bash_functions[] =
static const char bash_bindings[] =
"bind -x '\"\\C-y\": jumper-find-dir'\n"
"stty kill undef\n"
"bind -x '\"\\C-u\": jumper-find-file'\n"
"bind -x '\"\\C-u\": jumper-find-file'\n";

static const char bash_prompt[] =
"PROMPT_COMMAND=\"__jumper_update_db;$PROMPT_COMMAND\"\n";

static const char zsh_variables[] =
Expand Down Expand Up @@ -276,10 +279,11 @@ static const char zsh_functions[] =
static const char zsh_bindings[] = "zle -N jumper-find-dir\n"
"zle -N jumper-find-file\n"
"bindkey '^Y' jumper-find-dir\n"
"bindkey '^U' jumper-find-file\n"
"precmd() {\n"
" __jumper_update_db\n"
"}\n";
"bindkey '^U' jumper-find-file\n";

static const char zsh_prompt[] = "precmd() {\n"
" __jumper_update_db\n"
"}\n";

static const char fish_variables[] =
"if not set -q __JUMPER_FLAGS\n"
Expand Down Expand Up @@ -404,19 +408,21 @@ static const char fish_functions[] =
static const char fish_bindings[] = "bind \\cy jumper-find-dir\n"
"bind \\cu jumper-find-file\n";

void shell_setup(const char *shell) {
void shell_setup(const char *shell, bool no_bind) {
if (strcmp(shell, "bash") == 0) {
printf(bash_variables);
printf(bash_functions);
printf(bash_bindings);
if (!no_bind) printf(bash_bindings);
printf(bash_prompt);
} else if (strcmp(shell, "zsh") == 0) {
printf(zsh_variables);
printf(zsh_functions);
printf(zsh_bindings);
if (!no_bind) printf(zsh_bindings);
printf(zsh_prompt);
} else if (strcmp(shell, "fish") == 0) {
printf(fish_variables);
printf(fish_functions);
printf(fish_bindings);
if (!no_bind) printf(fish_bindings);
} else {
fprintf(stderr, "ERROR: Invalid argument for shell: %s.\n", shell);
fprintf(stderr, "Accepted arguments: bash, zsh, fish.\n");
Expand Down
2 changes: 1 addition & 1 deletion src/shell.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#pragma once

void shell_setup(const char *shell);
void shell_setup(const char *shell, bool no_bind);

0 comments on commit 3ea0aa8

Please sign in to comment.