diff --git a/README.md b/README.md index 7dd5dfa..c1f107c 100644 --- a/README.md +++ b/README.md @@ -46,8 +46,12 @@ It also prints the device, the devices label, if available (otherwise `???`) and You can mount a volume by simply specifying `mnt IDENTIFIER`, it will autocomplete available block devices for you, but you can also specify (parts of) labels or size, basically anything that identifies the volume uniquely. (The volume is selected by grepping for `IDENTIFIER` in the output of `mnt_core_list_mounts`) +![Mount autocompletion in action](img/mount-completions.png) + Unmounting is just as easy, you can run `mnt -u IDENTIFIER`, with the same rules for the identifier. The autocompletion will only offer you already mounted devices. +![Unmounting volumes](img/mount-unmount-cycle.png) + ### Configuration The script mounts block devices using `udisksctl` by default. If you have a more involved setup, you can edit the `.mounts` file in your home directory. The format is roughly documented in the file, but I'll go into more detail here: diff --git a/completions/mnt.fish b/completions/mnt.fish new file mode 100644 index 0000000..5b2a1fd --- /dev/null +++ b/completions/mnt.fish @@ -0,0 +1,43 @@ +complete -c mnt -f + +# add flags + +complete -c mnt -s h -l help -d 'Print a short help text and exit' +complete -c mnt -s l -l list -d 'List information on all available mounts' +complete -c mnt -s f -l full-paths -d 'Print full paths instead of the shorter versions' +complete -c mnt -s u -l unmount -d 'Unmount a volume' -a "(_mnt_complete_volumes unmount)" + +complete -c mnt -n 'not _mnt_seen_flag u unmount; and _mnt_not_seen_any_arg' -a '(_mnt_complete_volumes mount)' + +complete -c mnt -n '_mnt_seen_flag u unmount; and _mnt_not_seen_any_arg' -a '(_mnt_complete_volumes unmount)' + + +function _mnt_complete_volumes + for line in (mnt_core_list_mounts | mnt_core_filter $argv[1]) + mnt_core_pretty_print_line $argv[1] $line + end +end + + +function _mnt_seen_flag + # usage: _mnt_seen_flag + # short/longhand without leading dashes + set -l cmd (commandline -po) + set -e cmd[1] + + if string match -r -- '^(-[^-]*'"$argv[1]"'[^-]*|--'"$argv[2]"')$' $cmd + return 0 + end + return 1 +end + +function _mnt_not_seen_any_arg + set -l cmd (commandline -po) + set -e cmd[1] + + if not string match -v -- '-*' $cmd + return 0 + else + return 1 + end +end \ No newline at end of file diff --git a/functions/mnt.fish b/functions/mnt.fish index f5c81f6..7cdfde2 100644 --- a/functions/mnt.fish +++ b/functions/mnt.fish @@ -2,7 +2,7 @@ set _MNT_SEEN_DEVICES /dev/sda /dev/nvme0n1 function mnt - argparse --name=mnt 'h/help' 'l/list' 'f/full-paths' 'u/unmount' -- $argv + argparse --name=mnt 'h/help' 'l/list' 'f/full-paths' 'u/unmount=' -- $argv if set -q _flag_help echo "mnt - A mounting utility" @@ -25,12 +25,14 @@ function mnt return 0 end - if set -q _flag_list; or ! count $argv > /dev/null + set -l unmount_target (coalesce $_flag_unmount $argv[1]) + + if set -q _flag_list; or test -z $unmount_target mnt_core_pretty_list_mounts $_flag_full_paths | sort return 0 end - set -l selected_mount (mnt_core_list_mounts | rg -- (string trim -rc '/' -- $argv[1])) + set -l selected_mount (mnt_core_list_mounts | rg -- $unmount_target) if test (count $selected_mount) -gt 1 echo '"'"$argv[1]"'" is ambigous, it matched:' @@ -202,11 +204,11 @@ function mnt_core_filter if test -b $info[1] echo $line end - case mounted + case mounted unmount if mnt_core_mount_point $info[1] > /dev/null echo $line end - case unmounted + case unmounted mount if test -b $info[1]; and ! mnt_core_mount_point $info[1] > /dev/null echo $line end diff --git a/functions/mount-luks.fish b/functions/mount-luks.fish index a35e59e..79a9505 100644 --- a/functions/mount-luks.fish +++ b/functions/mount-luks.fish @@ -29,7 +29,7 @@ function mount-luks set -l pass_name $argv[2] set -l mapper $argv[3] - pass $PASS_NAME | head -n 1 | sudo cryptsetup luksOpen $device $mapper - + pass $pass_name | head -n 1 | sudo cryptsetup luksOpen $device $mapper - udisksctl mount -b /dev/mapper/$mapper end end diff --git a/img/mount-completions.png b/img/mount-completions.png new file mode 100644 index 0000000..f28edd8 Binary files /dev/null and b/img/mount-completions.png differ diff --git a/img/mount-unmount-cycle.png b/img/mount-unmount-cycle.png new file mode 100644 index 0000000..b7f20ec Binary files /dev/null and b/img/mount-unmount-cycle.png differ diff --git a/install.fish b/install.fish index a1a155c..211659a 100755 --- a/install.fish +++ b/install.fish @@ -54,6 +54,30 @@ function install_copy cp $argv[1] $argv[2] end +function uninstall_mnt + # uninstall mode! + echo "Uninstalling mnt..." + for file in $core_files $dependencies + if test -f $fish_config_path/$file + rm $fish_config_path/$file + echo " Removed $fish_config_path/$file" + else + echo " $file was not installed." + end + end + if set -q _flag_extras + echo "Uninstalling extras..." + for file in $extras + if test -f $fish_config_path/$file + rm $fish_config_path/$file + echo " Removed $fish_config_path/$file" + else + echo " $file was not installed." + end + end + end +end + function install argparse 'h/help' 'c/copy' 'f/force' 'e/extras' 'C/clean' -- $argv @@ -104,27 +128,7 @@ function install # check for uninstall flag if set -q _flag_clean - # uninstall mode! - echo "Uninstalling mnt..." - for file in $core_files $dependencies - if test -f $fish_config_path/$file - rm $fish_config_path/$file - echo " Removed $fish_config_path/$file" - else - echo " $file was not installed." - end - end - if set -q _flag_extras - echo "Uninstalling extras..." - for file in $extras - if test -f $fish_config_path/$file - rm $fish_config_path/$file - echo " Removed $fish_config_path/$file" - else - echo " $file was not installed." - end - end - end + uninstall_mnt return 0 end