Compare commits

...

5 Commits

5 changed files with 188 additions and 113 deletions

View File

@@ -1,13 +1,29 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Compare the current tmux version against a given version. # Compare the current tmux version against one or more version constraints.
# Exit: 0 if comparison is true, 1 if false #
# Each constraint pairs an operator and version in a single argument,
# e.g. ">= 3.2". Multiple constraints can be joined with the logical operators
# `and` / `or`, where `and` binds tighter than `or` (standard precedence). This
# allows a single invocation to express a range without spawning the script
# multiple times.
#
# Exit: 0 if the whole expression is true, 1 if false
#
# Examples:
# check-version.sh ">= 3.2"
# check-version.sh ">= 3.2" and "< 3.3"
# check-version.sh "< 2.9" or ">= 3.4"
set -euo pipefail set -euo pipefail
usage() { usage() {
echo "usage: $0 <operator> <version>" >&2 cat >&2 <<'EOF'
echo "operators: < <= == >= >" >&2 usage: check-version.sh <constraint> [and|or <constraint> ...]
constraint: <operator> <version>, e.g. ">= 3.2"
operators: < <= == >= >
logical: and or (and binds tighter than or)
EOF
} }
if [[ ${1:-} == "-h" || ${1:-} == "--help" ]]; then if [[ ${1:-} == "-h" || ${1:-} == "--help" ]]; then
@@ -15,14 +31,11 @@ if [[ ${1:-} == "-h" || ${1:-} == "--help" ]]; then
exit 0 exit 0
fi fi
if [[ $# -ne 2 ]]; then if [[ $# -eq 0 ]]; then
usage usage
exit 1 exit 1
fi fi
operator="$1"
target_version="$2"
# Extract tmux version (e.g., "tmux 3.3a" -> "3.3a") # Extract tmux version (e.g., "tmux 3.3a" -> "3.3a")
current_version=$(tmux -V | sed 's/^tmux //') current_version=$(tmux -V | sed 's/^tmux //')
@@ -75,22 +88,78 @@ compare_versions() {
echo 0 echo 0
} }
result=$(compare_versions "$current_version" "$target_version") # Evaluate a single constraint (e.g. ">= 3.2") against the current version.
# Returns 0 (true) or 1 (false); exits on a malformed constraint.
eval_constraint() {
local constraint="$1"
local operator version
read -r operator version <<< "$constraint"
case "$operator" in if [[ -z "$operator" || -z "$version" ]]; then
'<') echo "error: invalid constraint: '$constraint' (expected '<operator> <version>', e.g. '>= 3.2')" >&2
[[ "$result" -eq -1 ]] && exit 0 || exit 1 ;;
'<=')
[[ "$result" -le 0 ]] && exit 0 || exit 1 ;;
'==')
[[ "$result" -eq 0 ]] && exit 0 || exit 1 ;;
'>=')
[[ "$result" -ge 0 ]] && exit 0 || exit 1 ;;
'>')
[[ "$result" -eq 1 ]] && exit 0 || exit 1 ;;
*)
echo "error: invalid operator: $operator" >&2
usage usage
exit 1 exit 1
;; fi
local result
result=$(compare_versions "$current_version" "$version")
case "$operator" in
'<') [[ "$result" -eq -1 ]] ;;
'<=') [[ "$result" -le 0 ]] ;;
'==') [[ "$result" -eq 0 ]] ;;
'>=') [[ "$result" -ge 0 ]] ;;
'>') [[ "$result" -eq 1 ]] ;;
*)
echo "error: invalid operator: '$operator'" >&2
usage
exit 1 ;;
esac esac
}
# Walk the alternating constraint/logical sequence, accumulating each `and`
# group (1 = true) and folding completed groups into the `or` result. Truth
# values are tracked as ints and converted to an exit code at the end.
or_result=0
and_result=1
expect=constraint
for token in "$@"; do
if [[ "$expect" == "constraint" ]]; then
case "$token" in
and | or)
echo "error: expected a constraint, got logical operator: $token" >&2
usage
exit 1 ;;
esac
if eval_constraint "$token"; then
and_result=$(( and_result & 1 ))
else
and_result=$(( and_result & 0 ))
fi
expect=logical
else
case "$token" in
and)
;;
or)
or_result=$(( or_result | and_result ))
and_result=1 ;;
*)
echo "error: expected 'and' or 'or', got: $token" >&2
usage
exit 1 ;;
esac
expect=constraint
fi
done
if [[ "$expect" == "constraint" ]]; then
echo "error: expression ends with a logical operator" >&2
usage
exit 1
fi
or_result=$(( or_result | and_result ))
[[ "$or_result" -eq 1 ]] && exit 0 || exit 1

View File

