projects

code examples


Example: sparse-line

javascript example

#!/bin/env bash

ajlsfd=$(shopt -p expand_aliases)
shopt -u expand_aliases
if ! command -v git >/dev/null 2>&1
then
eval "${ajlsfd}"
return
else
eval "${ajlsfd}"
fi


[[ $PROMPT_COMMAND = "sparseline_ps1" ]] && return
unset _PS1
_PS1=$PS1
unset CDPATH

function getCommitCount() {
touch "${commits_path}.lock"
nohup command git rev-list --all --count | tee "${commits_path}"{,.old} >/dev/null 2>&1 &
wait
}


function sparseline_ps1()
{

# used between functions
declare commits_path



local __return_code=$?
local commits
# preserve shopt settings
prvOpt=$(shopt -p extglob nullglob|sed ':a;N;$!ba;s/\n/;/g')
shopt -s extglob;
shopt -s nullglob;
:
local sparseline_status
:
local path
local dirbase
local re
local S
local R
local B
local H
local T
local X
local C; # commit difference
local refcount
local W
:
local D
if [[ ${LC_ALL:-${LC_CTYPE:-$LANG}} == *UTF-8 ]]; then
D=$'\uE0B0'
S=$'\u25CF'
:
local _b_="\\[\\033[48;5;"
local _f_="\\[\\033[38;5;"
local __="\\[\\033[m\\]"


local r_="${_f_}219m\\]"
local _r="${_b_}219m\\]"

local b_="${_f_}33m\\]"
local _b="${_b_}33m\\]"

local g_="${_f_}40m\\]"
local _g="${_b_}40m\\]"

local k_="${_f_}232m\\]"
local _k="${_b_}232m\\]"

local w_="${_f_}231m\\]"

else
D='|'
S='•'
fi
:

for path in {'.','..','../..'}/.git/HEAD
do

[[ ! -r "${path}" ]] && continue
S="${g_}${S}"
[[ $__return_code -ne 0 ]] && S="${r_}×";

dirbase=$(dirname "${path}")
INODE=$(stat -f '%i' "${dirbase}")
commits_path="${TMPDIR}${INODE}/COMMITS"
mkdir -p "${TMPDIR}${INODE}"
touch "${commits_path}"{,".old"}



printf -v refcount '%s' "${dirbase}/refs/heads/"*

:
if [[ "${#refcount}" -eq 0 ]]
then
B=$'?????'
H=$'-------'
else
B=$(<"${path}")
B="${B//*\//}"
fi


# while read -r words
# do
# re=$"\\[remote \"(.*)\"\\]"
# [[ "${words}" =~ ${re} ]] && R="${BASH_REMATCH[1]}" && break
# done < "${dirbase:?}/config"


if [[ -r "${dirbase}/refs/heads/${B}" ]]
then
H=$(<"${dirbase}/refs/heads/${B}")
H="${H:0:7}"
else
B=$'<detached>'
#B="${B}"
:
fi


#R="${R:-remote?}"



# give default value if otherwise undefined

# if [[ -r "${dirbase}/refs/heads/${B}" ]]
# then
# H=$(<"${dirbase}/refs/heads/${B}")
# H="${H:0:7}"

# fi
# elif [[ "${#B}" -gt 0 ]]
# then
# :
# H=$'-------'
# else
# B="<${B:0:7}>"
# H="$B"
# #
# # B="${B:0:7}"
# fi
for tag_path in ${dirbase}/refs/tags/*
do
X=$(<"${tag_path}")
[[ $H == "${X:0:7}" ]] && \
T=$(basename "${tag_path}") && \
H="${T}"
done

:
if [[ ! -r "${commits_path}.lock" ]]
then
:
getCommitCount &
fi
# else
# rm "${commits_path}.lock"
# fi



commits=$(<"${commits_path}")
commits="${commits//[$'\t\r\n']}"



sparseline_status="${commits} ${_k} ${S} ${_b}${w_}${R:+$R/}${B}${H:+#$H}${C}${__}${b_}${D}${__}"

cd "$dirbase" || return
W=$(basename "$(dirname "${PWD}")")
cd - >/dev/null 2>&1 || return
done


export PS1="${W} ${sparseline_status}"
#${_PS1}"
# restore shopt settings
eval "${prvOpt}"
return 0
}
PROMPT_COMMAND="sparseline_ps1"

Example: sparse-line

sparse-line is a bash script that provides a dynamic PS1 prompt à la pureline. On a lower resource machine, one might prefer sparse-line as it limits PS1 augmentation to what can be achieved without invoking non-bash commands.

#!/bin/env bash

ajlsfd=$(shopt -p expand_aliases)
shopt -u expand_aliases
if ! command -v git >/dev/null 2>&1
then
eval "${ajlsfd}"
return
else
eval "${ajlsfd}"
fi


[[ $PROMPT_COMMAND = "sparseline_ps1" ]] && return
unset _PS1
_PS1=$PS1
unset CDPATH

function getCommitCount() {
touch "${commits_path}.lock"
nohup command git rev-list --all --count | tee "${commits_path}"{,.old} >/dev/null 2>&1 &
wait
}


function sparseline_ps1()
{

# used between functions
declare commits_path



local __return_code=$?
local commits
# preserve shopt settings
prvOpt=$(shopt -p extglob nullglob|sed ':a;N;$!ba;s/\n/;/g')
shopt -s extglob;
shopt -s nullglob;
:
local sparseline_status
:
local path
local dirbase
local re
local S
local R
local B
local H
local T
local X
local C; # commit difference
local refcount
local W
:
local D
if [[ ${LC_ALL:-${LC_CTYPE:-$LANG}} == *UTF-8 ]]; then
D=$'\uE0B0'
S=$'\u25CF'
:
local _b_="\\[\\033[48;5;"
local _f_="\\[\\033[38;5;"
local __="\\[\\033[m\\]"


local r_="${_f_}219m\\]"
local _r="${_b_}219m\\]"

local b_="${_f_}33m\\]"
local _b="${_b_}33m\\]"

local g_="${_f_}40m\\]"
local _g="${_b_}40m\\]"

local k_="${_f_}232m\\]"
local _k="${_b_}232m\\]"

local w_="${_f_}231m\\]"

else
D='|'
S='•'
fi
:

for path in {'.','..','../..'}/.git/HEAD
do

[[ ! -r "${path}" ]] && continue
S="${g_}${S}"
[[ $__return_code -ne 0 ]] && S="${r_}×";

dirbase=$(dirname "${path}")
INODE=$(stat -f '%i' "${dirbase}")
commits_path="${TMPDIR}${INODE}/COMMITS"
mkdir -p "${TMPDIR}${INODE}"
touch "${commits_path}"{,".old"}



printf -v refcount '%s' "${dirbase}/refs/heads/"*

:
if [[ "${#refcount}" -eq 0 ]]
then
B=$'?????'
H=$'-------'
else
B=$(<"${path}")
B="${B//*\//}"
fi


# while read -r words
# do
# re=$"\\[remote \"(.*)\"\\]"
# [[ "${words}" =~ ${re} ]] && R="${BASH_REMATCH[1]}" && break
# done < "${dirbase:?}/config"


if [[ -r "${dirbase}/refs/heads/${B}" ]]
then
H=$(<"${dirbase}/refs/heads/${B}")
H="${H:0:7}"
else
B=$'<detached>'
#B="${B}"
:
fi


#R="${R:-remote?}"



# give default value if otherwise undefined

# if [[ -r "${dirbase}/refs/heads/${B}" ]]
# then
# H=$(<"${dirbase}/refs/heads/${B}")
# H="${H:0:7}"

# fi
# elif [[ "${#B}" -gt 0 ]]
# then
# :
# H=$'-------'
# else
# B="<${B:0:7}>"
# H="$B"
# #
# # B="${B:0:7}"
# fi
for tag_path in ${dirbase}/refs/tags/*
do
X=$(<"${tag_path}")
[[ $H == "${X:0:7}" ]] && \
T=$(basename "${tag_path}") && \
H="${T}"
done

:
if [[ ! -r "${commits_path}.lock" ]]
then
:
getCommitCount &
fi
# else
# rm "${commits_path}.lock"
# fi



commits=$(<"${commits_path}")
commits="${commits//[$'\t\r\n']}"



sparseline_status="${commits} ${_k} ${S} ${_b}${w_}${R:+$R/}${B}${H:+#$H}${C}${__}${b_}${D}${__}"

cd "$dirbase" || return
W=$(basename "$(dirname "${PWD}")")
cd - >/dev/null 2>&1 || return
done


export PS1="${W} ${sparseline_status}"
#${_PS1}"
# restore shopt settings
eval "${prvOpt}"
return 0
}
PROMPT_COMMAND="sparseline_ps1"

Example: rgb2true

#!/usr/bin/env bash

[[ -z $COLORTERM ]] && \
printf "Please use a True Color terminal!\n" && \
exit 1

print_usage() {
printf '\a\n%s\n\n\x1b[38;4m%s\x1b[0m\n%s\n\v%s\n' \
"Usage:$0 RRR [GGG] [BBB]" \
"Note" \
"This program requires at least one argument, taking up to three of type integer. Each argument must be in range [0,255]. If an argument is unspecified, a 0-value is assumed." \
"The default background color is black."
}

if [[ "$*" =~ -h ]] ; then
print_usage
exit 0
fi

rgb2true() {
R=0;G=0;B=0
if [[ $# -eq 3 ]]; then
R=$1; G=$2; B=$3
else
[[ -z $1 ]] && print_usage && return 1
R=$1
fi

for arg in $@; do
if [[ $arg -lt 0 || $arg -gt 255 ]]; then
printf '\a%s\n' "$0: Error...Argument '${arg}' does not fall in the range [0,255]." >&2
return 1
fi
done

printf "\x1b[48;2;0;0;0m"
printf "\x1b[38;2;${R};${G};${B}m"
printf "TRUECOLOR"
printf "\x1b[0m"
printf "\t\v"
printf '%s' "\x1b[38;2;${R};${G};${B}mTRUECOLOR\x1b[0m"
printf "\n\v\t\t"
printf '%s\n' "printf \"\x1b[38;2;${R};${G};${B}m%s\x1b[0m\n\" \"example text\""
printf "\n"

}
rgb2true "${@}"

Example: harbinger


#!/usr/bin/env bash
# https://developer.apple.com/library/content/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html

function harbinger() {
[[ ${#@} -eq 0 ]] && printf '%s\n' "harbinger <message>[,title][,subtitle]" && return 0

local A
IFS=',' read -r -a A <<< "${@}"

local MSG="${A[0]}"
local TITLE="${A[1]:+with title \"${A[1]}\"}"
local SUBTITLE="${A[2]:+subtitle \"${A[2]}\"}"
:
local CMD

CMD=$"display notification \"${MSG}\" ${TITLE} ${SUBTITLE}"

#printf '%s\n' "${CMD}"
osascript -e "${CMD}"

}

## per bpkg guidelines
if [[ ${BASH_SOURCE[0]} != "${0}" ]]; then
export -f harbinger
else
harbinger "${@}"
fi

Example: piece-of-site

<?PHP
$menu = Menu::simple()
->addItem("USA")
->addItem("Canada")
->addItem(Menu::subMenu("Cities")
->addItem("Toronto")
->addItem("Montreal")
)
->addItem(Menu::subMenu("Dinosaurs")
->addItem("Trex")
->addItem(Menu::subMenu("Pterosaur")
->addItem("Pteranodon")))
->addItem("Barf")
;
?>

bash plugins

Here's a fun concept -- plugins for bash. Well, not exactly. This is just a method I use for compartmentalizing functions.

#!/usr/bin/env bash

# load plugin files from, say, ~/.bash_profile
for file in ~/.bash/plugins/*.bash; do
[ -r "$file" ] && source "$file"
done
unset file

Example: whereami

This bit of code will use an online service to provide a geolocation for a given IP address.

To make this into a plugin, it's best to wrap the code into a function. The last few lines of code allow for the function to be both sourced or run from the command line. I was inspired by the great work of the folks behind bash package manager bpkg.

#!/usr/bin/env bash

# file stored as ~/.bash/plugins/whereami.bash

function whereami() (
/usr/bin/curl --disable ipinfo.io/region
)

if [[ ${BASH_SOURCE[0]} != $0 ]]; then
export -f whereami
else
whereami "${@}"
exit $?
fi

Disclaimer: code is provided for display purposes only "as is" and "with all faults."