-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmux-peasant.sh
102 lines (84 loc) · 2.43 KB
/
tmux-peasant.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env bash
show_help() {
echo "Usage: $(basename "$0") [--help] [search_pattern]"
echo
echo "Options:"
echo " --help Show this help message and exit"
echo " search_pattern Fuzzy search pattern to filter results"
echo
echo "Tipp:"
echo " ending the search_pattern with \$ signals grep that the pattern should end here"
}
while getopts ":h-:" opt; do
case $opt in
-)
case "${OPTARG}" in
help)
show_help
exit 0
;;
*)
show_help >&2
exit 1
;;
esac
;;
h)
show_help
exit 0
;;
*)
show_help >&2
exit 1
;;
esac
done
shift $((OPTIND-1))
if ! command -v tmux &> /dev/null; then
echo "Error: tmux is not installed. Please install tmux before running this script."
exit 1
fi
# Create completet list
complete_list=""
if command -v fd &> /dev/null; then
complete_list=$(fd --hidden --type d . --base-directory ~/GitHub/ --no-ignore | grep "/.git/$" | xargs -I {} dirname {})
else
complete_list=$(find ~/GitHub/ -type d -name .git -exec dirname {} \; 2>/dev/null | grep -F -v -e "vendor var node_modules")
fi
last_input=""
echo "$complete_list"
while true; do
echo
echo -n "Enter a fuzzy search pattern: "
read -e -r -i "$last_input" fuzzy_pattern
filtered_list=$(echo "$complete_list" | grep --line-buffered -i "$fuzzy_pattern")
if [ -z "$filtered_list" ]; then
echo
echo "No matching results found."
continue
fi
if [ $(echo "$filtered_list" | wc -l) -eq 1 ]; then
echo
echo "Selected result: $filtered_list"
selected=$filtered_list
break
fi
echo
echo "Matching results:"
echo "$filtered_list"
# Save the current input as the last input for the next iteration
last_input="$fuzzy_pattern"
done
if [[ -z $selected ]]; then
exit 0
fi
selected_name=$(basename "$selected" | tr . _)
tmux_running=$(pgrep tmux)
if [[ -z $TMUX ]] && [[ -z $tmux_running ]]; then
tmux new-session -s $selected_name -c $selected "~/nvim.appimage ."
exit 0
fi
if ! tmux has-session -t=$selected_name 2> /dev/null; then
tmux new-session -ds $selected_name -c $selected "~/nvim.appimage ."
fi
tmux switch-client -t $selected_name