aboutsummaryrefslogtreecommitdiff
path: root/bin/muffle
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2024-10-12 14:58:06 -0300
committerEuAndreh <eu@euandre.org>2024-10-12 15:37:59 -0300
commit45d6c999cc5852cd59f23f250fcf3672232038a3 (patch)
tree9c776e0070bd5d03820648203fb6ee2975860561 /bin/muffle
parentetc/guix/home.scm: Add mpv package (diff)
downloaddotfiles-45d6c999cc5852cd59f23f250fcf3672232038a3.tar.gz
dotfiles-45d6c999cc5852cd59f23f250fcf3672232038a3.tar.xz
bin/muffle: Speed up
First, use sort -m, as both inputs are already sorted. This both changes from it from O(n*log(n)) to O(n), and allows sort(1) to produce streaming output, instead of sponging all the input, and potentially spilling to disk during the process. Second, use awk(1) when looping. Not only awk(1) is made for this type of job, but also we only call it once and let it do the loop, over looping manually and calling '['(1), printf() and cut(1) on each iteration. Now we call cut(1) only once. It went from being annoying enough to make me pause considering using to being unnoticeable.
Diffstat (limited to 'bin/muffle')
-rwxr-xr-xbin/muffle15
1 files changed, 5 insertions, 10 deletions
diff --git a/bin/muffle b/bin/muffle
index 0feed18..5cf0bdd 100755
--- a/bin/muffle
+++ b/bin/muffle
@@ -81,18 +81,13 @@ timed() {
echo 0 > "$STATUS_F"
{
- { "$@" || echo $? > "$STATUS_F"; } | timed | pre out > "$OUT"
-} 2>&1 | timed | pre err > "$ERR"
+ { "$@" || echo $? > "$STATUS_F"; } | pre -n out | timed > "$OUT"
+} 2>&1 | pre -n err | timed > "$ERR"
STATUS="$(cat "$STATUS_F")"
if [ "$INVERT" = false -a "$STATUS" != 0 ] || [ "$INVERT" = true -a "$STATUS" = 0 ]; then
- cat "$OUT" "$ERR" |
- sort -k2 | while read -r line; do
- if [ "$(printf '%s\n' "$line" | cut -d' ' -f1)" = 'out:' ]; then
- printf '%s\n' "$line" | cut -d' ' -f3- >&1
- else
- printf '%s\n' "$line" | cut -d' ' -f3- >&2
- fi
- done
+ sort -m "$OUT" "$ERR" | cut -d' ' -f2- | awk '{
+ print substr($0, length($1) + 2) > "/dev/std" $1
+ }'
exit "$STATUS"
fi