From 8a3cf1e6e0d4dc8edba62fdf76e2e56974abd184 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Thu, 22 Jun 2023 09:57:15 +0300 Subject: [PATCH 01/13] remove 'rotdir' | simplify and improve directory rotation This method reads image files directly from the current directory $PWD and then uses an array to keep track of all the images. The use of Bash built-in commands like for loop and if condition ensure a faster execution because there is no need to create a subprocess for each image file. The sxiv command is run only once when the selected image is found. Furthermore, this method takes advantage of array slicing in Bash ("${images[@]:i}" "${images[@]:0:i}"). This feature allows the program to pass the images that come after the selected image (including the selected image) and before the selected image, to sxiv in correct order. This maintains the file order of the directory. The use of sort -V (version sort) also contributes to maintaining the file order as it sorts version numbers within text. In terms of efficiency, using Bash built-in operations with arrays should consume less system resources compared to lots of pipelines with multiple processes. The old method uses the rotdir script which, when combined with awk, ls, grep, setsid, and sxiv, creates multiple subprocesses. This increases the overhead for context switching between these processes, leading to more system resource usage and a slower operation than the first method. Moreover, the use of setsid -f sxiv -aio 2>/dev/null might launch sxiv multiple times, increasing the number of processes and resource usage. In terms of file ordering, the rotdir script doesn't have explicit sorting. This might not necessarily match the desired order in some cases. Lastly, the second method uses lf -remote to interact with the lf file manager. The command is run each time a new image is selected, adding an additional level of complexity and overhead to the process. The first method is more readable and maintainable due to its use of simple Bash constructs. The logic is straightforward and doesn't involve external scripts. On the other hand, the second method requires understanding the rotdir script, making it more complex to read and understand. The maintenance of this script would also need to be considered alongside the lfrc file, adding an extra layer of complexity. We can even get rid of ls and sort by using bash globbing alone but then the files are ordered lexicographically, not numerically. Meaning they would go to 10th after 1st rather than 2nd. There could be a workaround though with naming the files with leading zeroes such as 01, 02 and so on. # Detailed Explanation shopt -s nullglob This line tells bash to treat patterns which don't match any files (globs) as expanding to a null string, rather than themselves. This avoids problems in case there are no image files in the directory. dir="$0" selected_file="$1" Here, dir and selected_file are being set to the two arguments that are passed to the bash script at the end ("$PWD" and "$fx"). They are the working directory and the selected file, respectively. images=($(ls "$dir"/*.{jpg,jpeg,png,webp,bmp,tiff,tif,raw,ico,exif,heic,heif,gif,avif,jxl} | sort -V)) The ls command lists all the files in the directory that have certain image extensions. This list of files is piped (|) to sort -V, which sorts the list in version number order (which is similar to natural order for filenames). This sorted list of images is stored in the images array. [[ "${images[i]}" = "$selected_file" ]] && { sxiv -abiof "${images[@]:i}" "${images[@]:0:i}" break } done This is a for loop that iterates over each image in the images array. For each image, it checks if the image equals the selected file. If it does, the sxiv command is executed with all images from the selected one to the end of the list, followed by all images from the start of the list to the selected one. This makes it possible to navigate through the images both forward and backward, preserving the order from lf. After this, the break command is used to exit the for loop because we've found our selected image. The "$PWD" and "$fx" at the end are the arguments that are passed to the bash script. "$PWD" is the current working directory in lf, and "$fx" is the currently selected file in lf. --- .config/lf/lfrc | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/.config/lf/lfrc b/.config/lf/lfrc index 25fb3d1e..a8eb2c57 100644 --- a/.config/lf/lfrc +++ b/.config/lf/lfrc @@ -34,13 +34,18 @@ cmd open ${{ text/*|application/json|inode/x-empty|application/x-subrip) $EDITOR $fx;; image/x-xcf) setsid -f gimp $f >/dev/null 2>&1 ;; image/svg+xml) display -- $f ;; - image/*) rotdir $f | grep -i "\.\(png\|jpg\|jpeg\|gif\|webp\|avif\|tif\|ico\)\(_large\)*$" | - setsid -f sxiv -aio 2>/dev/null | while read -r file; do - [ -z "$file" ] && continue - lf -remote "send select \"$file\"" - lf -remote "send toggle" - done & - ;; + image/*) bash -c ' + shopt -s nullglob + dir="$0" + selected_file="$1" + images=($(ls "$dir"/*.{jpg,jpeg,png,webp,bmp,tiff,tif,raw,ico,exif,heic,heif,gif,avif,jxl} | sort -V)) + for ((i=0; i<${#images[@]}; i++)); do + [[ "${images[i]}" = "$selected_file" ]] && { + sxiv -abiof "${images[@]:i}" "${images[@]:0:i}" + break + } + done + ' "$PWD" "$fx";; audio/*|video/x-ms-asf) mpv --audio-display=no $f ;; video/*) setsid -f mpv $f -quiet >/dev/null 2>&1 ;; application/pdf|application/vnd.djvu|application/epub*) setsid -f zathura $fx >/dev/null 2>&1 ;; From f2e89ab9845bcf07b0c038f632c352aed9844fe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Fri, 30 Jun 2023 11:06:42 +0300 Subject: [PATCH 02/13] Fix the whitespace issue | get rid of ls and sort --- .config/lf/lfrc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.config/lf/lfrc b/.config/lf/lfrc index a8eb2c57..35de708d 100644 --- a/.config/lf/lfrc +++ b/.config/lf/lfrc @@ -38,7 +38,10 @@ cmd open ${{ shopt -s nullglob dir="$0" selected_file="$1" - images=($(ls "$dir"/*.{jpg,jpeg,png,webp,bmp,tiff,tif,raw,ico,exif,heic,heif,gif,avif,jxl} | sort -V)) + images=() + for file in "$dir"/*.{jpg,jpeg,png,webp,bmp,tiff,tif,raw,ico,exif,heic,heif,gif,avif,jxl,JPG,PNG}; do + [[ -f "$file" ]] && images+=("$file") + done for ((i=0; i<${#images[@]}; i++)); do [[ "${images[i]}" = "$selected_file" ]] && { sxiv -abiof "${images[@]:i}" "${images[@]:0:i}" From a8e31d6a1dce82b3181e5f3212ee7448badf16fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Fri, 30 Jun 2023 15:27:06 +0300 Subject: [PATCH 03/13] Fix for sxiv structure --- .config/lf/lfrc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.config/lf/lfrc b/.config/lf/lfrc index 35de708d..f43fae6d 100644 --- a/.config/lf/lfrc +++ b/.config/lf/lfrc @@ -42,9 +42,10 @@ cmd open ${{ for file in "$dir"/*.{jpg,jpeg,png,webp,bmp,tiff,tif,raw,ico,exif,heic,heif,gif,avif,jxl,JPG,PNG}; do [[ -f "$file" ]] && images+=("$file") done - for ((i=0; i<${#images[@]}; i++)); do + for ((i=0; i<${#images[@]}; i++)); do [[ "${images[i]}" = "$selected_file" ]] && { - sxiv -abiof "${images[@]:i}" "${images[@]:0:i}" + images=( "${images[@]:i}" "${images[@]:0:i}" ) + sxiv -abfio "${images[@]}" break } done From 495a48b879735cdb3412a864447484c5ba4180de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Sat, 1 Jul 2023 03:54:06 +0300 Subject: [PATCH 04/13] Another fix for sxiv | slideshow --- .config/lf/lfrc | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.config/lf/lfrc b/.config/lf/lfrc index f43fae6d..2a626687 100644 --- a/.config/lf/lfrc +++ b/.config/lf/lfrc @@ -34,6 +34,10 @@ cmd open ${{ text/*|application/json|inode/x-empty|application/x-subrip) $EDITOR $fx;; image/x-xcf) setsid -f gimp $f >/dev/null 2>&1 ;; image/svg+xml) display -- $f ;; + image/*) bash -c ' + shopt -s nullglob + dir="$0" + selected_file="$1" image/*) bash -c ' shopt -s nullglob dir="$0" @@ -44,8 +48,7 @@ cmd open ${{ done for ((i=0; i<${#images[@]}; i++)); do [[ "${images[i]}" = "$selected_file" ]] && { - images=( "${images[@]:i}" "${images[@]:0:i}" ) - sxiv -abfio "${images[@]}" + sxiv -nabfio "$((i + 1))" "${images[@]}" break } done From 0f5dcce49a7801ce61d4962e29765fed8b742c74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Sat, 1 Jul 2023 03:58:47 +0300 Subject: [PATCH 05/13] Minor Correction --- .config/lf/lfrc | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.config/lf/lfrc b/.config/lf/lfrc index 2a626687..8b60bf49 100644 --- a/.config/lf/lfrc +++ b/.config/lf/lfrc @@ -34,10 +34,6 @@ cmd open ${{ text/*|application/json|inode/x-empty|application/x-subrip) $EDITOR $fx;; image/x-xcf) setsid -f gimp $f >/dev/null 2>&1 ;; image/svg+xml) display -- $f ;; - image/*) bash -c ' - shopt -s nullglob - dir="$0" - selected_file="$1" image/*) bash -c ' shopt -s nullglob dir="$0" From e2c750002ffb2b1f9e1c6eaced4badd0a019e2b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Sat, 1 Jul 2023 05:13:12 +0300 Subject: [PATCH 06/13] sxiv correction --- .config/lf/lfrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.config/lf/lfrc b/.config/lf/lfrc index 8b60bf49..451ae1ff 100644 --- a/.config/lf/lfrc +++ b/.config/lf/lfrc @@ -44,7 +44,7 @@ cmd open ${{ done for ((i=0; i<${#images[@]}; i++)); do [[ "${images[i]}" = "$selected_file" ]] && { - sxiv -nabfio "$((i + 1))" "${images[@]}" + sxiv -na "$((i + 1))" "${images[@]}" break } done From 7e0a3c4bae70067ac431843bd4def48b8b837385 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Sat, 1 Jul 2023 16:23:27 +0300 Subject: [PATCH 07/13] Update lfrc --- .config/lf/lfrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.config/lf/lfrc b/.config/lf/lfrc index 451ae1ff..cb30b49b 100644 --- a/.config/lf/lfrc +++ b/.config/lf/lfrc @@ -44,7 +44,7 @@ cmd open ${{ done for ((i=0; i<${#images[@]}; i++)); do [[ "${images[i]}" = "$selected_file" ]] && { - sxiv -na "$((i + 1))" "${images[@]}" + sxiv -aon "$((i + 1))" "${images[@]}" break } done From 861c9a9eaff0f5a29d9280dab590bccc73e126ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Sat, 1 Jul 2023 16:35:04 +0300 Subject: [PATCH 08/13] Add setsid --- .config/lf/lfrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.config/lf/lfrc b/.config/lf/lfrc index cb30b49b..09a89b4e 100644 --- a/.config/lf/lfrc +++ b/.config/lf/lfrc @@ -44,7 +44,7 @@ cmd open ${{ done for ((i=0; i<${#images[@]}; i++)); do [[ "${images[i]}" = "$selected_file" ]] && { - sxiv -aon "$((i + 1))" "${images[@]}" + setsid sxiv -aon "$((i + 1))" "${images[@]}" break } done From dfeb8e2ecd7a411de8eab63a61b6b0ab03ede998 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Sat, 1 Jul 2023 17:25:38 +0300 Subject: [PATCH 09/13] Add better sorting --- .config/lf/lfrc | 1 + 1 file changed, 1 insertion(+) diff --git a/.config/lf/lfrc b/.config/lf/lfrc index 09a89b4e..c3e08c46 100644 --- a/.config/lf/lfrc +++ b/.config/lf/lfrc @@ -42,6 +42,7 @@ cmd open ${{ for file in "$dir"/*.{jpg,jpeg,png,webp,bmp,tiff,tif,raw,ico,exif,heic,heif,gif,avif,jxl,JPG,PNG}; do [[ -f "$file" ]] && images+=("$file") done + images=($(printf "%s\n" "${images[@]}" | sort -V)) for ((i=0; i<${#images[@]}; i++)); do [[ "${images[i]}" = "$selected_file" ]] && { setsid sxiv -aon "$((i + 1))" "${images[@]}" From 224ae5b4064f53c9148dd785b8bfb4b76a172955 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Wed, 5 Jul 2023 23:11:14 +0300 Subject: [PATCH 10/13] Fix sorting with spaces --- .config/lf/lfrc | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/.config/lf/lfrc b/.config/lf/lfrc index c3e08c46..ec2fe8e2 100644 --- a/.config/lf/lfrc +++ b/.config/lf/lfrc @@ -42,10 +42,13 @@ cmd open ${{ for file in "$dir"/*.{jpg,jpeg,png,webp,bmp,tiff,tif,raw,ico,exif,heic,heif,gif,avif,jxl,JPG,PNG}; do [[ -f "$file" ]] && images+=("$file") done - images=($(printf "%s\n" "${images[@]}" | sort -V)) - for ((i=0; i<${#images[@]}; i++)); do - [[ "${images[i]}" = "$selected_file" ]] && { - setsid sxiv -aon "$((i + 1))" "${images[@]}" + sorted_images=() + while IFS= read -r line; do + sorted_images+=("$line") + done < <(printf "%s\n" "${images[@]}" | sort -V) + for ((i=0; i<${#sorted_images[@]}; i++)); do + [[ "${sorted_images[i]}" = "$selected_file" ]] && { + setsid sxiv -aon "$((i + 1))" "${sorted_images[@]}" break } done From a009de5a52cff6a45b9ad80db463ffa68bb9dba1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Thu, 6 Jul 2023 02:31:53 +0300 Subject: [PATCH 11/13] Add final touches | lf selection --- .config/lf/lfrc | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/.config/lf/lfrc b/.config/lf/lfrc index ec2fe8e2..856972ac 100644 --- a/.config/lf/lfrc +++ b/.config/lf/lfrc @@ -38,21 +38,24 @@ cmd open ${{ shopt -s nullglob dir="$0" selected_file="$1" + selection="$2" + IFS=":" read -r -a selected_files <<< "$selection" images=() - for file in "$dir"/*.{jpg,jpeg,png,webp,bmp,tiff,tif,raw,ico,exif,heic,heif,gif,avif,jxl,JPG,PNG}; do + for file in "${selected_files[@]}" "$dir"/*.{jpg,jpeg,png,webp,bmp,tiff,tif,raw,ico,exif,heic,heif,gif,avif,jxl,JPG,PNG}; do [[ -f "$file" ]] && images+=("$file") done sorted_images=() while IFS= read -r line; do sorted_images+=("$line") - done < <(printf "%s\n" "${images[@]}" | sort -V) + done < <(printf "%s\n" "${images[@]}" | sort -fV) for ((i=0; i<${#sorted_images[@]}; i++)); do [[ "${sorted_images[i]}" = "$selected_file" ]] && { setsid sxiv -aon "$((i + 1))" "${sorted_images[@]}" break } done - ' "$PWD" "$fx";; + lf -remote "send $id unselect" + ' "$PWD" "$fx" "$fs" "$id";; audio/*|video/x-ms-asf) mpv --audio-display=no $f ;; video/*) setsid -f mpv $f -quiet >/dev/null 2>&1 ;; application/pdf|application/vnd.djvu|application/epub*) setsid -f zathura $fx >/dev/null 2>&1 ;; From 78a999e3e08b149885053e4b67770533ea47e8f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Thu, 6 Jul 2023 02:35:46 +0300 Subject: [PATCH 12/13] correction and minimize --- .config/lf/lfrc | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/.config/lf/lfrc b/.config/lf/lfrc index 856972ac..760b08e8 100644 --- a/.config/lf/lfrc +++ b/.config/lf/lfrc @@ -38,23 +38,19 @@ cmd open ${{ shopt -s nullglob dir="$0" selected_file="$1" - selection="$2" - IFS=":" read -r -a selected_files <<< "$selection" - images=() - for file in "${selected_files[@]}" "$dir"/*.{jpg,jpeg,png,webp,bmp,tiff,tif,raw,ico,exif,heic,heif,gif,avif,jxl,JPG,PNG}; do + IFS=":" read -r -a selected_files <<< "$2" + files=("${selected_files[@]}" "$dir"/*.{jpg,jpeg,png,webp,bmp,tiff,tif,raw,ico,exif,heic,heif,gif,avif,jxl,JPG,PNG}) + for file in "${files[@]}"; do [[ -f "$file" ]] && images+=("$file") done - sorted_images=() - while IFS= read -r line; do - sorted_images+=("$line") - done < <(printf "%s\n" "${images[@]}" | sort -fV) + sorted_images=($(printf "%s\n" "${images[@]}" | sort -fV)) for ((i=0; i<${#sorted_images[@]}; i++)); do [[ "${sorted_images[i]}" = "$selected_file" ]] && { setsid sxiv -aon "$((i + 1))" "${sorted_images[@]}" break } done - lf -remote "send $id unselect" + lf -remote "send $3 unselect" ' "$PWD" "$fx" "$fs" "$id";; audio/*|video/x-ms-asf) mpv --audio-display=no $f ;; video/*) setsid -f mpv $f -quiet >/dev/null 2>&1 ;; From de3010047c77741cce5adee3f5848c33a33ab21a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20AKY=C3=9CZ?= Date: Thu, 6 Jul 2023 05:49:04 +0300 Subject: [PATCH 13/13] Return old functionality. --- .config/lf/lfrc | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/.config/lf/lfrc b/.config/lf/lfrc index 760b08e8..db23e99c 100644 --- a/.config/lf/lfrc +++ b/.config/lf/lfrc @@ -38,20 +38,21 @@ cmd open ${{ shopt -s nullglob dir="$0" selected_file="$1" - IFS=":" read -r -a selected_files <<< "$2" - files=("${selected_files[@]}" "$dir"/*.{jpg,jpeg,png,webp,bmp,tiff,tif,raw,ico,exif,heic,heif,gif,avif,jxl,JPG,PNG}) - for file in "${files[@]}"; do + images=() + for file in "$dir"/*.{jpg,jpeg,png,webp,bmp,tiff,tif,raw,ico,exif,heic,heif,gif,avif,jxl,JPG,PNG}; do [[ -f "$file" ]] && images+=("$file") done - sorted_images=($(printf "%s\n" "${images[@]}" | sort -fV)) + sorted_images=() + while IFS= read -r line; do + sorted_images+=("$line") + done < <(printf "%s\n" "${images[@]}" | sort -fV) for ((i=0; i<${#sorted_images[@]}; i++)); do [[ "${sorted_images[i]}" = "$selected_file" ]] && { setsid sxiv -aon "$((i + 1))" "${sorted_images[@]}" break } done - lf -remote "send $3 unselect" - ' "$PWD" "$fx" "$fs" "$id";; + ' "$PWD" "$fx";; audio/*|video/x-ms-asf) mpv --audio-display=no $f ;; video/*) setsid -f mpv $f -quiet >/dev/null 2>&1 ;; application/pdf|application/vnd.djvu|application/epub*) setsid -f zathura $fx >/dev/null 2>&1 ;;