about summary refs log tree commit diff
path: root/nixos/scripts/dm-websearch
blob: 3dcfd341d598122ade0f9eda6b0b14525cc53d41 (plain)
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
#!/usr/bin/env bash
#
# Script name: dm-websearch
# Description: Search various search engines (inspired by surfraw).
# Dependencies: dmenu, fzf, rofi, brave (change DMMBROWSER in dmscripts config if using another browser)
# GitLab: https://www.gitlab.com/dwt1/dmscripts
# License: https://www.gitlab.com/dwt1/dmscripts/LICENSE
# Contributors: Derek Taylor
#               Ali Furkan Yıldız
#               HostGrady
#               Simon Ingelsson

# Set with the flags "-e", "-u","-o pipefail" cause the script to fail
# if certain things happen, which is a good thing.  Otherwise, we can
# get hidden bugs that are hard to discover.
set -euo pipefail

# shellcheck disable=SC1091
source ./_dm-helper.sh 2>/dev/null || source _dm-helper.sh 2>/dev/null

source_dmscripts_configs

if configs_are_different; then
    echo "$(date): configs are different" >>"$DM_CONFIG_DIFF_LOGFILE"
    sleep 1
fi

main() {
    if [ -z "${!websearch[*]}" ]; then
        notify-send "dm-websearch: BREAKING CHANGES" "Due to breaking changes you must edit all declare statements in your config to include the g option. declare -A -> declare -Ag, declare -a -> declare -ag"
        echo "$(date): dm-websearch: BREAKING CHANGES: Due to breaking changes you must edit all declare statements in your config to include the g option.
are -A -> declare -Ag
are -a -> declare -ag" >>"$DM_CONFIG_DIFF_LOGFILE"
        sleep 2
        exit 1
    fi
    # As this is loaded from other file it is technically not defined
    # shellcheck disable=SC2154 # Choosing  a search engine by name from array above.
    engine=$(printf '%s\n' "${!websearch[@]}" | sort | ${MENU} 'Choose search engine:') || exit 1

    # Getting the URL of the search engine we chose.
    url="${websearch["${engine}"]}"

    # Searching the chosen engine.
    query=$(printf '%s' "$engine" | ${MENU} 'Enter search query:')

    query="$(echo -n "${query}" | jq -s -R -r @uri)"
    # Display search results in web browser
    # shellcheck disable=SC2154
    ${DMBROWSER} "${url}${query}"
}

MENU="$(get_menu_program "$@")"
[[ "${BASH_SOURCE[0]}" == "${0}" ]] && main