breaking stuff with sqlite

This commit is contained in:
Q
2024-09-16 14:15:45 +03:00
parent 71e155fcde
commit 2193eeebd6
5 changed files with 220 additions and 17 deletions

1
bin/sqlite3cat Symbolic link
View File

@@ -0,0 +1 @@
../tsv/sqlite3cat

View File

@@ -4,7 +4,7 @@ set -e
function helpexit() { function helpexit() {
echo Usage: $( basename "$0" ) [--nc] source [source] target echo Usage: $( basename "$0" ) [--nc] source [source] target
echo Copy files with a progress bar to a folder. echo Copy files with a progress bar to a folder.
echo This command always echo This command always
echo '* recurses to folders!' echo '* recurses to folders!'
echo '* overwrites existing files' echo '* overwrites existing files'
echo '--nc Dont count bytes first' echo '--nc Dont count bytes first'
@@ -21,7 +21,7 @@ for ((i=1; i<=${#@}; i++)) {
} }
[[ "${#SRC[@]}" -lt 2 ]] && helpexit [[ "${#SRC[@]}" -lt 2 ]] && helpexit
which pv &> /dev/null || { echo No \'pv\' installed; exit 1; } which pv &> /dev/null || { echo No \'pv\' installed; exit 1; }
TGT=${SRC[${#SRC[@]}-1]} TGT="${SRC[${#SRC[@]}-1]}"
unset 'SRC[${#SRC[@]}-1]' unset 'SRC[${#SRC[@]}-1]'
for path in ${SRC[@]}; do for path in ${SRC[@]}; do
[[ -e "$path" ]] || { echo $path missing; exit 1; } [[ -e "$path" ]] || { echo $path missing; exit 1; }
@@ -47,9 +47,9 @@ copy_dir() {
chown --reference="$1" "$2" chown --reference="$1" "$2"
mysize=$( getsize "$1" ) mysize=$( getsize "$1" )
TGT_ABS=$( abs-path "$2" ) TGT_ABS=$( abs-path "$2" )
pushd "$1" &> /dev/null pushd "$1" &> /dev/null
tar -c "." | pv -s $mysize | tar -x --strip-components=1 -C "$TGT_ABS" tar -c "." | pv -s $mysize | tar -x --strip-components=1 -C "$TGT_ABS"
popd &> /dev/null popd &> /dev/null
} }
copy_to() { copy_to() {
@@ -73,6 +73,6 @@ mkdir -p "$TGT"
for SRC_THIS in "${SRC[@]}"; do for SRC_THIS in "${SRC[@]}"; do
SRC_BASE=$( basename "$SRC_THIS") SRC_BASE=$( basename "$SRC_THIS")
echo "$SRC_THIS" echo "$SRC_THIS"
copy_to "$SRC_THIS" "$TGT"/"$SRC_BASE" copy_to "$SRC_THIS" "$TGT"/"$SRC_BASE"
done done

View File

@@ -9,6 +9,11 @@ fi
mkdir -p $HOME/.config/qcd/ mkdir -p $HOME/.config/qcd/
_QCD_HISTORY=$HOME/.config/qcd/cdhistory _QCD_HISTORY=$HOME/.config/qcd/cdhistory
_QCD_BOOKMARKS=$HOME/.config/qcd/bookmarks _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 if [[ -e "$HOME/.qcd" ]]; then
cat "$HOME/.qcd" >> "$_QCD_BOOKMARKS" cat "$HOME/.qcd" >> "$_QCD_BOOKMARKS"
mv "$HOME/.qcd" "$HOME/.qcd.bak" mv "$HOME/.qcd" "$HOME/.qcd.bak"
@@ -27,6 +32,10 @@ function gcd() {
} # gcd ends } # gcd ends
function hcd() { 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. # History cd. Run without arguments to see list of entries, number as arg. to change directory.
[[ "$1" = "-h" ]] && { [[ "$1" = "-h" ]] && {
echo History cd. Run without arguments to see list of entries, number as arg. to change directory. echo History cd. Run without arguments to see list of entries, number as arg. to change directory.
@@ -45,26 +54,167 @@ function hcd() {
fi 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 cd_history () {
# Function that replaces "cd". It stores visited folder in ~/.bash_cdhistory # Function that replaces "cd". It stores visited folder in ~/.bash_cdhistory
local old local old
local p
local b local b
if [ -z "$1" ] if [ -z "$1" ]
then \cd "$HOME" then \cd "$HOME"
return return
fi fi
\cd "$1" \cd "$1"
touch "$_QCD_HISTORY" b=$( basename "$PWD" )
old=$( tail -n 499 "$_QCD_HISTORY" ) if [[ -n "$_QCD_DB" ]]; then
echo "$old" > "$_QCD_HISTORY" local cd_time
p=$( pwd ) printf -v cd_time "%(%s)T000" -1
b=$( basename "$p" ) sqlite3 "$_QCD_DB" "INSERT OR REPLACE INTO history (path,key,time) VALUES ('$PWD','$b',$cd_time);" &>/dev/null || true
echo "$b:$p" >> "$_QCD_HISTORY" 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 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() { 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 # cd command, that jumps to folders visited in the near history, or user bookmarked folders
local OPTIND local OPTIND
local OPTARG local OPTARG
@@ -138,7 +288,7 @@ function qcd() {
h) h)
echo 'qcd [-hiLm]|[-al] [name] echo 'qcd [-hiLm]|[-al] [name]
Version: 2022-10-14 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"' 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

40
tsv/sqlite3cat Executable file
View 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"

View File

@@ -17,12 +17,18 @@ _current_mounts() {
cat /proc/mounts | grep fuse.rclone | awk '{ print $1 " " $2 }' | sed "s,$HOME,~," | xargs printf "%15s %s\n" 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() { _menu() {
_current_mounts _current_mounts
_askpass _askpass
choice=$( rclone --ask-password=false listremotes | \ choice=$( _get_remotes | \
sort | \
smenu -t 1 -a c:0/2 i:3 -n 25 -m "Select remote" \ smenu -t 1 -a c:0/2 i:3 -n 25 -m "Select remote" \
-N -D n:1 i:1 ) -N -D n:1 i:1 )
@@ -70,6 +76,12 @@ for (( i=1; i<=$#; i++ )); do
fi fi
done done
if [[ -n "$remote" ]]; then
# see that remote has "
if [[ ! "$remote" == *":"* ]]; then
remote="$remote":
fi
fi
mkdir -p ~/mnt mkdir -p ~/mnt
valid_name=$( echo "${remote%:}" | sed -e 's/:/-/g' | sed -e 's/[^A-Za-z0-9._@-]//g' ) valid_name=$( echo "${remote%:}" | sed -e 's/:/-/g' | sed -e 's/[^A-Za-z0-9._@-]//g' )