2020-12-02 10:13:02 +01:00
|
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
|
|
# Vars {{{
|
|
|
|
|
readonly PROGNAME=$(basename "${0}")
|
|
|
|
|
readonly NBARGS="${#}"
|
|
|
|
|
## Test if DEBUG is already defined (by parent script,…)
|
|
|
|
|
[ -z "${DEBUG}" ] && DEBUG=1
|
|
|
|
|
|
|
|
|
|
## Colors
|
2020-12-02 10:36:15 +01:00
|
|
|
|
readonly PURPLE='\033[1;35m'
|
2020-12-02 10:13:02 +01:00
|
|
|
|
readonly RED='\033[0;31m'
|
|
|
|
|
readonly RESET='\033[0m'
|
|
|
|
|
readonly COLOR_DEBUG="${PURPLE}"
|
|
|
|
|
# }}}
|
|
|
|
|
usage() { # {{{
|
|
|
|
|
|
|
|
|
|
cat <<- EOF
|
2020-12-02 15:48:38 +01:00
|
|
|
|
usage: $PROGNAME [--debug,--help] [--git,--package,--venv]
|
2020-12-02 10:13:02 +01:00
|
|
|
|
|
|
|
|
|
Try to launch the most appropriate qutebrowser binary :
|
|
|
|
|
1. From venv
|
|
|
|
|
2. From APT package
|
|
|
|
|
3. From Git repository
|
|
|
|
|
|
|
|
|
|
EXAMPLES :
|
|
|
|
|
- Launch qutebrowser
|
|
|
|
|
${PROGNAME}
|
|
|
|
|
|
|
|
|
|
OPTIONS :
|
|
|
|
|
-d,--debug
|
|
|
|
|
Enable debug messages.
|
|
|
|
|
|
2020-12-02 15:48:38 +01:00
|
|
|
|
--git
|
|
|
|
|
Force to use QuteBrowser from Git repository.
|
|
|
|
|
|
2020-12-02 10:13:02 +01:00
|
|
|
|
--help
|
|
|
|
|
Print this help message.
|
|
|
|
|
|
2020-12-02 15:48:38 +01:00
|
|
|
|
--package
|
|
|
|
|
Force to use QuteBrowser from package.
|
|
|
|
|
|
|
|
|
|
--venv
|
|
|
|
|
Force to use QuteBrowser from Python Virtual ENVironment.
|
|
|
|
|
|
2020-12-02 10:13:02 +01:00
|
|
|
|
EOF
|
|
|
|
|
|
2020-12-02 10:36:15 +01:00
|
|
|
|
}
|
|
|
|
|
# }}}
|
|
|
|
|
define_vars() { # {{{
|
|
|
|
|
|
|
|
|
|
## List of process pattern to monitor
|
|
|
|
|
qutebrowser_proc_pattern="(qutebrowser)"
|
|
|
|
|
|
2020-12-02 15:48:38 +01:00
|
|
|
|
## Use Qutebrowser from Python Virtual Environment by default
|
|
|
|
|
QB_VENV_MODE="0"
|
|
|
|
|
|
2020-12-02 14:38:03 +01:00
|
|
|
|
## Store selected content to a temp file
|
|
|
|
|
choice_temp_file="$(mktemp -t ${PROGNAME}-XXXXXX.tmp)"
|
|
|
|
|
|
2020-12-02 10:13:02 +01:00
|
|
|
|
}
|
|
|
|
|
# }}}
|
|
|
|
|
debug_message() { # {{{
|
|
|
|
|
|
|
|
|
|
local_debug_message="${1}"
|
|
|
|
|
|
|
|
|
|
## Print message if DEBUG is enable (=0)
|
|
|
|
|
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6b\e[m\n' "DEBUG − ${PROGNAME} : ${local_debug_message}"
|
|
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
# }}}
|
2020-12-02 15:58:35 +01:00
|
|
|
|
error_message() { # {{{
|
|
|
|
|
|
|
|
|
|
local_error_message="${1}"
|
|
|
|
|
local_error_code="${2}"
|
|
|
|
|
|
|
|
|
|
## Print message if DEBUG is enable (=0)
|
|
|
|
|
[ "${DEBUG}" -eq "0" ] && printf '%b\n' "ERROR − ${PROGNAME} : ${RED}${local_error_message}${RESET}"
|
|
|
|
|
|
|
|
|
|
exit "${local_error_code:=66}"
|
|
|
|
|
}
|
|
|
|
|
# }}}
|
2020-12-02 10:36:15 +01:00
|
|
|
|
is_proc_running() { # {{{
|
|
|
|
|
|
|
|
|
|
local_proc_pattern="${1}"
|
|
|
|
|
|
|
|
|
|
local_count_proc_pattern="$(pgrep -f -- "${local_proc_pattern}" | wc -l)"
|
|
|
|
|
|
|
|
|
|
case "${local_count_proc_pattern}" in
|
|
|
|
|
0 ) ## No procs related to this pattern are running
|
|
|
|
|
return_proc_running="1"
|
|
|
|
|
;;
|
|
|
|
|
* ) ## At least one proc seems running
|
|
|
|
|
return_proc_running="0"
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
## Simple debug message to valid current variables
|
|
|
|
|
debug_message "is_proc_running − \
|
|
|
|
|
procs running (with the pattern: ${RED}${local_proc_pattern}${COLOR_DEBUG}) on the current host: ${RED}${local_count_proc_pattern}${COLOR_DEBUG}."
|
|
|
|
|
|
|
|
|
|
return "${return_proc_running}"
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
# }}}
|
2020-12-02 15:48:38 +01:00
|
|
|
|
get_qutebrowser_bin() { # {{{
|
|
|
|
|
|
|
|
|
|
local_get_qutebrowser_bin_return="1"
|
|
|
|
|
|
|
|
|
|
## First try venv {{{
|
|
|
|
|
if [ "${QB_VENV_MODE}" -eq "0" ] && [ -f ~/src/qutebrowser-venv/bin/python3 ]; then
|
|
|
|
|
debug_message "get_qutebrowser_bin − \
|
|
|
|
|
Qutebrowser from ${RED}venv${COLOR_DEBUG} can be used."
|
|
|
|
|
QUTEBROWSER_BIN="${HOME}/src/qutebrowser-venv/bin/python3 -m qutebrowser"
|
|
|
|
|
local_get_qutebrowser_bin_return="0"
|
|
|
|
|
### Be sure to skip other MODE if not already defined
|
|
|
|
|
[ -z "${QB_PACKAGE_MODE}" ] && QB_PACKAGE_MODE="1"
|
|
|
|
|
[ -z "${QB_GIT_MODE}" ] && QB_GIT_MODE="1"
|
|
|
|
|
else
|
|
|
|
|
debug_message "get_qutebrowser_bin − \
|
|
|
|
|
Qutebrowser from ${RED}venv${COLOR_DEBUG} not selected or can't be used."
|
|
|
|
|
### Be sure to test package MODE
|
|
|
|
|
QB_PACKAGE_MODE="0"
|
2020-12-02 10:13:02 +01:00
|
|
|
|
fi
|
2020-12-02 15:48:38 +01:00
|
|
|
|
## }}}
|
|
|
|
|
|
|
|
|
|
## Then try package {{{
|
|
|
|
|
if [ "${QB_PACKAGE_MODE}" -eq "0" ] && dpkg -l | grep -q qutebrowser ; then
|
|
|
|
|
debug_message "get_qutebrowser_bin − \
|
|
|
|
|
Qutebrowser from ${RED}package${COLOR_DEBUG} will be used."
|
|
|
|
|
QUTEBROWSER_BIN="$(command qutebrowser)"
|
|
|
|
|
local_get_qutebrowser_bin_return="0"
|
|
|
|
|
### Be sure to skip other MODE if not already defined
|
|
|
|
|
[ -z "${QB_GIT_MODE}" ] && QB_GIT_MODE="1"
|
|
|
|
|
else
|
|
|
|
|
debug_message "get_qutebrowser_bin − \
|
|
|
|
|
Qutebrowser from ${RED}package${COLOR_DEBUG} not selected or can't be used."
|
|
|
|
|
### Allow to try last MODE if not already defined
|
|
|
|
|
[ -z "${QB_GIT_MODE}" ] && QB_GIT_MODE="0"
|
2020-12-02 10:13:02 +01:00
|
|
|
|
fi
|
2020-12-02 15:48:38 +01:00
|
|
|
|
## }}}
|
|
|
|
|
|
|
|
|
|
## Finally, try git repository {{{
|
|
|
|
|
if [ "${QB_GIT_MODE}" -eq "0" ] && [ -f ~/repos/qutebrowser/qutebrowser.py ]; then
|
|
|
|
|
QUTEBROWSER_BIN="${HOME}/repos/qutebrowser/qutebrowser.py --backend webengine"
|
|
|
|
|
debug_message "get_qutebrowser_bin − \
|
|
|
|
|
Qutebrowser from ${RED}Git repository${COLOR_DEBUG} will finally be used."
|
|
|
|
|
local_get_qutebrowser_bin_return="0"
|
|
|
|
|
else
|
|
|
|
|
debug_message "get_qutebrowser_bin − \
|
|
|
|
|
Qutebrowser from ${RED}git repository${COLOR_DEBUG} not selected or can't be used."
|
2020-12-02 10:13:02 +01:00
|
|
|
|
fi
|
2020-12-02 15:48:38 +01:00
|
|
|
|
## }}}
|
|
|
|
|
|
|
|
|
|
return "${local_get_qutebrowser_bin_return}"
|
2020-12-02 10:13:02 +01:00
|
|
|
|
|
2020-12-02 16:35:28 +01:00
|
|
|
|
}
|
|
|
|
|
# }}}
|
|
|
|
|
focus_to_qutebrowser() { # {{{
|
|
|
|
|
|
|
|
|
|
## Get desktop and window ID of the first "qutebrowser" window
|
|
|
|
|
qutebrowser_desktop_id=$(command wmctrl -l | grep --max-count=1 "qutebrowser" | cut -d" " -f3)
|
|
|
|
|
qutebrowser_window_id=$(command wmctrl -l | grep --max-count=1 "qutebrowser" | cut -d" " -f1)
|
|
|
|
|
|
|
|
|
|
debug_message "focus_to_qutebrowser − \
|
|
|
|
|
Qutebrowser window ID : ${RED}${qutebrowser_window_id}${COLOR_DEBUG} on desktop ID : ${RED}${qutebrowser_desktop_id}${COLOR_DEBUG}."
|
|
|
|
|
|
|
|
|
|
## Switch to Qutebrowser desktop and first window
|
|
|
|
|
command wmctrl -s "${qutebrowser_desktop_id}"
|
|
|
|
|
command wmctrl -i -R "${qutebrowser_window_id}"
|
|
|
|
|
|
2020-12-02 14:38:03 +01:00
|
|
|
|
}
|
|
|
|
|
# }}}
|
|
|
|
|
goto_existing_qutebrowser() { # {{{
|
|
|
|
|
|
|
|
|
|
debug_message "goto_existing_qutebrowser − \
|
|
|
|
|
Try to open content in existing instance."
|
|
|
|
|
|
2020-12-02 16:35:28 +01:00
|
|
|
|
## Try to open Qutebrowser content
|
2020-12-02 14:38:03 +01:00
|
|
|
|
search_qb_bookmark \
|
|
|
|
|
&& open_in_qutebrowser
|
|
|
|
|
|
2020-12-02 16:35:28 +01:00
|
|
|
|
return 0
|
|
|
|
|
|
2020-12-02 14:38:03 +01:00
|
|
|
|
}
|
|
|
|
|
# }}}
|
|
|
|
|
search_qb_bookmark() { # {{{
|
|
|
|
|
|
|
|
|
|
debug_message "search_qb_bookmark − \
|
|
|
|
|
Search in Qutebrowser's bookmarks."
|
|
|
|
|
|
|
|
|
|
st -g 90x30+0+540 -n QuteBrowser -t QuteBrowser -e sh -c "cat ~/.config/qutebrowser/quickmarks ~/.config/qutebrowser/bookmarks/urls | fzf +m > ${choice_temp_file}"
|
|
|
|
|
|
|
|
|
|
if [ -s "${choice_temp_file}" ]; then
|
|
|
|
|
debug_message "search_qb_bookmark − \
|
|
|
|
|
Store results in ${choice_temp_file}."
|
|
|
|
|
local_search_qb_bookmark_return="0"
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
debug_message "search_qb_bookmark − \
|
|
|
|
|
Search aborded or can't find matching bookmark."
|
|
|
|
|
local_search_qb_bookmark_return="1"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
return "${local_search_qb_bookmark_return}"
|
|
|
|
|
|
2020-12-02 16:51:15 +01:00
|
|
|
|
}
|
|
|
|
|
# }}}
|
|
|
|
|
start_qutebrowser() { # {{{
|
|
|
|
|
|
|
|
|
|
local_start_qutebrowser_return="1"
|
|
|
|
|
|
|
|
|
|
if is_proc_running "${qutebrowser_proc_pattern}"; then
|
|
|
|
|
debug_message "start_qutebrowser − \
|
|
|
|
|
Qutebrowser is already started."
|
|
|
|
|
else
|
|
|
|
|
debug_message "start_qutebrowser − \
|
|
|
|
|
No existing instance of Qutebrowser. Starting…"
|
|
|
|
|
"${QUTEBROWSER_BIN}"
|
|
|
|
|
local_start_qutebrowser_return="0"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
## In any case, be sure to focus to Qutebrowser
|
|
|
|
|
focus_to_qutebrowser
|
|
|
|
|
|
|
|
|
|
return "${local_start_qutebrowser_return}"
|
|
|
|
|
|
2020-12-02 14:38:03 +01:00
|
|
|
|
}
|
|
|
|
|
# }}}
|
|
|
|
|
open_in_qutebrowser() { # {{{
|
|
|
|
|
|
|
|
|
|
local_content=$(cat "${choice_temp_file}")
|
|
|
|
|
|
|
|
|
|
debug_message "open_in_qutebrowser − \
|
|
|
|
|
Try to manage ${RED}$(cat "${choice_temp_file}")${COLOR_DEBUG}."
|
|
|
|
|
|
|
|
|
|
case "${local_content}" in
|
|
|
|
|
http* ) ## Classic bookmark
|
|
|
|
|
local_url=$(printf "%s" "${local_content}" | sed -e "s;\(http.*\) .*;\1;")
|
|
|
|
|
debug_message "open_in_qutebrowser − \
|
|
|
|
|
Try to open classic bookmark URL ${RED}${local_url}${COLOR_DEBUG}."
|
|
|
|
|
;;
|
|
|
|
|
*http* ) ## Quickmark
|
|
|
|
|
local_url=$(printf "%s" "${local_content}" | sed -e "s;.*\(http.*\);\1;")
|
|
|
|
|
debug_message "open_in_qutebrowser − \
|
|
|
|
|
Try to open quickbookmark URL ${RED}${local_url}${COLOR_DEBUG}."
|
|
|
|
|
;;
|
|
|
|
|
* )
|
|
|
|
|
debug_message "open_in_qutebrowser − \
|
|
|
|
|
Not yet managed."
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
~/src/qutebrowser-venv/bin/python3 -m qutebrowser "${local_url}"
|
|
|
|
|
|
2020-12-02 10:13:02 +01:00
|
|
|
|
}
|
|
|
|
|
# }}}
|
|
|
|
|
|
2020-12-02 10:19:27 +01:00
|
|
|
|
main() { # {{{
|
|
|
|
|
|
2020-12-02 15:48:38 +01:00
|
|
|
|
## Define all vars
|
2020-12-02 10:36:15 +01:00
|
|
|
|
define_vars
|
|
|
|
|
|
2020-12-02 15:48:38 +01:00
|
|
|
|
## Try to get a working qutebrowser binary
|
2020-12-02 15:58:35 +01:00
|
|
|
|
### Or exit with error message and code 1
|
2020-12-02 15:48:38 +01:00
|
|
|
|
get_qutebrowser_bin \
|
2020-12-02 15:58:35 +01:00
|
|
|
|
|| error_message "Can't find a working qutebrowser binary." "1"
|
2020-12-02 15:48:38 +01:00
|
|
|
|
|
2020-12-02 16:51:15 +01:00
|
|
|
|
## Start Qutebrowser if needed
|
|
|
|
|
### And exit
|
|
|
|
|
start_qutebrowser \
|
|
|
|
|
&& exit 0
|
2020-12-02 14:38:03 +01:00
|
|
|
|
|
|
|
|
|
## Manage existing instance
|
2020-12-02 16:35:28 +01:00
|
|
|
|
goto_existing_qutebrowser \
|
|
|
|
|
&& exit 0
|
2020-12-02 10:19:27 +01:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
# }}}
|
|
|
|
|
|
2020-12-02 10:13:02 +01:00
|
|
|
|
# Manage arguments # {{{
|
|
|
|
|
|
|
|
|
|
## If there is argument(s)
|
|
|
|
|
if [ ! "${NBARGS}" -eq "0" ]; then
|
|
|
|
|
|
|
|
|
|
manage_arg="0"
|
|
|
|
|
|
|
|
|
|
# Parse all options (start with a "-") one by one
|
|
|
|
|
while printf -- '%s' "${1}" | grep -q -E -- "^-+"; do
|
|
|
|
|
|
|
|
|
|
case "${1}" in
|
2020-12-02 15:48:38 +01:00
|
|
|
|
-d|--debug ) ## Enable debug mode
|
2020-12-02 10:13:02 +01:00
|
|
|
|
## Enable DEBUG
|
|
|
|
|
DEBUG="0"
|
|
|
|
|
;;
|
2020-12-02 15:48:38 +01:00
|
|
|
|
--git ) ## Use Git repo
|
|
|
|
|
QB_GIT_MODE="0"
|
|
|
|
|
;;
|
2020-12-02 10:13:02 +01:00
|
|
|
|
-h|--help ) ## help
|
|
|
|
|
usage
|
|
|
|
|
## Exit after help informations
|
|
|
|
|
exit 0
|
|
|
|
|
;;
|
2020-12-02 15:48:38 +01:00
|
|
|
|
--package ) ## Use package
|
|
|
|
|
QB_PACKAGE_MODE="0"
|
|
|
|
|
;;
|
|
|
|
|
--venv ) ## Use Python venv
|
|
|
|
|
QB_VENV_MODE="0"
|
|
|
|
|
;;
|
2020-12-02 10:13:02 +01:00
|
|
|
|
* ) ## unknow option
|
|
|
|
|
printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
|
|
|
|
|
printf '%b\n' "---"
|
|
|
|
|
usage
|
|
|
|
|
exit 1
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
debug_message "Arguments management − \
|
|
|
|
|
${RED}${1}${COLOR_DEBUG} option managed."
|
|
|
|
|
|
|
|
|
|
## Move to the next argument
|
|
|
|
|
shift
|
|
|
|
|
manage_arg=$((manage_arg+1))
|
|
|
|
|
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
debug_message "Arguments management − \
|
|
|
|
|
${RED}${manage_arg}${COLOR_DEBUG} argument(s) successfully managed."
|
|
|
|
|
else
|
|
|
|
|
debug_message "Arguments management − \
|
|
|
|
|
No arguments/options to manage."
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# }}}
|
|
|
|
|
|
|
|
|
|
main
|
|
|
|
|
|
|
|
|
|
exit 255
|