From 638676a6894fa1ad0065254a272d529c7e59c496 Mon Sep 17 00:00:00 2001 From: Benawi Adha <43810055+wustho@users.noreply.github.com> Date: Tue, 23 Mar 2021 21:12:10 +0700 Subject: [PATCH] plugins: mtpmount: toggle mount of MTP devices (#909) * 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 | 1 + plugins/mtpmount | 76 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100755 plugins/mtpmount diff --git a/plugins/README.md b/plugins/README.md index 9f957472..c4a204da 100644 --- a/plugins/README.md +++ b/plugins/README.md @@ -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 index 00000000..a295ea07 --- /dev/null +++ b/plugins/mtpmount @@ -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 +