Compare commits

...

3 Commits

Author SHA1 Message Date
Emre AKYÜZ
a8dd63becf
Merge b157d54410b152095f6475e8df3fcdb16207aad8 into 7a96fb100cf385e05c211937b509d2bf166299e6 2023-12-30 10:47:12 -07:00
Jameson
7a96fb100c
script fixes (#1386)
* fix typo in lfub

* use setsid when editing scripts

* fix immediate exit on middle click

---------

Co-authored-by: 2084x <2084x@noreply.codeberg.org>
2023-12-30 17:04:51 +00:00
Emre AKYÜZ
b157d54410
improve bulkrename in lf
You can see the example video below (15 seconds).

## Improvements
- Directories are excluded. You can't mistakenly rename directories now or you don't have to exclude them manually.

- The extensions are excluded. Therefore, creating macros or editing in general is easier in the editor and there is no chance of messing up extensions. The extensions will be there as they are after editing.

## Justifications
- The original code fetched filenames without any specific order. In the new version, filenames are fetched in version sort order (sort -fV) to ensure a natural order, making it more intuitive for users. This is particularly useful when dealing with sequences of files (like episodes of a TV show or a series of images).

- Previously, the code used ls to get the file names. This approach can be problematic. The updated version uses find, a more robust and dependable method to retrieve files, and cut to trim the unnecessary ./ from the beginning. This change also helps us exclude directories from the list.

- It now separately stores the base filenames and their extensions. This prevents accidental renaming of file extensions, ensuring that only the desired part of the filename is modified. It adds an extra layer of precision, ensuring that a user doesn't mistakenly rename a file extension.

- It utilizes multiple file descriptors (3, 4, and 5) to read from the temporary files concurrently. This allows for a more structured loop when renaming files, making the code clearer and more maintainable. This removes the need for line by line operations and makes the process much more minimal and faster.

- The script can still work with Dash as a /bin/sh link.

## Performance Optimizations:
- The script still has minimal dependencies.

- Instead of reading and writing the entire content of files multiple times, the script uses the mktemp command to create temporary files and then employs file descriptors to read them concurrently. This minimizes file IO operations that can be a significant bottleneck.

- The script processes most data in-memory. Reading from and writing to memory is much faster than disk operations as you already know.

- The while loop uses file descriptors to read three files concurrently. This parallelization minimizes the number of loop iterations, making the loop more efficient than sequential alternatives.
2023-09-12 15:08:21 +03:00
23 changed files with 44 additions and 36 deletions

View File

@ -110,24 +110,32 @@ cmd copyto ${{
cmd setbg "$1"
cmd bulkrename ${{
tmpfile_old="$(mktemp)"
tmpfile_new="$(mktemp)"
tmpfile_old="$(mktemp)"
tmpfile_new="$(mktemp)"
tmpfile_ext="$(mktemp)"
[ -n "$fs" ] && fs=$(basename -a $fs) || fs=$(ls)
[ -n "$fs" ] && fs=$(basename -a $fs | sort -fV) || fs=$(find . -maxdepth 1 -type f | cut -c 3- | sort -fV)
echo "$fs" > "$tmpfile_old"
echo "$fs" > "$tmpfile_new"
$EDITOR "$tmpfile_new"
for f in $fs; do
echo "${f%.*}" >> "$tmpfile_old"
echo "${f##*.}" >> "$tmpfile_ext"
done
[ "$(wc -l < "$tmpfile_old")" -eq "$(wc -l < "$tmpfile_new")" ] || { rm -f "$tmpfile_old" "$tmpfile_new"; exit 1; }
cp "$tmpfile_old" "$tmpfile_new"
$EDITOR "$tmpfile_new"
paste "$tmpfile_old" "$tmpfile_new" | while IFS="$(printf '\t')" read -r src dst
do
[ "$src" = "$dst" ] || [ -e "$dst" ] || mv -- "$src" "$dst"
done
[ "$(wc -l < "$tmpfile_old")" -eq "$(wc -l < "$tmpfile_new")" ] || { rm -f "$tmpfile_old" "$tmpfile_new" "$tmpfile_ext"; exit 1; }
rm -f "$tmpfile_old" "$tmpfile_new"
lf -remote "send $id unselect"
exec 3<"$tmpfile_old"
exec 4<"$tmpfile_ext"
exec 5<"$tmpfile_new"
while IFS= read -r old_name <&3 && IFS= read -r ext <&4 && IFS= read -r new_name <&5; do
[ "$old_name" = "$new_name" ] || [ -e "$new_name.$ext" ] || mv -- "$old_name.$ext" "$new_name.$ext"
done
rm -f "$tmpfile_old" "$tmpfile_new" "$tmpfile_ext"
lf -remote "send $id unselect"
}}
# Bindings

View File

@ -1,6 +1,6 @@
#!/bin/sh
# This is a wrapper script for lb that allows it to create image previews with
# This is a wrapper script for lf that allows it to create image previews with
# ueberzug. This works in concert with the lf configuration file and the
# lf-cleaner script.

View File

@ -13,7 +13,7 @@ case $BLOCK_BUTTON in
- Scroll to change adjust xbacklight." ;;
4) xbacklight -inc 10 ;;
5) xbacklight -dec 10 ;;
6) "$TERMINAL" -e "$EDITOR" "$0" ;;
6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
esac
# Loop through all attached batteries and format the info

