From 6476766a210727366470f23f58c38c21bba38120 Mon Sep 17 00:00:00 2001 From: Emre AKYUZ Date: Sat, 22 Apr 2023 11:46:47 +0300 Subject: [PATCH 01/12] Extremely Simple and Efficient Script I Think You Like The inspiration: (Luke Smith's Video: "Unix Chad JUST WON'T STOP Piping!" youtu.be/w_37upFE-qw Execution Time: 0.15s to 0.30s on my system (opening a video then closing it to see the output of "time" command) Very Short Summary: 1- This script feeds the "dmenu" with a list of all videos in a hard drive. 2- File paths and extensions are removed for better readability for the dmenu list. 3- Opens the chosen video file in "mpv" using the complete paths and extensions. 4- An approach of having the least amount of lines & characters while keeping the simplicity and performance has been used. updatedb command needs to be executed at first showing the database file "~/.config/.mymlocate.db" Or the script should be changed with a new path. For example: $updatedb -o ~/.config/.mymlocate.db -U /mnt/externaldrive Thoroughly detailed explanation for everyone: temp_file=$(mktemp): This command creates a temporary file using the mktemp command and stores its path in the temp_file variable. This temporary file will be used later to store the search results. locate -d ~/.config/.mymlocate.db -b -r '.*\.\(mp4\|mkv\...)$': This command searches for files with the ".mp4" or other extension in the database located at "~/.config/.mymlocate.db". The -d flag specifies the database path, the -b flag searches for files with matching base names (ignores the path), and the -r flag allows the use of regular expressions for matching. awk -F/ '{name=$NF; gsub(/\.(mp4|mkv)$/, "", name); print name "\t" $0}': This command processes the search results obtained from the locate command. It uses / as a field separator and works on each line of the output: name=$NF: Stores the last field (file name with extension) in the name variable. gsub(/\.(mp4|mkv)$/, "", name): Removes the ".mp4" or ".mkv" file extension from the name variable. print name "\t" $0: Prints the modified file name (without extension) followed by a tab character and the original line (complete file path with the extension). > "$temp_file": Redirects the output of the previous awk command to the temporary file created earlier. chosen_file_name_and_path=$(cut -f1 "$temp_file" | dmenu -i -l 10 -p "Select a video file:"): This command reads the file names without extensions from the temporary file, presents them to the user using dmenu, and stores the user's selection in the chosen_file_name_and_path variable. The cut -f1 command extracts the file names, and the dmenu -i -l 10 -p "Select a video file:" command displays the list in a menu with a prompt. mpv "$(awk -F'\t' -v chosen="$chosen_file_name_and_path" '$1 == chosen {print $2; exit}' "$temp_file")": This command plays the selected file using mpv. It uses awk to search the temporary file for the line where the first field (file name without extension) matches the user's selection and then extracts the second field (the complete file path with the extension) to pass it as an argument to the mpv command. && rm "$temp_file": After the mpv command has finished executing, this command removes the temporary file. --- .local/bin/videosearch | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .local/bin/videosearch diff --git a/.local/bin/videosearch b/.local/bin/videosearch new file mode 100644 index 00000000..e696eb24 --- /dev/null +++ b/.local/bin/videosearch @@ -0,0 +1,6 @@ +#!/bin/bash + +temp_file=$(mktemp) +locate -d ~/.config/.mymlocate.db -b -r '.*\.\(mp4\|mkv\|webm\|mov\|m4v\wmv\|flv\|avi\)$' | awk -F/ '{name=$NF; gsub(/\.(mp4|mkv|webm|mov|m4v|wmv|flv|avi)$/, "", name); print name "\t" $0}' > "$temp_file" +chosen_file_name_and_path=$(cut -f1 "$temp_file" | dmenu -i -l 10 -p "Select a video file:") +mpv "$(awk -F'\t' -v chosen="$chosen_file_name_and_path" '$1 == chosen {print $2; exit}' "$temp_file")" && rm "$temp_file" From fe8f0bce94e051c0a88972134aafad6050ae8149 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Wed, 18 Oct 2023 03:04:57 +0300 Subject: [PATCH 02/12] improve && simplify --- .local/bin/videosearch | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/.local/bin/videosearch b/.local/bin/videosearch index e696eb24..09df1878 100644 --- a/.local/bin/videosearch +++ b/.local/bin/videosearch @@ -1,6 +1,10 @@ -#!/bin/bash +#!/bin/sh -temp_file=$(mktemp) -locate -d ~/.config/.mymlocate.db -b -r '.*\.\(mp4\|mkv\|webm\|mov\|m4v\wmv\|flv\|avi\)$' | awk -F/ '{name=$NF; gsub(/\.(mp4|mkv|webm|mov|m4v|wmv|flv|avi)$/, "", name); print name "\t" $0}' > "$temp_file" -chosen_file_name_and_path=$(cut -f1 "$temp_file" | dmenu -i -l 10 -p "Select a video file:") -mpv "$(awk -F'\t' -v chosen="$chosen_file_name_and_path" '$1 == chosen {print $2; exit}' "$temp_file")" && rm "$temp_file" +tempfile=$(mktemp) +locate -d ~/.config/.mymlocate.db -b -r '.*\.\(mp4\|mkv\|webm\|mov\|m4v\|wmv\|flv\|avi\)$' > "$tempfile" + +chosen_name=$(sed 's|.*/||; s/\.[^.]*$//' "$tempfile" | dmenu -i -l 10 -p "Select a video file:") +chosen_path=$(grep "/$chosen_name\." "$tempfile") + +mpv "$chosen_path" +rm -f "$tempfile" From 533c21c3af0e02b35fa7cb9c0d85c6c224dcba71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Thu, 9 Nov 2023 11:25:02 +0300 Subject: [PATCH 03/12] Use RAM instead of I/O | Minimize further --- .local/bin/videosearch | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/.local/bin/videosearch b/.local/bin/videosearch index 09df1878..259c2b16 100644 --- a/.local/bin/videosearch +++ b/.local/bin/videosearch @@ -1,10 +1,4 @@ #!/bin/sh - -tempfile=$(mktemp) -locate -d ~/.config/.mymlocate.db -b -r '.*\.\(mp4\|mkv\|webm\|mov\|m4v\|wmv\|flv\|avi\)$' > "$tempfile" - -chosen_name=$(sed 's|.*/||; s/\.[^.]*$//' "$tempfile" | dmenu -i -l 10 -p "Select a video file:") -chosen_path=$(grep "/$chosen_name\." "$tempfile") - -mpv "$chosen_path" -rm -f "$tempfile" +video_files=$(locate -d ~/.config/.mymlocate.db -b -r '.*\.\(mp4\|mkv\|webm\|mov\|m4v\|wmv\|flv\|avi\)$') +chosen_file=$(echo "$video_files" | sed 's|.*/||; s/\.[^.]*$//' | dmenu -p "Select Video") +mpv "$(echo "$video_files" | grep "/$chosen_file.")" From 18018b18ff0646d82f7d85471d755a4c73daecdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Fri, 10 Nov 2023 09:55:38 +0300 Subject: [PATCH 04/12] fgrep is 0.00001% faster and safer. --- .local/bin/videosearch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.local/bin/videosearch b/.local/bin/videosearch index 259c2b16..ce979ba0 100644 --- a/.local/bin/videosearch +++ b/.local/bin/videosearch @@ -1,4 +1,4 @@ #!/bin/sh video_files=$(locate -d ~/.config/.mymlocate.db -b -r '.*\.\(mp4\|mkv\|webm\|mov\|m4v\|wmv\|flv\|avi\)$') chosen_file=$(echo "$video_files" | sed 's|.*/||; s/\.[^.]*$//' | dmenu -p "Select Video") -mpv "$(echo "$video_files" | grep "/$chosen_file.")" +mpv "$(echo "$video_files" | fgrep "/$chosen_file.")" From 4c80fed876eb22912e0385d191a2c7673d8cb59b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Sat, 18 Nov 2023 22:39:31 +0300 Subject: [PATCH 05/12] fgrep outputs a deprecation message --- .local/bin/videosearch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.local/bin/videosearch b/.local/bin/videosearch index ce979ba0..5a4aa400 100644 --- a/.local/bin/videosearch +++ b/.local/bin/videosearch @@ -1,4 +1,4 @@ #!/bin/sh video_files=$(locate -d ~/.config/.mymlocate.db -b -r '.*\.\(mp4\|mkv\|webm\|mov\|m4v\|wmv\|flv\|avi\)$') chosen_file=$(echo "$video_files" | sed 's|.*/||; s/\.[^.]*$//' | dmenu -p "Select Video") -mpv "$(echo "$video_files" | fgrep "/$chosen_file.")" +mpv "$(echo "$video_files" | grep -F "/$chosen_file.")" From 9d921b33afcae945ed36671c7ffb5a02559f1ee6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Sat, 18 Nov 2023 23:43:26 +0300 Subject: [PATCH 06/12] Add dropdown menu --- .local/bin/videosearch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.local/bin/videosearch b/.local/bin/videosearch index 5a4aa400..9d3fe1d8 100644 --- a/.local/bin/videosearch +++ b/.local/bin/videosearch @@ -1,4 +1,4 @@ #!/bin/sh video_files=$(locate -d ~/.config/.mymlocate.db -b -r '.*\.\(mp4\|mkv\|webm\|mov\|m4v\|wmv\|flv\|avi\)$') -chosen_file=$(echo "$video_files" | sed 's|.*/||; s/\.[^.]*$//' | dmenu -p "Select Video") +chosen_file=$(echo "$video_files" | sed 's|.*/||; s/\.[^.]*$//' | dmenu -l 10 -p "Select Video") mpv "$(echo "$video_files" | grep -F "/$chosen_file.")" From e259c2af0a25a893e8e5033874e5bc1985cb10e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Sun, 19 Nov 2023 00:54:48 +0300 Subject: [PATCH 07/12] Make dmenu case insensitive --- .local/bin/videosearch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.local/bin/videosearch b/.local/bin/videosearch index 9d3fe1d8..eb19d92d 100644 --- a/.local/bin/videosearch +++ b/.local/bin/videosearch @@ -1,4 +1,4 @@ #!/bin/sh video_files=$(locate -d ~/.config/.mymlocate.db -b -r '.*\.\(mp4\|mkv\|webm\|mov\|m4v\|wmv\|flv\|avi\)$') -chosen_file=$(echo "$video_files" | sed 's|.*/||; s/\.[^.]*$//' | dmenu -l 10 -p "Select Video") +chosen_file=$(echo "$video_files" | sed 's|.*/||; s/\.[^.]*$//' | dmenu -i -l 10 -p "Select Video") mpv "$(echo "$video_files" | grep -F "/$chosen_file.")" From d68ba937be6a9feb5c756280d53a97e8e8500e7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Sun, 19 Nov 2023 17:13:36 +0300 Subject: [PATCH 08/12] Add robust error handling --- .local/bin/videosearch | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/.local/bin/videosearch b/.local/bin/videosearch index eb19d92d..d0230f9d 100644 --- a/.local/bin/videosearch +++ b/.local/bin/videosearch @@ -1,4 +1,22 @@ -#!/bin/sh +#!/bin/dash + +command -v locate >/dev/null || { + sudo pacman -S mlocate && + notify-send "Locate not found. Installing..." || { + notify-send "Failed. Run the script once on terminal OR change sudo permissions." + exit 1 + } +} + +[ -s "$HOME/.config/.mymlocate.db" ] || { + notify-send "You have no database. Creating it..." + disk_path=$(echo "" | rofi -dmenu -l 0 -p "Enter the disk path (e.g '/mnt/harddisk'): ") + doas updatedb -o ~/.config/.mymlocate.db -U "$disk_path" || { + notify-send "Failed. Run the script once on terminal OR change doas permissions." + exit 1 + } +} + video_files=$(locate -d ~/.config/.mymlocate.db -b -r '.*\.\(mp4\|mkv\|webm\|mov\|m4v\|wmv\|flv\|avi\)$') -chosen_file=$(echo "$video_files" | sed 's|.*/||; s/\.[^.]*$//' | dmenu -i -l 10 -p "Select Video") +chosen_file=$(echo "$video_files" | sed 's|.*/||; s/\.[^.]*$//' | rofi -dmenu -p "Select Video") mpv "$(echo "$video_files" | grep -F "/$chosen_file.")" From 539f3ac807f16fc1b2be013f41d8dd9bd2109879 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Sun, 19 Nov 2023 17:14:18 +0300 Subject: [PATCH 09/12] minor correction --- .local/bin/videosearch | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.local/bin/videosearch b/.local/bin/videosearch index d0230f9d..3a24a9a9 100644 --- a/.local/bin/videosearch +++ b/.local/bin/videosearch @@ -10,7 +10,7 @@ command -v locate >/dev/null || { [ -s "$HOME/.config/.mymlocate.db" ] || { notify-send "You have no database. Creating it..." - disk_path=$(echo "" | rofi -dmenu -l 0 -p "Enter the disk path (e.g '/mnt/harddisk'): ") + disk_path=$(echo "" | dmenu -l 0 -p "Enter the disk path (e.g '/mnt/harddisk'): ") doas updatedb -o ~/.config/.mymlocate.db -U "$disk_path" || { notify-send "Failed. Run the script once on terminal OR change doas permissions." exit 1 @@ -18,5 +18,5 @@ command -v locate >/dev/null || { } video_files=$(locate -d ~/.config/.mymlocate.db -b -r '.*\.\(mp4\|mkv\|webm\|mov\|m4v\|wmv\|flv\|avi\)$') -chosen_file=$(echo "$video_files" | sed 's|.*/||; s/\.[^.]*$//' | rofi -dmenu -p "Select Video") +chosen_file=$(echo "$video_files" | sed 's|.*/||; s/\.[^.]*$//' | dmenu -i -l 10 -p "Select Video") mpv "$(echo "$video_files" | grep -F "/$chosen_file.")" From b28bf324c4af38d01c55e2ade5a29112fb4f1623 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Sun, 19 Nov 2023 17:19:41 +0300 Subject: [PATCH 10/12] check dash as a dependency --- .local/bin/videosearch | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.local/bin/videosearch b/.local/bin/videosearch index 3a24a9a9..40dd47fa 100644 --- a/.local/bin/videosearch +++ b/.local/bin/videosearch @@ -1,6 +1,6 @@ #!/bin/dash -command -v locate >/dev/null || { +command -v locate dash >/dev/null || { sudo pacman -S mlocate && notify-send "Locate not found. Installing..." || { notify-send "Failed. Run the script once on terminal OR change sudo permissions." @@ -11,7 +11,7 @@ command -v locate >/dev/null || { [ -s "$HOME/.config/.mymlocate.db" ] || { notify-send "You have no database. Creating it..." disk_path=$(echo "" | dmenu -l 0 -p "Enter the disk path (e.g '/mnt/harddisk'): ") - doas updatedb -o ~/.config/.mymlocate.db -U "$disk_path" || { + sudo updatedb -o ~/.config/.mymlocate.db -U "$disk_path" || { notify-send "Failed. Run the script once on terminal OR change doas permissions." exit 1 } From a991f0982d245cd824b3599ef6b685c3431aa419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Sun, 19 Nov 2023 17:35:51 +0300 Subject: [PATCH 11/12] minor improvements --- .local/bin/videosearch | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/.local/bin/videosearch b/.local/bin/videosearch index 40dd47fa..597b8013 100644 --- a/.local/bin/videosearch +++ b/.local/bin/videosearch @@ -1,16 +1,16 @@ -#!/bin/dash +#!/bin/sh -command -v locate dash >/dev/null || { - sudo pacman -S mlocate && - notify-send "Locate not found. Installing..." || { - notify-send "Failed. Run the script once on terminal OR change sudo permissions." - exit 1 - } +command -v locate >/dev/null || { + notify-send "Locate not found. Installing..." + sudo pacman -S mlocate +} || { + notify-send "Failed. Run the script once on terminal OR change sudo permissions." + exit 1 } [ -s "$HOME/.config/.mymlocate.db" ] || { notify-send "You have no database. Creating it..." - disk_path=$(echo "" | dmenu -l 0 -p "Enter the disk path (e.g '/mnt/harddisk'): ") + disk_path=$(echo "" | rofi -dmenu -l 0 -p "Enter the disk path (e.g '/mnt/harddisk'): ") sudo updatedb -o ~/.config/.mymlocate.db -U "$disk_path" || { notify-send "Failed. Run the script once on terminal OR change doas permissions." exit 1 @@ -18,5 +18,5 @@ command -v locate dash >/dev/null || { } video_files=$(locate -d ~/.config/.mymlocate.db -b -r '.*\.\(mp4\|mkv\|webm\|mov\|m4v\|wmv\|flv\|avi\)$') -chosen_file=$(echo "$video_files" | sed 's|.*/||; s/\.[^.]*$//' | dmenu -i -l 10 -p "Select Video") +chosen_file=$(echo "$video_files" | sed 's|.*/||; s/\.[^.]*$//' | rofi -dmenu -p "Select Video") mpv "$(echo "$video_files" | grep -F "/$chosen_file.")" From ca7debcaabe9419c21b4bdf1f15b259692385be9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Tue, 21 Nov 2023 16:26:36 +0300 Subject: [PATCH 12/12] Correct typo on notification --- .local/bin/videosearch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.local/bin/videosearch b/.local/bin/videosearch index 597b8013..0d7a8a3c 100644 --- a/.local/bin/videosearch +++ b/.local/bin/videosearch @@ -12,7 +12,7 @@ command -v locate >/dev/null || { notify-send "You have no database. Creating it..." disk_path=$(echo "" | rofi -dmenu -l 0 -p "Enter the disk path (e.g '/mnt/harddisk'): ") sudo updatedb -o ~/.config/.mymlocate.db -U "$disk_path" || { - notify-send "Failed. Run the script once on terminal OR change doas permissions." + notify-send "Failed. Run the script once on terminal OR change sudo permissions." exit 1 } }