@@ -1,27 +0,0 @@
#!/usr/bin/env bash
# Open neovim in the notes directory.
# Detaches the client if all panes in the window are dead afterwards.
notes_dir="$HOME/Documents/Notes"
nvim "$notes_dir" || true
# Brief delay to let tmux update pane status
sleep 0.1
window_id=$(tmux display-message -p '#{window_id}')
my_pane=$(tmux display-message -p '#{pane_id}')
# Count live sibling panes (not us, not dead)
other_live=$(tmux list-panes -t "$window_id" -F '#{pane_id} #{pane_dead}' \
| awk -v me="$my_pane" '$1 != me && $2 == "0"' | wc -l | tr -d ' ')
if [ "$other_live" -eq 0 ]; then
# No live siblings — close the popup and kill the window
# (also cleans up any dead siblings).
tmux detach-client
tmux kill-window -t "$window_id" 2>/dev/null || true
else
# User has split off other live panes — only kill ours, keep popup open.
tmux kill-pane 2>/dev/null || true
fi

View File

@@ -1,20 +0,0 @@
#!/usr/bin/env bash
set -e
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
notes_dir="$HOME/Documents/Notes"
# Create the notes session if it doesn't exist
if ! tmux has-session -t notes; then
tmux new-session -ds notes -c "$notes_dir" -n notes "$script_dir/notes-cmd.sh"
fi
# Apply session options on every invocation so updates propagate to live sessions
tmux set-option -t notes status off
tmux set-option -t notes remain-on-exit on
tmux set-option -t notes default-command "$script_dir/notes-cmd.sh"
# Attach to the session
tmux attach-session -t notes

View File