View File

@ -23,7 +23,7 @@ case $BLOCK_BUTTON in
2) setsid -f "$TERMINAL" -e calcurse ;;
3) notify-send "📅 Time/date module" "\- Left click to show upcoming appointments for the next three days via \`calcurse -d3\` and show the month via \`cal\`
- Middle click opens calcurse if installed" ;;
6) "$TERMINAL" -e "$EDITOR" "$0" ;;
6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
esac
date "+%Y %b %d (%a) $icon%I:%M%p"

View File

@ -6,7 +6,7 @@ case $BLOCK_BUTTON in
3) notify-send "🖥 CPU module " "\- Shows CPU temperature.
- Click to show intensive processes.
- Middle click to open htop." ;;
6) "$TERMINAL" -e "$EDITOR" "$0" ;;
6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
esac
sensors | awk '/Core 0/ {print "🌡" $3}'

View File

@ -12,7 +12,7 @@ case $BLOCK_BUTTON in
2) setsid -f "$TERMINAL" -e htop ;;
3) notify-send "🪨 CPU load module" "Each bar represents
one CPU core";;
6) "$TERMINAL" -e "$EDITOR" "$0" ;;
6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
esac
# id total idle

View File

@ -11,7 +11,7 @@ case $BLOCK_BUTTON in
1) notify-send "💽 Disk space" "$(df -h --output=target,used,size)" ;;
3) notify-send "💽 Disk module" "\- Shows used hard drive space.
- Click to show all disk info." ;;
6) "$TERMINAL" -e "$EDITOR" "$0" ;;
6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
esac
case "$location" in

View File

@ -274,7 +274,7 @@ case $BLOCK_BUTTON in
3) notify-send "🗺️ Doppler RADAR module" "\- Left click for local Doppler RADAR.
- Middle click to update RADAR location.
After $secs seconds, new clicks will also automatically update the doppler RADAR." ;;
6) "$TERMINAL" -e "$EDITOR" "$0" ;;
6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
esac
echo 🌅

View File

@ -45,7 +45,7 @@ case $BLOCK_BUTTON in
☔: Chance of rain/snow
🥶: Daily low
🌞: Daily high" ;;
6) "$TERMINAL" -e "$EDITOR" "$0" ;;
6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
esac
checkforecast || getforecast

View File

@ -13,5 +13,5 @@ case $BLOCK_BUTTON in
2) restartwm ;;
3) notify-send "❓ Help module" "\- Left click to open LARBS guide.
- Middle click to refresh window manager." ;;
6) "$TERMINAL" -e "$EDITOR" "$0" ;;
6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
esac; echo "❓"

View File

@ -14,7 +14,7 @@ case $BLOCK_BUTTON in
🌐: ethernet working
🔒: vpn is active
" ;;
6) "$TERMINAL" -e "$EDITOR" "$0" ;;
6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
esac
# Wifi

View File

@ -11,7 +11,7 @@ case $BLOCK_BUTTON in
pkill -RTMIN+30 "${STATUSBAR:-dwmblocks}";;
3) notify-send "⌨ Keyboard/language module" "$(printf "%s" "\- Current layout: $(setxkbmap -query | grep -oP 'layout:\s*\K\w+')")
- Left click to change keyboard.";;
6) "$TERMINAL" -e "$EDITOR" "$0" ;;
6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
esac
echo "$kb"

View File

