Add search engine possibility!

This commit is contained in:
Jeremy Gardais 2020-12-03 20:01:42 +01:00
parent c50ac146eb
commit 04c41955c3
Signed by: jegardai
GPG Key ID: E759BAA22501AF32
1 changed files with 71 additions and 10 deletions

81
qb
View File

@ -71,6 +71,11 @@ define_vars() { # {{{
QUTEBROWSER_QUICKMARK_FILE="${HOME}/.config/qutebrowser/quickmarks" QUTEBROWSER_QUICKMARK_FILE="${HOME}/.config/qutebrowser/quickmarks"
QUTEBROWSER_BOOKMARK_FILE="${HOME}/.config/qutebrowser/bookmarks/urls" QUTEBROWSER_BOOKMARK_FILE="${HOME}/.config/qutebrowser/bookmarks/urls"
## Qutebrowser search engines from current user configuration
QUTEBROWSER_SEARCHENGINE_FILE="${HOME}/.config/qutebrowser/config.py"
QUTEBROWSER_SEARCHENGINE_LIST="/tmp/qutebrowser_searchengine.list"
rm -f -- "${QUTEBROWSER_SEARCHENGINE_LIST}" ; touch "${QUTEBROWSER_SEARCHENGINE_LIST}"
## Default window pattern to search ## Default window pattern to search
QUTEBROWSER_WINDOW_TITLE="qutebrowser" QUTEBROWSER_WINDOW_TITLE="qutebrowser"
} }
@ -245,32 +250,58 @@ get_qutebrowser_content() { # {{{
[ -S "${QUTEBROWSER_SOCKET_FILE}" ] && get_qutebrowser_buffers [ -S "${QUTEBROWSER_SOCKET_FILE}" ] && get_qutebrowser_buffers
get_qutebrowser_searchengine
[ -f "${QUTEBROWSER_QUICKMARK_FILE}" ] && sed "s/^/qq /g" "${QUTEBROWSER_QUICKMARK_FILE}" >> "${QUTEBROWSER_GLOBAL_CONTENT}" [ -f "${QUTEBROWSER_QUICKMARK_FILE}" ] && sed "s/^/qq /g" "${QUTEBROWSER_QUICKMARK_FILE}" >> "${QUTEBROWSER_GLOBAL_CONTENT}"
[ -f "${QUTEBROWSER_BOOKMARK_FILE}" ] && sed "s/^/bb /g" "${QUTEBROWSER_BOOKMARK_FILE}" >> "${QUTEBROWSER_GLOBAL_CONTENT}" [ -f "${QUTEBROWSER_BOOKMARK_FILE}" ] && sed "s/^/bb /g" "${QUTEBROWSER_BOOKMARK_FILE}" >> "${QUTEBROWSER_GLOBAL_CONTENT}"
} }
# }}} # }}}
search_qb_content() { # {{{ get_qutebrowser_searchengine() { # {{{
if [ -f "${QUTEBROWSER_SEARCHENGINE_FILE}" ]; then
### Store search engine in global list (for display)
sed -n "s/c.url.searchengines\['\(.*\)'\] = '\(.*\)'$/\1 \2/p" "${QUTEBROWSER_SEARCHENGINE_FILE}" >> "${QUTEBROWSER_GLOBAL_CONTENT}"
### And in specific list to verify is an engine was used
sed -n "s/c.url.searchengines\['\(.*\)'\] = '\(.*\)'$/\1 \2/p" "${QUTEBROWSER_SEARCHENGINE_FILE}" >> "${QUTEBROWSER_SEARCHENGINE_LIST}"
fi
if [ -s "${QUTEBROWSER_SEARCHENGINE_LIST}" ]; then
return_get_qutebrowser_searchengine="0"
debug_message "get_qutebrowser_searchengine \
successfully add search engines from ${QUTEBROWSER_SEARCHENGINE_FILE} file to temp list."
else
return_get_qutebrowser_searchengine="1"
debug_message "get_qutebrowser_searchengine \
Can't get search engines list.\
${RED}${QUTEBROWSER_SEARCHENGINE_FILE}${COLOR_DEBUG} file doesn't exist or impossible to parse."
fi
return "${return_get_qutebrowser_searchengine}"
}
# }}}
goto_qutebrowser_content() { # {{{
get_qutebrowser_content get_qutebrowser_content
debug_message "search_qb_content \ debug_message "goto_qutebrowser_content \
Search in Qutebrowser's content ${QUTEBROWSER_GLOBAL_CONTENT} file." Search in Qutebrowser's content ${QUTEBROWSER_GLOBAL_CONTENT} file."
st -g 90x30+0+540 -n QuteBrowser -t QuteBrowser -e sh -c "cat ${QUTEBROWSER_GLOBAL_CONTENT} | fzf +m > ${choice_temp_file}" st -g 90x30+0+540 -n QuteBrowser -t QuteBrowser -e sh -c "cat ${QUTEBROWSER_GLOBAL_CONTENT} | fzf +m > ${choice_temp_file}"
if [ -s "${choice_temp_file}" ]; then if [ -s "${choice_temp_file}" ]; then
debug_message "search_qb_content \ debug_message "goto_qutebrowser_content \
Store results in ${choice_temp_file}." Store results in ${choice_temp_file}."
return_search_qb_content="0" return_goto_qutebrowser_content="0"
else else
debug_message "search_qb_content \ debug_message "goto_qutebrowser_content \
Search aborded or can't find matching bookmark." Search aborded or can't find matching bookmark."
return_search_qb_content="1" return_goto_qutebrowser_content="1"
fi fi
return "${return_search_qb_content}" return "${return_goto_qutebrowser_content}"
} }
# }}} # }}}
@ -326,6 +357,33 @@ Try to switch to buffer id: ${buffer_id}."
## Ensure to focus on expected buffer ## Ensure to focus on expected buffer
QUTEBROWSER_WINDOW_TITLE="${buffer_title}" QUTEBROWSER_WINDOW_TITLE="${buffer_title}"
}
# }}}
use_searchengine() { # {{{
return_use_searchengine="0"
## If the search engine can't be found
if ! grep --quiet -E "^${search_engine}" -- "${QUTEBROWSER_SEARCHENGINE_LIST}"; then
### Set search engine to DEFAULT
search_engine="DEFAULT"
### Be sure to take the complete request
search_request=$(cat "${choice_temp_file}")
fi
debug_message "use_searchengine \
Use ${RED}${search_engine}${COLOR_DEBUG} search engine."
search_url=$(grep -E "^${search_engine}" -- ${QUTEBROWSER_SEARCHENGINE_LIST} | cut --delimiter=" " --field=3)
debug_message "use_searchengine \
Use URL ${RED}${search_url}${COLOR_DEBUG}"
url=$(printf "%s" "${search_url}" | sed "s/{}/${search_request}/")
goto_url
return "${return_use_searchengine}"
} }
# }}} # }}}
get_url() { # {{{ get_url() { # {{{
@ -361,8 +419,11 @@ Buffer title from Qutebrowser: ${RED}${buffer_title}${COLOR_DEBUG}."
switch_qutebrowser_buffer switch_qutebrowser_buffer
;; ;;
* ) * )
search_engine=$(printf "%s" "${local_content}" | cut -d" " -f1)
search_request=$(printf "%s" "${local_content}" | cut --complement --delimiter=" " --field=1)
debug_message "get_url \ debug_message "get_url \
Content not yet managed." Try to search the content."
use_searchengine
;; ;;
esac esac
@ -375,8 +436,8 @@ goto_existing_qutebrowser() { # {{{
debug_message "goto_existing_qutebrowser \ debug_message "goto_existing_qutebrowser \
Try to open content in existing instance." Try to open content in existing instance."
## Try to open Qutebrowser content ## Try to open Qutebrowser content (buffer, bookmark, quickmark,…)
search_qb_content \ goto_qutebrowser_content \
&& get_url && get_url
## Be sure to focus to the expected buffer ## Be sure to focus to the expected buffer