Skip to content

Instantly share code, notes, and snippets.

View charlesthomas's full-sized avatar

Charles Thomas charlesthomas

View GitHub Profile
@charlesthomas
charlesthomas / check_module
Last active August 29, 2015 13:57
Check for the existence of a Perl module
#!/usr/bin/perl
use strict;
eval "use $ARGV[0]";
my $path=$ARGV[0];
$path =~ s/::/\//g;
$path.='.pm';
my $location=$INC{$path};
@charlesthomas
charlesthomas / osx_screen_locked
Last active August 29, 2015 13:57
Is OSX screen locked?
#!/usr/bin/python
from Quartz import CGSessionCopyCurrentDictionary
from sys import exit
d = CGSessionCopyCurrentDictionary()
exit(
d
and d.get("CGSSessionScreenIsLocked", 0) == 0
and d.get("kCGSSessionOnConsoleKey", 0) == 1
@charlesthomas
charlesthomas / url_decode
Last active August 29, 2015 13:57
Quick URL decode
#!/usr/bin/perl
use strict;
use URI::Escape;
print uri_unescape($ARGV[0]) . "\n";
exit;
@charlesthomas
charlesthomas / engage
Created March 21, 2014 20:42
engage: white noise generator mimics Enterprise background noise
alias engage='play -q -c2 -n synth whitenoise band -n 100 24 band -n 300 100 gain +20 &'
@charlesthomas
charlesthomas / pbcopy
Created March 21, 2014 20:46
OSX's pbcopy/pbpaste in Linux
# mimic osx pbcopy/pbpaste
alias pbcopy="xclip -selection clipboard -i"
alias pbpaste="xclip -selection clipboard -o"
@charlesthomas
charlesthomas / finf
Created March 21, 2014 20:48
finf: find text in files
function finf() {
find `pwd` \( \( -name .git -o -name '*.pyc' \) -prune \) \
-o \( -type f -print0 \) \
| xargs -0 grep -in --color "$@" 2>/dev/null;
}
@charlesthomas
charlesthomas / hl
Last active August 29, 2015 13:57
hl: highlight matching text
function hl() { egrep --color -ie $@ -e '$'; }
# example: `cat some_file | hl what_i_want_highlighted`
@charlesthomas
charlesthomas / last_exit_prompt
Last active August 29, 2015 13:57
color prompt based on exit status of previous command
NONE="\e[0m"
RED="\e[0;31m"
GREEN="\e[0;32m"
function last_exit_prompt() {
if [[ $? -eq 0 ]]; then
PS1="${PS1}\n\[${GREEN}\]>> \[${NONE}\]"
else
PS1="${PS1}\n\[${RED}\]>> \[${NONE}\]"
fi
@charlesthomas
charlesthomas / wipe_ve
Last active August 29, 2015 13:57
deactive Python virtualenv when the virtualenv's name is no longer in current working directory
function wipe_ve() {
if [ "$VIRTUAL_ENV" ] && [[ $(pwd) != *`basename $VIRTUAL_ENV`* ]]; then
deactivate
fi
}
PROMPT_COMMAND="wipe_ve;$PROMPT_COMMAND"
@charlesthomas
charlesthomas / gs
Created March 21, 2014 21:21
gs: alone: do git show / with argument: do git status
function gs() {
if [ -z $1 ]; then
git status
else
git show $1
fi
}