-
-
Save SpotlightKid/0200bf2e53a375bc6cc901fd442e8c37 to your computer and use it in GitHub Desktop.
Use rofi to select file or folder until file is selected, then print it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# rofi-selectfile | |
# | |
# Use rofi to select file or folder until file is selected, then print it. | |
# | |
# Arguments | |
# $1=directory to start, defaults to "." (specified in variable default_dir) | |
# | |
# Adapted from: https://gist.github.com/thingsiplay/6c4bd13a106a4a609d69c402e675c137 | |
# | |
# Filter modes: normal, regex, glob, fuzzy, prefix | |
rofi_filter_mode="regex" | |
# Prompt in rofi, defaults to scriptname. | |
prompt="${0##*/}" | |
# rofi command to run for each selection. | |
rofi="rofi -dmenu -p "$prompt" -lines 15 -matching $rofi_filter_mode -i" | |
if [[ "$1" = "--save" ]]; then | |
SAVE_MODE=1 | |
shift | |
fi | |
dir="$(readlink -f "${1:-.}")" | |
while :; do | |
selected="" | |
file="" | |
# List all folders in the directory and add ".." as top entry. | |
filelist=$(ls --color=never -1Np --group-directories-first "$dir") | |
selected=$(echo -e "..\n$filelist" | $rofi -mesg "$dir") | |
if [[ "$selected" == "" ]]; then | |
file="" | |
elif [[ "$selected" == ".." ]]; then | |
# ".." will be translated to go up one folder level and run rofi again. | |
dir=${dir%/*} | |
else | |
file="$dir/$selected" | |
dir="$file" | |
fi | |
if [[ -d "$file" ]]; then | |
# Need to select a file, not a directory | |
continue | |
elif [[ -n "$selected" ]]; then | |
if [[ "$SAVE_MODE" -eq 1 || -f "$file" ]]; then | |
echo "$file" | |
fi | |
fi | |
break | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment