Bash directory suggest

From EggeWiki
Revision as of 14:43, 17 April 2013 by Brianegge (talk | contribs) (Created page with "This cd replacement function will suggest locations based the on frequency you have switch to those directories. I started with the acd_func and enhanced it. == acd_func.sh =...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This cd replacement function will suggest locations based the on frequency you have switch to those directories. I started with the acd_func and enhanced it.

acd_func.sh

<geshi lang="bash">

  1. do ". acd_func.sh"
  2. acd_func 1.0.5, 10-nov-2004
  3. petar marinov, http:/geocities.com/h2428, this is public domain

cd_func () {

   local x2 the_new_dir adir index dir_history tmp;
   local -i cnt;
   if $1 == "--" ; then
       dirs -v;
       return 0;
   fi;
   the_new_dir=$1;
   -z $1  && the_new_dir=$HOME;
   if [[ ${the_new_dir:0:1} == '-' ]]; then
       index=${the_new_dir:1};
       -z $index  && index=1;
       adir=$(dirs +$index);
       -z $adir  && return 1;
       the_new_dir=$adir;
   fi;
   [[ ${the_new_dir:0:1} == '~' ]] && the_new_dir="${HOME}${the_new_dir:1}";
   dir_history="${HOME}/.bash/dirs-$(hostname)";
   builtin pushd "${the_new_dir}" >/dev/null;
   if $? -ne 0 ; then
       grep -q "${the_new_dir}" ${dir_history};
       if $? -eq 0 ; then
           [ -d /var/tmp/${USER} ] || mkdir -p /var/tmp/${USER};
           tmp=/var/tmp/${USER}/$$.out;
           # requires uniq >= 5.97
           ${HOME}/bin/choice.py <(grep "${the_new_dir}" ${dir_history} | sort | uniq -c | sort -rn | tr -s ' ' | cut -d' ' -f3 | while read d; do [ -d $d ] && echo $d; done | head -10) $tmp;
           if $? -eq 0 ; then
               the_new_dir=$(cat $tmp);
               unlink $tmp;
               cd_func "$the_new_dir";
               return $?;
           fi;
       else
           return 1;
       fi;
   fi;
   the_new_dir=$(pwd);
   echo $the_new_dir >>${dir_history};
   builtin popd -n +11 2>/dev/null >/dev/null;
   for ((cnt=1 ; cnt <= 10 ; cnt++))
   do
       x2=$(dirs +${cnt} 2>/dev/null);
       $? -ne 0  && return 0;
       [[ ${x2:0:1} == '~' ]] && x2="${HOME}${x2:1}";
       if [[ "${x2}" == "${the_new_dir}" ]]; then
           popd -n +$cnt 2>/dev/null >/dev/null;
           cnt=cnt-1;
       fi;
   done;
   return 0

} alias cd=cd_func

if [[ $BASH_VERSION > "2.05a" ]]; then

 # ctrl+w shows the menu
 bind -x "\"\C-w\":cd_func -- ;"

fi </geshi>


choice.py

<geshi lang="python">

  1. !/bin/python
  2. Tested with Python 2.64

import os, sys, re, collections, string

def choice():

   f = open(sys.argv[1])
   lines = f.readlines()
   i=0
   for line in lines:
       if i > 9:
           break
       print ' ' + str(i) + '  ' + line.rstrip()
       i += 1
   c=raw_input('Choose [0]-' + str(i - 1) + ': ')
   if len(c) == 0:
       c = 0
   else:
       c= int(c)
   if c >= 0 and c < i:
       w = open(sys.argv[2], 'w')
       w.write(lines[int(c)])
       return 0
   else:
       return 1


if __name__ == "__main__":

   sys.exit(choice())

</geshi>