|
| 1 | +# brew install peco |
| 2 | +# PECO |
| 3 | +function lhs_peco_select_history() { |
| 4 | + local tac |
| 5 | + if which tac >/dev/null; then |
| 6 | + tac="tac" |
| 7 | + else |
| 8 | + # Displays the output from the end of the file in reverse order. |
| 9 | + tac="tail -r" |
| 10 | + fi |
| 11 | + BUFFER=$(history -n 1 | uniq | |
| 12 | + eval $tac | |
| 13 | + peco --query "$LBUFFER") |
| 14 | + # Move the cursor at then end of the input($#variable_name is to get the length itself) |
| 15 | + CURSOR=$#BUFFER |
| 16 | + # zle clear-screen |
| 17 | +} |
| 18 | + |
| 19 | +function lhs_peco_history() { |
| 20 | + peco_select_history |
| 21 | +} |
| 22 | + |
| 23 | +function lhs_peco_repo_list() { |
| 24 | + project_list=$( |
| 25 | + lhs_peco_commandline_input "\ |
| 26 | + find ${LHS_PROJECTS_DIR} -type d -name '.git' -maxdepth 6 \ |
| 27 | + | awk -F '/' '{for (i=1; i<NF; i++) printf \$i \"/\"; print '\n'}'" 'true' '60' |
| 28 | + ) |
| 29 | + input_project=$(echo ${project_list} | peco) |
| 30 | + echo ${input_project} |
| 31 | +} |
| 32 | + |
| 33 | +function lhs_peco_format_name_convention_pre_defined() { |
| 34 | + local peco_input=$1 |
| 35 | + echo "${peco_input}" | tr "\t" "\n" | tr -s " " "\n" | tr -s '\n' |
| 36 | +} |
| 37 | + |
| 38 | +function lhs_peco_format_output_text() { |
| 39 | + local peco_input=$1 |
| 40 | + echo "${peco_input}" | tr "\t" "\n" |
| 41 | +} |
| 42 | + |
| 43 | +function lhs_peco_name_convention_input() { |
| 44 | + local text_input=$1 |
| 45 | + local format_text=$(lhs_peco_format_name_convention_pre_defined $text_input) |
| 46 | + echo $format_text |
| 47 | +} |
| 48 | + |
| 49 | +function lhs_peco_create_menu_with_array_input() { |
| 50 | + local text_input=$1 |
| 51 | + local format_text=$(lhs_peco_format_name_convention_pre_defined $text_input) |
| 52 | + echo $format_text |
| 53 | +} |
| 54 | + |
| 55 | +function lhs_peco_disable_input_cached() { |
| 56 | + export lhs_cli_peco_input_expired_time=0 |
| 57 | +} |
| 58 | + |
| 59 | +function lhs_peco_run_command_to_get_input() { |
| 60 | + peco_commandline=$1 |
| 61 | + eval ${peco_commandline} |
| 62 | +} |
| 63 | + |
| 64 | +function lhs_peco_commandline_input() { |
| 65 | + |
| 66 | + commandline="${1}" |
| 67 | + local result_cached=${2:-'false'} |
| 68 | + local input_expired_time="${3:-$lhs_cli_peco_input_expired_time}" |
| 69 | + |
| 70 | + local md5_hash=$(echo $commandline | md5) |
| 71 | + local input_folder="${lhs_cli_input:-/tmp/inputs}" |
| 72 | + mkdir -p ${input_folder} |
| 73 | + local input_file_path="${input_folder}/${md5_hash}.txt" |
| 74 | + local empty_file=$(find ${input_folder} -name ${md5_hash}.txt -empty) |
| 75 | + local valid_file=$(find ${input_folder} -name ${md5_hash}.txt -mmin +${input_expired_time}) |
| 76 | + |
| 77 | + # The file is existed and not empty and the flag result_cached is not empty |
| 78 | + if [ -z "${valid_file}" ] && [ -f "${input_file_path}" ] && [ -z "${empty_file}" ] && [ "true" = "${result_cached}" ]; then |
| 79 | + # Ignore the first line. |
| 80 | + grep -Ev "\*\*\*\*\*\*\*\* \[.*\]" $input_file_path |
| 81 | + # cat $input_file_path | |
| 82 | + else |
| 83 | + local commandline_result=$(lhs_peco_run_command_to_get_input "$commandline") |
| 84 | + |
| 85 | + local format_text=$(lhs_peco_format_output_text $commandline_result) |
| 86 | + |
| 87 | + if [ -n "${format_text}" ]; then |
| 88 | + commandline=$(lhs_util_format_commandline_one_line ${commandline}) |
| 89 | + echo "******** [ ${commandline} ] ********" >${input_file_path} |
| 90 | + echo ${format_text} | tee -a ${input_file_path} |
| 91 | + else |
| 92 | + echo "Can not get the data" |
| 93 | + fi |
| 94 | + |
| 95 | + fi |
| 96 | + |
| 97 | +} |
| 98 | + |
| 99 | +function lhs_peco_create_menu() { |
| 100 | + local input_function=$1 |
| 101 | + local peco_options=$2 |
| 102 | + local peco_command="peco ${peco_options}" |
| 103 | + local input_value=$(echo "$(eval $input_function)" | eval ${peco_command}) |
| 104 | + echo ${input_value:?'Can not get the input from peco menu'} |
| 105 | +} |
0 commit comments