aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2021-04-16 23:06:23 -0700
committerBruce Hill <bruce@bruce-hill.com>2021-04-16 23:06:23 -0700
commit2c44abc2c4940c9db3e76f8f24de35dd88256848 (patch)
treef86ebc08e88becc4a24e2756c5535f16abc345d2
parent0597d77f3f49ac3ed1acecf88c6f6821ba166d15 (diff)
Deprecated "<selection>" in favor of a better system using symlinks
-rw-r--r--bb.c71
-rwxr-xr-xscripts/bbkeys21
2 files changed, 36 insertions, 56 deletions
diff --git a/bb.c b/bb.c
index 590b327..197189d 100644
--- a/bb.c
+++ b/bb.c
@@ -30,7 +30,7 @@
#define BB_NAME "bb"
#endif
-#define BB_VERSION "0.28.1"
+#define BB_VERSION "0.29.0"
#define MAX_BINDINGS 1024
#define SCROLLOFF MIN(5, (winsize.ws_row-4)/2)
@@ -456,18 +456,7 @@ static int populate_files(bb_t *bb, const char *path)
char pbuf[PATH_MAX] = {0}, prev[PATH_MAX] = {0};
strcpy(prev, bb->path);
- if (path == NULL) {
- pbuf[0] = '\0';
- } else if (strcmp(path, "<selection>") == 0) {
- strcpy(pbuf, path);
- } else if (strcmp(path, "..") == 0 && strcmp(bb->path, "<selection>") == 0) {
- if (!bb->prev_path[0]) return -1;
- strcpy(pbuf, bb->prev_path);
- if (chdir(pbuf)) {
- warn("Could not cd to: \"%s\"", pbuf);
- return -1;
- }
- } else {
+ if (path != NULL) {
if (!normalize_path(bb->path, path, pbuf))
warn("Could not normalize path: \"%s\"", path);
if (pbuf[strlen(pbuf)-1] != '/')
@@ -478,10 +467,8 @@ static int populate_files(bb_t *bb, const char *path)
}
}
- if (strcmp(bb->path, "<selection>") != 0) {
- strcpy(bb->prev_path, prev);
- setenv("BBPREVPATH", bb->prev_path, 1);
- }
+ strcpy(bb->prev_path, prev);
+ setenv("BBPREVPATH", bb->prev_path, 1);
bb->dirty = 1;
strcpy(bb->path, pbuf);
@@ -505,33 +492,24 @@ static int populate_files(bb_t *bb, const char *path)
return 0;
size_t space = 0;
- if (strcmp(bb->path, "<selection>") == 0) {
- for (entry_t *e = bb->selected; e; e = e->selected.next) {
- if ((size_t)bb->nfiles + 1 > space)
- bb->files = memcheck(realloc(bb->files, (space += 100)*sizeof(void*)));
- e->index = bb->nfiles;
- bb->files[bb->nfiles++] = e;
- }
- } else {
- glob_t globbuf = {0};
- char *pat, *tmpglob = memcheck(strdup(bb->globpats));
- while ((pat = strsep(&tmpglob, " ")) != NULL)
- glob(pat, GLOB_NOSORT|GLOB_APPEND, NULL, &globbuf);
- free(tmpglob);
- for (size_t i = 0; i < globbuf.gl_pathc; i++) {
- // Don't normalize path so we can get "." and ".."
- entry_t *entry = load_entry(bb, globbuf.gl_pathv[i]);
- if (!entry) {
- warn("Failed to load entry: '%s'", globbuf.gl_pathv[i]);
- continue;
- }
- entry->index = bb->nfiles;
- if ((size_t)bb->nfiles + 1 > space)
- bb->files = memcheck(realloc(bb->files, (space += 100)*sizeof(void*)));
- bb->files[bb->nfiles++] = entry;
+ glob_t globbuf = {0};
+ char *pat, *tmpglob = memcheck(strdup(bb->globpats));
+ while ((pat = strsep(&tmpglob, " ")) != NULL)
+ glob(pat, GLOB_NOSORT|GLOB_APPEND, NULL, &globbuf);
+ free(tmpglob);
+ for (size_t i = 0; i < globbuf.gl_pathc; i++) {
+ // Don't normalize path so we can get "." and ".."
+ entry_t *entry = load_entry(bb, globbuf.gl_pathv[i]);
+ if (!entry) {
+ warn("Failed to load entry: '%s'", globbuf.gl_pathv[i]);
+ continue;
}
- globfree(&globbuf);
+ entry->index = bb->nfiles;
+ if ((size_t)bb->nfiles + 1 > space)
+ bb->files = memcheck(realloc(bb->files, (space += 100)*sizeof(void*)));
+ bb->files[bb->nfiles++] = entry;
}
+ globfree(&globbuf);
// RNG is seeded with a hash of all the inodes in the current dir
// This hash algorithm is based on Python's frozenset hashing
@@ -699,16 +677,13 @@ static void run_bbcmd(bb_t *bb, const char *cmd)
} else if (matches_cmd(cmd, "glob:")) { // +glob:
set_globs(bb, value[0] ? value : "*");
populate_files(bb, bb->path);
- } else if (matches_cmd(cmd, "goto:")) { // +goto:
- entry_t *e = load_entry(bb, value);
+ } else if (matches_cmd(cmd, "goto:") || matches_cmd(cmd, "goto")) { // +goto:
+ if (!value && !bb->selected) return;
+ entry_t *e = load_entry(bb, value ? value : bb->selected->fullname);
if (!e) {
warn("Could not find file to go to: \"%s\"", value);
return;
}
- if (IS_VIEWED(e)) {
- set_cursor(bb, e->index);
- return;
- }
char pbuf[PATH_MAX];
strcpy(pbuf, e->fullname);
char *lastslash = strrchr(pbuf, '/');
diff --git a/scripts/bbkeys b/scripts/bbkeys
index bf5960a..ccfc374 100755
--- a/scripts/bbkeys
+++ b/scripts/bbkeys
@@ -36,7 +36,8 @@ bbcmd move:-1
bbcmd cd:..
## l,Right: Enter directory
-if [ -d "$BB" ]; then bbcmd cd:"$BB"; fi
+if [ -d "$BB" ]; then bbcmd cd:"$BB";
+elif [ -L "$BB" ]; then bbcmd goto:"$(readlink -f "$BB")"; fi
## Ctrl-f: Search for file
file="$(find . -mindepth 1 -printf '%P\0' | bbpick "Find: ")"
@@ -70,7 +71,7 @@ bbcmd cd:"$mark"
[ "$BBPREVPATH" ] && bbcmd cd:"$BBPREVPATH"
## ;: Show selected files
-bbcmd cd:'<selection>'
+printf '%s\n' "$@" | less
## 0: Go to intitial directory
bbcmd cd:"$BBINITIALPATH"
@@ -120,17 +121,21 @@ bbconfirm "Save the current settings? "
echo "bbcmd glob:'$BBGLOB' sort:'$BBSORT' columns:'$BBCOLUMNS' $BBINTERLEAVE" > "$XDG_DATA_HOME"/bb/settings.sh
## Ctrl-s: Save the selection
-[ $# -gt 0 ]
savename="$(bbask "Save selection as: ")"
-savename="${savename%.sel}.sel"
-mkdir -p "$XDG_DATA_HOME"/bb
-printf '%s\0' "$@" > "$XDG_DATA_HOME/bb/$savename"
+savedir="$XDG_DATA_HOME/bb/${savename%.sel}.sel"
+! [ -d "$savedir" ] || bbconfirm "Do you want to overwrite the existing save? "
+rm -rf "$savedir"
+[ $# -gt 0 ] && mkdir -p "$savedir"
+for f; do ln -sT "$f" "$savedir/${f##*/}" || exit 1; done
+echo "Saved."
+bbpause
## Ctrl-o: Open a saved selection
[ -d "$XDG_DATA_HOME"/bb ]
[ $# -gt 0 ] && bbconfirm "The current selection will be discarded. "
-loadpath="$(find "$XDG_DATA_HOME"/bb/ -mindepth 1 -name '*.sel' -printf '%P\0' | bbpick "Load selection: ")"
-bbcmd deselect select: <"$XDG_DATA_HOME/bb/$loadpath"
+loadpath="$(find "$XDG_DATA_HOME"/bb/ -mindepth 1 -maxdepth 1 -name '*.sel' -printf '%P\0' | bbpick "Load selection: ")"
+find "$XDG_DATA_HOME/bb/$loadpath" -mindepth 1 -maxdepth 1 -printf '%l\0' | bbcmd deselect select:
+bbcmd goto
## J: Spread selection down
bbcmd spread:+1