Files
config/bash/bashrc-osx
T
bchanot 5b135a9fc0 feat(shell): cc resumes last project conversation via claude --continue
Falls back to a fresh claude session when --continue fails.
2026-10-29 12:40:51 +01:00

245 lines
8.5 KiB
Plaintext

# Homebrew on PATH (Apple Silicon: /opt/homebrew, Intel: /usr/local).
# Must come first: brew's bash, vim, git, fzf and dtach shadow the system ones.
for brew_bin in /opt/homebrew/bin/brew /usr/local/bin/brew; do
if [ -x "$brew_bin" ]; then
eval "$("$brew_bin" shellenv)"
break
fi
done
unset brew_bin
## Enable color support of ls and also add handy aliases
# Some colors (BSD ls: -G instead of GNU --color)
alias ls='ls -G'
alias grep='grep --color=auto'
## Some export
# colored GCC warnings and errors
export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
# XDG cache dir: tmux.conf saves resurrect/continuum sessions under it
export XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}"
# Ensure ~/.local/bin is on PATH (pipx CLIs + personal scripts from bin/)
case ":$PATH:" in
*":$HOME/.local/bin:"*) ;;
*) export PATH="$HOME/.local/bin:$PATH" ;;
esac
# Rust toolchain (install.sh, rustup): puts ~/.cargo/bin on PATH, dedup done by the env file.
if [ -f "$HOME/.cargo/env" ]; then
# shellcheck source=/dev/null
. "$HOME/.cargo/env"
fi
# Set history size to unlimited
if [[ $EUID == 0 ]] ; then
export HISTSIZE=0
export HISTFILESIZE=0
else
export HISTSIZE=-1
export HISTFILESIZE=-1
fi
#export LANG=en_US.UTF-8
# Identity for git commits and vim headers. Filled by install.sh (asked once,
# reused from an existing ~/.bashrc / ~/.zshrc on re-runs), never stored here.
export USER="@USER@"
export EMAIL="@EMAIL@"
## Activate and custom bash completion
#bind 'TAB:menu-complete'
#bind 'set show-all-if-ambiguous on'
## Setting prompt style
# Get current branch in git repo
function parse_git_branch() {
BRANCH=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
if [ ! "${BRANCH}" == "" ]
then
STAT=`parse_git_dirty`
echo " [${BRANCH}${STAT}]"
else
echo ""
fi
}
# BSD date has no %N: use bash 5's microsecond clock, scaled to nanoseconds.
# Falls back to whole seconds under the system bash 3.2.
function timer_now {
if [ -n "${EPOCHREALTIME:-}" ]; then
echo "${EPOCHREALTIME/[.,]/}000"
else
echo "$(date +%s)000000000"
fi
}
function timer_start {
timer_start=${timer_start:-$(timer_now)}
}
function timer_stop {
local delta_us=$((($(timer_now) - $timer_start) / 1000))
local us=$((delta_us % 1000))
local ms=$(((delta_us / 1000) % 1000))
local s=$(((delta_us / 1000000) % 60))
local m=$(((delta_us / 60000000) % 60))
local h=$((delta_us / 3600000000))
# Goal: always show around 3 digits of accuracy
if ((h > 0)); then timer_show=${h}h${m}m
elif ((m > 0)); then timer_show=${m}m${s}s
elif ((s >= 10)); then timer_show=${s}.$((ms / 100))s
elif ((s > 0)); then timer_show=${s}.$(printf %03d $ms)s
elif ((ms >= 100)); then timer_show=${ms}ms
elif ((ms > 0)); then timer_show=${ms}.$((us / 100))ms
else timer_show=${us}us
fi
unset timer_start
}
# get current status of git repo
function parse_git_dirty {
status=`git status 2>&1 | tee`
dirty=`echo -n "${status}" 2> /dev/null | grep "modified:" &> /dev/null; echo "$?"`
untracked=`echo -n "${status}" 2> /dev/null | grep "Untracked files" &> /dev/null; echo "$?"`
ahead=`echo -n "${status}" 2> /dev/null | grep "Your branch is ahead of" &> /dev/null; echo "$?"`
newfile=`echo -n "${status}" 2> /dev/null | grep "new file:" &> /dev/null; echo "$?"`
renamed=`echo -n "${status}" 2> /dev/null | grep "renamed:" &> /dev/null; echo "$?"`
deleted=`echo -n "${status}" 2> /dev/null | grep "deleted:" &> /dev/null; echo "$?"`
bits=''
if [ "${renamed}" == "0" ] || [ "${newfile}" == "0" ] || [ "${untracked}" == "0" ] || [ "${dirty}" == "0" ]; then
bits="+${bits}"
fi
if [ "${ahead}" == "0" ]; then
bits="*${bits}"
fi
if [ "${deleted}" == "0" ]; then
bits="-${bits}"
fi
if [ ! "${bits}" == "" ]
then
echo " ${bits}"
else
echo ""
fi
}
function set_prompt {
timer_stop
if [[ $EUID == 0 ]] ; then
export PS1='`if [ $? = 0 ]; then echo "\[\033[01;36m\]✔"; else echo "\[\033[01;31m\]✘"; fi` ($timer_show) \[\033[01;31m\]\u [\[\033[00;0m\] \w \[\033[01;31m\]]\[\033[01;34m\]$(parse_git_branch " %s") \[\033[00;00m\]> '
else
export PS1='`if [ $? = 0 ]; then echo "\[\033[01;36m\]✔"; else echo "\[\033[01;31m\]✘"; fi` ($timer_show) \[\033[01;32m\]\u [\[\033[00;0m\] \w \[\033[01;32m\]]\[\033[01;34m\]$(parse_git_branch " %s") \[\033[00;00m\]> '
fi
}
trap 'timer_start' DEBUG
PROMPT_COMMAND='set_prompt'
## Lancement des commandes au demarrages
# claude-dans-dtach : creer une session (claude tournant dans dtach, detache via Ctrl-\).
# Reprend la derniere conversation du dossier (--continue), sinon session neuve.
# Usage : cd ~/projets/seo && cc seo -> session nommee "seo".
# Pas de systemd sur macOS : dtach est lance directement (pas de slice cgroup).
dtach_claude() {
local name="${1:-claude-$(date +%H%M%S)}"
mkdir -p "$HOME/.dtach"
dtach -c "$HOME/.dtach/${name}" -e '^\' \
bash -c 'set -a; [ -f "$HOME/.claude/.env" ] && . "$HOME/.claude/.env"; set +a; claude --continue || exec claude'
}
alias cc='dtach_claude'
# Rappeler a la demande le menu de reprise.
alias d='source ~/.local/bin/dtach-router'
# openrouter : bascule Claude Code sur OpenRouter dans le shell courant (fonction, pas
# script : un script ne peut pas modifier l'environnement de son parent). `openrouter`
# active, `openrouter -d` (ou -u) desactive. La cle vient de $OPENROUTER_API_KEY, a
# exporter depuis un fichier prive non versionne (~/.bashrc.local, ~/.zshrc.local).
# Les valeurs qu'avaient deja ces variables sont gardees dans $_OPENROUTER_SAVED
# (commandes export/unset) et rejouees a la desactivation.
openrouter() {
local v saved=""
case "${1:-}" in
-d | -u)
if [ -n "${_OPENROUTER_SAVED+x}" ]; then
saved="$_OPENROUTER_SAVED"
elif [ "${ANTHROPIC_BASE_URL:-}" != "https://openrouter.ai/api" ]; then
echo "openrouter: not active"
return 0
fi
unset ANTHROPIC_BASE_URL ANTHROPIC_AUTH_TOKEN ANTHROPIC_API_KEY \
CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY
eval "$saved"
unset _OPENROUTER_SAVED
echo "openrouter: off"
echo "Pensez a vous log in de Claude Code"
;;
"")
[ -n "${OPENROUTER_API_KEY:-}" ] || {
echo "openrouter: OPENROUTER_API_KEY is not set" >&2
return 1
}
if [ -z "${_OPENROUTER_SAVED+x}" ]; then
for v in ANTHROPIC_BASE_URL ANTHROPIC_AUTH_TOKEN ANTHROPIC_API_KEY \
CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY; do
if printenv "$v" >/dev/null; then
saved="$saved export $v=$(printf %q "$(printenv "$v")");"
else
saved="$saved unset $v;"
fi
done
_OPENROUTER_SAVED="$saved"
fi
export ANTHROPIC_BASE_URL="https://openrouter.ai/api"
export ANTHROPIC_AUTH_TOKEN="$OPENROUTER_API_KEY"
export ANTHROPIC_API_KEY="" # doit etre explicitement vide
export CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY=1
echo "openrouter: on"
echo "Pensez a vous logout de Claude Code"
;;
*)
echo "usage: openrouter [-d|-u]" >&2
return 1
;;
esac
}
# repo-sync : un arbre local (~/repos) pour tous les depots des forges declarees
# dans ~/.config/repos/forges.conf. `repo <projet> [namespace]` clone si besoin
# puis s'y place ; la liste se rafraichit en arriere-plan, au plus 1 fois par jour.
repo() {
local dir
dir="$(repo-sync path "$@")" && cd "$dir" || return
}
_repo_complete() {
local cache="${REPOS_CACHE:-${XDG_CACHE_HOME:-$HOME/.cache}/repos/list}" words
[ -f "$cache" ] || return 0
if [ "$COMP_CWORD" -eq 1 ]; then
words="$(cut -f1 "$cache" | sort -u)"
else
words="$(awk -F'\t' -v p="${COMP_WORDS[1]}" '$1 == p { print $2 }' "$cache")"
fi
# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "$words" -- "${COMP_WORDS[COMP_CWORD]}"))
}
complete -F _repo_complete repo
case $- in *i*) command -v repo-sync >/dev/null && (repo-sync refresh --quiet >/dev/null 2>&1 &) ;; esac
# SSH interactif hors tmux (et hors terminal VS Code Remote-SSH) : entrer dans tmux,
# session partagee "main" (rattachee si elle existe). Le shell lance dans tmux relit ce
# fichier et ouvre le menu dtach ci-dessous ; quitter tmux ferme la connexion (exec).
case $- in *i*)
if [ -n "${SSH_CONNECTION:-}" ] && [ -z "${TMUX:-}" ] \
&& [ "${TERM_PROGRAM:-}" != vscode ] && command -v tmux >/dev/null; then
exec tmux new-session -A -s main
fi
;;
esac
# Au demarrage d'un shell interactif, proposer de reprendre une session dtach existante
# (silencieux si aucune). Place ici plutot que dans ~/.profile car les terminaux VS Code
# Remote-SSH sont non-login et ne lisent pas ~/.profile ; ~/.bashrc, lui, est toujours lu.
case $- in *i*) [ -x "$HOME/.local/bin/dtach-router" ] && . "$HOME/.local/bin/dtach-router" ;; esac