breaking stuff with sqlite
This commit is contained in:
1
bin/sqlite3cat
Symbolic link
1
bin/sqlite3cat
Symbolic link
@@ -0,0 +1 @@
|
||||
../tsv/sqlite3cat
|
||||
@@ -21,7 +21,7 @@ for ((i=1; i<=${#@}; i++)) {
|
||||
}
|
||||
[[ "${#SRC[@]}" -lt 2 ]] && helpexit
|
||||
which pv &> /dev/null || { echo No \'pv\' installed; exit 1; }
|
||||
TGT=${SRC[${#SRC[@]}-1]}
|
||||
TGT="${SRC[${#SRC[@]}-1]}"
|
||||
unset 'SRC[${#SRC[@]}-1]'
|
||||
for path in ${SRC[@]}; do
|
||||
[[ -e "$path" ]] || { echo $path missing; exit 1; }
|
||||
|
||||
@@ -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
|
||||
|
||||
40
tsv/sqlite3cat
Executable file
40
tsv/sqlite3cat
Executable file
@@ -0,0 +1,40 @@
|
||||
#!/bin/bash
|
||||
help() {
|
||||
echo 'sqlite3cat: simple sqlite3 DB viewer.
|
||||
Usage: sqlite3cat [FILE] [TABLE/QUERY/-l]
|
||||
|
||||
first argument: sqlite3 file
|
||||
second, optional argument:
|
||||
TABLE: If not given, first in schema used.
|
||||
QUERY: If the second argument contains spaces, it is assumed to be a query.
|
||||
-l: List table names
|
||||
|
||||
'
|
||||
}
|
||||
[[ "$1" == "-h" ]] && {
|
||||
help; exit
|
||||
}
|
||||
[[ -f "$1" ]] || {
|
||||
help; exit
|
||||
}
|
||||
|
||||
sqlfile="$1"
|
||||
[[ "$2" == "-l" ]] && {
|
||||
sqlite3 "$sqlfile" "SELECT name FROM sqlite_master WHERE name NOT LIKE 'sqlite_%'"
|
||||
exit
|
||||
}
|
||||
|
||||
[[ -z "$2" ]] && {
|
||||
table=$( sqlite3 "$sqlfile" "SELECT name FROM sqlite_master WHERE name NOT LIKE 'sqlite%' LIMIT 1;" )
|
||||
} || {
|
||||
table="$2"
|
||||
}
|
||||
[[ "$table" = "${table% *}" ]] && {
|
||||
query="SELECT * FROM $table;"
|
||||
echo "$query"
|
||||
} || {
|
||||
query="$table"
|
||||
}
|
||||
|
||||
sqlite3 -header -column -nullvalue NA "$sqlfile" "$query"
|
||||
|
||||
@@ -17,12 +17,18 @@ _current_mounts() {
|
||||
cat /proc/mounts | grep fuse.rclone | awk '{ print $1 " " $2 }' | sed "s,$HOME,~," | xargs printf "%15s %s\n"
|
||||
}
|
||||
|
||||
_get_remotes() {
|
||||
if [[ -e ~/.config/rclone/rclone.conf ]]; then
|
||||
cat ~/.config/rclone/rclone.conf | awk '/^\[/ {gsub(/[\[\]]/,"",$1);print($1 ":") }'
|
||||
else
|
||||
rclone --ask-password=false listremotes
|
||||
fi
|
||||
}
|
||||
|
||||
_menu() {
|
||||
_current_mounts
|
||||
_askpass
|
||||
choice=$( rclone --ask-password=false listremotes | \
|
||||
sort | \
|
||||
choice=$( _get_remotes | \
|
||||
smenu -t 1 -a c:0/2 i:3 -n 25 -m "Select remote" \
|
||||
-N -D n:1 i:1 )
|
||||
|
||||
@@ -70,6 +76,12 @@ for (( i=1; i<=$#; i++ )); do
|
||||
fi
|
||||
done
|
||||
|
||||
if [[ -n "$remote" ]]; then
|
||||
# see that remote has "
|
||||
if [[ ! "$remote" == *":"* ]]; then
|
||||
remote="$remote":
|
||||
fi
|
||||
fi
|
||||
mkdir -p ~/mnt
|
||||
|
||||
valid_name=$( echo "${remote%:}" | sed -e 's/:/-/g' | sed -e 's/[^A-Za-z0-9._@-]//g' )
|
||||
|
||||
Reference in New Issue
Block a user