]> Sergey Matveev's repositories - nnn.git/blob - plugins/mtpmount
Clear less'es screen
[nnn.git] / plugins / mtpmount
1 #!/usr/bin/env sh
2
3 # Description: Toggle mount of MTP device (eg. Android device)
4 #              'l' to list mountable devices
5 #              'n' integer associated to device to mount
6 #              'q'/'Return' exit
7 #
8 # Dependencies: gvfs-mtp
9 #
10 # Notes: The MTP device should be mounted at /run/user/$UID/gvfs.
11 #        Put /run/user/$UID/gvfs to bookmark entries (NNN_BMS) for faster access.
12 #        Make sure the device is unlocked when mounting.
13 #
14 #        When doing copy-paste into MTP device, you will get an error like this:
15 #            cp: preserving times for './gambar1.png': Operation not supported
16 #        That just means the file is copied but timestamp won't be preserved.
17 #        It's like doing `cp -p localfile.txt file-to-SMB.txt`.
18 #
19 # Shell: POSIX compliant
20 # Author: Benawi Adha
21
22 prompt="Device number ('l' to list): "
23
24 IFS='
25 '
26
27 lsmtp () {
28     devs=$(gio mount -li | grep -e 'activation_root' | sed 's/\s*activation_root=//g')
29     c=1
30     printf "Devices list:\n"
31     for i in $devs; do
32         printf "%s %s\\n" "$c" "$i"
33         c=$(( c + 1 ))
34     done
35     echo
36 }
37
38 lsmtp
39 printf "%s" "$prompt"
40 read -r input
41
42 while [ -n "$input" ]
43 do
44     if [ "$input" = "l" ]; then
45         lsmtp
46     elif [ "$input" = "q" ] || [ "$input" -eq 0 ]; then
47         exit
48     elif [ "$input" -le "$(printf '%s\n' "${devs}" | grep -c '^')" ]; then
49         # dev=$(printf "%s\n" "$devs" | cut -d$'\n' -f${input})
50         c=1
51         for i in $devs; do
52             dev=$i
53             if [ "$input" -eq $c ]; then
54                 break
55             fi
56             c=$(( c + 1 ))
57         done
58
59         if (gio mount -l | grep '^Mount([1-9]).*'"$dev" ) 1>/dev/null; then
60             if gio mount -u "${dev}"; then
61                 printf "%s unmounted\n" "$dev"
62             fi
63         else
64             if gio mount "${dev}"; then
65                 printf "%s mounted to /run/user/\$UID/gvfs\n" "$dev"
66             fi
67         fi
68         echo
69     else
70         printf "Invalid input\n"
71     fi
72
73     printf "%s" "$prompt"
74     read -r input
75 done
76