Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completes 406: stores the content of keybindings variable in a file for reuse. #410

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

arkochan
Copy link

@arkochan arkochan commented Nov 5, 2024

This pull requests holds upgrade to hypr/scripts/keybindings.sh as i mentioned before on #406.

  1. Rather than generating the keybinds variable from the keybind .conf (eg. hypr/keybindings/default.conf) file each time hypr/scripts/keybindings.sh script is ran, it is be generated once after each modification to keybind .conf file selected by user.

  2. This variable is saved as [configfile.conf]_[unix_time_stamp].help file. the unix time stamp indicates the last time the config file is modified. if it dosent match the help file is regenerated according to the keybinds variable and older .help file is removed. The .help extension might not be the best choice. and i am a bit confused with the extension name. Also the help file is stored in the same directory as the conf direcotry. (eg .config/hypr/conf/keybindings by default)

This is my first PR in an open source project. Please do guide me if mistakes are made.

prevents regenerating the keybinds variable each time.
done through :
 - check if a generated help file already exists for the selected
keybind .conf file.
 - if not: generate a file from the keybinds variable.
 - else: use already generated help file
To ensure for each modification a single file is generated if can be
checked either using the last modification using stat -c '%Y'
$config_file or by generating a hash value for the file.
prevents regenerating the keybinds variable each time.
done through :
 - check if a generated help file already exists for the selected
keybind .conf file.
 - if not: generate a file from the keybinds variable.
 - else: use already generated help file
To ensure for each modification a single file is generated if can be
checked either using the last modification using stat -c '%Y'
$config_file or by generating a hash value for the file.
@arkochan arkochan changed the title fix 406: stores the content of keybindings variable in a file for reuse. Completes 406: stores the content of keybindings variable in a file for reuse. Nov 5, 2024
@mylinuxforwork mylinuxforwork added this to the v2.9.7 milestone Nov 10, 2024
@Stewart86
Copy link

@arkochan hope you don't mind that I tested out your script and added a few modification to my own, as I also felt that the rofi is taking too long to show. Your idea has significantly improved the performance.

As this is your PR, you decide if you want to include my suggestion in. Here are what I discovered.

  1. reading $mainMod from file instead of hardcoded "SUPER" key. Personally I use "ALT" key for my $mainMod so had to change the script a little. or we can simply use this line in your script.
mod_key=$(rg "^*mainMod =" <"$config_file" | awk '{print $3}')
  1. Your "help" file feels a lot like a "cache" file. I suggest that you store it in '$XDG_CACHE_HOME'.
  2. remove sleep from the script. There might be a reason why sleep is there, but from my test, it was okay without it, so I don't know about this.

In any case, I will just paste the modified script here for your to reference.

config_file=~/.config/hypr/conf/keybinding.conf
config_file=${config_file/source = ~/}
config_file=${config_file/source=~/}
cache_dir=${XDG_CACHE_HOME:-$HOME/.cache}
cache_file_prefix=.hypr_keymaps_

echo "Reading from: $config_file"
mod_key=$(rg "^*mainMod =" <"$config_file" | awk '{print $3}')
generated_filename="$cache_dir/$cache_file_prefix$(stat -c '%Y' "$config_file")"

keybinds=""

if [ -f "$generated_filename" ]; then
    echo "Help file for $config_file already exists in $generated_filename"
    keybinds=$(cat "$generated_filename")
else
    echo "Generated cache with similar modification time not found. Removing any other cache files"
    rm -f "$cache_dir"/"$cache_file_prefix"
    echo "Generating new cache for $config_file"
    while read -r line; do
        if [[ "$line" == "bind"* ]]; then

            line=${line/\$mainMod/$mod_key}
            line=${line/bind = /}
            line=${line/bindm = /}

            IFS='#'
            read -a strarr <<<"$line"
            kb_str=${strarr[0]}
            cm_str=${strarr[1]}

            IFS=','
            read -a kbarr <<<"$kb_str"

            item="${kbarr[0]}  + ${kbarr[1]}"$'\r'"${cm_str:1}"
            keybinds=$keybinds$item$'\n'
        fi
    done <"$config_file"
    echo "$keybinds" >"$generated_filename"
fi

rofi -dmenu -i -markup -eh 2 -replace -p "Keybinds" -config ~/.config/rofi/config-compact.rasi <<<"$keybinds"

@Stewart86
Copy link

Stewart86 commented Dec 5, 2024

hmm..... you know what? I just discovered that we don't need that help file to be fast. it is just the inefficient code that is making it slow.

instead of using echo pipe to sed to replace the string

        line="$(echo "$line" | sed 's/$mainMod/ALT/g')"
        line="$(echo "$line" | sed 's/bind = //g')"
        line="$(echo "$line" | sed 's/binde = //g')"
        line="$(echo "$line" | sed 's/bindm = //g')"

do this, and keep everything else as usual.

        line=${line/\$mainMod/ALT}
        line=${line/bind = /}
        line=${line/bindm = /}
        line=${line/binde = /}

🤯 🤯

@Stewart86
Copy link

I have definitely gone too far. 😶 😅 Rewrote the entire script with another >100% speed improvement.

Here is my entire script

#!/bin/bash

config_file=~/.config/hypr/conf/keybinding.conf
echo "Reading from: $config_file"

mod_key=$(rg "^*mainMod =" <"$config_file" | awk '{print $3}')
binds=$(rg "^bind* =" <"$config_file" |
    awk -F, '{print $1,"+", $2, "\r#",$3,$4}' | # added # after \r to easily capture unwanted strings
    sed 's/bind* = //g' | # remove "bind = "
    sed "s/\$mainMod/$mod_key/g" | # replace $mainMod
    sed 's/#.*#//g') # remove execution commands containing withing #<unwanted>#

rofi -dmenu -i -markup -eh 2 -replace -p "Keybinds" -config ~/.config/rofi/config-compact.rasi <<<"$binds"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants