#!/usr/bin/sh # @author nate zhou # @since 2025,2026 # teleport-genesis - Generate keybindings to teleport in POSIX shells, and file # managers like lf and ranger. #set +x author="nate zhou" year="2025,2026" TELEPORT_CONF="$HOME/.config/shell/teleport.conf" TELEPORT_SHELL="$HOME/.config/shell/teleport.sh" TELEPORT_RANGER="$HOME/.config/ranger/teleport.conf" TELEPORT_LF="$HOME/.config/lf/teleport.conf" usage() { cat << _EOF_ [$(basename $0)] usage: -h,--help print this manual -b,--bash generate teleport.sh for bash -r,--ranger generate teleport.conf for ranger -l,--lf generate teleport.conf for lf -a,--all generate for all (this is the default behavior) _EOF_ } clear_up() { printf "# This file is auto generated by teleport-genesis\\n# @author $author\\n# @since $year\\n\\n" } gen_shell() { echo "clearing old shell bindings..." clear_up > "$TELEPORT_SHELL" while IFS= read -r line; do if [ -n "$line" ] && ! echo "$line" | grep -q '^#.*'; then parsed_key="$(echo "$line" | cut -d'=' -f1)" parsed_dir="$(echo "$line" | cut -d'"' -f2)" printf "alias %s=\"%s\"\\n" "$parsed_key" "$parsed_dir" >> "$TELEPORT_SHELL" fi done <$TELEPORT_CONF echo "teleport.sh generated for bash" } gen_ranger() { echo "clearing old shell bindings..." clear_up > "$TELEPORT_RANGER" local LEADER_CHAR="." # leader key for open in new tab while IFS= read -r line; do if [ -n "$line" ] && ! echo "$line" | grep -q '^#.*'; then parsed_key="$(echo $line | cut -d'=' -f1)" parsed_key_tab="${LEADER_CHAR}${parsed_key:1}" # remove backslashes: Ranger and Bash expect the exact opposite parsed_dir="$(echo $line | cut -d'"' -f2 | sed 's/\\//g')" printf "map %s cd %s\\n" "$parsed_key" "$parsed_dir" >> "$TELEPORT_RANGER" printf "map %s chain tab_new; cd %s\\n" "$parsed_key_tab" "$parsed_dir" >> "$TELEPORT_RANGER" fi done <$TELEPORT_CONF echo "teleport.conf generated for ranger" } gen_lf() { echo "clearing old shell bindings..." clear_up > "$TELEPORT_LF" while IFS= read -r line; do if [ -n "$line" ] && ! echo "$line" | grep -q '^#.*'; then parsed_key="$(echo $line | cut -d'=' -f1)" parsed_dir="$(echo "$line" | cut -d'"' -f2)" printf "map %s cd %s\\n" "$parsed_key" "$parsed_dir" >> "$TELEPORT_LF" fi done <$TELEPORT_CONF echo "teleport.conf generated for lf" } gen_all() { gen_shell #gen_ranger gen_lf } if [ ! -f "$TELEPORT_CONF" ]; then cat >&2 << _EOF_ error: $TELEPORT_CONF doesn't exist create it in the format of: Bh="~/doc/heart" _EOF_ exit 1 fi if [ "$#" -eq 0 ]; then gen_all exit 0 fi while [ -n "$1" ]; do case "$1" in -h | --help) usage exit 0 ;; -a | --all) gen_all exit 0 ;; -b | --bash) gen_shell ;; -r | --ranger) gen_ranger ;; -l | --lf) gen_lf ;; *) echo "invalid option" usage exit 1 esac shift done