This commit is contained in:
2026-02-11 07:32:06 +02:00
parent 540b18e6e8
commit 49dd72c170
4 changed files with 83 additions and 31 deletions

View File

@@ -3,7 +3,7 @@
set -euo pipefail
usage() {
echo "Usage: $0 -c COMMENT_SYMBOL [-e EXCLUDE_PATTERN]... TARGET"
echo "Usage: $(basename "$0") -c COMMENT_SYMBOL [-e EXCLUDE_PATTERN]... TARGET"
echo " -c Comment symbol (e.g., '#' or '//')"
echo " -e Exclude pattern (can be specified multiple times)"
echo " TARGET File or directory to process"
@@ -29,21 +29,10 @@ shift $((OPTIND - 1))
target="$(realpath "$1")"
base_dir="$(pwd)"
should_exclude() {
local path="$1"
for pattern in "${excludes[@]}"; do
if [[ "$path" == *"$pattern"* ]]; then
return 0
fi
done
return 1
}
process_file() {
local file="$1"
local rel_path="${file#"$base_dir"/}"
should_exclude "$rel_path" && return
# shellcheck disable=SC2295
local rel_path="${file#$base_dir/}"
local path_comment="$comment_sym path: $rel_path"
@@ -52,22 +41,37 @@ process_file() {
IFS= read -r line1 <"$file" 2>/dev/null || line1=""
IFS= read -r line2 < <(tail -n +2 "$file") 2>/dev/null || line2=""
# Check if path comment exists in first or second line
[[ "$line1" == *"path: "* ]] && return
[[ "$line2" == *"path: "* ]] && return
# Insert after shebang if present, otherwise at top
# Handle shebang case
if [[ "$line1" =~ ^#! ]]; then
{
echo "$line1"
echo "$path_comment"
tail -n +2 "$file"
} >"$file.tmp"
if [[ "$line2" == *"path: "* ]]; then
# Replace existing path comment on line 2
{
echo "$line1"
echo "$path_comment"
tail -n +3 "$file"
} >"$file.tmp"
else
# Insert new path comment after shebang
{
echo "$line1"
echo "$path_comment"
tail -n +2 "$file"
} >"$file.tmp"
fi
else
{
echo "$path_comment"
cat "$file"
} >"$file.tmp"
if [[ "$line1" == *"path: "* ]]; then
# Replace existing path comment on line 1
{
echo "$path_comment"
tail -n +2 "$file"
} >"$file.tmp"
else
# Insert new path comment at top
{
echo "$path_comment"
cat "$file"
} >"$file.tmp"
fi
fi
mv "$file.tmp" "$file"
@@ -76,9 +80,26 @@ process_file() {
if [[ -f "$target" ]]; then
process_file "$target"
elif [[ -d "$target" ]]; then
find_cmd=(find "$target")
# Always exclude hidden files and directories
find_cmd+=(\( -name ".*" -prune \))
if [[ ${#excludes[@]} -gt 0 ]]; then
find_cmd+=(-o \()
for i in "${!excludes[@]}"; do
[[ $i -gt 0 ]] && find_cmd+=(-o)
find_cmd+=(-path "*/${excludes[$i]}" -prune)
find_cmd+=(-o -path "*/${excludes[$i]}/*" -prune)
done
find_cmd+=(\))
fi
find_cmd+=(-o -type f -print0)
while IFS= read -r -d '' file; do
process_file "$file"
done < <(find "$target" -type f -print0)
done < <("${find_cmd[@]}")
else
echo "Error: $target is not a file or directory" >&2
exit 1

View File

@@ -44,6 +44,12 @@ bind -r j select-pane -D
bind -r k select-pane -U
bind -r l select-pane -R
# Resize Opt/Alt-hjkl
bind -n M-h resize-pane -L 5
bind -n M-j resize-pane -D 5
bind -n M-k resize-pane -U 5
bind -n M-l resize-pane -R 5
# Last window instead of session
bind ';' last-window

View File

@@ -25,7 +25,26 @@ git_prompt_info() {
echo " %F{green}($branch)%f"
fi
}
PROMPT='%n@%m%f %F{blue}%~%f$(git_prompt_info) $ '
abbrev_path() {
local pwd="${PWD/#$HOME/~}"
local parts=("${(@s:/:)pwd}")
local len=${#parts}
if (( len <= 1 )); then
echo "$pwd"
return
fi
local result=""
for (( i=1; i<len; i++ )); do
result+="${parts[i]:0:1}/"
done
result+="${parts[len]}"
echo "$result"
}
PROMPT='%n@%m%f %F{blue}$(abbrev_path)%f$(git_prompt_info) $ '
# PROMPT='%n@%m%f %F{blue}%~%f$(git_prompt_info) $ '
autoload -U up-line-or-beginning-search down-line-or-beginning-search
zle -N up-line-or-beginning-search
@@ -45,6 +64,10 @@ case "$OSTYPE" in
darwin*) alias ls='ls --color=auto' ;;
esac
h() {
history 0 | grep -iE --color=always "$@" | tail -20
}
alias ll='ls -lF'
alias lla='ll -a'
alias ld='ls -ld */'
@@ -55,3 +78,5 @@ alias gp='git push'
alias gst='git status'
alias gd='git diff --patience --color-moved=dimmed-zebra --word-diff=plain --function-context --ignore-space-change -U3'
alias glg='git log --oneline --graph --decorate --all'
alias k='kubectl'