Compare commits

..

7 Commits

4 changed files with 192 additions and 64 deletions

View File

@@ -30,6 +30,10 @@ agent=$(printf '%s\n' "${agents[@]}" | fzf \
--margin="0,$hmargin" \
--padding=1)
# Reset cursor so the agent doesn't render starting where fzf left it.
tput cup 0 0
tput ed
if [ -n "$agent" ]; then
"$agent" || true
fi

View File

@@ -1,13 +1,29 @@
#!/usr/bin/env bash
# Compare the current tmux version against a given version.
# Exit: 0 if comparison is true, 1 if false
# Compare the current tmux version against one or more version constraints.
#
# 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
usage() {
echo "usage: $0 <operator> <version>" >&2
echo "operators: < <= == >= >" >&2
cat >&2 <<'EOF'
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
@@ -15,14 +31,11 @@ if [[ ${1:-} == "-h" || ${1:-} == "--help" ]]; then
exit 0
fi
if [[ $# -ne 2 ]]; then
if [[ $# -eq 0 ]]; then
usage
exit 1
fi
operator="$1"
target_version="$2"
# Extract tmux version (e.g., "tmux 3.3a" -> "3.3a")
current_version=$(tmux -V | sed 's/^tmux //')
@@ -75,22 +88,78 @@ compare_versions() {
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
'<')
[[ "$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
if [[ -z "$operator" || -z "$version" ]]; then
echo "error: invalid constraint: '$constraint' (expected '<operator> <version>', e.g. '>= 3.2')" >&2
usage
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
}
# 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

@@ -4,8 +4,11 @@ set -e
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
projects=()
for dir in "$projects_dir"/*/*/; do
[ -d "$dir" ] || continue
relative="${dir#$projects_dir/}"
@@ -29,10 +32,60 @@ else
sed -E -e "s|^$projects_dir/||" -e 's|/\.git/?$||')
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=$(
printf '%s\n' "${projects[@]}" | sort -u |
fzf --layout=reverse --info=hidden --border=rounded --cycle
)
fzf --layout=reverse --info=hidden --border=rounded --cycle < "$listfile"
) || exit 0
[ -n "$project" ] || exit 0
tmux new-window -n "$project" -c "$projects_dir/$project"
~/.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"
# 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*:RGB"'
@@ -52,7 +52,7 @@ unbind -T copy-mode-vi MouseDragEnd1Pane
# Enable changing cursor shape per pane, vim or zsh should emit escape
# sequences to change cursor shape.
# 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"'
# VTE compatible terminals.
if '[ ! -n $ITERM_PROFILE ]' \
@@ -61,27 +61,29 @@ if '[ ! -n $ITERM_PROFILE ]' \
# Enable strikethrough on VTE compatible terminals.
set -ga terminal-overrides 'xterm*:smxx=\E[9m'
# Binding to create window-auto layout
bind a run-shell ~/.local/share/tmux/layouts/window-auto
# TODO: bind A run-shell ~/.local/share/tmux/layouts/window-auto --refresh
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
if -b '~/.config/tmux/check-version.sh ">= 3.2" and "< 3.3"' {
bind A if -F '#{==:#{session_name},agent}' { detach-client } {
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 I display-popup -d '#{pane_current_path}' -E ~/.config/tmux/interpreter.sh
bind P display-popup -w 60 -h 10 -E ~/.config/tmux/project.sh
bind N display-popup -d '#{pane_current_path}' -w 60% -h 75% -E nvim
bind S display-popup -w 60 -h 10 -E ~/.config/tmux/session.sh
bind T display-popup -d '#{pane_current_path}' -E $SHELL
}
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
if -b '~/.config/tmux/check-version.sh ">= 3.3"' {
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 I display-popup -S fg=#54546D -b rounded -d '#{pane_current_path}' -E ~/.config/tmux/interpreter.sh
bind P display-popup -B -w 60 -h 10 -E ~/.config/tmux/project.sh
bind N display-popup -S fg=#54546D -b rounded -d '#{pane_current_path}' -w 60% -h 75% -E nvim
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 P run-shell ~/.config/tmux/project.sh
# Restore old next/previous window bindings
bind C-n next-window
bind C-p previous-window