Last active
August 29, 2015 14:10
-
-
Save cr7pt0gr4ph7/068017a131fdbabeb69e to your computer and use it in GitHub Desktop.
Small utilities for working with bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# +-----------------+ | |
# | Path management | | |
# +-----------------+ | |
export ___ORIGINAL_PATH="${PATH}" | |
function get_path() { | |
echo "Current path: ${PATH}" | |
} | |
function set_path() { | |
export PATH="${1}" | |
echo "New path: ${PATH}" | |
} | |
function reset_path() { | |
set_path "${___ORIGINAL_PATH}" | |
} | |
function add_to_path() { | |
set_path "${PATH}:`pwd`" | |
} | |
# +---------------------+ | |
# | Directory bookmarks | | |
# +---------------------+ | |
# !defined(name) test is based upon: | |
# http://unix.stackexchange.com/questions/56837/how-to-test-if-a-variable-is-defined-at-all-in-bash-prior-to-version-4-2-with- | |
if [ ! -n "${___BOOKMARKS+1}" ]; then | |
export ___BOOKMARKS=() | |
fi | |
___CURRENT=${CURRENT:-0} | |
function ___resolve_path() { | |
echo "$( cd "$1" && pwd )" | |
} | |
function ___is_number() { | |
[[ "$1" =~ ^-?[0-9]+$ ]] && return 0 || return 1 | |
} | |
# Array operations are based upon: | |
# http://www.yourownlinux.com/2013/10/working-with-arrays-in-bash-scripting.html | |
function ___append_bookmark() { | |
if ___contains_bookmark "${1}"; then return -1; fi | |
local LENGTH=${#___BOOKMARKS[@]} | |
___BOOKMARKS[$LENGTH]="${1}" | |
___fix_current | |
} | |
function ___remove_bookmark() { | |
local idx=0 | |
for target in "${___BOOKMARKS[@]}"; do | |
if [ "${target}" == "${1}" ]; then | |
___BOOKMARKS=(${___BOOKMARKS[@]:0:$idx} ${___BOOKMARKS[@]:$(expr $idx + 1)}) | |
___fix_current | |
return 0; | |
fi | |
idx=$((idx+1)) | |
done | |
return 1 | |
} | |
function ___move_to_front() { | |
___remove_bookmark "${1}" || return -1 | |
___BOOKMARKS=("$1" "{___BOOKMARKS[@]}") | |
} | |
function ___update_completion() { | |
complete -W "${___BOOKMARKS[*]}" goto | |
} | |
# Update on loading (in case ___BOOKMARKS was set by a parent script) | |
___update_completion | |
function ___contains_bookmark() { | |
for target in "${___BOOKMARKS[@]}"; do | |
if [ "${target}" == "${1}" ]; then return 0; fi | |
done | |
return 1 | |
} | |
function ___index_of_bookmark() { | |
local i=0 | |
for target in "${___BOOKMARKS[@]}"; do | |
if [ "${target}" == "${1}" ]; then | |
echo "${i}" | |
return 0 | |
fi | |
i=$((i+1)) | |
done | |
return 1 | |
} | |
function ___fix_current() { | |
local COUNT=${#___BOOKMARKS[@]} | |
local MAX_INDEX=$((COUNT - 1)) | |
if [ ${___CURRENT} -gt ${MAX_INDEX} ]; then | |
___CURRENT=${MAX_INDEX} | |
fi | |
if [ ${___CURRENT} -lt 0 ]; then | |
___CURRENT=0 | |
fi | |
} | |
# List all bookmarks | |
function bookmarks() { | |
local i=0 | |
for target in "${___BOOKMARKS[@]}"; do | |
local marker=" " | |
if [ ${i} == ${___CURRENT} ]; then marker="*"; fi | |
echo "${marker} Bookmark ${i}: ${target}" | |
i=$((i+1)) | |
done | |
} | |
# Create a new bookmark | |
function mark() { | |
local DIR=`___resolve_path "${1:-.}"` | |
if [ "${DIR}" == "" ]; then return -1; fi | |
___append_bookmark "${DIR}" && \ | |
echo "New bookmark: ${DIR}" || \ | |
echo "Already present: ${DIR}" | |
___update_completion | |
} | |
# Remove a bookmark | |
function unmark() { | |
local dir_or_num="${1:-}" | |
if ___is_number "${dir_or_num}"; then | |
local DIR="${___BOOKMARKS[${dir_or_num}]}" | |
else | |
local DIR=`___resolve_path "${dir_or_num}"` | |
fi | |
if [ "${DIR}" == "" ]; then return -1; fi | |
___remove_bookmark "${DIR}" && \ | |
echo "Removed bookmark: ${DIR}" || \ | |
echo "No bookmark found: ${DIR}" | |
___update_completion | |
} | |
# Goto a bookmark | |
function goto() { | |
local dir_or_num="${1:-}" | |
if ___is_number "${dir_or_num}"; then | |
local DIR="${___BOOKMARKS[${dir_or_num}]}" | |
else | |
local DIR=`___resolve_path "${dir_or_num}"` | |
fi | |
if [ "${DIR}" == "" ]; then return -1; fi | |
# ___move_to_front "${DIR}" | |
___CURRENT=`___index_of_bookmark "${DIR}"` | |
cd "${DIR}" | |
___update_completion | |
} | |
# Navigate backward | |
function back() { | |
___CURRENT=$((___CURRENT - 1)) | |
___fix_current | |
goto ${___CURRENT} | |
} | |
# Navigate forward | |
function forward() { | |
___CURRENT=$((___CURRENT + 1)) | |
___fix_current | |
goto ${___CURRENT} | |
} | |
# +--------------+ | |
# | UI utilities | | |
# +--------------+ | |
function print_separator() { | |
# Term window detection is based upon: | |
# http://stackoverflow.com/questions/263890/how-do-i-find-the-width-height-of-a-terminal-window | |
local COLS="`tput cols`" | |
local TIMES="${1:-3}" | |
# Char repetition is based upon: | |
# http://stackoverflow.com/questions/5349718/how-can-i-repeat-a-character-in-bash | |
for i in `eval "echo {1..$TIMES}"`; do | |
printf '=%.0s' `eval "echo {1..$COLS}"` | |
echo "" | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment