diff --git a/README.md b/README.md index c2a1315..bb3021d 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ Install the requirements: - [bat](https://github.com/sharkdp/bat) for file previews - [broot](https://github.com/Canop/broot) +- [envsubst](https://www.gnu.org/software/gettext/manual/html_node/envsubst-Invocation.html) - [fish shell](https://fishshell.com/) - [gh](https://cli.github.com/) - [howdoi](https://github.com/gleitz/howdoi) @@ -66,6 +67,7 @@ Install the requirements: - [ripgrep](https://github.com/BurntSushi/ripgrep) for grep-like searching - [tig](https://jonas.github.io/tig/) - [tgpt](https://github.com/aandrew-me/tgpt) +- [yq](https://github.com/mikefarah/yq) Add the following into `~/.config/helix/config.toml`: diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..691484b --- /dev/null +++ b/config.yaml @@ -0,0 +1,20 @@ +actions: + blame: + command: tig blame $filename +$line_number + explorer: + direction: left + percent: 20 + command: br + fzf: + command: | + helix-fzf.sh \$(rg --line-number --column --no-heading --smart-case . | fzf --delimiter : --preview 'bat --style=full --color=always --highlight-line {2} {1}' --preview-window '~3,+{2}+3/2' | awk '{ print \$1 }' | cut -d: -f1,2,3) + lazygit: + command: lazygit + open: + command: gh browse $filename:$line_number + run: + extensions: + go: go run $basedir/*.go + test: + extensions: + go: go test -run=$test_name -v ./$basedir/... diff --git a/helix-wezterm.sh b/helix-wezterm.sh index 725be8b..7f89dbd 100755 --- a/helix-wezterm.sh +++ b/helix-wezterm.sh @@ -2,130 +2,81 @@ set -x -status_line=$(wezterm cli get-text | rg -e "(?:NOR\s+|NORMAL|INS\s+|INSERT|SEL\s+|SELECT)\s+[\x{2800}-\x{28FF}]*\s+(\S*)\s[^│]* (\d+):*.*" -o --replace '$1 $2') -filename=$(echo $status_line | awk '{ print $1}') -line_number=$(echo $status_line | awk '{ print $2}') +# Get the current filename and the line number from the status line +status_line=$(wezterm cli get-text | rg -e "(?:NORMAL|INSERT|SELECT)\s+[\x{2800}-\x{28FF}]*\s+(\S*)\s[^│]* (\d+):*.*" -o --replace '$1 $2') +export filename=$(echo $status_line | awk '{ print $1}') +export line_number=$(echo $status_line | awk '{ print $2}') -split_pane_down() { - bottom_pane_id=$(wezterm cli get-pane-direction down) - if [ -z "${bottom_pane_id}" ]; then - bottom_pane_id=$(wezterm cli split-pane) - fi - - wezterm cli activate-pane-direction --pane-id $bottom_pane_id down - - send_to_bottom_pane="wezterm cli send-text --pane-id $bottom_pane_id --no-paste" - program=$(wezterm cli list | awk -v pane_id="$bottom_pane_id" '$3==pane_id { print $6 }') - if [ "$program" = "lazygit" ]; then - echo "q" | $send_to_bottom_pane - fi -} - -pwd=$PWD -basedir=$(dirname "$filename") +pwd=$(PWD) +export basedir=$(dirname "$filename") basename=$(basename "$filename") basename_without_extension="${basename%.*}" extension="${filename##*.}" -case "$1" in - "blame") - split_pane_down - echo "cd $pwd; tig blame $filename +$line_number" | $send_to_bottom_pane - ;; - "check") - split_pane_down - case "$extension" in - "rs") - run_command="cd $pwd/$(echo $filename | sed 's|src/.*$||'); cargo check; if [ \$status = 0 ]; wezterm cli activate-pane-direction up; end;" - ;; - esac - echo "$run_command" | $send_to_bottom_pane - ;; - "explorer") - left_pane_id=$(wezterm cli get-pane-direction left) - if [ -z "${left_pane_id}" ]; then - left_pane_id=$(wezterm cli split-pane --left --percent 20) - fi +# Load the configuration file +config_file="${HOME}/.config/helix-wezterm/config.yaml" - left_program=$(wezterm cli list | awk -v pane_id="$left_pane_id" '$3==pane_id { print $6 }') - if [ "$left_program" != "br" ]; then - echo "br" | wezterm cli send-text --pane-id $left_pane_id --no-paste - fi +# Get the action from the first argument +action=$1 - wezterm cli activate-pane-direction left - ;; - "fzf") - split_pane_down - echo "cd $pwd; helix-fzf.sh \$(rg --line-number --column --no-heading --smart-case . | fzf --delimiter : --preview 'bat --style=full --color=always --highlight-line {2} {1}' --preview-window '~3,+{2}+3/2' | awk '{ print \$1 }' | cut -d: -f1,2,3)" | $send_to_bottom_pane - ;; - "howdoi") - split_pane_down - echo "howdoi -c `pbpaste`" | $send_to_bottom_pane - ;; - "lazygit") - split_pane_down - program=$(wezterm cli list | awk -v pane_id="$pane_id" '$3==pane_id { print $6 }') - if [ "$program" = "lazygit" ]; then - wezterm cli activate-pane-direction down - else - echo "cd $pwd; lazygit" | $send_to_bottom_pane - fi - ;; - "open") - gh browse $filename:$line_number - ;; - "run") - split_pane_down - case "$extension" in - "c") - run_command="clang -lcmocka -lmpfr -Wall -g -O1 $filename -o $basedir/$basename_without_extension && $basedir/$basename_without_extension" - ;; - "go") - run_command="go run $basedir/*.go" - ;; - "md") - run_command="mdcat -p $filename" - ;; - "rkt"|"scm") - run_command="racket $filename" - ;; - "rs") - run_command="cd $pwd/$(echo $filename | sed 's|src/.*$||'); cargo run; if [ \$status = 0 ]; wezterm cli activate-pane-direction up; end" - ;; - "sh") - run_command="sh $filename" - ;; - esac - echo "$run_command" | $send_to_bottom_pane - ;; +# Extract the direction, percent and command from the YAML configuration +direction=$(yq e ".actions.$action.direction" "$config_file") +if [ "$direction" == "null" ]; then + direction=bottom +fi + +percent=$(yq e ".actions.$action.percent" "$config_file") +if [ "$percent" == "null" ]; then + percent=50 +fi +command=$(yq e ".actions.$action.command" "$config_file") + +case "$action" in "test") case "$extension" in "go") - test_name=$(head -$line_number $filename | tail -1 | sed -n 's/func \([^(]*\).*/\1/p') - if [ -n "$test_name" ]; then - run_command="go test -run=$test_name -v ./$basedir/...; if [ \$status = 0 ]; wezterm cli activate-pane-direction up; end;" - else - run_command="go test -v ./$basedir/...; if [ \$status = 0 ]; wezterm cli activate-pane-direction up; end;" - fi - ;; - "hurl") - run_command="hurl --test --very-verbose --pretty-print --color --variable epoch=$(date +%s) $filename" + export test_name=$(head -$line_number $filename | tail -1 | sed -n 's/func \([^(]*\).*/\1/p') ;; "rs") - test_name=$(head -$line_number $filename | tail -1 | sed -n 's/^.*fn \([^ ]*\)().*$/\1/p') - if [ -n $test_name ]; then - run_command="cd $pwd/$(echo $filename | sed 's|src/.*$||'); cargo test $test_name; if [ \$status = 0 ]; wezterm cli activate-pane-direction up; end;" - else - run_command="cd $pwd/$(echo $filename | sed 's|src/.*$||'); cargo test; if [ \$status = 0 ]; wezterm cli activate-pane-direction up; end;" - fi + export test_name=$(head -$line_number $filename | tail -1 | sed -n 's/^.*fn \([^ ]*\)().*$/\1/p') ;; esac - - split_pane_down - echo "$run_command" | $send_to_bottom_pane ;; - "tgpt") - split_pane_down - echo "tgpt '`pbpaste`'" | $send_to_bottom_pane +esac + +case "$direction" in + "left") + get_direction="left" + ;; + "right") + get_direction="right" + ;; + "top") + get_direction="up" + ;; + "bottom") + get_direction="down" ;; esac + +# Split pane in direction +split_pane() { + pane_id=$(wezterm cli get-pane-direction $get_direction) + if [ -z "$pane_id" ]; then + pane_id=$(wezterm cli split-pane --$direction --percent $percent) + fi + + wezterm cli activate-pane-direction $get_direction + send_to_pane="wezterm cli send-text --pane-id $pane_id --no-paste" +} + +split_pane $direction + +# Send command to the target pane +ext=$(yq e ".actions.$action.extensions" "$config_file") +if [ "$ext" != "null" ]; then + extension="${filename##*.}" + command=$(yq e ".actions.$action.extensions.$extension" "$config_file") +fi + +echo $(echo $command | envsubst) | $send_to_pane