Move Qutebrowser bin selection to dedicated script

This commit is contained in:
Jeremy Gardais 2020-12-04 11:35:41 +01:00
parent 116389d6a4
commit f73e5945e2
Signed by: jegardai
GPG Key ID: E759BAA22501AF32
2 changed files with 216 additions and 99 deletions

94
qb
View File

@ -15,39 +15,30 @@ readonly COLOR_DEBUG="${PURPLE}"
usage() { # {{{ usage() { # {{{
cat <<- EOF cat <<- EOF
usage: $PROGNAME [--debug,--help] [--git,--package,--venv] usage: $PROGNAME [--debug,--help]
Try to launch the most appropriate qutebrowser binary: Try to open Qutebrowser content (bookmarks, buffers,…).
1. From venv
2. From APT package
3. From Git repository
EXAMPLES: EXAMPLES:
- Launch qutebrowser - Display content if Qutebrowser is already opened
${PROGNAME} ${PROGNAME}
OPTIONS: OPTIONS:
-d,--debug -d,--debug
Enable debug messages. Enable debug messages.
--git -h,--help
Force to use QuteBrowser from Git repository.
--help
Print this help message. Print this help message.
--package
Force to use QuteBrowser from package.
--venv
Force to use QuteBrowser from Python Virtual ENVironment.
EOF EOF
} }
# }}} # }}}
define_vars() { # {{{ define_vars() { # {{{
## Test if BROWSER is already defined (by parent script,…)
[ -z "${BROWSER}" ] && BROWSER="qutebrowser"
## List of process pattern to monitor ## List of process pattern to monitor
qutebrowser_proc_pattern="(qutebrowser)" qutebrowser_proc_pattern="(qutebrowser)"
@ -101,59 +92,6 @@ error_message() { # {{{
exit "${local_error_code:=66}" exit "${local_error_code:=66}"
} }
# }}} # }}}
get_qutebrowser_bin() { # {{{
return_get_qutebrowser_bin="1"
## First try venv {{{
if [ "${QB_VENV_MODE}" -eq "0" ] && [ -f "${QB_VENV_PYTHON_PATH}" ]; then
debug_message "get_qutebrowser_bin \
Qutebrowser from ${RED}venv${COLOR_DEBUG} can be used."
QUTEBROWSER_BIN="${QB_VENV_PYTHON_PATH} -m qutebrowser"
return_get_qutebrowser_bin="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"
fi
## }}}
## 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)"
return_get_qutebrowser_bin="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"
fi
## }}}
## Finally, try git repository {{{
if [ "${QB_GIT_MODE}" -eq "0" ] && [ -f "${QB_GIT_REPOSITORY}/qutebrowser.py" ]; then
QUTEBROWSER_BIN="${QB_GIT_REPOSITORY}/qutebrowser.py --backend webengine"
debug_message "get_qutebrowser_bin \
Qutebrowser from ${RED}Git repository${COLOR_DEBUG} will finally be used."
return_get_qutebrowser_bin="0"
else
debug_message "get_qutebrowser_bin \
Qutebrowser from ${RED}git repository${COLOR_DEBUG} not selected or can't be used."
fi
## }}}
return "${return_get_qutebrowser_bin}"
}
# }}}
is_proc_running() { # {{{ is_proc_running() { # {{{
local_proc_pattern="${1}" local_proc_pattern="${1}"
@ -187,7 +125,7 @@ Qutebrowser is already started."
else else
debug_message "start_qutebrowser \ debug_message "start_qutebrowser \
No existing instance of Qutebrowser. Starting…" >> /tmp/qb.log No existing instance of Qutebrowser. Starting…" >> /tmp/qb.log
sh -c "${QUTEBROWSER_BIN}" sh -c "${BROWSER}"
return_start_qutebrowser="0" return_start_qutebrowser="0"
fi fi
@ -312,7 +250,7 @@ open_in_qutebrowser() { # {{{
debug_message "open_in_qutebrowser \ debug_message "open_in_qutebrowser \
Try to open ${RED}${url}${COLOR_DEBUG}" Try to open ${RED}${url}${COLOR_DEBUG}"
sh -c "${QUTEBROWSER_BIN} ${url}" sh -c "${BROWSER} ${url}"
} }
# }}} # }}}
@ -455,11 +393,6 @@ main() { # {{{
## Define all vars ## Define all vars
define_vars define_vars
## Try to get a working qutebrowser binary
### Or exit with error message and code 1
get_qutebrowser_bin \
|| error_message "Can't find a working qutebrowser binary." "1"
## Start Qutebrowser if needed ## Start Qutebrowser if needed
### And exit ### And exit
start_qutebrowser \ start_qutebrowser \
@ -487,20 +420,11 @@ if [ ! "${NBARGS}" -eq "0" ]; then
## Enable DEBUG ## Enable DEBUG
DEBUG="0" DEBUG="0"
;; ;;
--git ) ## Use Git repo
QB_GIT_MODE="0"
;;
-h|--help ) ## help -h|--help ) ## help
usage usage
## Exit after help informations ## Exit after help informations
exit 0 exit 0
;; ;;
--package ) ## Use package
QB_PACKAGE_MODE="0"
;;
--venv ) ## Use Python venv
QB_VENV_MODE="0"
;;
* ) ## unknow option * ) ## unknow option
printf '%b\n' "${RED}Invalid option: ${1}${RESET}" printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
printf '%b\n' "---" printf '%b\n' "---"

