Skip to content

Commit

Permalink
group by topic letter (A-Z) when nothing searched
Browse files Browse the repository at this point in the history
  • Loading branch information
sunaku committed Mar 18, 2016
1 parent 75209c3 commit 1937421
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 23 deletions.
2 changes: 1 addition & 1 deletion bin/dasht
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# ### Examples
#
# `dasht`
# First 100 topics from each installed docset.
# Topics (A-Z) from each installed docset.
#
# `dasht` 'c - x'
# Search for "c - x" in all installed docsets.
Expand Down
47 changes: 34 additions & 13 deletions bin/dasht-query-exec
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
# located inside a [Dash] docset at `/Contents/Resources/docSet.dsidx`, while
# passing the optional *OPTIONS\_FOR\_SQLITE3* arguments down to sqlite3(1).
#
# If *PATTERN* is empty or solely consists of unescaped wildcard characters,
# topics having distinct leading characters are returned from the *DATABASE*.
#
# ### Searching
#
# SQL LIKE wildcard characters in *PATTERN* can be escaped with a backslash.
Expand Down Expand Up @@ -74,18 +77,17 @@ pattern=$(echo "$pattern" | sed "s/'/&&/g")
# You should have received a copy of the GNU General Public License
# along with Zeal. If not, see <http://www.gnu.org/licenses/>.
#
searchIndex_topic=name
searchIndex_query="
SELECT name,
type,
SELECT name AS name,
type AS type,
path AS url
FROM searchIndex
WHERE name LIKE '$pattern' ESCAPE '\\'
ORDER BY name COLLATE NOCASE
LIMIT 100;
"
ztoken_topic=ztokenname
ztoken_query="
SELECT ztokenname AS name,
ztypename AS type,
SELECT ztokenname AS name,
ztypename AS type,
zpath || ifnull('#' || nullif(zanchor, ''), '') AS url
FROM ztoken
JOIN ztokenmetainformation
Expand All @@ -94,9 +96,6 @@ ztoken_query="
ON zfilepath.z_pk = ztokenmetainformation.zfile
JOIN ztokentype
ON ztokentype.z_pk = ztoken.ztokentype
WHERE ztokenname LIKE '$pattern' ESCAPE '\\'
ORDER BY ztokenname COLLATE NOCASE
LIMIT 100;
"
# _ __ ____ ____ _ _
# ___ _ __ __| | ___ / _| / ___| _ \| | ___ ___ __| | ___
Expand All @@ -106,9 +105,31 @@ ztoken_query="
#
#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

if sqlite3 "$database" .tables | grep -F -q searchIndex
then query=$searchIndex_query
else query=$ztoken_query
if sqlite3 "$database" .tables | grep -F -q searchIndex; then
topic=$searchIndex_topic
query=$searchIndex_query
else
topic=$ztoken_topic
query=$ztoken_query
fi

