-
Notifications
You must be signed in to change notification settings - Fork 11
/
graphene
executable file
·118 lines (106 loc) · 3.62 KB
/
graphene
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
#!/usr/bin/env bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
display_usage() {
echo -e "\nUsage: $0 [options] [engine] [query]"
echo -e " Search for [query] using specified search engine."
echo -e "\n $0 [options] url [url]"
echo -e " Parse a specific website/url."
echo -e "Options:\n"
echo -e "\t-h, --help\t\tPrint this help message and exit.\n"
echo -e "\t-r [number]\t\tNumber of results you want the search to return (not all engines will respect this).\n"
echo -e "\t-o\t\t\tShow only the main result group, and omit all other groups.\n"
}
show_search() {
local engine query controls selection
engine=$1
query="${@:2}"
controls="f1:execute(bash $DIR/open_in_graphene.sh $engine {}),f2:toggle-preview"
selection="$(node "$DIR/search.js" $engine "$query" |
fzf --reverse --ansi --tiebreak=begin,index --bind "$controls" \
--preview-window=right:40% --header-lines=1 --preview="node '$DIR/preview.js' $engine {}")"
if [ ! -z "$selection" ]; then
bash "$DIR/open.sh" $engine "$selection"
fi
}
# populates caches for all engines this script is aware of
initialize() {
local engine
engine=$1
echo "Rebuilding cache for $engine..."
mkdir -p .cache
export CACHING=1
node "$DIR/search.js" $engine
unset CACHING
}
# open history item
open_history() {
local timestamp urltype url
date=$1
time=$2
urltype=$3
url=$4
case $urltype in
U) # direct url
show_search url $url
;;
S) # search query
show_search url $url
;;
N) # navigational link
show_search url $url
;;
R) # result opened in graphene
bash "$DIR/open_in_graphene.sh" $url
;;
X) # externally opened result
open $url
;;
esac
}
if [ "$#" -lt 2 ]; then
display_usage
exit 1
fi
while getopts ":hr:o" opt; do
case $opt in
h|help) # help
display_usage
exit 0
;;
r|results) # number of results to prefer
export RESULTS=$OPTARG; shift
;;
o|only) # number of results to prefer
export ONLY_MAIN=1; shift
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
if [ "$1" == "history" ]; then
# figure out if this has GNU date, or fallback to OSX date if not
date="date -d @"
date -d @1550000000 &>/dev/null
if [ $? -eq 1 ]; then
date="date -r "
fi
legend="\x1b[1m\x1b[30mS: Search page\tU: Direct URL\tN: Navigational page\tR: Graphene-opened search result\tX: Externally-opened search result\x1b[0m"
esc=$(echo -e "\x1b") # sed seems to have trouble generating this sequence
open_history $(cat "$DIR/.cache/history" | awk -v dt="$date" '{
cmd = dt substr( $1, 1, length($1) - 3 ) " +\"%Y-%m-%d %H:%M\""
if ( (cmd | getline dd) > 0 ) {
$1 = dd
}
close(cmd)
print
}' | sed 's/\([0-9-]\{10\} [0-9:]\{5\}\) \([A-Z"]\)\( [a-z>]*>\)\{0,1\}\(.*\)\{0,1\}\( [^[:space:]]*\)$/'$esc'[1m'$esc'[30m\1'$esc'[0m '$esc'[31m\2'$esc'[36m\3'$esc'[0m\4'$esc'[0m '$esc'[34m\5'$esc'[0m/; s/"\(.*\)"/\1/' |
fzf --reverse --ansi --header " Browsing History " --tiebreak=begin,index --tac --preview-window=bottom:1 --preview="echo -e '$legend'")
exit
elif [ "$1" != "url" ]; then
if [ ! -d "$DIR/.cache" ] || [ ! -f "$DIR/.cache/$1-template.json" ]; then
initialize $1
fi
fi
show_search $1 "${@:2}"