From f4272c272161a625f63425651c11a564c1fdfbf6 Mon Sep 17 00:00:00 2001 From: Dave Hulbert Date: Mon, 4 Dec 2023 13:16:02 +0000 Subject: [PATCH] Fix zsh WIP --- README.md | 24 ++++++++++++----- clipea/clipea.zsh | 65 +++++++++++++++++++++++++++++------------------ 2 files changed, 58 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index c41fb9a..6b3ebfa 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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 diff --git a/clipea/clipea.zsh b/clipea/clipea.zsh index f883b68..2c5ad71 100755 --- a/clipea/clipea.zsh +++ b/clipea/clipea.zsh @@ -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