zshrc: Move lsdisk to its own file
authorFrank Terbeck <ft@bewatermyfriend.org>
Mon, 28 Nov 2011 21:44:22 +0000 (22:44 +0100)
committerMichael Prokop <mika@grml.org>
Tue, 6 Dec 2011 13:50:45 +0000 (14:50 +0100)
And make the code vaguely readable...

Signed-off-by: Frank Terbeck <ft@bewatermyfriend.org>
etc/zsh/zshrc
usr_share_grml/zsh/functions/lsdisk [new file with mode: 0644]

index 0cafc3e..93055d8 100644 (file)
@@ -2924,26 +2924,6 @@ purge() {
     fi
 }
 
-#f5# show labels and uuids of disk devices
-if is439 && [[ -d /dev/disk/by-id/ ]]; then
-    lsdisk() {
-        emulate -L zsh
-        setopt extendedglob
-        local -a -U disks
-        local -A mountinfo
-        disks=( /dev/disk/by-id/*(@:A) )
-        [[ -r /proc/mounts ]] && for cline ( "${(f)$(</proc/mounts)[@]}" ) mountinfo["${cline[(w)1]:A}"]="${cline[(w)2,-1]}"
-        for dev in "$disks[@]"; do
-            print ${fg_bold[red]}${dev}${reset_color} /dev/disk/by-label/*(@e/'[[ ${REPLY:A} == $dev ]] && REPLY=${fg[blue]}LABEL=${REPLY:t}${reset_color}'/N) /dev/disk/by-uuid/*(@e/'[[ ${REPLY:A} == $dev ]] && REPLY=${fg[green]}UUID=${REPLY:t}${reset_color}'/N)
-            [[ -n "${mountinfo["$dev"]}" ]] && print -f " Mount: %s -t %s -o %s\n" ${mountinfo["$dev"][(w)1]} ${mountinfo["$dev"][(w)2]} "${mountinfo["$dev"][(w)3,-5]}"
-            for sysdevsize ( /sys/block/${dev:t}/size(N) /sys/block/${${dev:t}%%<->}/${dev:t}/size(N) ) \
-                print -f "  Size: %.3f GiB (%d Byte)\n" $(($(<$sysdevsize)/(2.0*1024.0*1024.0))) $(($(<$sysdevsize)*512))
-
-            print -f "    Id: %s\n" /dev/disk/by-id/*(@e/'[[ ${REPLY:A} == $dev ]]'/N:t)
-        done
-    }
-fi
-
 # Translate DE<=>EN
 # 'translate' looks up fot a word in a file with language-to-language
 # translations (field separator should be " : "). A typical wordlist looks
diff --git a/usr_share_grml/zsh/functions/lsdisk b/usr_share_grml/zsh/functions/lsdisk
new file mode 100644 (file)
index 0000000..ef0a64e
--- /dev/null
@@ -0,0 +1,40 @@
+# show labels and uuids of disk devices
+emulate -L zsh
+setopt extendedglob
+
+local -a -U disks
+local -A mountinfo
+
+disks=( /dev/disk/by-id/*(N@:A) )
+
+if (( ${#disks} == 0 )); then
+    printf 'No disks detected.\n'
+    return 1
+fi
+
+if [[ -r /proc/mounts ]]; then
+    for cline in "${(f)$(</proc/mounts)[@]}"; do
+        mountinfo["${cline[(w)1]:A}"]="${cline[(w)2,-1]}"
+    done
+fi
+
+for dev in "$disks[@]"; do
+    print ${fg_bold[red]}${dev}${reset_color} \
+          /dev/disk/by-label/*(@e/'[[ ${REPLY:A} == $dev ]] && REPLY=${fg[blue]}LABEL=${REPLY:t}${reset_color}'/N) \
+          /dev/disk/by-uuid/*(@e/'[[ ${REPLY:A} == $dev ]] && REPLY=${fg[green]}UUID=${REPLY:t}${reset_color}'/N)
+    if [[ -n "${mountinfo["$dev"]}" ]]; then
+        print -f " Mount: %s -t %s -o %s\n" \
+                 ${mountinfo["$dev"][(w)1]} \
+                 ${mountinfo["$dev"][(w)2]} \
+                 "${mountinfo["$dev"][(w)3,-5]}"
+    for sysdevsize in \
+        /sys/block/${dev:t}/size(N) \
+        /sys/block/${${dev:t}%%<->}/${dev:t}/size(N)
+    do
+        print -f "  Size: %.3f GiB (%d Byte)\n" \
+                 $(($(<$sysdevsize)/(2.0*1024.0*1024.0))) \
+                 $(($(<$sysdevsize)*512))
+    done
+
+    print -f "    Id: %s\n" /dev/disk/by-id/*(@e/'[[ ${REPLY:A} == $dev ]]'/N:t)
+done