#!/bin/bash # config file gmpy_cdirs_config="$(gettop)/scripts/cdirs/cdirsrc.tina" cdir_options_list="hn:l:p:k:t:f:F:" lsdir_options_list="hp:" cldir_options_list="gha" setdir_options_list="hg" cdir_options_list_full="reload,reset,num:,label:,path:,help,key:tag:,find:,Find:,default-key:,find-maxdepth:" lsdir_options_list_full="path:,help" cldir_options_list_full="all,reset,help,reload,global" setdir_options_list_full="global,help" init_options_list_full="replace-cd,help" cdir() { local flag word opts key f_dirname F_dirname find_maxdepth opts="$(getopt -l "${cdir_options_list_full}" -o "${cdir_options_list}" -- $@)" || return 1 eval set -- "${opts}" while true do case "$1" in -h|--help) gmpy_cdirs_print_help "cdir" return 0 ;; -l|--label) flag="label" shift word="$1" shift break ;; -n|--num) flag="num" shift word="$1" shift break ;; -p|--path) flag="path" shift word="$1" shift break ;; --reload) gmpy_cdirs_load_config gmpy_cdirs_load_global_labels return 0 ;; --reset) gmpy_cdirs_reset return 0 ;; -k|--key|-t|--tag) shift gmpy_cdirs_check_key "${1}" || { echo "$1: No this key" return 1 } key="$1" shift ;; -f|--find) shift f_dirname="$1" shift ;; -F|--Find) shift F_dirname="$1" shift ;; --default-key) shift gmpy_cdirs_set_default_key "$1" || { echo "$1: No This Key" return 1 } return 0 ;; --find-maxdepth) shift gmpy_cdirs_check_whether_num "$1" \ && find_maxdepth="$1" \ || { echo "$1: Invaild Input - Not Num" return 1 } shift ;; --) shift break esac done [ -n "${find_maxdepth}" ] && { [ "$#" -eq 0 -a -z "${f_dirname}" -a -z "${F_dirname}" ] && { gmpy_cdirs_set_find_maxdepth "${find_maxdepth}" return 0 } } # -f|--find|-F|--Find if [ -n "${f_dirname}" ]; then key=${key:-${gmpy_cdirs_default_key}} [ -z "${key}" ] && return 0 gmpy_cdirs_cd_find_process "${f_dirname}" "${key}" "${find_maxdepth}" return 0 elif [ -n "${F_dirname}" ]; then gmpy_cdirs_cd_find_process "${F_dirname}" "${find_maxdepth}" return 0 fi if [ -n "${flag}" ]; then gmpy_cdirs_replace_cd "$(_cdir "${word}" "${flag}")" elif [ "$#" -gt "1" ]; then #for path with space gmpy_cdirs_replace_cd "$*" elif [ "$#" -eq "0" ]; then gmpy_cdirs_replace_cd elif [ "$#" -eq "1" ] && ([ "$1" = "," ] || [ "$1" = '0' ]); then local path="$(gmpy_cdirs_get_path_from_num "0")" [ -z "${path}" ] && return 0 gmpy_cdirs_replace_cd "${path}" else gmpy_cdirs_replace_cd "$(_cdir "$1")" fi } setdir() { local flag opts flag="no_global" opts="$(getopt -l "${setdir_options_list_full}" -o "${setdir_options_list}" -- $@)" || return 1 eval set -- "${opts}" while true do case "$1" in -h|--help) gmpy_cdirs_print_help "setdir" return 0 ;; -g|--global) flag="global" shift ;; --) shift break esac done if [ "$#" -lt "2" ]; then if [ "$1" = ',' ]; then gmpy_cdirs_set_mark else gmpy_cdirs_print_help "setdir" fi elif [ "$#" -eq "2" ]; then _setdir $@ "${flag}" elif [ "$#" -gt "2" ]; then _setdir "$1" "$(shift;echo "$*")" "${flag}" fi } lsdir() { local opts word local flag="not_only_path" opts=`getopt -o "${lsdir_options_list}" -l "${lsdir_options_list_full}" -- $@` || return 1 eval set -- "${opts}" while true do case "$1" in -h|--help) gmpy_cdirs_print_help "lsdir" return 0 ;; -p|--path) flag="only_path" shift word="$1" shift ;; --) shift break ;; esac done [ -n "${word}" -a "${flag}" = "only_path" ] \ && _lsdir "${flag}" "${word}" \ || _lsdir "${flag}" $@ } cldir() { local opts all_flag local global_flag="no_global" opts="$(getopt -l "${cldir_options_list_full}" -o "${cldir_options_list}" -- $@)" || return 1 eval set -- "${opts}" while true do case "$1" in -h|--help) gmpy_cdirs_print_help "cldir" return 0 ;; --reset) gmpy_cdirs_reset return 0 ;; --reload) gmpy_cdirs_load_global_labels return 0 ;; -a|--all) shift all_flag="all" ;; -g|--global) shift global_flag="global" ;; --) shift break ;; esac done if [ "${all_flag}" = "all" ]; then local res="n" while true do read -n 1 -p "Are you sure to clear all labels$([ "${global_flag}" = "global" ] \ && echo " but also global labels")? [y/n] " res echo case "${res}" in y|Y) break ;; n|N) return 0 ;; *) continue ;; esac done gmpy_cdirs_clear_all "${global_flag}" return 0 fi if [ $# -lt 1 ]; then gmpy_cdirs_print_help "cldir" return 1 fi _cldir "${global_flag}" $@ } # _cdir [num|label|path](point out the type) _cdir() { [ -f "$1" -a ! "$2" = "num" -a ! "$2" = "label" ] && { echo "$1" return 0 } echo "$(gmpy_cdirs_get_path "$1" "$2")" } # _lsdir [num1|label1|path1] [num2|label2|path2] ... _lsdir() { local path_flag="$1" && shift [ "${path_flag}" = "not_only_path" ] && { printf '\033[32m%s\t%-16s\t%s\033[0m\n' "num" "label" "path" printf '\033[32m%s\t%-16s\t%s\033[0m\n' "---" "-----" "----" } if [ "$#" -gt 0 ]; then for para in $@ do if [ "${para}" = "," ]; then if [ "${path_flag}" = "not_only_path" ]; then gmpy_cdirs_list_mark else gmpy_cdirs_get_path "0" "num" fi else if [ "${path_flag}" = "not_only_path" ]; then gmpy_cdirs_ls_one_dir "${para}" else gmpy_cdirs_get_path "${para}" fi fi done else gmpy_cdirs_ls_all_dirs fi } # _setdir