From 32506837ad63b7ffaedd1299fc5b60c52a813ef8 Mon Sep 17 00:00:00 2001 From: caandt Date: Wed, 11 Dec 2024 04:17:37 -0600 Subject: [PATCH] shell functions --- user/bin/edit | 2 +- user/config/bash/custom | 1 + user/config/bash/show | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 user/config/bash/show diff --git a/user/bin/edit b/user/bin/edit index e4b9fe9..d61054f 100755 --- a/user/bin/edit +++ b/user/bin/edit @@ -1,6 +1,6 @@ #!/bin/sh if [ "$#" -ne 1 ]; then - echo "usage: $0 " + echo "usage: ${0##*/} " exit 1 fi diff --git a/user/config/bash/custom b/user/config/bash/custom index 1a0830a..8593c52 100644 --- a/user/config/bash/custom +++ b/user/config/bash/custom @@ -24,6 +24,7 @@ _load history _load aliases # set xdg env vars to keep home directory clean _load xdg +_load show if _exists fzf; then export FZF_DEFAULT_OPTS="--bind=ctrl-u:page-up,ctrl-d:page-down" diff --git a/user/config/bash/show b/user/config/bash/show new file mode 100644 index 0000000..17bc40e --- /dev/null +++ b/user/config/bash/show @@ -0,0 +1,32 @@ +#!/usr/bin/env bash +function show() { + if [[ "$#" -ne 1 ]]; then + echo "usage: show " >&2 + return 1 + fi + case "$(type -t "$1")" in + alias) + alias "$1" | bat -pl sh ;; + keyword|builtin) + help "$1" ;; + function) + declare -f "$1" | bat -l sh ;; + file) + local path="$(which "$1")" + local real="$(realpath "$path")" + [[ "$path" != "$real" ]] && echo "$path" + bat "$real" ;; + "") + echo "$1 doesn't exist" ;; + esac +} +function goto() { + if [[ "$#" -ne 1 ]]; then + echo "usage: goto " >&2 + return 1 + fi + local path="$(which "$1")" + [[ -z "$path" ]] && return 2 + local real="$(realpath "$path")" + cd "${real%/*}" +}