#!/bin/zsh #dep:rofi emulate -L zsh SYSTEM_CONFIG_PATH='/etc/dmsearch' CONFIG_PATH="$HOME/.config/dmsearch" CACHE_PATH="$HOME/.cache/dmsearch" mkdir -p "${CACHE_PATH}" ################################################################################ # helpers ################################################################################ # define a module option # Parameters: # 1: module id # 2: option name # 3: default value # 4: description optdef() { typeset -A -g opts_${1} local optname="opt_${1}_${2}" eval "if [ -z \"\$${optname}\" ]; then ${optname}=\"$3\"; fi" eval "opts_${1}[$2]=\"${4:-"undocumented"}\"" } # get history for a search # parameters: # 1: search, for which to get history dmhist() { local search=$1 local dmhistfile="$CACHE_PATH/history_$search" if [[ -e "$dmhistfile" ]] then <"$dmhistfile" else > "$dmhistfile" mv "$dmhistfile" "${dmhistfile}.tmp" tail -n ${truncate} "${dmhistfile}.tmp" > "${dmhistfile}" rm "${dmhistfile}.tmp" } # freetext rofi with a default value. # # When called, opens a rofi with only one choice. Pressing enter without any dmdefault() { value=$(echo "$2" | rofi -dmenu -l 1 -p "$1" -sort-method fzf) if [[ "$value" == "$2" ]]; then $=3 else echo "$value" fi } # adapted from https://gist.github.com/lucasad/6474224 # urlencodes the string given as argument, or, if no string is given, its input # on stdin urlencode() { setopt localoptions extendedglob local input if [[ -n $1 ]]; then input=( ${(s::)1} ) else input=( ${(s::)$(&p 6<&p; coproc exit; exec 5>&- # get dmenu result search=$(read -eu 6 | cut -d" " -f1) [[ -z "$search" ]] && exit 1 #user aborted if which "s_${search}" &>/dev/null; then echo >&2 "Module s_${search} starting" qs=$(s_${search}) else echo >&2 "No module for ${search}, using generic" qs="${SERCHILO}${search}" result=$(dmhist "$search" | dmenu -i -l 20 -p "params to $search") dmhistadd "$search" "$result" qs+="+$(echo $result| urlencode)" fi echo ${webbrowser_cmd} $qs ${webbrowser_cmd} $qs