-
Notifications
You must be signed in to change notification settings - Fork 1
/
quite-intriguing
executable file
·156 lines (142 loc) · 5.69 KB
/
quite-intriguing
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/bash
#Strongly inspired by [macho](https://hiphish.github.io/blog/2020/05/31/macho-man-command-on-steroids/)
CacheFile=$HOME/.config/qi_cachefile
TempFile=$(mktemp)
Long=0
NumResults=0
declare -a ManName
declare -a ManDescription
declare -a TldrName
declare -a TldrDescription
declare -a CheatName
declare -a CheatDescription
SearchTerm="$@"
CliOnly="true"
Rebuild="false"
Run="true"
Query=""
SCRIPTDIR="$( cd "$(dirname "$0")" ; pwd -P )"
building_our_database (){
#do this first
RawResults=""
echo "§ Reading in manpages"
RawResults=$(apropos . | grep -v -E '^.+ \(0\)')
#zsoelim (1) - satisfy .so requests in roff input
mapfile -t ManName < <(echo "$RawResults" | awk -F ' - ' '{print $1 }' | sed 's/[[:space:]]*$//')
mapfile -t ManDescription < <(echo "$RawResults" | awk -F ' - ' '{print $2 }' | sed 's/[[:space:]]*$//')
#Now for cheatsheets
#Follow the same template for any other
echo "§ Reading in cheatsheets"
mapfile -t CheatName < <(cheat -l | cat | awk '{print $1 }'| sed 's/[[:space:]]*$//')
echo "§ Reading in tldr"
mapfile -t TldrName < <(tldr -l 2>/dev/null | awk '{print $1 }'| sed 's/[[:space:]]*$//')
echo "§ Adding descriptions to cheats"
# Now we're going to add descriptions to the cheatsheets...
for ((i = 0; i < ${#CheatName[@]}; i++));do
(CheatDescription[$i]=$(echo "$RawResults" | grep -e "^${CheatName[$i]} "| awk -F '-' '{print $2 }'|head -1)) &
done
wait
echo "§ Adding descriptions to tldr"
for ((i = 0; i < ${#TldrName[@]}; i++));do
(TldrDescription[$i]=$(echo "$RawResults" | grep -e "^${TldrName[$i]} "| awk -F '-' '{print $2 }'|head -1)) &
done
wait
echo "§ Finished supplementing cheatsheets and tldr"
}
make_choices () {
echo "§ Compiling options"
for ((i = 0; i < ${#TldrName[@]}; i++));do
echo -e "${TldrName[$i]} :tldr: ${TldrDescription[$i]}" >> "$TempFile"
done
for ((i = 0; i < ${#CheatName[@]}; i++));do
echo -e "${CheatName[$i]} :cheat: ${CheatDescription[$i]}" >> "$TempFile"
done
for ((i = 0; i < ${#ManName[@]}; i++));do
echo -e "${ManName[$i]} :man: ${ManDescription[$i]}" >> "$TempFile"
done
echo "§ Sorting options"
cat "$TempFile" | sort | uniq -u > "$CacheFile"
rm -rf "$TempFile"
}
##############################################################################
# Show the Help
##############################################################################
display_help(){
echo "###################################################################"
echo "# quite-intriguing [-h|-g|-p|-r] [QUERY]"
echo "# -h show help "
echo "# -g GUI interface only. Default is CLI/TUI. "
echo "# NOTE: gui display of results is a bit wonky for some reason. "
echo "# -p Prefetch and compile options. "
echo "# -r Rebuild available options and run (raw mode). "
echo "# QUERY (optional) starts with search for QUERY (assumes TUI mode)"
echo "###################################################################"
}
choose_page (){
#cat mansearch_cachefile | fzf --no-hscroll -m --height 60% --border --ansi --no-bold --preview="/home/steven/documents/programming/multiple_scripts/mansearch-fzf-preview {}"
if [ "$CliOnly" == "true" ];then
if [ -z "$Query" ]; then
#No Preview
#SelectedFile=$(cat "$CacheFile" | fzf --no-hscroll -m --height 60% --border --ansi --no-bold)
#With Preview
SelectedFile=$(cat "$CacheFile" | fzf --no-hscroll -m --height 80% --border --ansi --no-bold --preview="$SCRIPTDIR/quite-intriguing-preview {}" | sed 's/ (/./g' | sed 's/)//g' | sed 's/:man:/:man -Pcat:/g' | awk -F ':' '{print $2 " " $1}')
else
#No Preview
#SelectedFile=$(cat "$CacheFile" | fzf --no-hscroll -m --height 60% --border --ansi --no-bold -1 --query="$Query")
#With Preview
SelectedFile=$(cat "$CacheFile" | fzf --no-hscroll -m --height 80% --border --ansi --no-bold --preview="$SCRIPTDIR/quite-intriguing-preview {}" --query="$Query" | sed 's/ (/./g' | sed 's/)//g' | sed 's/:man:/:man -Pcat:/g' | awk -F ':' '{print $2 " " $1}')
fi
if [ -z "$SelectedFile" ];then
exit 0
else
eval "$SelectedFile"
fi
else
#use ROFI, not zenity
SelectedFile=$(cat "$CacheFile" | rofi -i -dmenu -p "Which manual?" -theme DarkBlue | sed 's/ (/./g' | sed 's/)//g' | sed 's/:man:/:man -Pcat:/g' | awk -F ':' '{print $2 " " $1}')
if [ -z "$SelectedFile" ];then
exit 0
else
OutPut=$(eval "$SelectedFile")
rofi -location 1 -click-to-exit -lines 15 -fixed-num-lines -theme DarkBlue -e "$OutPut"
fi
fi
}
##############################################################################
# Get options
##############################################################################
while [ $# -gt 0 ]; do
option="$1"
case $option in
-h) display_help
exit
shift ;;
-r) echo "§ Rebuilding cache before run; please be patient."
Rebuild="true"
Run="true"
shift ;;
-p) echo "§ Rebuilding cache only."
Rebuild="true"
Run="false"
shift ;;
-g) CliOnly="false"
shift ;;
*) Query=$(echo "$1")
Run="true"
CliOnly="true"
shift;;
esac
done
if [ ! -f "$CacheFile" ];then
echo "§ Cachefile not found; rebuilding."
Rebuild="true"
fi
if [ "$Rebuild" == "true" ];then
building_our_database
make_choices
fi
if [ "$Run" == "true" ];then
choose_page
else
exit 0
fi