Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 99000d713c154e65c3453c58895bf54883665f0b @rauchg committed Mar 4, 2012
Showing with 194 additions and 0 deletions.
  1. +74 −0 README.md
  2. +120 −0 spot.sh
@@ -0,0 +1,74 @@
+
+## spot(1)
+
+Tiny ack-style file search utility.
+
+### Features
+
+* Short & written in Bash: you can edit it easily to suit your liking.
+* Fast. Just `find` + `grep`.
+* Searches most things by default instead of some known predefined extensions.
+* Ignores .git, .svn, devices and binary files.
+
+### Usage
+
+#### Smart phrases
+
+All arguments constitute the search text. No need to wrap most searches
+in double quotes.
+
+![](http://f.cl.ly/items/1Z063i0o3O2m0y2n0Q0d/Image%202012.03.04%2012:26:39%20PM.png)
+
+#### Smart case
+
+`spot` is case-insensitive by default. However, if your search term
+contains an uppercase letter, it becomes sensitive!
+
+![](http://f.cl.ly/items/2N332F0V302x1X47042c/Image%202012.03.04%2012:35:22%20PM.png)
+
+#### Smart targets
+
+If the first argument contains a slash _and_ is a valid directory, the
+search is constrained to that particular target.
+
+![](http://f.cl.ly/items/2u3x3T3j0B0q3s0T310t/Image%202012.03.04%2012:40:08%20PM.png)
+
+#### Options
+
+`spot -h` to see them.
+
+### Installation
+
+Run this command:
+
+```
+curl https://raw.github.com/guille/spot/master/spot.sh -o ~/bin/spot
+```
+
+If you don't have `~/bin`, replace it with another directory in your
+`$PATH`, like `/usr/local/bin`.
+
+### License
+
+(The MIT License)
+
+Copyright (c) 2011 Guillermo Rauch <guillermo@learnboost.com>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
120 spot.sh
@@ -0,0 +1,120 @@
+#!/usr/bin/env bash
+
+# search directory defaults to current
+dir=.
+
+# case sensitive search
+sensitive=
+
+# colors enabled by default in ttys
+if [ -t 1 ]; then
+ colors=1
+else
+ colors=
+fi
+
+# line numbers shown by default
+linenums=1
+
+# ansi colors
+cyan=`echo -e '\033[96m'`
+reset=`echo -e '\033[39m'`
+
+# usage info
+function usage {
+ cat <<EOF
+
+ Usage: spot [options] [directory] [term ...]
+
+ Options:
+
+ -s, --sensitive Force case sensitive search.
+ -i, --insensitive Force case insensitive search.
+ -C, --no-colors Force avoid colors.
+ -L, --no-linenums Hide line numbers.
+ -h, --help This message.
+
+EOF
+}
+
+# parse options
+while [[ "$1" =~ "-" ]]; do
+ case $1 in
+ -s | --sensitive )
+ sensitive=1
+ ;;
+ -i | --insensitive )
+ sensitive=
+ ;;
+ -C | --no-colors )
+ colors=
+ ;;
+ -L | --no-linenums )
+ linenums=
+ ;;
+ -h | --help )
+ usage
+ exit
+ ;;
+ esac
+ shift
+done
+
+# check for directory as first parameter
+if [[ "$1" =~ / ]]; then
+ if [ -d "$1" ]; then
+ dir=`echo $1 | sed "s/\/$//"`
+ shift
+ fi
+fi
+
+# check for empty search
+if [[ "" = "$@" ]]; then
+ echo "(no search term. \`spot -h\` for usage)"
+ exit 1
+fi
+
+# auto-detect case sensitive based on an uppercase letter
+if [[ "$@" =~ [A-Z] ]]; then
+ sensitive=1
+fi
+
+# grep default params
+grepopt="--binary-files=without-match --devices=skip"
+
+# add case insensitive search
+if [ ! $sensitive ]; then
+ grepopt="$grepopt --ignore-case"
+fi
+
+# add padding options
+if [ $padding ]; then
+ grepopt="$grepopt -A $padding -B $padding"
+fi
+
+# add line number options
+if [ $linenums ]; then
+ grepopt="$grepopt -n"
+fi
+
+# add force colors
+if [ $colors ]; then
+ grepopt="$grepopt --color=always"
+fi
+
+# run search
+if [ $colors ]; then
+ find $dir -type f ! -path '*/.git*' ! -path '*/.svn' -print0 \
+ | GREP_COLOR="1;33;40" xargs -0 grep $grepopt "`echo $@`" \
+ | sed "s/^\([^:]*:\)\(.*\)/ \\
+ $cyan\1$reset \\
+ \2 /"
+else
+ find $dir -type f ! -path '*/.git*' ! -path '*/.svn' -print0 \
+ | xargs -0 grep $grepopt "$@" \
+ | sed "s/^\([^:]*:\)\(.*\)/ \\
+ \1 \\
+ \2 /"
+fi
+
+echo ""

0 comments on commit 99000d7

Please sign in to comment.