if echo "$pattern" | tr -d '%_' | grep -q . ; then
# something particular was searched; fetch first 100 matching topics
where="WHERE $topic LIKE '$pattern' ESCAPE '\\'"
group=
limit='LIMIT 100'
else
# nothing particular was searched; group topics by leading character
query=$(echo "$query" | awk '
# min() gets FIRST topic from each group; else we get the LAST one
$1 == "SELECT" && $3 == "AS" { $2 = "min(" $2 " COLLATE NOCASE)" }
{ print }
')
where=
group="GROUP BY substr($topic,1,1)"
limit=
fi
order="ORDER BY $topic COLLATE NOCASE"
query="$query $where $group $order $limit ;"

exec sqlite3 "$@" "$database" "$query"
3 changes: 2 additions & 1 deletion bin/dasht-query-html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# ### Examples
#
# `dasht-query-html`
# First 100 topics from each installed docset.
# Topics (A-Z) from each installed docset.
#
# `dasht-query-html` 'c - x'
# Search for "c - x" in all installed docsets.
Expand Down Expand Up @@ -103,6 +103,7 @@ dasht-query-line "$@" | awk -v pattern="$1" '
sub("[[:space:]]+$", "", pattern) # strip trailing whitespace
gsub("[[:space:]]+", ".*", pattern) # treat whitespace as wildcards
pattern = ignorecase(pattern) # emulate IGNORECASE=1 for POSIX
if (pattern == "") pattern = "^." # grouped by leading character
}
NR == 1 { print "<table>" }
$2 == "=" { result[$1] = substr($0, index($0, $2) + length($2) + 1) }
Expand Down
2 changes: 1 addition & 1 deletion bin/dasht-query-line
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# ### Examples
#
# `dasht-query-line`
# First 100 topics from each installed docset.
# Topics (A-Z) from each installed docset.
#
# `dasht-query-line` 'c - x'
# Search for "c - x" in all installed docsets.
Expand Down
4 changes: 2 additions & 2 deletions bin/dasht-server-http
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
# ### Examples
#
# printf 'GET / HTTP/1.1\r\n' | `dasht-server-http`
# Shows first 100 topics from all installed [Dash] docsets.
# Shows topics (A-Z) from each installed [Dash] docset.
#
# printf 'GET /?docsets=*DOCSETS* HTTP/1.1\r\n' | `dasht-server-http`
# Shows first 100 topics from installed [Dash] docsets matching *DOCSETS*.
# Shows topics (A-Z) from installed [Dash] docsets matching *DOCSETS*.
#
# printf 'GET /?query=*PATTERN* HTTP/1.1\r\n' | `dasht-server-http`
# Searches for *PATTERN* in all installed [Dash] docsets.
Expand Down
3 changes: 3 additions & 0 deletions man/man1/dasht-query-exec.1
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Searches for \fIPATTERN\fP in \fIDATABASE\fP, which is an SQLite3 database file
located inside a Dash \[la]https://kapeli.com/dash\[ra] docset at \fB\fC/Contents/Resources/docSet.dsidx\fR, while
passing the optional \fIOPTIONS_FOR_SQLITE3\fP arguments down to
.BR sqlite3 (1).
.PP
If \fIPATTERN\fP is empty or solely consists of unescaped wildcard characters,
topics having distinct leading characters are returned from the \fIDATABASE\fP\&.
.SS Searching
.PP
SQL LIKE wildcard characters in \fIPATTERN\fP can be escaped with a backslash.
Expand Down
2 changes: 1 addition & 1 deletion man/man1/dasht-query-html.1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dasht\-query\-html \- searches Dash \[la]https://kapeli.com/dash\[ra] docsets an
.SS Examples
.TP
\fB\fCdasht\-query\-html\fR
First 100 topics from each installed docset.
Topics (A\-Z) from each installed docset.
.TP
\fB\fCdasht\-query\-html\fR 'c \- x'
Search for "c \- x" in all installed docsets.
Expand Down
2 changes: 1 addition & 1 deletion man/man1/dasht-query-line.1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dasht\-query\-line \- searches Dash \[la]https://kapeli.com/dash\[ra] docsets an
.SS Examples
.TP
\fB\fCdasht\-query\-line\fR
First 100 topics from each installed docset.
Topics (A\-Z) from each installed docset.
.TP
\fB\fCdasht\-query\-line\fR 'c \- x'
Search for "c \- x" in all installed docsets.
Expand Down
4 changes: 2 additions & 2 deletions man/man1/dasht-server-http.1
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ dasht\-server\-http \- simple search engine that powers
.SS Examples
.TP
printf 'GET / HTTP/1.1\[rs]r\[rs]n' | \fB\fCdasht\-server\-http\fR
Shows first 100 topics from all installed Dash \[la]https://kapeli.com/dash\[ra] docsets.
Shows topics (A\-Z) from each installed Dash \[la]https://kapeli.com/dash\[ra] docset.
.TP
printf 'GET /?docsets=\fIDOCSETS\fP HTTP/1.1\[rs]r\[rs]n' | \fB\fCdasht\-server\-http\fR
Shows first 100 topics from installed Dash \[la]https://kapeli.com/dash\[ra] docsets matching \fIDOCSETS\fP\&.
Shows topics (A\-Z) from installed Dash \[la]https://kapeli.com/dash\[ra] docsets matching \fIDOCSETS\fP\&.
.TP
printf 'GET /?query=\fIPATTERN\fP HTTP/1.1\[rs]r\[rs]n' | \fB\fCdasht\-server\-http\fR
Searches for \fIPATTERN\fP in all installed Dash \[la]https://kapeli.com/dash\[ra] docsets.
Expand Down
2 changes: 1 addition & 1 deletion man/man1/dasht.1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dasht \- API documentation in your terminal
.SS Examples
.TP
\fB\fCdasht\fR
First 100 topics from each installed docset.
Topics (A\-Z) from each installed docset.
.TP
\fB\fCdasht\fR 'c \- x'
Search for "c \- x" in all installed docsets.
Expand Down

0 comments on commit 1937421

Please sign in to comment.