@@ -4,8 +4,11 @@ set -e
projects_dir=$HOME/Projects projects_dir=$HOME/Projects
# Print the sorted, de-duplicated list of selectable projects.
list_projects() {
local projects=() dir relative line
# All depth-2 directories without @ are included unconditionally # All depth-2 directories without @ are included unconditionally
projects=()
for dir in "$projects_dir"/*/*/; do for dir in "$projects_dir"/*/*/; do
[ -d "$dir" ] || continue [ -d "$dir" ] || continue
relative="${dir#$projects_dir/}" relative="${dir#$projects_dir/}"
@@ -29,10 +32,60 @@ else
sed -E -e "s|^$projects_dir/||" -e 's|/\.git/?$||') sed -E -e "s|^$projects_dir/||" -e 's|/\.git/?$||')
fi fi
printf '%s\n' "${projects[@]}" | sort -u
}
# Second pass: runs inside the popup. Read the list the first pass already
# built (so the directory scan only happens once) and open the selection.
if [[ "${1:-}" == --pick ]]; then
listfile=$2
trap 'rm -f "$listfile"' EXIT
# Cancelling fzf (Esc/^C) exits non-zero; treat it as "no selection" and
# leave quietly, rather than letting the status propagate out of the popup
# and print '...project.sh returned 130' in the parent pane.
project=$( project=$(
printf '%s\n' "${projects[@]}" | sort -u | fzf --layout=reverse --info=hidden --border=rounded --cycle < "$listfile"
fzf --layout=reverse --info=hidden --border=rounded --cycle ) || exit 0
) [ -n "$project" ] || exit 0
tmux new-window -n "$project" -c "$projects_dir/$project" tmux new-window -n "$project" -c "$projects_dir/$project"
~/.local/share/tmux/layouts/window-auto ~/.local/share/tmux/layouts/window-auto
exit
fi
# First pass: scan once, size a popup to fit the longest project name, then
# re-launch ourselves inside it. fzf has no width of its own — it fills the
# popup — so the popup width is what we scale.
listfile=$(mktemp)
list_projects > "$listfile"
longest=0
while IFS= read -r line; do
if (( ${#line} > longest )); then
longest=${#line}
fi
done < "$listfile"
# We're launched from run-shell, which owns no client, so resolve the client
# that triggered us. It both anchors the popup (-c, without which the popup
# gets no tty and fzf can't draw) and gives the terminal width for the cap.
client=$(tmux display -p '#{client_name}')
cols=$(tmux display -p -c "$client" '#{client_width}')
# Clamp between the original fixed width and 90% of the terminal. In between,
# pad for fzf's chrome: rounded border (2) + pointer gutter (2) + scrollbar (1)
# + a column of breathing room.
min=60
max=$(( cols * 90 / 100 ))
width=$(( longest + 6 ))
(( width < min )) && width=$min
(( width > max )) && width=$max
flags=(-c "$client" -w "$width" -h 10)
# Match the borderless popup the keybinding used on tmux >= 3.3.
if ~/.config/tmux/check-version.sh ">= 3.3"; then
flags+=(-B)
fi
tmux display-popup "${flags[@]}" -E "'$0' --pick '$listfile'"

View File

@@ -28,7 +28,7 @@ set -g status-interval 2
set -g default-terminal "tmux-256color" set -g default-terminal "tmux-256color"
# Enable true-color # Enable true-color
if '~/.config/tmux/check-version.sh "<" 3.2' \ if '~/.config/tmux/check-version.sh "< 3.2"' \
'set-option -as terminal-features ",xterm*:Tc"' \ 'set-option -as terminal-features ",xterm*:Tc"' \
'set-option -as terminal-features ",xterm*:RGB"' 'set-option -as terminal-features ",xterm*:RGB"'
@@ -52,7 +52,7 @@ unbind -T copy-mode-vi MouseDragEnd1Pane
# Enable changing cursor shape per pane, vim or zsh should emit escape # Enable changing cursor shape per pane, vim or zsh should emit escape
# sequences to change cursor shape. # sequences to change cursor shape.
# iTerm2 only requires this before tmux 2.9 # iTerm2 only requires this before tmux 2.9
if '[ -n $ITERM_PROFILE ] && ~/.config/tmux/check-version.sh "<" 2.9' \ if '[ -n $ITERM_PROFILE ] && ~/.config/tmux/check-version.sh "< 2.9"' \
'set -ga terminal-overrides "*:Ss=\E]1337;CursorShape=%p1%d\7"' 'set -ga terminal-overrides "*:Ss=\E]1337;CursorShape=%p1%d\7"'
# VTE compatible terminals. # VTE compatible terminals.
if '[ ! -n $ITERM_PROFILE ]' \ if '[ ! -n $ITERM_PROFILE ]' \
@@ -61,29 +61,29 @@ if '[ ! -n $ITERM_PROFILE ]' \
# Enable strikethrough on VTE compatible terminals. # Enable strikethrough on VTE compatible terminals.
set -ga terminal-overrides 'xterm*:smxx=\E[9m' set -ga terminal-overrides 'xterm*:smxx=\E[9m'
# Binding to create window-auto layout if -b '~/.config/tmux/check-version.sh ">= 3.2" and "< 3.3"' {
bind a run-shell ~/.local/share/tmux/layouts/window-auto bind A if -F '#{==:#{session_name},agent}' { detach-client } {
# TODO: bind A run-shell ~/.local/share/tmux/layouts/window-auto --refresh display-popup -d '#{pane_current_path}' -w 50% -h 90% -E ~/.config/tmux/agent.sh
}
if -b '~/.config/tmux/check-version.sh ">=" 3.2 && ~/.config/tmux/check-version.sh "<" 3.3' {
bind A display-popup -d '#{pane_current_path}' -w 50% -h 90% -E ~/.config/tmux/agent.sh
bind H display-popup -w 75% -h 75% -E htop bind H display-popup -w 75% -h 75% -E htop
bind I display-popup -d '#{pane_current_path}' -E ~/.config/tmux/interpreter.sh bind I display-popup -d '#{pane_current_path}' -E ~/.config/tmux/interpreter.sh
bind N display-popup -w 75% -h 90% -E ~/.config/tmux/notes.sh bind N display-popup -d '#{pane_current_path}' -w 60% -h 75% -E nvim
bind P display-popup -w 60 -h 10 -E ~/.config/tmux/project.sh
bind S display-popup -w 60 -h 10 -E ~/.config/tmux/session.sh bind S display-popup -w 60 -h 10 -E ~/.config/tmux/session.sh
bind T display-popup -d '#{pane_current_path}' -E $SHELL bind T display-popup -d '#{pane_current_path}' -E $SHELL
} }
if -b '~/.config/tmux/check-version.sh ">=" 3.3' { if -b '~/.config/tmux/check-version.sh ">= 3.3"' {
bind A display-popup -S fg=#54546D -b rounded -d '#{pane_current_path}' -w 75% -h 90% -E ~/.config/tmux/agent.sh bind A if -F '#{==:#{session_name},agent}' { detach-client } {
display-popup -S fg=#54546D -b rounded -d '#{pane_current_path}' -w 75% -h 90% -E ~/.config/tmux/agent.sh
}
bind H display-popup -S fg=#54546D -b rounded -w 50% -h 75% -E htop bind H display-popup -S fg=#54546D -b rounded -w 50% -h 75% -E htop
bind I display-popup -S fg=#54546D -b rounded -d '#{pane_current_path}' -E ~/.config/tmux/interpreter.sh bind I display-popup -S fg=#54546D -b rounded -d '#{pane_current_path}' -E ~/.config/tmux/interpreter.sh
bind N display-popup -S fg=#54546D -b rounded -w 75% -h 90% -E ~/.config/tmux/notes.sh bind N display-popup -S fg=#54546D -b rounded -d '#{pane_current_path}' -w 60% -h 75% -E nvim
bind P display-popup -B -w 60 -h 10 -E ~/.config/tmux/project.sh
bind S display-popup -B -w 60 -h 10 -E ~/.config/tmux/session.sh bind S display-popup -B -w 60 -h 10 -E ~/.config/tmux/session.sh
bind T display-popup -S fg=#54546D -d '#{pane_current_path}' -E $SHELL bind T display-popup -S fg=#54546D -d '#{pane_current_path}' -E $SHELL
} }
bind P run-shell ~/.config/tmux/project.sh
# Restore old next/previous window bindings # Restore old next/previous window bindings
bind C-n next-window bind C-n next-window
bind C-p previous-window bind C-p previous-window