You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
3.1 KiB
66 lines
3.1 KiB
#!/bin/bash
|
|
###############################################################################
|
|
# Author: Nils Gersnter #
|
|
# Date: 2023-04-14 #
|
|
# #
|
|
# DESCRIPTION #
|
|
# #
|
|
# This script creates an interface to select a project folder. #
|
|
# It sorts the folders in the order, last selected. #
|
|
# #
|
|
# SETUP #
|
|
# #
|
|
# Put the script in your ${HOME}/bin folder and add the following alias to #
|
|
# your .bashrc (with the allias of your choice) #
|
|
# alias fp='cd "$(findProject.sh)"' #
|
|
# #
|
|
# DEPENDENCIES #
|
|
# #
|
|
# This script depends on fzf and sponge being installed on the system. #
|
|
# On debian like systems, sponge can be obtained by installing the moreutils #
|
|
# package. #
|
|
# fzf can be installed in the ways described here: #
|
|
# https://github.com/junegunn/fzf#installation #
|
|
###############################################################################
|
|
|
|
# The number of selections that is to be kept
|
|
LINES_TO_SAVE=200
|
|
SELECTED_FOLDERS_LOG=${HOME}/.local/share/findProject_selection.log
|
|
|
|
# Root folder per customer that contains project folders
|
|
CUSTOMER_ROOT_FOLDERS="${HOME}/Documents/Code/BOLIDEN
|
|
${HOME}/Documents/Code/GERSTNER
|
|
${HOME}/Documents/Code/REPLYTO
|
|
${HOME}/Documents/Code/RISE
|
|
${HOME}/Documents/Code/VOLVOCARS
|
|
${HOME}/Documents/Code/POLIS"
|
|
|
|
_get_root() {
|
|
if [ -f "${SELECTED_FOLDERS_LOG}" ] && \
|
|
[ $( wc -l "${SELECTED_FOLDERS_LOG}" | grep -oe "^[0-9]\+") -gt 0 ]; then
|
|
tac "$SELECTED_FOLDERS_LOG" | grep -oe "^\S\+"
|
|
fi
|
|
echo -e "${CUSTOMER_ROOT_FOLDERS}"
|
|
}
|
|
|
|
_get_subfolder() {
|
|
if [ -f "${SELECTED_FOLDERS_LOG}" ] && \
|
|
[ $( wc -l "${SELECTED_FOLDERS_LOG}" | grep -oe "^[0-9]\+") -gt 0 ]; then
|
|
tac "$SELECTED_FOLDERS_LOG" | grep -e "^${root_dir} \S\+" | grep -oe "\S\+$"
|
|
fi
|
|
find "$root_dir" -mindepth 1 -maxdepth 1 -type d -printf "%Ts\t%f\n" | sort -rn| cut -f 2
|
|
}
|
|
|
|
_select_uniq() {
|
|
local root="$1"
|
|
|
|
cat < /dev/stdin | awk '!x[$0]++' - | fzf --preview "tree -L 1 ${root}{}"
|
|
}
|
|
|
|
root_dir="$(_get_root | _select_uniq)"
|
|
base_name="$(_get_subfolder | _select_uniq "${root_dir}/")"
|
|
|
|
echo "${root_dir}/${base_name}"
|
|
echo "${root_dir} ${base_name}" >> "$SELECTED_FOLDERS_LOG"
|
|
tail -n $LINES_TO_SAVE "$SELECTED_FOLDERS_LOG"| sponge "$SELECTED_FOLDERS_LOG"
|