Skip to content

Commit

Permalink
Fix zsh WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dave1010 authored and Dave Hulbert committed Dec 14, 2023
1 parent c37db25 commit f4272c2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 31 deletions.
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

0 comments on commit f4272c2

Please sign in to comment.