From e2f2aa9caf50dbd0403ba8729b745ca5412633bf Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 6 Aug 2020 13:28:39 +0300 Subject: [PATCH] cd on exit, subshell stuff --- README | 25 +++++++++++++++++++++++++ shfm | 13 +++++++------ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/README b/README index 0475f59..2bb9336 100644 --- a/README +++ b/README @@ -15,6 +15,8 @@ ________________________________________________________________________________ * no compilation needed * correctly handles files with funky names (newlines, etc) * works with very small terminal sizes. +* cd on exit +* works when run in subshell $(shfm) *** see portability notes towards bottom of README. @@ -50,6 +52,29 @@ ________________________________________________________________________________ - [x] look into whether tput is feasible. +cd on exit +________________________________________________________________________________ + +On exit, the utility will print the path to working directory to . To +disable this behavior, run with 'shfm >/dev/null'. Usage of this output is +rather flexible. + + # cd to directory on exit + cd "$(shfm)" + + # store pwd in var on exit + var=$(shfm) + + # store pwd in a file one exit + shfm > file + +For ease of use, a wrapper function can be added to your .shellrc (.bashrc, etc). + + shfm() { + cd "$(command shfm)" + } + + opener ________________________________________________________________________________ diff --git a/shfm b/shfm index 42a4469..d896898 100755 --- a/shfm +++ b/shfm @@ -32,10 +32,13 @@ term_setup() { term_reset() { esc DECAWM h - esc DECTCEM h - esc ED2 + esc DECTCEM h >&2 + esc ED2 >&2 esc DECSTBM stty "$stty" + + # needed for cd-on-exit + printf '%s\n' "$PWD" >&1 } term_resize() { @@ -173,15 +176,13 @@ status_line() { *) esc SGR 34 7 ;; esac - printf '%s ' "($y/$1)" + printf '%*s\r%s ' "$COLUMNS" "" "($y/$1)" case $ltype in '') printf %s "$PWD" ;; *) printf %s "$ltype" esac - printf '%*s' "$COLUMNS" "" - esc SGR esc DECRC } @@ -401,4 +402,4 @@ main() { done } -main "$@" +main "$@" >/dev/tty