From 772774517d18ce238a2d5c893117e8726cfb5511 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20La=C3=ADn?= Date: Sat, 24 Aug 2024 01:46:58 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(bin):=20dev=20script=20to=20cr?= =?UTF-8?q?eate=20new=20sessions=20based=20on=20projects=20and=20zoxide=20?= =?UTF-8?q?entries?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit improved repos script --- .config/tmux/conf/binds.conf | 3 ++ .local/bin/dev | 80 ++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100755 .local/bin/dev diff --git a/.config/tmux/conf/binds.conf b/.config/tmux/conf/binds.conf index c9be1f7c..ac521cd8 100644 --- a/.config/tmux/conf/binds.conf +++ b/.config/tmux/conf/binds.conf @@ -70,3 +70,6 @@ bind-key -T copy-mode-vi 'C-j' select-pane -D bind-key -T copy-mode-vi 'C-k' select-pane -U bind-key -T copy-mode-vi 'C-l' select-pane -R bind-key -T copy-mode-vi 'C-\' select-pane -l + +# Projects +bind-key Enter popup -E -h 80% -w 80% "dev" diff --git a/.local/bin/dev b/.local/bin/dev new file mode 100755 index 00000000..8b9357bb --- /dev/null +++ b/.local/bin/dev @@ -0,0 +1,80 @@ +#!/bin/bash + +# Constants +gfetch="gfetch -d churn contributors --no-color-palette --no-title" +eza="eza --header --icons --all --color=always --group-directories-first --hyperlink" +projects_dir="$HOME/dev" +projects_command="find $projects_dir -mindepth 2 -maxdepth 2 -type d" +fzf_command="fzf --height=100%" + +yaml_config() { + local yaml_file="$1" + local dest_dir="$2" + local dest_yaml_file="$dest_dir/.tmuxp.yaml" + + cp "$yaml_file" "$dest_yaml_file" + sed -i "1i start_directory: $dest_dir\nsession_name: $tab_title" "$dest_yaml_file" +} + +tmuxp_load() { + if [[ -n "$TMUX" ]]; then + tmuxp load -d "$selected_dir" + else + tmuxp load "$selected_dir" + + fi + tmux switch-client -t "$tab_title" +} + +handle_tmuxp() { + if [[ -f "$selected_dir/.tmuxp.yaml" ]]; then + tmuxp_load + else + if [[ -d "$XDG_CONFIG_HOME/tmuxp" ]]; then + local yaml_file=$(find "$XDG_CONFIG_HOME/tmuxp" -name "*.yaml" -print0 | + xargs -0 -n 1 basename | sed 's/\.yaml$//' | + fzf --preview "bat --style=numbers --color=always $XDG_CONFIG_HOME/tmuxp/{}.yaml" \ + --prompt="Select a session file: ") + if [[ -n "$yaml_file" ]]; then + yaml_config "$XDG_CONFIG_HOME/tmuxp/$yaml_file.yaml" "$selected_dir" + tmuxp_load + fi + fi + fi +} + +handle_tmux() { + if [[ -n "$TMUX" ]]; then + tmux new-session -ds "$tab_title" -c "$selected_dir" + tmux switch-client -t "$tab_title" + else + tmux new-session -s "$tab_title" -c "$selected_dir" + fi +} + +handle_kitty() { + kitty @ set-tab-title "$tab_title" + cd "$selected_dir" + $gfetch + $eza +} + +selected_dir=$($projects_command | $fzf_command --prompt='Select a directory: ' \ + --header='CTRL-P: Projects / CTRL-Z: Zoxide' \ + --bind "ctrl-p:reload($projects_command)" \ + --bind "ctrl-z:reload(zoxide query -l)" \ + --preview "$gfetch {} 2>&1 | grep -v 'Error: Could not find a git repository in' || true && $eza {} || $eza {}") + +tab_title=$(basename "$selected_dir") + +if [[ -n "$selected_dir" ]]; then + if command -v tmux &>/dev/null; then + if command -v tmuxp &>/dev/null; then + handle_tmuxp + else + handle_tmux + fi + elif [[ "$TERM" == "xterm-kitty" ]]; then + handle_kitty + fi +fi