renaming functions file, modularizing qcd

This commit is contained in:
Ville Rantanen
2022-10-14 19:02:20 +03:00
parent 4344303e7c
commit 0aa277a921
2 changed files with 42 additions and 31 deletions

View File

@@ -6,6 +6,14 @@ if [[ "$OSTYPE" = "darwin"* ]]; then
_QCD_FIND=gfind _QCD_FIND=gfind
fi fi
mkdir -p $HOME/.config/qcd/
_QCD_HISTORY=$HOME/.config/qcd/cdhistory
_QCD_BOOKMARKS=$HOME/.config/qcd/bookmarks
if [[ -e "$HOME/.qcd" ]]; then
cat "$HOME/.qcd" >> "$_QCD_BOOKMARKS"
mv "$HOME/.qcd" "$HOME/.qcd.bak"
fi
function gcd() { function gcd() {
# guess cd, find first match in folder, or ask if multiple hits # guess cd, find first match in folder, or ask if multiple hits
local cdto QCDPATH local cdto QCDPATH
@@ -27,10 +35,10 @@ function hcd() {
local d local d
if [ -z "$1" ] if [ -z "$1" ]
then tail -n 20 "$HOME/.bash_cdhistory" | head -n 19 | cut -d: -f2 | tac | cat -n | sed 's,^ \+,,' | tac then tail -n 20 "$_QCD_HISTORY" | head -n 19 | cut -d: -f2 | tac | cat -n | sed 's,^ \+,,' | tac
return return
fi fi
d=$( tail -n 20 "$HOME/.bash_cdhistory" | head -n 19 | cut -d: -f2 | tac | cat -n | sed 's,^ \+,,' | grep -h "^$1 " ) d=$( tail -n 20 "$_QCD_HISTORY" | head -n 19 | cut -d: -f2 | tac | cat -n | sed 's,^ \+,,' | grep -h "^$1 " )
d=${d/* /} d=${d/* /}
if [ ! -z "$d" ] if [ ! -z "$d" ]
then \cd "$d" then \cd "$d"
@@ -47,12 +55,12 @@ function cd_history () {
return return
fi fi
\cd "$1" \cd "$1"
touch "$HOME/.bash_cdhistory" touch "$_QCD_HISTORY"
old=$( tail -n 149 "$HOME/.bash_cdhistory" ) old=$( tail -n 499 "$_QCD_HISTORY" )
echo "$old" > "$HOME/.bash_cdhistory" echo "$old" > "$_QCD_HISTORY"
p=$( pwd ) p=$( pwd )
b=$( basename "$p" ) b=$( basename "$p" )
echo "$b:$p" >> "$HOME/.bash_cdhistory" echo "$b:$p" >> "$_QCD_HISTORY"
} }
alias cd=cd_history alias cd=cd_history
@@ -63,44 +71,47 @@ function qcd() {
local opt local opt
local case local case
local d local d
[ -e ~/.qcd ] || touch ~/.qcd [ -e "$_QCD_BOOKMARKS" ] || touch "$_QCD_BOOKMARKS"
[ -e ~/.bash_cdhistory ] || touch ~/.bash_cdhistory [ -e "$_QCD_HISTORY" ] || touch "$_QCD_HISTORY"
while getopts ae:hiILl:m opt while getopts ae:hiILl:m opt
do case "$opt" in do case "$opt" in
a) a)
# Adding # Adding
local name=${!OPTIND} local name=${!OPTIND}
# Remove / chars in name
name=${name//\//}
name=${name//:/}
if [ -z "$name" ] if [ -z "$name" ]
then name=$( basename $( pwd )) then name=$( basename $( pwd ))
fi fi
echo "$name":$( pwd ) echo "$name":$( pwd )
echo "$name":$( pwd ) >> ~/.qcd echo "$name":$( pwd ) >> "$_QCD_BOOKMARKS"
return return
;; ;;
i) i)
case="-i" case="-i"
;; ;;
I) I)
touch ~/.qcd ~/.bash_cdhistory touch "$_QCD_BOOKMARKS" "$_QCD_HISTORY"
return return
;; ;;
L) L)
echo '## History ~/.bash_cdhistory ##' echo "## History $_QCD_HISTORY ##"
cat ~/.bash_cdhistory cat "$_QCD_HISTORY"
echo echo
echo '## Saved ~/.qcd ##' echo "## Saved $_QCD_BOOKMARKS ##"
cat ~/.qcd cat "$_QCD_BOOKMARKS"
return return
;; ;;
l) l)
local d=$( grep $case -h ^"$OPTARG" ~/.qcd <( tac ~/.bash_cdhistory ) | head -n 1 ) local d=$( grep $case -h ^"$OPTARG" "$_QCD_BOOKMARKS" <( tac "$_QCD_HISTORY" ) | head -n 1 )
d=${d/*:/} d=${d/*:/}
echo $d echo $d
return return
;; ;;
e) e)
local d=$( grep $case -h ^"$OPTARG" ~/.qcd <( tac ~/.bash_cdhistory ) | head -n 1 ) local d=$( grep $case -h ^"$OPTARG" "$_QCD_BOOKMARKS" <( tac "$_QCD_HISTORY" ) | head -n 1 )
d="${d/*:/}" d="${d/*:/}"
echo QCD=$d echo QCD=$d
QCD="$d" QCD="$d"
@@ -108,27 +119,27 @@ function qcd() {
;; ;;
m) m)
local IFS=$'\n' local IFS=$'\n'
touch ~/.qcd ~/.bash_cdhistory touch "$_QCD_BOOKMARKS" "$_QCD_HISTORY"
touch ~/.qcd.tmp ~/.bash_cdhistory.tmp touch "$_QCD_BOOKMARKS".tmp "$_QCD_HISTORY".tmp
for line in $( cat ~/.bash_cdhistory ); for line in $( cat "$_QCD_HISTORY" );
do if [ -d "${line/*:/}" ]; do if [ -d "${line/*:/}" ];
then echo "$line" >> ~/.bash_cdhistory.tmp then echo "$line" >> "$_QCD_HISTORY".tmp
fi fi
done done
mv ~/.bash_cdhistory.tmp ~/.bash_cdhistory mv "$_QCD_HISTORY".tmp "$_QCD_HISTORY"
for line in $( cat ~/.qcd ); for line in $( cat "$_QCD_BOOKMARKS" );
do if [ -d "${line/*:/}" ]; do if [ -d "${line/*:/}" ];
then echo "$line" >> ~/.qcd.tmp then echo "$line" >> "$_QCD_BOOKMARKS".tmp
fi fi
done done
mv ~/.qcd.tmp ~/.qcd mv "$_QCD_BOOKMARKS".tmp "$_QCD_BOOKMARKS"
return return
;; ;;
h) h)
echo 'qcd [-hiLm]|[-al] [name] echo 'qcd [-hiLm]|[-al] [name]
Version: 2018-11-16 Version: 2022-10-14
Change current working path based on the list ~/.qcd Change current working path based on the list '"$_QCD_BOOKMARKS"'
Keeps a history of folders visited in ~/.bash_cdhistory Keeps a history of folders visited in '"$_QCD_HISTORY"'
-a [name] Adds the path to the list -a [name] Adds the path to the list
You may add the name of the path, but when omitted You may add the name of the path, but when omitted
@@ -151,12 +162,12 @@ Keeps a history of folders visited in ~/.bash_cdhistory
unset OPTSTRING unset OPTSTRING
unset OPTIND unset OPTIND
if [ "$1" = "-" ] if [ "$1" = "-" ]
then d=$( tail -n 1 ~/.bash_cdhistory ) then d=$( tail -n 1 "$_QCD_HISTORY" )
d=${d/*:/} d=${d/*:/}
\cd "$d" \cd "$d"
return return
fi fi
d=$( grep $case -h ^"$1" ~/.qcd <( tac ~/.bash_cdhistory ) | head -n 1 ) d=$( grep $case -h ^"$1" "$_QCD_BOOKMARKS" <( tac "$_QCD_HISTORY" ) | head -n 1 )
d=${d/*:/} d=${d/*:/}
if [ ! -z "$d" ] if [ ! -z "$d" ]
then \cd "$d" then \cd "$d"
@@ -171,7 +182,7 @@ _qcd()
COMPREPLY=( $( compgen -W "-a -i -h -l -L -m" -- $cur ) ) COMPREPLY=( $( compgen -W "-a -i -h -l -L -m" -- $cur ) )
return 0 return 0
fi fi
COMPREPLY=( $( compgen -W "$( grep -h ^"$cur" ~/.qcd <( tac ~/.bash_cdhistory ) | cut -d: -f1 )" ) ) COMPREPLY=( $( compgen -W "$( grep -h ^"$cur" "$_QCD_BOOKMARKS" <( tac "$_QCD_HISTORY" ) | cut -d: -f1 )" ) )
} }
complete -F _qcd qcd complete -F _qcd qcd
# qcd ends # qcd ends

2
rc
View File

@@ -8,7 +8,7 @@ fi
PATH=$PATH:"$QTOOLSPATH"/bin PATH=$PATH:"$QTOOLSPATH"/bin
[[ "$1" = "-f" ]] && PATH="$QTOOLSPATH"/bin:$PATH [[ "$1" = "-f" ]] && PATH="$QTOOLSPATH"/bin:$PATH
. "$QTOOLSPATH"/qcd_function . "$QTOOLSPATH"/q-tools-functions
path_remove_duplicates path_remove_duplicates
[[ -f "$QTOOLSPATH"/tsv/tsvkit.sh ]] && . "$QTOOLSPATH"/tsv/tsvkit.sh &>/dev/null [[ -f "$QTOOLSPATH"/tsv/tsvkit.sh ]] && . "$QTOOLSPATH"/tsv/tsvkit.sh &>/dev/null