Skip to content

Fix path used by zsh alias and allow zsh to use clipea in different paths #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,25 @@ Clipea doesn't have any context of what it said before, though this may be added

## 📦 Installation and setup

### Manual installation

Python >=3.10 is required.

### Quck install with PyPi (recommended)

Install for the current user with [`pipx`](https://pypa.github.io/pipx/):

pipx install clipea-cli

`pipx` is recommended but you can also install in the global context with `pip install clipea-cli`.

### Zsh Shell integration and Alias

> [!TIP]
> The `??` shell alias is highly recommended if you use zsh

clipea alias

### Manual installation and development

You can use the provided `setup.py`. ([setuptools docs](https://setuptools.pypa.io/en/latest/deprecated/easy_install.html))

You can install it quickly like so:
Expand All @@ -177,16 +192,13 @@ Or development mode:

pip install -e .

### With PyPi

pipx install clipea-cli

### Zsh Shell integration and Alias

> [!TIP]
> The `??` shell alias is highly recommended if you use zsh

clipea alias
You can tell the zsh script to use this with `CLIPEA_PATH=./clipea source ./clipea/clipea.zsh`.

## Internals

Expand Down
65 changes: 40 additions & 25 deletions clipea/clipea.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,43 @@ if [[ $ZSH_EVAL_CONTEXT != 'toplevel:file' ]]; then
return 1 2>/dev/null || exit 1
fi

CLIPEA_TMP_FILE=$(mktemp)

# https://stackoverflow.com/questions/9901210/bash-source0-equivalent-in-zsh
CLIPEA_SCRIPT_DIR=$(dirname $(readlink -f ${(%):-%x}))

CLIPEA_PYTHON=

CLIPEA_PATH=$(builtin which clipea)

# Run clipea from the current dir if possible
if [[ -f $CLIPEA_SCRIPT_DIR/__main__.py ]]; then
CLIPEA_PATH=$CLIPEA_SCRIPT_DIR
CLIPEA_PYTHON="$(builtin which python3 || builtin which python)"
fi

# Execute clipea with an environment variable
CLIPEA_CMD_OUTPUT_FILE="$CLIPEA_TMP_FILE" $CLIPEA_PYTHON "$CLIPEA_PATH" "$@"

# Read the command to be placed on the Zsh command line
CLIPEA_COMMAND_TO_PLACE=$(< "$CLIPEA_TMP_FILE")

# Place it on the Zsh command line
print -rz "$CLIPEA_COMMAND_TO_PLACE"

rm "$CLIPEA_TMP_FILE"
CLIPEA_ARGS="$@"

# wrap in a fn so we don't polute the shell with vars
clipea_init() {
local CLIPEA_TMP_FILE=$(mktemp)
local CLIPEA_PYTHON_INTERNAL
local CLIPEA_PATH_INTERNAL

# Allow overriding clipea location for development
# eg CLIPEA_PATH=./clipea source clipea/clipea.zsh
if [[ -z $CLIPEA_PATH ]]; then
# default to the installed clipea
CLIPEA_PATH_INTERNAL=$(builtin which clipea)
else
CLIPEA_PATH_INTERNAL=$CLIPEA_PATH
# only if clipea is overridden then we need to run it with python
# allow overriding python too
if [[ -z $CLIPEA_PYTHON ]]; then
CLIPEA_PYTHON_INTERNAL="$(builtin which python3 || builtin which python)"
else
CLIPEA_PYTHON_INTERNAL=$CLIPEA_PYTHON
fi
fi

#echo Debug: $CLIPEA_PYTHON_INTERNAL $CLIPEA_PATH_INTERNAL $CLIPEA_ARGS

# Execute clipea with an environment variable
CLIPEA_CMD_OUTPUT_FILE="$CLIPEA_TMP_FILE" $CLIPEA_PYTHON_INTERNAL "$CLIPEA_PATH_INTERNAL" "$CLIPEA_ARGS"

# Read the command to be placed on the Zsh command line
COMMAND_TO_PLACE=$(< "$CLIPEA_TMP_FILE")

# Place it on the Zsh command line
print -rz "$COMMAND_TO_PLACE"

rm "$CLIPEA_TMP_FILE"
}
clipea_init

unset CLIPEA_ARGS