breaking stuff with sqlite
This commit is contained in:
@@ -9,6 +9,11 @@ fi
|
||||
mkdir -p $HOME/.config/qcd/
|
||||
_QCD_HISTORY=$HOME/.config/qcd/cdhistory
|
||||
_QCD_BOOKMARKS=$HOME/.config/qcd/bookmarks
|
||||
if which sqlite3 &>/dev/null; then
|
||||
_QCD_DB=$HOME/.config/qcd/db
|
||||
sqlite3 "$_QCD_DB" "CREATE TABLE IF NOT EXISTS history (path TEXT PRIMARY KEY UNIQUE, key TEXT NOT NULL, time INT NOT NULL); CREATE TABLE IF NOT EXISTS bookmarks (key TEXT PRIMARY KEY UNIQUE, path TEXT NOT NULL, time INT NOT NULL);"
|
||||
fi
|
||||
|
||||
if [[ -e "$HOME/.qcd" ]]; then
|
||||
cat "$HOME/.qcd" >> "$_QCD_BOOKMARKS"
|
||||
mv "$HOME/.qcd" "$HOME/.qcd.bak"
|
||||
@@ -27,6 +32,10 @@ function gcd() {
|
||||
} # gcd ends
|
||||
|
||||
function hcd() {
|
||||
if [[ -n "Q_CD_DB" ]]; then
|
||||
hcd_sqlite "$@"
|
||||
return $?
|
||||
fi
|
||||
# History cd. Run without arguments to see list of entries, number as arg. to change directory.
|
||||
[[ "$1" = "-h" ]] && {
|
||||
echo History cd. Run without arguments to see list of entries, number as arg. to change directory.
|
||||
@@ -45,26 +54,167 @@ function hcd() {
|
||||
fi
|
||||
}
|
||||
|
||||
function hcd_sqlite() {
|
||||
# History cd. Run without arguments to see list of entries, number as arg. to change directory.
|
||||
[[ "$1" = "-h" ]] && {
|
||||
echo History cd. Run without arguments to see list of entries, number as arg. to change directory.
|
||||
return
|
||||
}
|
||||
local d
|
||||
|
||||
if [ -z "$1" ]; then
|
||||
sqlite3 -column "$_QCD_DB" "SELECT ROW_NUMBER() OVER (ORDER BY time DESC) AS row, path FROM history ORDER BY row LIMIT 20"
|
||||
return
|
||||
fi
|
||||
|
||||
d=$( sqlite3 -column "$_QCD_DB" "SELECT path FROM (SELECT ROW_NUMBER() OVER (ORDER BY time DESC) AS row, path FROM history) WHERE row = $1" )
|
||||
if [ ! -z "$d" ]
|
||||
then \cd "$d"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
function cd_history () {
|
||||
# Function that replaces "cd". It stores visited folder in ~/.bash_cdhistory
|
||||
local old
|
||||
local p
|
||||
local b
|
||||
if [ -z "$1" ]
|
||||
then \cd "$HOME"
|
||||
return
|
||||
return
|
||||
fi
|
||||
\cd "$1"
|
||||
touch "$_QCD_HISTORY"
|
||||
old=$( tail -n 499 "$_QCD_HISTORY" )
|
||||
echo "$old" > "$_QCD_HISTORY"
|
||||
p=$( pwd )
|
||||
b=$( basename "$p" )
|
||||
echo "$b:$p" >> "$_QCD_HISTORY"
|
||||
b=$( basename "$PWD" )
|
||||
if [[ -n "$_QCD_DB" ]]; then
|
||||
local cd_time
|
||||
printf -v cd_time "%(%s)T000" -1
|
||||
sqlite3 "$_QCD_DB" "INSERT OR REPLACE INTO history (path,key,time) VALUES ('$PWD','$b',$cd_time);" &>/dev/null || true
|
||||
else
|
||||
touch "$_QCD_HISTORY"
|
||||
old=$( tail -n 499 "$_QCD_HISTORY" )
|
||||
echo "$old" > "$_QCD_HISTORY"
|
||||
b=$( basename "$PWD" )
|
||||
echo "$b:$PWD" >> "$_QCD_HISTORY"
|
||||
fi
|
||||
}
|
||||
alias cd=cd_history
|
||||
|
||||
function qcd_sqlite() {
|
||||
# cd command, that jumps to folders visited in the near history, or user bookmarked folders
|
||||
local OPTIND
|
||||
local OPTARG
|
||||
local opt
|
||||
local case
|
||||
local d
|
||||
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
|
||||
local cd_time
|
||||
printf -v cd_time "%(%s)T000" -1
|
||||
sqlite3 "$_QCD_DB" "INSERT OR REPLACE INTO bookmarks (path,key,time) VALUES ('$PWD','$name',$cd_time);" &>/dev/null || true
|
||||
return
|
||||
;;
|
||||
i)
|
||||
case="-i"
|
||||
;;
|
||||
I)
|
||||
sqlite3 "$_QCD_DB" "CREATE TABLE IF NOT EXISTS history (path TEXT PRIMARY KEY UNIQUE, key TEXT NOT NULL, time INT NOT NULL); CREATE TABLE IF NOT EXISTS bookmarks (key TEXT PRIMARY KEY UNIQUE, path TEXT NOT NULL, time INT NOT NULL);"
|
||||
return
|
||||
;;
|
||||
L)
|
||||
echo "## History ##"
|
||||
sqlite3 -column "$_QCD_DB" "SELECT key,path FROM (SELECT key, path, time FROM history ORDER BY time DESC LIMIT 500) ORDER BY time"
|
||||
echo
|
||||
echo "## Bookmarks ##"
|
||||
sqlite3 -column "$_QCD_DB" "SELECT key, path FROM bookmarks ORDER BY key"
|
||||
return
|
||||
;;
|
||||
l)
|
||||
local d=$( sqlite3 "$_QCD_DB" "SELECT path FROM bookmarks WHERE key LIKE '$OPTARG%' LIMIT 1" )
|
||||
if [[ -z "$d" ]]; then
|
||||
d=$( sqlite3 "$_QCD_DB" "SELECT path FROM (SELECT key, path, time FROM history ORDER BY time DESC) WHERE key LIKE '$OPTARG%' LIMIT 1" )
|
||||
fi
|
||||
echo $d
|
||||
return
|
||||
;;
|
||||
e)
|
||||
local d=$( sqlite3 "$_QCD_DB" "SELECT path FROM bookmarks WHERE key LIKE '$OPTARG%' LIMIT 1" )
|
||||
if [[ -z "$d" ]]; then
|
||||
d=$( sqlite3 "$_QCD_DB" "SELECT path FROM (SELECT key, path, time FROM history ORDER BY time DESC) WHERE key LIKE '$OPTARG%' LIMIT 1" )
|
||||
fi
|
||||
echo QCD=$d
|
||||
QCD="$d"
|
||||
return
|
||||
;;
|
||||
m)
|
||||
sqlite3 -column "$_QCD_DB" "DROP TABLE IF EXISTS tmp_history"
|
||||
sqlite3 -column "$_QCD_DB" "CREATE TABLE tmp_history AS
|
||||
SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY time DESC) AS row,key,path,time FROM history ORDER BY row) WHERE row < 10000"
|
||||
sqlite3 -column "$_QCD_DB" "DELETE FROM history"
|
||||
sqlite3 -column "$_QCD_DB" "INSERT INTO history SELECT path,key,time FROM tmp_history"
|
||||
sqlite3 -column "$_QCD_DB" "DROP TABLE IF EXISTS tmp_history"
|
||||
|
||||
sqlite3 -column "$_QCD_DB" "CREATE TABLE IF NOT EXISTS tmp_notexists (path TEXT); DELETE FROM tmp_notexists;"
|
||||
local db_dir
|
||||
while read db_dir; do
|
||||
if [[ ! -d "$db_dir" ]]; then
|
||||
sqlite3 "$_QCD_DB" "INSERT INTO tmp_notexists (path) VALUES ('$db_dir');"
|
||||
fi
|
||||
done < <( sqlite3 "$_QCD_DB" "SELECT path FROM history" )
|
||||
sqlite3 -column "$_QCD_DB" "DELETE FROM history WHERE path IN ( SELECT path FROM tmp_notexists );"
|
||||
return
|
||||
;;
|
||||
h)
|
||||
echo 'qcd [-hiLm]|[-al] [name]
|
||||
Version: 2024-10-14
|
||||
Change current working path based on the sqlite3 db '"$_QCD_DB"'
|
||||
|
||||
-a [name] Adds the path to the list
|
||||
You may add the name of the path, but when omitted
|
||||
the basename will be used
|
||||
-e [name] Show the match, store in variable QCD
|
||||
-i Case insensitive search, must come first.
|
||||
-I Install (use only once)
|
||||
-l [name] Show the match, but do not change
|
||||
-L Lists the paths
|
||||
-m Maintain the list, deleting non-existing entries'
|
||||
return
|
||||
;;
|
||||
esac
|
||||
done
|
||||
shift $(($OPTIND - 1))
|
||||
if [ -z "$1" ]
|
||||
then [[ $OPTIND -gt 1 ]] && return
|
||||
\cd; return
|
||||
fi
|
||||
unset OPTSTRING
|
||||
unset OPTIND
|
||||
if [ "$1" = "-" ]
|
||||
then hcd_sqlite 1
|
||||
return
|
||||
fi
|
||||
local d=$( sqlite3 "$_QCD_DB" "SELECT path FROM bookmarks WHERE key LIKE '$1%' LIMIT 1" )
|
||||
if [[ -z "$d" ]]; then
|
||||
d=$( sqlite3 "$_QCD_DB" "SELECT path FROM (SELECT key, path, time FROM history ORDER BY time DESC) WHERE key LIKE '$1%' LIMIT 1" )
|
||||
fi
|
||||
if [ ! -z "$d" ]
|
||||
then \cd "$d"
|
||||
fi
|
||||
}
|
||||
function qcd() {
|
||||
if [[ -n "$_QCD_DB" ]]; then
|
||||
qcd_sqlite "$@"
|
||||
return $?
|
||||
fi
|
||||
# cd command, that jumps to folders visited in the near history, or user bookmarked folders
|
||||
local OPTIND
|
||||
local OPTARG
|
||||
@@ -138,7 +288,7 @@ function qcd() {
|
||||
h)
|
||||
echo 'qcd [-hiLm]|[-al] [name]
|
||||
Version: 2022-10-14
|
||||
Change current working path based on the list '"$_QCD_BOOKMARKS"'
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user