From a0eacf4607f0262f6c3bb6ddf84113253b76d477 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Eric=20Janouch?= Date: Wed, 21 Oct 2020 08:18:39 +0200 Subject: Add an installation script Copying snippets from the README was uncomfortable and laborious, and wasted a lot of space in the document, especially after the recent additions. Closes #3 --- CMakeLists.txt | 1 + README.adoc | 120 ++++----------------------------------- sdn-install | 176 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ sdn.cpp | 1 + 4 files changed, 188 insertions(+), 110 deletions(-) create mode 100755 sdn-install diff --git a/CMakeLists.txt b/CMakeLists.txt index 9c6d06b..e6cba04 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,6 +27,7 @@ target_compile_definitions (${PROJECT_NAME} PUBLIC include (GNUInstallDirs) install (TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR}) +install (PROGRAMS ${PROJECT_NAME}-install DESTINATION ${CMAKE_INSTALL_BINDIR}) install (FILES LICENSE DESTINATION ${CMAKE_INSTALL_DOCDIR}) set (CPACK_PACKAGE_DESCRIPTION_SUMMARY "Directory navigator") diff --git a/README.adoc b/README.adoc index bedacd6..9c162a6 100644 --- a/README.adoc +++ b/README.adoc @@ -47,117 +47,17 @@ into the PATH of any machine you want to have 'sdn' on. Integration ----------- +The package contains an installation script called 'sdn-install' which will bind +'sdn' to M-o in your shell's initialisation file. The supported shells are: -zsh -~~~ -To start using this navigator, put the following in your '.zshrc': - ----- -sdn-navigate () { - # ... possibly zle-line-finish - while eval "`sdn "$BUFFER" "$CURSOR"`"; do - [ -z "$cd" ] || cd "$cd" - [ -z "$insert" ] || LBUFFER="$LBUFFER$insert " - [ -z "$helper" ] && break - - # Workaround for "zsh: suspended (tty output)" when invoking - # helpers after the terminal has been resized while running sdn - command true - - /bin/sh -c "$helper" /dev/tty 2>&1 } - - # XXX: the -dot is not a stable API, and may hence break soon - local:buffer = $edit:current-command - local:cursor = (str:to-codepoints $buffer[0..$edit:-dot] | count) - local:ns = (ns [&]) - while ?(eval ($reesc (sdn $buffer $cursor | - sed 's/^local //' | slurp)) &ns=$ns) { - if (not-eq $ns[cd] "") { cd $ns[cd] } - if (not-eq $ns[insert] "") { edit:insert-at-dot $ns[insert]" " } - if (or (eq $ns[helper] "") (not ?($posix $ns[helper]))) { break } - } - edit:redraw &full=$true -} ----- - -This shell is absolutely perverse. And so is integrating 'sdn' into it because -it already includes a custom file manager, bound to Ctrl-N (though I find -the ranger-like interface confusing and resource-demanding). Version 0.14.1 or -newer is required. + - *zsh*: works well + - *bash*: minor issue: exiting the navigator confirms an empty prompt + - *fish*: works well + - *elvish*: version 0.14.1 and above, an unstable API is used, works well + +elvish is absolutely perverse. And so is integrating 'sdn' into it because it +already includes a custom file manager, bound to Ctrl-N (though I find the +ranger-like interface confusing and resource-demanding). Configuration ------------- diff --git a/sdn-install b/sdn-install new file mode 100755 index 0000000..954b061 --- /dev/null +++ b/sdn-install @@ -0,0 +1,176 @@ +#!/bin/sh -e +# sdn-install: integrate sdn with the shell, binding to M-o +# vim: set sw=2 ts=2 sts=2 et tw=80: + +zsh() { +cat <<'EOF' +sdn-navigate () { + # ... possibly zle-line-finish + while eval "`SDN=1 sdn "$BUFFER" "$CURSOR"`" + do + [ -z "$cd" ] || cd "$cd" + [ -z "$insert" ] || LBUFFER="$LBUFFER$insert " + [ -z "$helper" ] && break + + # Workaround for "zsh: suspended (tty output)" when invoking + # helpers after the terminal has been resized while running sdn + command true + + /bin/sh -c "$helper" /dev/tty 2>&1 } + + # XXX: the -dot is not a stable API, and may hence break soon + local:buffer = $edit:current-command + local:cursor = (str:to-codepoints $buffer[0..$edit:-dot] | count) + local:ns = (ns [&]) + while ?(eval ($reesc (E:SDN=1 sdn $buffer $cursor | + sed 's/^local //' | slurp)) &ns=$ns) { + if (not-eq $ns[cd] "") { cd $ns[cd] } + if (not-eq $ns[insert] "") { edit:insert-at-dot $ns[insert]" " } + if (or (eq $ns[helper] "") (not ?($posix $ns[helper]))) { break } + } + edit:redraw &full=$true +} +EOF +} + +shell= path= +while getopts s:f:h opt +do + case $opt in + s) shell=$OPTARG;; + f) path=$OPTARG;; + *) echo "Usage: $0 [-s SHELL] [-f RCPATH | -]"; exit 2 + esac +done + +# Figure out the shell to integrate with +login=$(basename "$SHELL") +actual=$(ps -p $$ -o ppid= | xargs ps -o comm= -p) +if [ -z "$shell" ] +then + if [ "$login" != "$actual" ] + then + echo "Conflict between login ($actual) and invoking ($actual) shell." + echo "Specify the shell with the -s option." + exit 1 + fi + shell=$actual +fi + +# Figure out the default initialisation file +case "$shell" in +zsh|bash) + rc=~/.${shell}rc;; +fish) + rc=${XDG_CONFIG_HOME:-$HOME/.config}/fish/conf.d/sdn.fish;; +elvish) + rc=~/.elvish/rc.elv;; +*) + echo "$shell is not supported." + exit 1 +esac + +# Just print out the snippet if requested +if [ "$path" = "-" ] +then + $shell + exit 0 +fi + +# Finally append to or update the appropriate file +b="# sdn-install begin" +e="# sdn-install end" +[ -z "$path" ] && path=$rc +mkdir -p "$(dirname "$path")" +touch "$path" + +if ! grep -q "^$b" "$path" 2>/dev/null || ! grep -q "^$e" "$path" 2>/dev/null +then + printf "\n$b\n%s\n$e\n" "$($shell)" >> "$path" + echo "The snippet has been added to $path" + exit 0 +fi + +# POSIX-compliant in-place sed, trying to retain permissions here +temp=$path.sdn.new +cp -p -- "$path" "$temp" +sed < "$path" > "$temp" "/^$b/,/^$e/c\\ +$b\\ +$($shell | sed 's/\\/&&/g; s/$/\\/') +$e" +mv -- "$temp" "$path" +echo "The snippet in $path has been updated." diff --git a/sdn.cpp b/sdn.cpp index ff1e397..5222df8 100644 --- a/sdn.cpp +++ b/sdn.cpp @@ -1717,6 +1717,7 @@ int main (int argc, char *argv[]) { // We can't portably create a standard stream from an FD, so modify the FD dup2 (output_fd, STDOUT_FILENO); + // TODO: avoid printing any of this unless the SDN envvar is set if (g.cwd != g.start_dir && !g.no_chdir) cout << "local cd=" << shell_escape (g.cwd) << endl; else -- cgit v1.2.3