From 7018974b2abb1d03e6ce4416109957d92d24fef9 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Fri, 9 Sep 2022 21:49:37 +0200 Subject: [PATCH] bin: make moul interactive --- .config/shell/functions | 6 +++++- .local/bin/scripts/moul | 18 ++++++++++++------ .zshenv | 2 +- README.org | 3 ++- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/.config/shell/functions b/.config/shell/functions index 3a20cdf..4b416c7 100644 --- a/.config/shell/functions +++ b/.config/shell/functions @@ -515,7 +515,11 @@ umoul() { mountpoint "$mountpoint" >/dev/null && break mountpoint="$(dirname "$mountpoint")" done - test "$(dirname "$mountpoint")" = "/" && return 1 + test "$(dirname "$mountpoint")" != "/" || + mountpoint="$(mount | + grep --invert-match -e " /[^/]*\(/[^/]*\)\? " -e "/sys" -e "/run/user" -e "/run/docker" | + fzf --select-1 --exit-0 | awk '{print $3}')" || + return $? fi case "$PWD" in ("$mountpoint"*) builtin cd "$(dirname $mountpoint)";; diff --git a/.local/bin/scripts/moul b/.local/bin/scripts/moul index d291812..04d37db 100755 --- a/.local/bin/scripts/moul +++ b/.local/bin/scripts/moul @@ -1,9 +1,11 @@ -#!/bin/sh -e +#!/bin/sh # Mount a partition by label or device identifier automatically -# TODO make this interactive with a fuzzy finder :) +set -eo pipefail _help="$0 [mountpoint] [options...]" case $1 in - ("") ds && echo "$_help" && exit 2;; + ("") arg=$(lsblk --list --noheadings --output name,label,fstype,mountpoint | + grep -v '/' | grep ".\+ [^ ]\+" | fzf --select-1 --exit-0 | + sed "s/^\([^ ]\+ \+\)\?\([^ ]\+\) \+[^ ]\+ *$/\2/");; ("--help") echo "$_help" && exit 0;; ("-u") shift for last; do true; done @@ -11,9 +13,9 @@ case $1 in code=$? sudo rm -df "$last" exit $code;; + (*) arg=$1;; esac -arg=$1 # FSTAB: BY LABEL if grep --word-regexp "LABEL=$arg" /etc/fstab then # have to mount twice as the first one might be creating the directory @@ -28,14 +30,18 @@ fi # MANUALLY mountpoint="${2:-${MNT:-${XDG_RUNTIME_DIR}/mnt}/$(basename "$arg")}" if grep -e "[^\w=/]$mountpoint[^\w/]" /etc/fstab -then shift; mount "$mountpoint" "$@"; code=$?; cd $mountpoint; exit $code +then test $# -gt 0 && shift + mount "$mountpoint" "$@" + code=$? + cd $mountpoint + exit $code fi case "$arg" in (sd*|loop*|nvme*|mm*|md*|dm*|vg*) partition="/dev/$arg";; (/dev/*) partition="$arg";; (*) partition="-L $arg";; esac -shift $(expr 2 \& $# \> 1 \| 1) +shift $(expr 2 \& $# \> 2 \| $#) #uid=$(id --user),gid=$(id --group), \ if ! mountpoint "$mountpoint" 2>/dev/null then mp="/run/media/$USER/$arg" && test -e "$mp" && mountpoint="$mp" diff --git a/.zshenv b/.zshenv index 98b8b08..a1aaad0 100644 --- a/.zshenv +++ b/.zshenv @@ -128,7 +128,7 @@ alt-shift-down:preview-down,alt-shift-up:preview-up,esc:close") #alt-r:preview(bat {}), export FZF_HISTDIR="$XDG_STATE_HOME/fzf" mkdir -p "$XDG_STATE_HOME/fzf" -export FZF_DEFAULT_OPTS="--select-1 --ansi +export FZF_DEFAULT_OPTS="--select-1 --exit-0 --ansi --tiebreak=$($_fzf_latest && echo "chunk,")end,length --history=$FZF_HISTDIR/history --bind='$FZF_BINDINGS' $($_fzf_latest && echo '--preview-window=60%,border-left --marker=o')" FD_BASE="fd --hidden --color=always --no-ignore-vcs" diff --git a/README.org b/README.org index e73cd40..7158daf 100644 --- a/README.org +++ b/README.org @@ -24,7 +24,8 @@ Here are some basic commands you should remember: - loc :: quickly find a file on your system, case-insensitive - loca :: do a deep search for a file on the whole system, case-insensitive - loce :: find a file/directory with exact name match - +- moul :: mount a device interatively (unmount with ~umoul~) +*** Mounting Manually To mount a device, first check ~ds~ ([d]isk [s]tats, a prettification of ~lsblk --fs~) and find your device,