Skip to content

Commit

Permalink
make it configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
quantonganh committed Jul 12, 2024
1 parent 34a1f02 commit 432b277
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 111 deletions.
20 changes: 20 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -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/...
158 changes: 47 additions & 111 deletions helix-wezterm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,130 +2,66 @@

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

# Split pane in direction
split_pane() {
pane_id=$(wezterm cli get-pane-direction $direction)
if [ -z "$pane_id" ]; then
pane_id=$(wezterm cli split-pane --$direction --percent $percent)
fi

wezterm cli activate-pane-direction $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

0 comments on commit 432b277

Please sign in to comment.