-
Notifications
You must be signed in to change notification settings - Fork 0
/
asdf
executable file
·142 lines (131 loc) · 3.8 KB
/
asdf
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/bin/bash
shell=unknown
sourced=false
current_dir=
warnings=true
_shellcheck() {
if [[ -n $BASH_VERSION ]]; then
if [[ "${BASH_SOURCE}" != "$0" ]]; then
sourced=true
fi
shell="bash"
elif [[ -n $ZSH_VERSION ]]; then
if [[ "${(%):-%x} != "$0"" ]]; then
sourced=true
fi
shell="zsh"
else
if [[ $warnings == true ]]; then
echo "WARNING: Unknown shell. This was made for bash/zsh."
echo " It may not work for your shell."
fi
fi
if [[ $sourced == true ]]; then
if [[ "$shell" =~ "bash" ]]; then
current_dir=`dirname ${BASH_SOURCE}`
elif [[ "$shell" =~ "zsh" ]]; then
current_dir=`dirname ${(%):-%x}`
fi
else
if [[ $warnings == true ]]; then
echo "WARNING: It seems like this is running as a script."
echo " Some commands (like cd), and features (like adding to HISTFILE) won't work."
echo " Source this script instead of just running it to resolve these issues."
fi
current_dir=`dirname $0`
fi
if [[ ! -f "$HISTFILE" ]]; then
echo "ERROR: HISTFILE not found. Currently set to '$HISTFILE'"
echo " Make sure it's set and exported. Ex. in your .bashrc/.zshrc"
echo " try 'export HISTFILE=~/.eternal_history'"
return 1
fi
return 0
}
# command line switch defaults
write=true
ps1hack=false
_help() {
echo "Usage: asdf [OPTION]"
echo "Fast and flexible history searching and exploring."
echo "Type a command to search your history for it."
echo "Spaces allow you to search for multiple strings at the same time."
echo ""
echo "Controls:"
echo " Press Enter to run the command, or ^Space to edit the command and then run it."
echo " ^Del to remove the selected command from your HISTFILE."
echo " ^C to exit at any time without running anything."
echo ""
echo "Available options:"
echo " --write:[true|false] Whether to write to HISTFILE when running a command."
echo " Default:true"
echo " --warnings:[true|false] Whether to display warnings."
echo " Default:true"
echo " --ps1hack:[true|false] Whether to try to show the PS1 when editing a command."
echo " Default:false"
echo " --help Print this help text and exit."
}
_asdf_main() {
results=`HISTFILE="$HISTFILE" ${current_dir}/asdf_search.exe`
asdf_status=$?
if [ $asdf_status -ge 10 ]; then
to_run=$results
if [ $asdf_status -eq 11 ]; then
if [[ $shell == "bash" ]]; then
if [[ $ps1hack == true ]]; then
# hack to render a user's PS1
expPS1=$(echo "HISTFILE=/dev/null PS1=\"$PS1@\" && cd `pwd`" | bash -i 2>&1 | grep "@exit$" | head -n 1 | sed 's/\(.*\)@exit$/\1/')
read -e -p "$expPS1" -i "$results" to_run
else
read -e -i "$results" to_run
fi
elif [[ $shell == "zsh" ]]; then
to_run=$results
vared to_run
fi
fi
if [[ "$to_run" != "" && "$to_run" ]]; then
if [ $asdf_status -ne 11 ]; then
echo "$to_run"
fi
if [[ $write == true ]]; then
if [[ $shell == "bash" ]]; then
history -s "$to_run"
elif [[ $shell == "zsh" ]]; then
print -S "$to_run"
fi
fi
eval $to_run
fi
fi
}
runasdf=true
# process arguments
while [ "$1" != "" ]; do
case $1 in
--help*)
_help
runasdf=false
;;
--write:*)
write=`echo $1 | sed 's/^--[^:]*:\(.*\)$/\1/'`
;;
--warnings:*)
warnings=`echo $1 | sed 's/^--[^:]*:\(.*\)$/\1/'`
;;
--ps1hack:*)
ps1hack=`echo $1 | sed 's/^--[^:]*:\(.*\)$/\1/'`
;;
*)
echo "Unknown argument, printing helptext and exiting..."
_help
runasdf=false
esac
shift
done
if [[ "$runasdf" = true ]]; then
_shellcheck
if [[ $? == 0 ]]; then
_asdf_main
fi
fi