Skip to content
This repository has been archived by the owner. It is now read-only.

Commit

Permalink
pass OS version into prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
not-poma committed Mar 3, 2023
1 parent 24e34dc commit faff49f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# LazyShell

LazyShell is a GPT-3 powered utility for Zsh that helps you write and modify console commands using natural language. Perfect for those times when you can't remember the command line arguments for `tar` and `ffmpeg`, or when you just want to save time by having AI do the heavy lifting. The tool uses your current command line content (if any) as a base for your query, so you can issue modification requests for it. Invoke the completion with ALT+G hotkey; you still have to manually press enter to execute the suggested command.
LazyShell is a GPT powered utility for Zsh that helps you write and modify console commands using natural language. Perfect for those times when you can't remember the command line arguments for `tar` and `ffmpeg`, or when you just want to save time by having AI do the heavy lifting. The tool uses your current command line content (if any) as a base for your query, so you can issue modification requests for it. Invoke the completion with ALT+G hotkey; you still have to manually press enter to execute the suggested command.

![Screenshot](https://raw.githubusercontent.com/not-poma/lazyshell/master/screenshot.gif)

Expand Down
24 changes: 19 additions & 5 deletions lazyshell.zsh
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
#!/usr/bin/env zsh

__get_distribution_name() {
if [[ "$(uname)" == "Darwin" ]]; then
echo "$(sw_vers -productName) $(sw_vers -productVersion)" 2>/dev/null
else
echo "$(cat /etc/*-release 2>/dev/null | grep PRETTY_NAME | cut -d'"' -f2)"
fi
}

__lazyshell_complete() {
if [ -z "$OPENAI_API_KEY" ]; then
echo ""
Expand All @@ -20,10 +28,17 @@ __lazyshell_complete() {
BUFFER="$buffer_context"
CURSOR=$#BUFFER

local os=$(__get_distribution_name)
if [[ -n "$os" ]]; then
os=" for $os"
else
os=""
fi

if [[ -z "$buffer_context" ]]; then
local prompt="Write a bash command for query: \`$REPLY\`. Answer with the command only."
local prompt="Write a zsh command for query: \`$REPLY\`. Answer with a single command$os only, without any additional characters so it can be pasted into the terminal."
else
local prompt="Alter bash command \`$buffer_context\` to comply with query \`$REPLY\`. Answer with the command only."
local prompt="Alter zsh command \`$buffer_context\` to comply with query \`$REPLY\`. Answer with a single command$os only, without any additional characters so it can be pasted into the terminal."
fi

# todo: better escaping
Expand Down Expand Up @@ -58,7 +73,7 @@ __lazyshell_complete() {
local response=$(cat "$response_file")
rm "$response_file"

local generated_text=$(echo -E $response | jq -r '.choices[0].message.content' | xargs | sed -e 's/^`\(.*\)`$/\1/')
local generated_text=$(echo -E $response | jq -r '.choices[0].message.content' | xargs)
local error=$(echo -E $response | jq -r '.error.message')

if [ $? -ne 0 ]; then
Expand All @@ -67,7 +82,6 @@ __lazyshell_complete() {
fi

if [[ -n "$error" && "$error" != "null" ]]; then
echo "Error: $error"
zle -R "Error: $error"
return 1
fi
Expand All @@ -85,4 +99,4 @@ fi

# Bind the __lazyshell_complete function to the Alt-g hotkey
zle -N __lazyshell_complete
bindkey '\eg' __lazyshell_complete
bindkey '\eg' __lazyshell_complete

0 comments on commit faff49f

Please sign in to comment.