Skip to content

Instantly share code, notes, and snippets.

@coderofsalvation
Created November 12, 2012 13:15
Show Gist options
  • Save coderofsalvation/4059334 to your computer and use it in GitHub Desktop.
Save coderofsalvation/4059334 to your computer and use it in GitHub Desktop.
minimalistic bashfilebrowser which enables 4-button navigation. Highly customizable/configurable for all needs.
#!/bin/bash
#
# minimalistic filebrowser script which enables 4-button navigation.
#
# Why: I needed a simple way to navigate thru files using simple keyboardbindings.
#
# Usage:
# nbrowser next dir
# nbrowser next file
# nbrowser prev dir
# nbrowser prev file
# nbrowser getfile
# nbrowser getfiles
# nbrowser getsubdir
# nbrowser getsubdirs
# nbrowser show
#
# File/dir info is saved in /tmp/.nbrowser*, remove them in case of trouble
#
# changes:
# [Thu Nov 15 21:18:27 CET 2012] added cachefiles to speed up laaaarge dirs
#
# Copyright (C) 2012, Leon van Kammen, Coder of Salvation
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
path="/home/sqz/.wine/drive_c/Program Files/buze/Gear/Vst"
dbfile="/tmp/.nbrowser.$(basename "$path")"
cachefiles="$dbfile.cache"
cachedirs="$dbfile.cachedirs"
showallfiles=1 # subdirectories show all their subdirectories contents
showfiletypes="(\.dll|\.DLL)" # leave empty for all files
cache=""
dbinit='
offset_dir=1
offset_file=1
size_file=0
size_dir=0
'
init(){
if [[ ! -f "$dbfile" ]]; then
echo "$dbinit" > "$dbfile";
update file
update dir
fi
}
update(){
[[ "$1" == "file" ]] && max=$(getfiles | wc -l)
[[ "$1" == "file" ]] && updatevar "offset_file" 1
[[ "$1" == "dir" ]] && max=$(getsubdirs "$path" | wc -l)
[[ "$1" == "dir" ]] && : > "$dbfile.cache" && getfiles &>/dev/null # update cache
updatevar "size_$1" "$max"
}
updatevar(){
var="$1"
value="$2"
sed -i 's/'$var'=.*/'$var'='$value'/g' "$dbfile"
}
prev(){
label="offset_$1"
size=$(grep "size_$1" "$dbfile" | sed 's/.*'$1'=//g')
offset=$(grep "$label" "$dbfile" | sed 's/'$label'=//g')
((offset=offset-1))
[[ "$offset" == "0" ]] && offset=$size
updatevar "$label" "$offset"
[[ "$1" == "dir" ]] && update dir && update file && getsubdir # update file amount for speed
[[ "$1" == "file" ]] && getfile
}
next(){
offset=$(grep "offset_$1" "$dbfile" | sed 's/.*'$1'=//g')
size=$(grep "size_$1" "$dbfile" | sed 's/.*'$1'=//g')
[[ "$size" -gt "$offset" ]] && ((offset++)) || offset=1;
updatevar "offset_$1" "$offset"
[[ "$1" == "dir" ]] && update dir && update file && getsubdir # update file amount for speed
[[ "$1" == "file" ]] && getfile
}
getsubdir(){
offset_dir=$(grep "offset_dir" "$dbfile" | sed 's/offset_dir=//g')
getsubdirs | sed $offset_dir'q;d'
}
getsubdirs(){
cache=$( cat "$cachedirs" 2>/dev/null)
[[ ${#cache} > 0 ]] && echo "$cache" && return 0;
echo "$path/." > "$cachedirs"
find -L "$path" -mindepth 1 -maxdepth 1 -type d | while read dir; do
ls "$dir" | grep -q -E "$showfiletypes" && echo "$dir" >> "$cachedirs"
done
cat "$cachedirs"
}
getfiles(){
cache=$( cat "$cachefiles" 2>/dev/null)
[[ ${#cache} > 0 ]] && echo "$cache" && return 0;
if [[ $showallfiles == 1 ]]; then
find -L "$(getsubdir)" 2>/dev/null| grep -E "$showfiletypes" > "$cachefiles"
else
find -L "$(getsubdir)" -mindepth 1 -maxdepth 1 2>/dev/null| grep -E "$showfiletypes" > "$cachefiles"
fi
cat "$cachefiles"
}
getfile(){
offset_file=$(grep "offset_file" "$dbfile" | sed 's/offset_file=//g')
files=$(getfiles)
lines=$(echo "$files" | wc -l)
if [[ $lines < 2 ]]; then echo "$files";
else getfiles | sed $offset_file'q;d'; fi
}
show(){
file=$(getfile)
msg="$(echo $(getsubdir) | sed 's/.*\///g') ::: "
[[ ${#file} > 0 ]] && msg="$msg "$(basename "$file")""
echo $msg;
#DISPLAY=:0.0 notify-send "$msg"
}
init
"$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment