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
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() {
# guess cd, find first match in folder, or ask if multiple hits
local cdto QCDPATH
@@ -27,10 +35,10 @@ function hcd() {
local d
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
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/* /}
if [ ! -z "$d" ]
then \cd "$d"
@@ -47,12 +55,12 @@ function cd_history () {
return
fi
\cd "$1"
touch "$HOME/.bash_cdhistory"
old=$( tail -n 149 "$HOME/.bash_cdhistory" )
echo "$old" > "$HOME/.bash_cdhistory"
touch "$_QCD_HISTORY"
old=$( tail -n 499 "$_QCD_HISTORY" )
echo "$old" > "$_QCD_HISTORY"
p=$( pwd )
b=$( basename "$p" )
echo "$b:$p" >> "$HOME/.bash_cdhistory"
echo "$b:$p" >> "$_QCD_HISTORY"
}
alias cd=cd_history
@@ -63,44 +71,47 @@ function qcd() {
local opt
local case
local d
[ -e ~/.qcd ] || touch ~/.qcd
[ -e ~/.bash_cdhistory ] || touch ~/.bash_cdhistory
[ -e "$_QCD_BOOKMARKS" ] || touch "$_QCD_BOOKMARKS"
[ -e "$_QCD_HISTORY" ] || touch "$_QCD_HISTORY"
while getopts ae:hiILl:m opt
do case "$opt" in
a)
# Adding
local name=${!OPTIND}
# Remove / chars in name
name=${name//\//}
name=${name//:/}
if [ -z "$name" ]
then name=$( basename $( pwd ))
fi
echo "$name":$( pwd )
echo "$name":$( pwd ) >> ~/.qcd
echo "$name":$( pwd ) >> "$_QCD_BOOKMARKS"
return
;;
i)
case="-i"
;;
I)
touch ~/.qcd ~/.bash_cdhistory
touch "$_QCD_BOOKMARKS" "$_QCD_HISTORY"
return
;;
L)
echo '## History ~/.bash_cdhistory ##'
cat ~/.bash_cdhistory
echo "## History $_QCD_HISTORY ##"
cat "$_QCD_HISTORY"
echo
echo '## Saved ~/.qcd ##'
cat ~/.qcd
echo "## Saved $_QCD_BOOKMARKS ##"
cat "$_QCD_BOOKMARKS"
return
;;
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/*:/}
echo $d
return
;;
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/*:/}"
echo QCD=$d
QCD="$d"
@@ -108,27 +119,27 @@ function qcd() {
;;
m)
local IFS=$'\n'
touch ~/.qcd ~/.bash_cdhistory
touch ~/.qcd.tmp ~/.bash_cdhistory.tmp
for line in $( cat ~/.bash_cdhistory );
touch "$_QCD_BOOKMARKS" "$_QCD_HISTORY"
touch "$_QCD_BOOKMARKS".tmp "$_QCD_HISTORY".tmp
for line in $( cat "$_QCD_HISTORY" );
do if [ -d "${line/*:/}" ];
then echo "$line" >> ~/.bash_cdhistory.tmp
then echo "$line" >> "$_QCD_HISTORY".tmp
fi
done
mv ~/.bash_cdhistory.tmp ~/.bash_cdhistory
for line in $( cat ~/.qcd );
mv "$_QCD_HISTORY".tmp "$_QCD_HISTORY"
for line in $( cat "$_QCD_BOOKMARKS" );
do if [ -d "${line/*:/}" ];
then echo "$line" >> ~/.qcd.tmp
then echo "$line" >> "$_QCD_BOOKMARKS".tmp
fi
done
mv ~/.qcd.tmp ~/.qcd
mv "$_QCD_BOOKMARKS".tmp "$_QCD_BOOKMARKS"
return
;;
h)
echo 'qcd [-hiLm]|[-al] [name]
Version: 2018-11-16
Change current working path based on the list ~/.qcd
Keeps a history of folders visited in ~/.bash_cdhistory
Version: 2022-10-14
Change current working path based on the list '"$_QCD_BOOKMARKS"'
Keeps a history of folders visited in '"$_QCD_HISTORY"'
-a [name] Adds the path to the list
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 OPTIND
if [ "$1" = "-" ]
then d=$( tail -n 1 ~/.bash_cdhistory )
then d=$( tail -n 1 "$_QCD_HISTORY" )
d=${d/*:/}
\cd "$d"
return
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/*:/}
if [ ! -z "$d" ]
then \cd "$d"
@@ -171,7 +182,7 @@ _qcd()
COMPREPLY=( $( compgen -W "-a -i -h -l -L -m" -- $cur ) )
return 0
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
# qcd ends

2
rc
View File

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