View File

@ -1,19 +1,212 @@
#!/bin/sh #!/bin/sh
# Version with user's venv # Vars {{{
if [ -f ~/src/qutebrowser-venv/bin/python3 ]; then readonly PROGNAME=$(basename "${0}")
~/src/qutebrowser-venv/bin/python3 -m qutebrowser readonly PROGDIR=$(readlink -m $(dirname "${0}"))
readonly ARGS="${*}"
readonly NBARGS="${#}"
[ -z "${DEBUG}" ] && DEBUG=0
## Export DEBUG for sub-script
export DEBUG
## Colors
readonly PURPLE='\033[1;35m'
readonly RED='\033[0;31m'
readonly RESET='\033[0m'
readonly COLOR_DEBUG="${PURPLE}"
# }}}
usage() { # {{{
cat <<- EOF
usage: $PROGNAME [--debug,--help] [--git,--package,--venv]
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.
--git
Force to use QuteBrowser from Git repository.
--help
Print this help message.
--package
Force to use QuteBrowser from package.
--venv
Force to use QuteBrowser from Python Virtual ENVironment.
EOF
}
# }}}
define_vars() { # {{{
## Use Qutebrowser from Python Virtual Environment by default
QB_VENV_MODE="0"
## Qutebrowser possibles paths
QB_VENV_PYTHON_PATH="${HOME}/src/qutebrowser-venv/bin/python3"
QB_GIT_REPOSITORY="${HOME}/repos/qutebrowser"
}
# }}}
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
}
# }}}
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}"
}
# }}}
get_qutebrowser_bin() { # {{{
return_get_qutebrowser_bin="1"
## First try venv {{{
if [ "${QB_VENV_MODE}" -eq "0" ] && [ -f "${QB_VENV_PYTHON_PATH}" ]; then
debug_message "get_qutebrowser_bin \
Qutebrowser from ${RED}venv${COLOR_DEBUG} can be used."
QUTEBROWSER_BIN="${QB_VENV_PYTHON_PATH} -m qutebrowser"
return_get_qutebrowser_bin="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"
fi
## }}}
## 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)"
return_get_qutebrowser_bin="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"
fi
## }}}
## Finally, try git repository {{{
if [ "${QB_GIT_MODE}" -eq "0" ] && [ -f "${QB_GIT_REPOSITORY}/qutebrowser.py" ]; then
QUTEBROWSER_BIN="${QB_GIT_REPOSITORY}/qutebrowser.py --backend webengine"
debug_message "get_qutebrowser_bin \
Qutebrowser from ${RED}Git repository${COLOR_DEBUG} will finally be used."
return_get_qutebrowser_bin="0"
else
debug_message "get_qutebrowser_bin \
Qutebrowser from ${RED}git repository${COLOR_DEBUG} not selected or can't be used."
fi
## }}}
return "${return_get_qutebrowser_bin}"
}
# }}}
main() { # {{{
## Define all vars
define_vars
## Try to get a working qutebrowser binary
### Or exit with error message and code 1
get_qutebrowser_bin \
|| error_message "Can't find a working qutebrowser binary." "1"
## Open Qutebrowser (with arguments)
### And exit
sh -c "${QUTEBROWSER_BIN} ${ARGS}" \
exit 0 exit 0
}
# }}}
# 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
-d|--debug ) ## Enable debug mode
## Enable DEBUG
DEBUG="0"
;;
--git ) ## Use Git repo
QB_GIT_MODE="0"
;;
-h|--help ) ## help
usage
## Exit after help informations
exit 0
;;
--package ) ## Use package
QB_PACKAGE_MODE="0"
;;
--venv ) ## Use Python venv
QB_VENV_MODE="0"
;;
* ) ## 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 fi
# Version from APT package # }}}
if dpkg -l | grep -q qutebrowser ; then
command qutebrowser
exit 0
fi
# Version from Git repository main
if [ -f ~/repos/qutebrowser/qutebrowser.py ]; then
~/repos/qutebrowser/qutebrowser.py --backend webengine "$@" exit 255
exit0
fi