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

make it configurable #11

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ 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)
- [lazygit](https://github.com/jesseduffield/lazygit)
- [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`:

Expand Down
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/...
171 changes: 61 additions & 110 deletions helix-wezterm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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