config/.zsh/40_function.zsh

231 lines
6.3 KiB
Bash
Raw Normal View History

#Search on Google
google() {
emulate -L zsh
${=BROWSER} "http://www.google.com/search?&num=100&q=$*"
}
#Search French Wiktionary
wikdic_fr() {
emulate -L zsh
${=BROWSER} http://fr.wiktionary.org/wiki/${(C)1// /_}
}
#Search English Wiktionary
wikdic_en() {
emulate -L zsh
${=BROWSER} http://en.wiktionary.org/wiki/${(C)1// /_}
}
#Search French Wikipedia
wiki_fr() {
emulate -L zsh
${=BROWSER} "http://fr.wikipedia.org/w/index.php?title=Special%3ASearch&search=$*"
}
#Search English Wikipedia
wiki_en() {
emulate -L zsh
${=BROWSER} "http://en.wikipedia.org/w/index.php?title=Special%3ASearch&search=$*"
}
#French conjugaison
verbe() {
emulate -L zsh
${=BROWSER} "http://www.la-conjugaison.fr/du/verbe/${*}.php"
}
weather() {
emulate -L zsh
[[ -n "$1" ]] || {
print 'Usage: weather <station_id>' >&2
print 'List of stations: http://en.wikipedia.org/wiki/List_of_airports_by_ICAO_code'>&2
return 1
}
local VERBOSE="yes" # TODO: Make this a command line switch
local ODIR=`pwd`
local PLACE="${1:u}"
local DIR="${HOME}/.weather"
local LOG="${DIR}/log"
[[ -d ${DIR} ]] || {
print -n "Creating ${DIR}: "
mkdir ${DIR}
print 'done'
}
print "Retrieving information for ${PLACE}:"
print
cd ${DIR} && wget -T 10 --no-verbose --output-file=$LOG --timestamping http://weather.noaa.gov/pub/data/observations/metar/decoded/$PLACE.TXT
if [[ $? -eq 0 ]] ; then
if [[ -n "$VERBOSE" ]] ; then
cat ${PLACE}.TXT
else
DATE=$(grep 'UTC' ${PLACE}.TXT | sed 's#.* /##')
TEMPERATURE=$(awk '/Temperature/ { print $4" degree Celcius / " $2" degree Fahrenheit" }' ${PLACE}.TXT | tr -d '(')
echo "date: $DATE"
echo "temp: $TEMPERATURE"
fi
else
print "There was an error retrieving the weather information for $PLACE" >&2
cat $LOG
cd $ODIR
return 1
fi
cd $ODIR
}
battery() {
if [[ $BATTERY -gt 0 ]] ; then
PERCENT="${${"$(acpi 2>/dev/null)"}/(#b)[[:space:]]#Battery <->: [^0-9]##, (<->)%*/${match[1]}}"
if [[ -z "$PERCENT" ]] ; then
PERCENT='acpi not present'
else
if [[ "$PERCENT" -lt 20 ]] ; then
PERCENT="warning: ${PERCENT}%%"
else
PERCENT="${PERCENT}%%"
fi
fi
fi
}
2013-12-13 10:46:31 +01:00
mgrep () {
find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -regextype posix-egrep -iregex '(.*\/Makefile|.*\/Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -type f -print0 | xargs -0 grep --color -n "$@"
}
cgrep () {
2014-09-01 14:36:19 +02:00
find . -name .repo -prune -o -name .git -prune -o -type f \( -name '*.c' -o -name '*.cc' -o -name '*.cpp' -o -name '*.h' -o -name '*.hpp' \) -print0 | xargs -0 grep --color -n "$@"
2013-12-13 10:46:31 +01:00
}
jgrep () {
find . -name .repo -prune -o -name .git -prune -o -type f -name "*\.java" -print0 | xargs -0 grep --color -n "$@"
}
2014-12-22 16:22:01 +01:00
cjgrep () {
find . -name .repo -prune -o -name .git -prune -o -type f \( -name '*.c' -o -name '*.cc' -o -name '*.cpp' -o -name '*.h' -o -name '*.hpp' -o -name "*.java" \) -print0 | xargs -0 grep --color -n "$@"
}
2013-12-13 10:46:31 +01:00
resgrep () {
for dir in `find . -name .repo -prune -o -name .git -prune -o -name res -type d`
do
find $dir -type f -name '*\.xml' -print0 | xargs -0 grep --color -n "$@"
2015-11-25 11:35:22 +01:00
done
2013-12-13 10:46:31 +01:00
}
2015-11-25 11:35:22 +01:00
sgrep() {
find -E . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.(c|h|cc|cpp|S|java|xml|sh|mk|aidl)' -print0 | xargs -0 grep --color -n "$@"
}
mangrep() {
find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' -print0 | xargs -0 grep --color -n "$@"
}
sepgrep() {
find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d -print0 | xargs -0 grep --color -n -r --exclude-dir=\.git "$@"
}
gettop ()
{
2013-12-13 10:46:31 +01:00
local TOPFILE=build/core/envsetup.mk;
if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ]; then
echo $TOP;
else
if [ -f $TOPFILE ]; then
PWD= /bin/pwd;
else
local HERE=$PWD;
T=;
while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
\cd ..;
T=`PWD= /bin/pwd`;
done;
\cd $HERE;
if [ -f "$T/$TOPFILE" ]; then
echo $T;
fi;
fi;
fi
}
croot ()
{
T=$(gettop);
if [ "$T" ]; then
\cd $(gettop);
else
echo "Couldn't locate the top of the tree. Try setting TOP.";
fi
}
2014-08-02 01:08:24 +02:00
function zsh_stats() {
fc -l 1 | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n20
}
2014-12-22 16:22:01 +01:00
function secure_chromium {
echo "make sure to have run \"ssh -TND 4711 $USER@host\" before"
port=4711
export SOCKS_SERVER=localhost:$port
export SOCKS_VERSION=5
chromium-browser &
exit
}
2015-07-03 14:03:38 +02:00
apush() {
adb root && adb wait-for-device && adb remount && adb wait-for-device && adb push $1 $2
}
2015-06-11 11:18:17 +02:00
sdocker (){
DOCKER_SSH_AUTH=$HOME/docker_ssh_auth
2015-07-03 14:04:17 +02:00
AOSP_IMAGE=${AOSP_IMAGE:-$HOME/Project}
2015-07-03 14:08:24 +02:00
if [ $# -eq 0 ]; then
echo "Usage: $0 DOCKER_IMAGE_NAME"
return
fi
2015-06-11 11:18:17 +02:00
if [ -n "$SSH_AUTH_SOCK" ]; then
$(ls -l $HOME/docker_ssh_auth 2> /dev/null | grep $SSH_AUTH_SOCK > /dev/null)
if [ $? ];
then
unlink $DOCKER_SSH_AUTH > /dev/null
ln -s $SSH_AUTH_SOCK $DOCKER_SSH_AUTH
fi
SSH_AUTH_ARGS="-v $DOCKER_SSH_AUTH:/tmp/ssh_auth -e SSH_AUTH_SOCK=/tmp/ssh_auth"
2015-06-11 11:18:17 +02:00
fi
AOSP_ARGS=${AOSP_ARGS:--it}
img=$(docker ps | grep $1 | awk '{print $1}');
if [ -z $img ]; then
img=$(docker ps -a | grep $1 | awk '{print $1}') | head -1;
if [ -z $img ]; then
echo "Running a new container"
2015-07-03 14:04:17 +02:00
docker run ${AOSP_ARGS} ${=SSH_AUTH_ARGS} -v ${AOSP_IMAGE}:/aosp/Project $1
2015-06-11 11:18:17 +02:00
else
echo "Starting $img"
docker start -i -a $img
fi
else
echo "Attach to $img"
docker exec -it $img /bin/bash
fi
}
_sdocker () {
local cur
cur="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=( $(compgen -W "$(docker images | tail -n +2 | grep -v "<none>" | awk '{print $1}')" -- ${cur} ) )
}
complete -o default -F _sdocker sdocker
_flash_all () {
local cur
cur="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=( $(compgen -W "$(ls *.img)" -- ${cur} ) )
}
complete -o default -F _flash_all flash_all