]> Sergey Matveev's repositories - nnn.git/commitdiff
plugins: mtpmount: toggle mount of MTP devices (#909)
authorBenawi Adha <43810055+wustho@users.noreply.github.com>
Tue, 23 Mar 2021 14:12:10 +0000 (21:12 +0700)
committerGitHub <noreply@github.com>
Tue, 23 Mar 2021 14:12:10 +0000 (19:42 +0530)
* plugins: mtpmount: toggle mount of MTP devices

* plugins: mtpmount: added some quotes

* plugins: mtpmount: toggle mount of MTP devices

* plugins: mtpmount: toggle mount of MTP devices

* plugins: mtpmount: toggle mount of MTP devices

* plugins: mtpmount: toggle mount of MTP devices

plugins/README.md
plugins/mtpmount [new file with mode: 0755]

index 9f957472fadc899d4dba1d0fa17ba83c7fd3314e..c4a204daee91cc05c9f93113edfd26c53edfc45e 100644 (file)
@@ -45,6 +45,7 @@ Plugins extend the capabilities of `nnn`. They are _executable_ scripts (or bina
 | [moclyrics](moclyrics) | Show lyrics of the track playing in moc | sh | [ddgr](https://github.com/jarun/ddgr), [moc](http://moc.daper.net/) |
 | [mocplay](mocplay) | Append (and/or play) selection/dir/file in moc | sh | [moc](http://moc.daper.net/) |
 | [mp3conv](mp3conv) | Extract audio from multimedia as mp3 | sh | ffmpeg |
+| [mtpmount](mtpmount) | Toggle mount of MTP device (eg. Android) | sh | gvfs-mtp |
 | [nbak](nbak) | Backs up `nnn` config | sh | tar, awk, mktemp |
 | [nmount](nmount) | Toggle mount status of a device as normal user | sh | pmount, udisks2 |
 | [nuke](nuke) | Sample file opener (CLI-only by default) | sh | _see in-file docs_ |
diff --git a/plugins/mtpmount b/plugins/mtpmount
new file mode 100755 (executable)
index 0000000..a295ea0
--- /dev/null
@@ -0,0 +1,76 @@
+#!/usr/bin/env sh
+
+# Description: Toggle mount of MTP device (eg. Android device)
+#              'l' to list mountable devices
+#              'n' integer associated to device to mount
+#              'q'/'Return' exit
+#
+# Notes: The MTP device should be mounted at /run/user/$UID/gvfs.
+#        Put /run/user/$UID/gvfs to bookmark entries (NNN_BMS) for faster access.
+#        Make sure the device is unlocked when mounting.
+#
+#        When doing copy-paste into MTP device, you will get an error like this:
+#            cp: preserving times for './gambar1.png': Operation not supported
+#        That just means the file is copied but timestamp won't be preserved.
+#        It's like doing `cp -p localfile.txt file-to-SMB.txt`.
+#
+# Dependencies: gvfs-mtp
+#
+# Shell: POSIX compliant
+# Author: Benawi Adha
+
+prompt="Device number ('l' to list): "
+
+IFS='
+'
+
+lsmtp () {
+    devs=$(gio mount -li | grep -e 'activation_root' | sed 's/\s*activation_root=//g')
+    c=1
+    printf "Devices list:\n"
+    for i in $devs; do
+        printf "%s %s\\n" "$c" "$i"
+        c=$(( c + 1 ))
+    done
+    echo
+}
+
+lsmtp
+printf "%s" "$prompt"
+read -r input
+
+while [ -n "$input" ]
+do
+    if [ "$input" = "l" ]; then
+        lsmtp
+    elif [ "$input" = "q" ] || [ "$input" -eq 0 ]; then
+        exit
+    elif [ "$input" -le "$(printf '%s\n' "${devs}" | grep -c '^')" ]; then
+        # dev=$(printf "%s\n" "$devs" | cut -d$'\n' -f${input})
+        c=1
+        for i in $devs; do
+            dev=$i
+            if [ "$input" -eq $c ]; then
+                break
+            fi
+            c=$(( c + 1 ))
+        done
+
+        if (gio mount -l | grep '^Mount([1-9]).*'"$dev" ) 1>/dev/null; then
+            if gio mount -u "${dev}"; then
+                printf "%s unmounted\n" "$dev"
+            fi
+        else
+            if gio mount "${dev}"; then
+                printf "%s mounted to /run/user/\$UID/gvfs\n" "$dev"
+            fi
+        fi
+        echo
+    else
+        printf "Invalid input\n"
+    fi
+
+    printf "%s" "$prompt"
+    read -r input
+done
+