Skip to content

Instantly share code, notes, and snippets.

@NigelThorne
Last active February 5, 2025 01:40
Show Gist options
  • Save NigelThorne/385ebbbe2060cc16414ced49e853f12f to your computer and use it in GitHub Desktop.
Save NigelThorne/385ebbbe2060cc16414ced49e853f12f to your computer and use it in GitHub Desktop.
ask-claude uses Zellij and llm to give you an AI helper in your terminal that can see what you are doing.
#!/bin/bash
# Check for newer version at gist
local_hash=$(tr -d '\r\n' < "$0" | md5sum | cut -d ' ' -f 1)
remote_hash=$(curl -s https://gist.githubusercontent.com/NigelThorne/385ebbbe2060cc16414ced49e853f12f/raw/ | tr -d '\r\n' | md5sum | cut -d ' ' -f 1)
if [ "$local_hash" != "$remote_hash" ]; then
echo "A newer version is available at: https://gist.github.com/NigelThorne/385ebbbe2060cc16414ced49e853f12f" >&2
fi
##########
# This script helps you get AI assistance for code and text analysis.
#
# Two ways to use it:
# 1. Pipe input directly:
# echo "some text" | ask claude "analyze this"
#
# 2. Use your current zellij terminal screen:
# ask claude "analyze what's on my screen"
# (Defaults to last 300 lines, use -n <number> to adjust)
#
# The script will send your context and question to Claude and return the response.
#
# Options:
# -n <number> Number of lines to include from screen (default: 300)
#
##########
##########
# Example use:
#
# rewrite my last git commit comment
# git diff main | ask-claude "write a git commit comment for this change. It can be multiline. I will pipe your response directly into my git commit -m command" | git commit --amend -F -
#########
# Model mapping using case statement instead of associative array
get_model_name() {
case "$1" in
"sonnet") echo "claude-3.5-sonnet" ;;
"haiku") echo "claude-3.5-haiku" ;;
"claude") echo "claude-3.5-sonnet" ;;
"quen") echo "qwen2.5-coder:32b" ;;
"local") echo "llama2" ;;
*) echo "$1" ;;
esac
}
# Default model and number of lines
model=${1:-claude}
model_name=$(get_model_name "$model")
shift
num_lines=300
# Parse command line options
while getopts "n:" opt; do
case $opt in
n)
num_lines=$OPTARG
shift 2
;;
*)
;;
esac
done
# Check if we're getting input from pipe
if [ -p /dev/stdin ]; then
# Read from pipe
log=$(cat)
else
# Get from zellij
zellij action dump-screen /tmp/screen-dump.txt 2>/dev/null
if [ $? -ne 0 ]; then
echo "You need to be in a zellij session"
exit 1
fi
log=$(cat /tmp/screen-dump.txt | tail -n $num_lines)
fi
# Get the user's prompt from command line arguments
user_prompt="$*"
# Make sure we have a prompt
if [ -z "$user_prompt" ]; then
echo "Error: Please provide a prompt"
exit 1
fi
# Combine the context and prompt
if [ "$num_lines" -eq 0 ]; then
full_prompt=$user_prompt
else
full_prompt="Context:
------
$log
------
User question: $user_prompt"
fi
# Send to llm with specified model
echo "$full_prompt" | llm -m $model_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment