What’s New in 0.12.0?

The latest release of common-utils introduces a new feature that leverages OpenAI to generate contextually appropriate emojis for your shell script and Makefile output messages. This makes your terminal output more visually appealing and easier to scan, helping you quickly identify different types of messages.

The gen-emoji.py Script

The gen-emoji.py script uses OpenAI’s API to generate contextually appropriate emojis based on the content of your messages.

How It Works

The script takes a text input and returns a formatted string with an emoji prefix. The emoji is chosen based on the context of the message using OpenAI’s API.

def get_emoji_response(input_text: str) -> str:
    client = openai.OpenAI(api_key=get_api_key())
    with Halo(text='thinking...', spinner='dots'):
        response = client.chat.completions.create(
            model="gpt-4",
            messages=[
                {"role": "system", "content": INSTRUCTIONS},
                {"role": "user", "content": input_text}
            ]
        )
    return response.choices[0].message.content

The script uses a typical prompt to guide the OpenAI model to generate an appropriate emoji for the given context (see INSTRUCTIONS in the gen-emojy.py code).

Examples

Here are some examples of how the script transforms plain text messages:

$ gen-emoji "Building Docker image"
"--- 🐳 Building Docker image"

$ gen-emoji "Compiling program"
"--- ⚙️ Compiling program"

$ gen-emoji "Running tests"
"--- 🧪 Running tests"

The emojify Function in common.mk

The common.mk template now includes a convenient function to add emojis to your Makefile output messages:

# Function to add emoji to messages
# Usage:
#   $(call emojify,Your message here)
#   or
#   emojify "Your message here"
define emojify
    @if [ -n "$(GEN_EMOJI)" ] && [ -x "$(GEN_EMOJI)" ] && [ -n "$$OPENAI_KEY" ]; then \
        $(GEN_EMOJI) "$(1)" | tr -d '"'; \
    else \
        echo "--- 🔧 $(1)"; \
    fi
endef

This function checks if the gen-emoji script is available and if the OPENAI_KEY environment variable is set. If both conditions are met, it uses OpenAI to generate a contextually appropriate emoji. Otherwise, it falls back to a default wrench emoji.

Using emojify in Your Makefile

There are two ways to use the emojify functionality in your Makefile:

  1. As a function call within your Makefile targets:
compile:  ## Compiles the binary
    $(call emojify,Compiling program)
    # Your compilation commands here
  1. As a standalone command:
deploy:  ## Deploys the application
    emojify "Deploying application"
    # Your deployment commands here

Using emojify() from utils.sh in Shell Scripts

The utils.sh script now includes an emojify function that you can use directly in your shell scripts:

# Adds an emoji prefix to a message based on its context.
#
# If OPENAI_KEY is set, it uses OpenAI to generate a contextually appropriate emoji.
# Otherwise, it falls back to a default wrench emoji.
#
# Usage: emojify MESSAGE
function emojify {
    if [ -n "$OPENAI_KEY" ]; then
      gen-emoji.py "$1" | tr -d '"'
    else
      echo "--- 🔧 $1"
    fi
}

To use this function in your shell scripts, simply source the utils.sh file and call the emojify function:

#!/bin/bash

# Source the utils.sh file from COMMON_UTILS
source $COMMON_UTILS/scripts/utils.sh

# Use the emojify function in your script
emojify "Starting backup process"
# Your backup commands here

emojify "Compressing files"
# Your compression commands here

emojify "Backup completed successfully"

Getting Started

To start using the emojify functionality, you’ll need:

  1. Python 3.6 or higher
  2. The openai and halo Python modules:
   pip install openai halo
  1. An OpenAI API key set in the OPENAI_KEY environment variable:
   export OPENAI_KEY=your-api-key
  1. The COMMON_UTILS environment variable set to the directory containing the common-utils repository:
   export COMMON_UTILS=/path/to/common-utils
   export PATH=$PATH:$COMMON_UTILS

Conclusion

The new emojify functionality in common-utils 0.12.0 provides a simple yet powerful way to enhance your shell scripts and Makefiles with contextually appropriate emojis. This makes your terminal output more visually appealing and easier to read, helping you quickly identify different types of messages.

Give it a try and let us know what you think!

Leave a comment

Trending