summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAyush Agarwal <ayush@fastmail.in>2022-02-16 22:31:23 +0530
committerAyush Agarwal <ayush@fastmail.in>2022-02-16 22:31:23 +0530
commitd4315c63b4defca0bbcee0668262cb596256badd (patch)
tree5586343c6ad265c6d8cf999bffa637a56b7c3a5b
parentcd4608e31b3f4f42891c69e64c7b5f78ca507ff3 (diff)
feat: parse the config file for tessen
the config file also supports pointing rofi and wofi to their config files which allows for context dependent usage which wasn't possible earlier (without siginficant annoyance and having to accept arbitrary input) when environment variables were supported
-rwxr-xr-xtessen78
1 files changed, 78 insertions, 0 deletions
diff --git a/tessen b/tessen
index 46bb24d..c8f6799 100755
--- a/tessen
+++ b/tessen
@@ -644,6 +644,84 @@ _die() {
exit 1
}
+# this block of code is needed because we can't source the file and execute
+# arbitrary input
+parse_config() {
+ local _opt
+ while [[ "$#" -gt 0 ]]; do
+ _opt="$1"
+ case "$_opt" in
+ -c | --config)
+ if [[ "$#" -lt 2 ]] || ! [[ -s "$2" ]]; then
+ _die "please specify a valid path for the configuration file of tessen"
+ fi
+ tsn_config="$2"
+ shift
+ ;;
+ --config=*)
+ if ! [[ -s "${_opt##--config=}" ]]; then
+ _die "please specify a valid path for the configuration file of tessen"
+ fi
+ tsn_config="${_opt##--config=}"
+ ;;
+ esac
+ shift
+ done
+ unset -v _opt
+
+ local line idx key val
+ local -a config_arr
+ local config_regex='^[[:alpha:]_]+="[[:alnum:]~_.\/^$|()*?-]+"$'
+ # in case the user hasn't provided an explicit location, we'll have to check
+ # if the default file exists before we parse it
+ if [[ -s "$tsn_config" ]]; then
+ while read -r line || [[ -n "$line" ]]; do
+ if [[ "$line" == \#* ]]; then
+ continue
+ elif [[ "$line" =~ $config_regex ]]; then
+ config_arr+=("$line")
+ fi
+ done < "$tsn_config"
+ for idx in "${config_arr[@]}"; do
+ key="${idx%=*}"
+ val="${idx#*\"}"
+ val="${val%*\"}"
+ # here comes the ladder
+ if [[ "$key" == "pass_backend" ]]; then
+ validate_pass_backend "$val"
+ elif [[ "$key" == "dmenu_backend" ]]; then
+ validate_dmenu_backend "$val"
+ elif [[ "$key" == "action" ]]; then
+ validate_action "$val"
+ elif [[ "$key" == "rofi_config_file" ]] && [[ "$dmenu_backend" == "rofi" ]]; then
+ validate_dmenu_backend rofi
+ dmenu_backend_opts+=("-config $val")
+ tmp_opts+=("-config $val")
+ elif [[ "$key" == "wofi_config_file" ]] && [[ "$dmenu_backend" == "wofi" ]]; then
+ dmenu_backend_opts+=("-c $val")
+ tmp_opts+=("-c $val")
+ elif [[ "$key" == "wofi_style_file" ]] && [[ "$dmenu_backend" == "wofi" ]]; then
+ dmenu_backend_opts+=("-s $val")
+ tmp_opts+=("-s $val")
+ elif [[ "$key" == "wofi_color_file" ]] && [[ "$dmenu_backend" == "wofi" ]]; then
+ dmenu_backend_opts+=("-C $val")
+ tmp_opts+=("-C $val")
+ elif [[ "$key" == "userkey" ]]; then
+ tsn_userkey="$val"
+ elif [[ "$key" == "urlkey" ]]; then
+ tsn_urlkey="$val"
+ elif [[ "$key" == "autotype_key" ]]; then
+ tsn_autokey="$val"
+ elif [[ "$key" == "delay" ]]; then
+ tsn_delay="$val"
+ elif [[ "$key" == "web_browser" ]] && is_installed "$val"; then
+ tsn_web_browser="$val"
+ fi
+ done
+ fi
+ unset -v line key val idx config_arr config_regex
+}
+
main() {
local _opt