@ -10,7 +10,7 @@ case $BLOCK_BUTTON in
- Shows 🔃 if syncing mail
- Left click opens neomutt
- Middle click syncs mail" ;;
6) "$TERMINAL" -e "$EDITOR" "$0" ;;
6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
esac
unread="$(find "${XDG_DATA_HOME:-$HOME/.local/share}"/mail/*/[Ii][Nn][Bb][Oo][Xx]/new/* -type f | wc -l 2>/dev/null)"

View File

@ -6,7 +6,7 @@ case $BLOCK_BUTTON in
3) notify-send "🧠 Memory module" "\- Shows Memory Used/Total.
- Click to show memory hogs.
- Middle click to open htop." ;;
6) "$TERMINAL" -e "$EDITOR" "$0" ;;
6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
esac
free --mebi | sed -n '2{p;q}' | awk '{printf ("🧠%2.2fGiB/%2.2fGiB\n", ( $3 / 1024), ($2 / 1024))}'

View File

@ -33,5 +33,5 @@ case $BLOCK_BUTTON in
- 🌖: Waning Gibbous
- 🌗: Last Quarter
- 🌘: Waning Crescent" ;;
6) "$TERMINAL" -e "$EDITOR" "$0" ;;
6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
esac

View File

@ -14,6 +14,6 @@ case $BLOCK_BUTTON in
- Scroll changes track.";; # right click, pause/unpause
4) mpc prev | filter ;; # scroll up, previous
5) mpc next | filter ;; # scroll down, next
6) mpc status | filter ; "$TERMINAL" -e "$EDITOR" "$0" ;;
6) mpc status | filter ; setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
*) mpc status | filter ;;
esac

View File

@ -8,7 +8,7 @@ case $BLOCK_BUTTON in
1) setsid -f "$TERMINAL" -e bmon ;;
3) notify-send "🌐 Network traffic module" "🔻: Traffic received
🔺: Traffic transmitted" ;;
6) "$TERMINAL" -e "$EDITOR" "$0" ;;
6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
esac
update() {

View File

@ -5,13 +5,13 @@
case $BLOCK_BUTTON in
1) setsid "$TERMINAL" -e newsboat ;;
2) setsid -f newsup >/dev/null exit ;;
2) setsid -f newsup >/dev/null && exit ;;
3) notify-send "📰 News module" "\- Shows unread news items
- Shows 🔃 if updating with \`newsup\`
- Left click opens newsboat
- Middle click syncs RSS feeds
<b>Note:</b> Only one instance of newsboat (including updates) may be running at a time." ;;
6) "$TERMINAL" -e "$EDITOR" "$0" ;;
6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
esac
cat /tmp/newsupdate 2>/dev/null || echo "$(newsboat -x print-unread | awk '{ if($1>0) print "📰" $1}')$(cat "${XDG_CONFIG_HOME:-$HOME/.config}"/newsboat/.update 2>/dev/null)"

View File

@ -23,7 +23,7 @@ case $BLOCK_BUTTON in
3) notify-send "🎁 Upgrade module" "📦: number of upgradable packages
- Left click to upgrade packages
- Middle click to show upgradable packages" ;;
6) "$TERMINAL" -e "$EDITOR" "$0" ;;
6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
esac
pacman -Qu | grep -Fcv "[ignored]" | sed "s/^/📦/;s/^📦0$//g"

View File

@ -44,7 +44,7 @@ case $BLOCK_BUTTON in
- Shows 🔃 if updating prices.
- <b>Last updated:
$uptime</b>" ;;
6) "$TERMINAL" -e "$EDITOR" "$0" ;;
6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
esac
[ -n "$updateme" ] &&

View File

@ -13,7 +13,7 @@ case $BLOCK_BUTTON in
1) setsid -f "$TERMINAL" -e tsp -l ;;
3) notify-send "Tasks module" "🤖: number of running/queued background tasks
- Left click opens tsp" ;; # Right click
2) $EDITOR "$0" ;; # Middle click
6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
esac
[ "$num" != "0(0)" ] &&

View File

@ -23,5 +23,5 @@ Module shows number of torrents:
🔽: downloading
✅: done
🌱: done and seeding" ;;
6) "$TERMINAL" -e "$EDITOR" "$0" ;;
6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
esac

View File

@ -10,7 +10,7 @@ case $BLOCK_BUTTON in
3) notify-send "📢 Volume module" "\- Shows volume 🔊, 🔇 if muted.
- Middle click to mute.
- Scroll to change." ;;
6) "$TERMINAL" -e "$EDITOR" "$0" ;;
6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
esac
vol="$(wpctl get-volume @DEFAULT_AUDIO_SINK@)"