From 4952e3fdd57322ff5ac9260e6dd0a8317f3df02c Mon Sep 17 00:00:00 2001 From: Nick Plekhanov Date: Sun, 4 Sep 2016 03:23:58 +0200 Subject: [PATCH] bash: Load handy binaries --- bin/crlf | 25 +++++++++++++ bin/dups | 55 +++++++++++++++++++++++++++ bin/git-delete-merged-branches | 32 ++++++++++++++++ bin/nyan | 24 ++++++++++++ bin/passive | 68 ++++++++++++++++++++++++++++++++++ bin/proofread | 27 ++++++++++++++ bin/ssh-key | 10 +++++ bin/weasel | 41 ++++++++++++++++++++ setup.sh | 41 +++++++++++++++++++- 9 files changed, 321 insertions(+), 2 deletions(-) create mode 100755 bin/crlf create mode 100755 bin/dups create mode 100755 bin/git-delete-merged-branches create mode 100755 bin/nyan create mode 100755 bin/passive create mode 100755 bin/proofread create mode 100755 bin/ssh-key create mode 100755 bin/weasel diff --git a/bin/crlf b/bin/crlf new file mode 100755 index 0000000..48efc9e --- /dev/null +++ b/bin/crlf @@ -0,0 +1,25 @@ +#!/usr/bin/env bash + +# Find files with Windows line endings (and convert them to Unix in force mode) +# +# Usage: +# crlf [file] [--force] + +local force= + +function _crlf_file() { + grep -q $'\x0D' "$1" && echo "$1" && [ $2 ] && dos2unix "$1" +} + +# Single file +if [ "$1" != "" ] && [ "$1" != "--force" ]; then + [ "$2" == "--force" ] && force=1 || force=0 + _crlf_file $1 $force + return +fi + +# All files +[ "$1" == "--force" ] && force=1 || force=0 +for file in $(find . -type f -not -path "*/.git/*" -not -path "*/node_modules/*" | xargs file | grep ASCII | cut -d: -f1); do + _crlf_file $file $force +done diff --git a/bin/dups b/bin/dups new file mode 100755 index 0000000..211ab64 --- /dev/null +++ b/bin/dups @@ -0,0 +1,55 @@ +#!/usr/bin/env perl + +# Finds duplicate adjacent words. +# http://matt.might.net/articles/shell-scripts-for-passive-voice-weasel-words-duplicates/ + +use strict ; + +my $DupCount = 0 ; + +if (!@ARGV) { + print "Usage: dups ...\n" ; + exit ; +} + +while (1) { + my $FileName = shift @ARGV ; + + # Exit code = number of duplicates found. + exit $DupCount if (!$FileName) ; + + open FILE, $FileName or die $!; + + my $LastWord = "" ; + my $LineNum = 0 ; + + while () { + chomp ; + + $LineNum ++ ; + + my @words = split (/(\W+)/) ; + + foreach my $word (@words) { + # Skip spaces: + next if $word =~ /^\s*$/ ; + + # Skip punctuation: + if ($word =~ /^\W+$/) { + $LastWord = "" ; + next ; + } + + # Found a dup? + if (lc($word) eq lc($LastWord)) { + print "$FileName:$LineNum $word\n" ; + $DupCount ++ ; + } # Thanks to Sean Cronin for tip on case. + + # Mark this as the last word: + $LastWord = $word ; + } + } + + close FILE ; +} diff --git a/bin/git-delete-merged-branches b/bin/git-delete-merged-branches new file mode 100755 index 0000000..a7da87a --- /dev/null +++ b/bin/git-delete-merged-branches @@ -0,0 +1,32 @@ +#!/bin/bash + +branches_to_die=$(git branch --no-color --merged origin/master | grep -v '\smaster$') +echo "Local branches to be deleted:" +echo "$branches_to_die" + +kill_branches () { + echo $branches_to_die | xargs -n 1 git branch -d +} + +remote_branches_to_die=$(git branch --no-color --remote --merged origin/master | grep -v '\smaster$' | grep -v '\/master$' | grep -v "origin\/HEAD" | grep -v "origin\/master") +echo "Remote branches to be deleted:" +echo "$remote_branches_to_die" + +kill_remote_branches () { + # Remove remote branches + for remote in $remote_branches_to_die + do + # branches=`echo "$remote_branches" | grep "$remote/" | sed 's/\(.*\)\/\(.*\)/:\2 /g' | tr -d '\n'` + git branch -rd "$remote" + done +} + +echo "" +echo "Enter Y to confirm" +read -p "> " confirm + +[[ $confirm == "Y" ]] && kill_branches && kill_remote_branches + +echo "" +echo "Pruning all remotes" +git remote | xargs -n 1 git remote prune diff --git a/bin/nyan b/bin/nyan new file mode 100755 index 0000000..5929327 --- /dev/null +++ b/bin/nyan @@ -0,0 +1,24 @@ +#!/usr/bin/env bash + +# Print nyan cat +# https://github.com/steckel/Git-Nyan-Graph/blob/master/nyan.sh +# If you want big animated version: `telnet miku.acm.uiuc.edu` + +RED="$(tput setaf 1)" +GREEN="$(tput setaf 2)" +YELLOW="$(tput setaf 3)" +CYAN="$(tput setaf 6)" +WHITE="$(tput setaf 7)" +BOLD="$(tput bold)" +NOCOLOR="$(tput sgr0)" + +echo +echo -en $RED'-_-_-_-_-_-_-_' +echo -e $NOCOLOR$BOLD$WHITE',------,'$NOCOLOR +echo -en $YELLOW$WHIT'_-_-_-_-_-_-_-' +echo -e $NOCOLOR$BOLD$WHITE'| /\_/\\'$NOCOLOR +echo -en $GREEN'-_-_-_-_-_-_-' +echo -e $NOCOLOR$BOLD$WHITE'~|__( ^ .^)'$NOCOLOR +echo -en $CYAN'-_-_-_-_-_-_-' +echo -e $NOCOLOR$BOLD$WHITE'"" ""'$NOCOLOR +echo diff --git a/bin/passive b/bin/passive new file mode 100755 index 0000000..e95c70c --- /dev/null +++ b/bin/passive @@ -0,0 +1,68 @@ +#!/usr/bin/env bash + +# Abuse of the passive voice +# http://matt.might.net/articles/shell-scripts-for-passive-voice-weasel-words-duplicates/ + +irregulars="awoken|\ +been|born|beat|\ +become|begun|bent|\ +beset|bet|bid|\ +bidden|bound|bitten|\ +bled|blown|broken|\ +bred|brought|broadcast|\ +built|burnt|burst|\ +bought|cast|caught|\ +chosen|clung|come|\ +cost|crept|cut|\ +dealt|dug|dived|\ +done|drawn|dreamt|\ +driven|drunk|eaten|fallen|\ +fed|felt|fought|found|\ +fit|fled|flung|flown|\ +forbidden|forgotten|\ +foregone|forgiven|\ +forsaken|frozen|\ +gotten|given|gone|\ +ground|grown|hung|\ +heard|hidden|hit|\ +held|hurt|kept|knelt|\ +knit|known|laid|led|\ +leapt|learnt|left|\ +lent|let|lain|lighted|\ +lost|made|meant|met|\ +misspelt|mistaken|mown|\ +overcome|overdone|overtaken|\ +overthrown|paid|pled|proven|\ +put|quit|read|rid|ridden|\ +rung|risen|run|sawn|said|\ +seen|sought|sold|sent|\ +set|sewn|shaken|shaven|\ +shorn|shed|shone|shod|\ +shot|shown|shrunk|shut|\ +sung|sunk|sat|slept|\ +slain|slid|slung|slit|\ +smitten|sown|spoken|sped|\ +spent|spilt|spun|spit|\ +split|spread|sprung|stood|\ +stolen|stuck|stung|stunk|\ +stridden|struck|strung|\ +striven|sworn|swept|\ +swollen|swum|swung|taken|\ +taught|torn|told|thought|\ +thrived|thrown|thrust|\ +trodden|understood|upheld|\ +upset|woken|worn|woven|\ +wed|wept|wound|won|\ +withheld|withstood|wrung|\ +written" + +if [ "$1" = "" ]; then + echo "Usage: `basename $0` ..." + exit +fi + +egrep -n -i --color \ + "\\b(am|are|were|being|is|been|was|be)\ +\\b[ ]*(\w+ed|($irregulars))\\b" $* + +exit $? diff --git a/bin/proofread b/bin/proofread new file mode 100755 index 0000000..24d77fb --- /dev/null +++ b/bin/proofread @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +# http://matt.might.net/articles/shell-scripts-for-passive-voice-weasel-words-duplicates/ + +# Common stuff +CYAN="$(tput setaf 6)" +UNDERLINE="$(tput sgr 0 1)" +NOCOLOR="$(tput sgr0)" +function header() { + echo -e "$UNDERLINE$CYAN$1$NOCOLOR" +} + +if [ "$1" = "" ]; then + echo "Usage: `basename $0` ..." + exit +fi + +header "Weasel words" +weasel $1 +echo + +header "Passive voice" +passive $1 +echo + +header "Duplicates" +dups $1 diff --git a/bin/ssh-key b/bin/ssh-key new file mode 100755 index 0000000..47549b7 --- /dev/null +++ b/bin/ssh-key @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +# Print public SSH key to clipboard. Generate it if necessary + +file="$HOME/.ssh/id_rsa.pub" +if [ ! -f "$file" ]; then + ssh-keygen -t rsa +fi + +cat "$file" diff --git a/bin/weasel b/bin/weasel new file mode 100755 index 0000000..052dacd --- /dev/null +++ b/bin/weasel @@ -0,0 +1,41 @@ +#!/usr/bin/env bash + +# Weasel words +# http://matt.might.net/articles/shell-scripts-for-passive-voice-weasel-words-duplicates/ + +weasels="many|various|very|fairly|several|extremely\ +|exceedingly|quite|remarkably|few|surprisingly\ +|mostly|largely|huge|tiny|((are|is) a number)\ +|excellent|interestingly|significantly\ +|substantially|clearly|vast|relatively|completely" + +wordfile="" + +# Check for an alternate weasel file +if [ -f $HOME/etc/words/weasels ]; then + wordfile="$HOME/etc/words/weasels" +fi + +if [ -f $WORDSDIR/weasels ]; then + wordfile="$WORDSDIR/weasels" +fi + +if [ -f words/weasels ]; then + wordfile="words/weasels" +fi + +if [ ! "$wordfile" = "" ]; then + weasels="xyzabc123"; + for w in `cat $wordfile`; do + weasels="$weasels|$w" + done +fi + +if [ "$1" = "" ]; then + echo "Usage: `basename $0` ..." + exit +fi + +egrep -i -n --color "\\b($weasels)\\b" $* + +exit $? diff --git a/setup.sh b/setup.sh index 9e802c8..424d01e 100755 --- a/setup.sh +++ b/setup.sh @@ -1,5 +1,7 @@ #!/bin/bash +# https://github.com/kaicataldo/dotfiles/blob/master/bin/install.sh + # This symlinks all the dotfiles (and .atom/) to ~/ # It also symlinks ~/bin for easy updating @@ -121,6 +123,20 @@ print_success() { printf "\e[0;32m [✔] $1\e[0m\n" } +# Warn user this script will overwrite current dotfiles +while true; do + read -p "Warning: this will overwrite your current dotfiles. Continue? [y/n] " yn + case $yn in + [Yy]* ) break;; + [Nn]* ) exit;; + * ) echo "Please answer yes or no.";; + esac +done + +# Get the dotfiles directory's absolute path +SCRIPT_DIR="$(cd "$(dirname "$0")"; pwd -P)" +DOTFILES_DIR="$(dirname "$SCRIPT_DIR")" + dir=~/dotfiles # dotfiles directory dir_backup=~/dotfiles_old # old dotfiles backup directory @@ -183,6 +199,7 @@ for i in ${FILES_TO_SYMLINK[@]}; do mv ~/.${i##*/} ~/dotfiles_old/ done + # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - main() { @@ -214,9 +231,29 @@ main() { done - # Copy batcharge + unset FILES_TO_SYMLINK + + # Copy binaries ln -fs $HOME/dotfiles/bin $HOME - chmod +rwx $HOME/bin/batcharge.py + + declare -a BINARIES=( + 'batcharge.py' + 'crlf' + 'dups' + 'git-delete-merged-branches' + 'nyan' + 'passive' + 'proofread' + 'ssh-key' + 'weasel' + ) + + for i in ${BINARIES[@]}; do + echo "Changing access permissions for binary script :: ${i##*/}" + chmod +rwx $HOME/bin/${i##*/} + done + + unset BINARIES # Symlink online-check.sh ln -fs $HOME/dotfiles/lib/online-check.sh $HOME/online-check.sh