Zsh Custom Function- FCC: Fabric Change Context #1384
Telhassani
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
🧵 fcc – Fabric Change Context
fcc (Fabric Change Context) is a custom Zsh function that streamlines the use of the fabric CLI by providing an interactive, user-friendly interface for selecting context, patterns, flags, and optional arguments — all in one flow.
**_**NOTE:
I am not a programmer and this is the first i write a Zsh function using AI.
please feel free to drop you comments and tell me what you think.
Copy the fcc function file into your /.zshrc**_**
**fcc() {
local context_path="$HOME/.config/fabric/contexts"
local pattern_path="$HOME/.config/fabric/patterns"
Verify required directories
[[ ! -d "$context_path" ]] && echo "Context directory not found!" && return 1
[[ ! -d "$pattern_path" ]] && echo "Pattern directory not found!" && return 1
CONTEXT SELECTION
local context_filename=$(find "$context_path" -type f -exec basename {} ; |
nl -w2 -s'. ' |
fzf --height 20%
--prompt="Choose Context > "
--preview 'file=$(echo {} | awk "{print $2}"); fmt -w 80 "'"$context_path"'/$file"'
--preview-window=right:wrap |
cut -d'.' -f2- | sed 's/^ *//')
[[ -z "$context_filename" ]] && echo "No context selected!" && return 1
PATTERN SELECTION
local pattern_lines=$(ls -1 "$pattern_path")
local selected_patterns=$(printf "%s\n" "$pattern_lines" | nl -w2 -s'. ' |
fzf --multi --height 50%
--prompt="Choose Pattern(s) > "
--preview 'name=$(echo {} | cut -d"." -f2- | sed "s/^ *//"); cat "'"$pattern_path"'/$name/system.md" 2>/dev/null | fmt -w 80'
--preview-window=right:wrap |
cut -d'.' -f2- | sed 's/^ *//' | paste -sd "," -)
[[ -z "$selected_patterns" ]] && echo "No pattern selected!" && return 1
FLAG INPUT (re-prompt if empty)
unset flag
while [[ -z "$flag" ]]; do
read "flag?Enter flag: "
done
CLIPBOARD
local clip=$(pbpaste)
[[ -z "$clip" ]] && echo "Clipboard is empty!" && return 1
OPTIONAL EXTRA ARGUMENT
read "extra?Enter optional arguments (or leave blank): "
RUN COMMAND (with eval to support pipes, redirects)
local context_file="$context_filename"
echo ""
echo "Running the following command:"
if [[ -n "$extra" ]]; then
cmd="fabric -C "$context_file" "$clip" "$flag" "$selected_patterns" $extra"
else
cmd="fabric -C "$context_file" "$clip" "$flag" "$selected_patterns""
fi
echo "$cmd"
eval "$cmd"
}**
⸻
🛠 How It Works
When you run fcc, it guides you through a step-by-step process:
1. Select a Context
Uses fzf to choose a markdown file from your ~/.config/fabric/contexts/ directory. This context informs Fabric how to process your input.
2. Select Patterns
Displays a list of available pattern folders from ~/.config/fabric/patterns/. You can preview each pattern’s system.md file live as you scroll.
3. Enter a Flag
Prompts you to type a required fabric flag (e.g. -p, -e, etc). You must provide one to continue.
4. Pull Clipboard Content
Automatically grabs the current clipboard text (via pbpaste on macOS) to use as the input prompt for Fabric.
5. Optional Arguments
Allows you to append an optional final argument, such as | save "my run" or --silent, which will be passed directly to the shell using eval.
6. Preview and Run
Displays the full fabric command for review, then executes it.
Beta Was this translation helpful? Give feedback.
All reactions