Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
412 lines (395 sloc) 7.97 KB
#!/usr/bin/env bash
less()
{
typeset param parsed
typeset -a params
if (( $# == 1 )) && [[ -f "$1" ]] && (( $(wc -l < "$1") < ${LINES:-20} ))
then
cat $1
return 0
fi
for param in "$@"
do
case $param in
(*:*)
if [[ -f "$param" ]]
then
params+=( "$param" )
else
parsed=( ${param//:/ } )
params+=( "-N" "+j${parsed[1]}" "${parsed[0]}" )
fi
;;
(*)
params+=( "$param" )
;;
esac
done
command less "${params[@]}"
}
get()
{
typeset count url file
count=$1
url=$2
file=$3
if [[ -z "$count" ]]
then
echo "Usage: $0 [count] url [target]"
return 1
fi
if [[ -z "$url" ]]
then
url="$count"
count=2
fi
if [[ -z "$file" ]]
then
file="$(basename "${url%%\?*}")";
fi
echo "Save $url to $file in $count threads."
aria2c -c -x $count -j $count -s $count $url -o $file
}
if ! which wget > /dev/null 2>&1
then
wget()
{
curl $1 -o $(basename $1)
}
fi
if ! which ldd > /dev/null 2>&1 && which otool > /dev/null 2>&1
then
ldd()
{
otool -L "$@"
}
fi
d()
{
[[ -d $1 ]] || mkdir -p $1
cd $1
}
m()
{
typeset -a args
typeset filename
while [[ -n "$1" ]]
do
if [[ -f "$1" ]]
then filename="$1"
else args+=( "$1" )
fi
shift
done
if
[[ -f "${filename}.part" ]]
then
mpv --ontop --title "${filename}" "${args[@]}" "${filename}.part" # --osd-level=3
else
mpv --ontop --title "${filename}" "${args[@]}" "${filename}" # --osd-level=3
fi
}
ml()
{
typeset filename
while [[ $# > 0 ]]
do
filename="$1"
if [[ -f "$1.part" ]]
then filename+=".part"
fi
echo "$1# $(ffmpeg -i "$filename" 2>&1 | grep Duration)"
shift
done | column -s"#" -t
}
s2ram()
{
sudo true
(
if [[ -f /usr/lib/x86_64-linux-gnu/libexec/kscreenlocker_greet ]]
then /usr/lib/x86_64-linux-gnu/libexec/kscreenlocker_greet &
else /usr/lib64/libexec/kscreenlocker_greet &
fi
)
sleep 0.5
if [[ -f /lib/systemd/systemd-sleep ]]
then sudo /lib/systemd/systemd-sleep suspend
else sudo /usr/lib/systemd/systemd-sleep suspend
fi
}
gri()
{
git rebase --interactive "$@"
}
gwmpush()
{
git pull $@ && git pull bitbucket master $@ && git push && git push bitbucket master
}
gpp()
{
git pull $@ && git push
}
gpf()
{
git push --force-with-lease
}
gcaa()
{
git add .
git commit --amend --no-edit "$@"
}
gcaam()
{
git add .
git commit --amend "$*"
}
gcamp()
{
gcam "$@"
gpp
}
gcam()
{
git add .
git commit -m "$*"
}
gcm()
{
git commit -m "$*"
}
gt()
{
git tag -s "$1" -m "$1"
}
gf()
{
typeset command subcommand version
command=$1
subcommand=$2
version=${3:-}
case $command in
(r*) command=release ;;
(h*) command=hotfix ;;
esac
case $subcommand in
(s*) subcommand=start ;;
(f*) subcommand=finish ;;
esac
if [[ -z "$version" ]]
then
case "$command-$subcommand" in
(release-start) version=$(awk -F "[.-]" '{ if ($4=="") print $1"."$2+1".0"; else print $1"."$2"."$3;}' < VERSION ) ;; #'
(hotfix-start) version=$(awk -F "." '{ print $1"."$2"."$3+1}' < VERSION ) ;;
(*-finish) version=$(cat VERSION) ;;
esac
fi
git flow $command $subcommand $version
if [[ "$subcommand" == "start" ]]
then
echo $version > VERSION
git add VERSION
git commit -m $version
fi
}
show_git() {
typeset limit path top
limit=$(git config dir.limit 2>/dev/null)
path="$PWD"
top=$(git rev-parse --show-toplevel 2>/dev/null)
[[ -z "$limit" ]] && return 0
while (( limit >= 0 ))
do
case "$path" in
($top) return 0 ;;
(/|$HOME) return 1 ;;
esac
path="$( dirname "$path" )"
: $(( --limit ))
done
return 1
}
function pgd {
show_git && git status --porcelain 2>/dev/null |
sed 's/^\(.\)\(.\).*$/\1 \2/' | # ?? ... => ? ?
awk -v out="$1" '
BEGIN { x=0;y=0; }
$1!=" " && $1!="?"{ x++; }
$2!=" "{ y++; }
END{
if(out==""){
if(x>0)print "uncomitted"; else if (y>0) print "dirty";
}
else{
if(x>0||y>0)print out;
}
}
'
}
function pgb {
show_git && __git_ps1 "${1:-%s}" 2>/dev/null
}
function pgr {
show_git && git status 2>/dev/null | awk -v out="$1" '/# Your branch is / { if(out=="") printf $5; else printf out }' #'
}
function _rvm_curr {
typeset __rvm_curr
__rvm_curr=$(__rvm_env_string)
[[ -n "${__rvm_curr}" ]] && printf "${__rvm_curr##ruby-} "
}
function show_function() {
typeset -f $@
}
function edit_function() {
typeset body name
name="$*"
name="${name// /-}"
if
(( $# > 0 )) &&
show_function $@ >/dev/null
then
show_function $@ > ${TMPDIR:-/tmp}/__function_${name}_1.sh
cp -f ${TMPDIR:-/tmp}/__function_${name}_1.sh ${TMPDIR:-/tmp}/__function_${name}_2.sh
${EDITOR:-vim} ${TMPDIR:-/tmp}/__function_${name}_1.sh
diff -qwB ${TMPDIR:-/tmp}/__function_${name}_1.sh ${TMPDIR:-/tmp}/__function_${name}_2.sh >/dev/null ||
source ${TMPDIR:-/tmp}/__function_${name}_1.sh
rm -f ${TMPDIR:-/tmp}/__function_${name}_{1,2}.sh
else
echo "there is no function $name"
fi
}
function edit_variable() {
typeset body name
name="$*"
name="${name// /-}"
if
(( $# > 0 )) &&
eval "[[ -n \${$1} ]]"
then
set | grep -a "^$1=" > ${TMPDIR:-/tmp}/__function_${name}_1.sh
cp -f ${TMPDIR:-/tmp}/__function_${name}_1.sh ${TMPDIR:-/tmp}/__function_${name}_2.sh
${EDITOR:-vim} ${TMPDIR:-/tmp}/__function_${name}_1.sh
diff -qwB ${TMPDIR:-/tmp}/__function_${name}_1.sh ${TMPDIR:-/tmp}/__function_${name}_2.sh >/dev/null ||
source ${TMPDIR:-/tmp}/__function_${name}_1.sh
rm -f ${TMPDIR:-/tmp}/__function_${name}_{1,2}.sh
else
echo "there is no function $name"
fi
}
__setup_ssh_agent()
{
typeset agent_file
agent_file="$HOME/.ssh/agent-$HOSTNAME"
# check if already loaded
if
[[ -n "$SSH_AGENT_PID" ]] && ps -p "$SSH_AGENT_PID" | grep "ssh-agent" >/dev/null
then
# check if agent file is up to date
if
! grep -q SSH_AGENT_PID=$SSH_AGENT_PID $agent_file
then
# update it if needed
env | grep -E 'SSH_AGENT_PID=|SSH_AUTH_SOCK=' | sed 's/^/export /' > $agent_file
fi
# skip initialization
return 0
fi
if [[ -f $agent_file ]]
then source $agent_file >/dev/null
fi
if [[ -n "$SSH_AGENT_PID" ]] && ps -p $SSH_AGENT_PID | grep ssh-agent >/dev/null
then return 0
fi
ssh-agent > $agent_file
chmod 600 $agent_file
source $agent_file >/dev/null
if [[ -t 0 ]] && ! ssh-add -l | grep $HOME/.ssh/id_rsa >/dev/null
then
ssh-add
fi
}
__setup_gpg_agent()
{
export GPG_TTY=$(tty)
# skip if already loaded
if ps aux | grep "[g]pg-agent" >/dev/null
then return 0
fi
gpg-agent --daemon
}
function print_part()
{
(( $1 )) || return 0
typeset ret
ret="$1 $2"
(( $1 == 1 )) || ret="${ret}s"
printf "$ret "
}
function time_offset()
{
typeset years months days hours minutes seconds
minutes="$(( $1 / 60 % 60 ))"
hours="$(( $1 / 3600 % 24 ))"
days="$(( $1 / 86400 % 31 ))"
months="$(( $1 / 2678400 % 12 ))"
years="$(( $1 / 31536000 ))"
seconds="$(( $1 % 60 ))"
for part in year month day hour minute second
do eval "print_part \${${part}s} ${part}"
done
}
function n()
{
typeset return_status
typeset __n_time=$( date +%s )
if "$@"
then return_status=0
else return_status=$?
fi
__n_time=$(( $( date +%s ) - __n_time ))
if
(( __n_time > ${n_time:-10} ))
then
notify-send --app-name="N $1" "
Finished: $*
Took: $(time_offset __n_time)
Status: $return_status
"
fi;
return $return_status
}
__backtrace()
{
typeset counter
counter=0
while caller $counter
do : $(( counter+=1 ))
done
}
if ! which mcedit >/dev/null
then
mcedit()
{
vim "$@"
}
fi
decrypt()
{
dir="$(cd "$(dirname "${1:-$PWD}")" && pwd)" #"
name="${1%/}"
name="${name##*/}"
if
mount | grep -q "$dir/$name"
then
cd "$1"
else
encfs "$dir/.${name}-encrypted/" "$dir/$name" &&
cd "$1"
fi
}
fixblue()
{
#ubuntu
# bluetoothctl <<<"power off" && sudo hciconfig hci0 up
#opensuse
sudo /usr/sbin/modprobe -r btusb && sleep 1 && sudo /usr/sbin/modprobe btusb
}