File: commandlinefu

package info (click to toggle)
surfraw 2.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,624 kB
  • sloc: sh: 7,977; perl: 824; makefile: 277
file content (65 lines) | stat: -rwxr-xr-x 2,201 bytes parent folder | download | duplicates (3)
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
#!/bin/sh
# $Id$
# elvis: commandlinefu		-- search on www.commandlinefu.com
# The API of the site is described at http://www.commandlinefu.com/site/api.
. surfraw || exit 1

w3_config_hook () {
    defyn SURFRAW_commandlinefu_votes  1
    def   SURFRAW_commandlinefu_format html
}

w3_usage_hook () {
    cat <<EOF
Usage: $w3_argv0 [options] [search words]...
Description:
  Surfraw search www.commandlinefu.com
Local options:
  -html                         get results as html
  -txt                          get results as plain text
  -json                         get results as json
  -rss                          get results as rss data
                                Default: $SURFRAW_commandlinefu_format
  -v, -s, -votes, -sort         sort results by votes
  -u, -n, -unsort, -nosort      do not sort results
                                Default: $SURFRAW_commandlinefu_votes
EOF
    w3_global_usage
}

w3_parse_option_hook () {
    opt="$1"
    optarg="$2"
    case "$opt" in
    -json)                 setopt   SURFRAW_commandlinefu_format json      ;;
    -html)                 setopt   SURFRAW_commandlinefu_format html      ;;
    -rss)                  setopt   SURFRAW_commandlinefu_format rss       ;;
    -txt)                  setopt   SURFRAW_commandlinefu_format plaintext ;;
    -v|-votes|-s|-sort)    setoptyn SURFRAW_commandlinefu_votes  1         ;;
    -u|-unsort|-n|-nosort) setoptyn SURFRAW_commandlinefu_votes  0         ;;
    *) return 1 ;;
    esac
    return 0
}

w3_config
w3_parse_args "$@"
# w3_args now contains a list of arguments

url=https://www.commandlinefu.com/commands/matching
# The next part of the url is a normalized plaintext version of the search
# query.  It seems to be unused by the CGI but it needs to be present. So we
# normalize to "x".
url=$url/x
# What is really used is the base64 encoded query. (echo -n is not portable)
url=$url/"`printf %s "$w3_args" | base64`"
# possibly add the sorting option
if test $SURFRAW_commandlinefu_votes -eq 1; then
    url="$url/sort-by-votes"
fi
# add the output format option
if test $SURFRAW_commandlinefu_format != html; then
    url="$url/$SURFRAW_commandlinefu_format"
fi

w3_browse_url "$url"